|
1 /* |
|
2 * reserved comment block |
|
3 * DO NOT REMOVE OR ALTER! |
|
4 */ |
|
5 /* |
|
6 * Licensed to the Apache Software Foundation (ASF) under one or more |
|
7 * contributor license agreements. See the NOTICE file distributed with |
|
8 * this work for additional information regarding copyright ownership. |
|
9 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
10 * (the "License"); you may not use this file except in compliance with |
|
11 * the License. You may obtain a copy of the License at |
|
12 * |
|
13 * http://www.apache.org/licenses/LICENSE-2.0 |
|
14 * |
|
15 * Unless required by applicable law or agreed to in writing, software |
|
16 * distributed under the License is distributed on an "AS IS" BASIS, |
|
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
18 * See the License for the specific language governing permissions and |
|
19 * limitations under the License. |
|
20 */ |
|
21 |
|
22 package com.sun.org.apache.xpath.internal.functions; |
|
23 |
|
24 import com.sun.org.apache.xml.internal.dtm.DTM; |
|
25 import com.sun.org.apache.xml.internal.dtm.DTMIterator; |
|
26 import com.sun.org.apache.xpath.internal.XPathContext; |
|
27 import com.sun.org.apache.xpath.internal.axes.SubContextList; |
|
28 import com.sun.org.apache.xpath.internal.compiler.Compiler; |
|
29 import com.sun.org.apache.xpath.internal.objects.XNumber; |
|
30 import com.sun.org.apache.xpath.internal.objects.XObject; |
|
31 |
|
32 /** |
|
33 * Execute the Position() function. |
|
34 * @xsl.usage advanced |
|
35 */ |
|
36 public class FuncPosition extends Function |
|
37 { |
|
38 static final long serialVersionUID = -9092846348197271582L; |
|
39 private boolean m_isTopLevel; |
|
40 |
|
41 /** |
|
42 * Figure out if we're executing a toplevel expression. |
|
43 * If so, we can't be inside of a predicate. |
|
44 */ |
|
45 public void postCompileStep(Compiler compiler) |
|
46 { |
|
47 m_isTopLevel = compiler.getLocationPathDepth() == -1; |
|
48 } |
|
49 |
|
50 /** |
|
51 * Get the position in the current context node list. |
|
52 * |
|
53 * @param xctxt Runtime XPath context. |
|
54 * |
|
55 * @return The current position of the itteration in the context node list, |
|
56 * or -1 if there is no active context node list. |
|
57 */ |
|
58 public int getPositionInContextNodeList(XPathContext xctxt) |
|
59 { |
|
60 |
|
61 // System.out.println("FuncPosition- entry"); |
|
62 // If we're in a predicate, then this will return non-null. |
|
63 SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList(); |
|
64 |
|
65 if (null != iter) |
|
66 { |
|
67 int prox = iter.getProximityPosition(xctxt); |
|
68 |
|
69 // System.out.println("FuncPosition- prox: "+prox); |
|
70 return prox; |
|
71 } |
|
72 |
|
73 DTMIterator cnl = xctxt.getContextNodeList(); |
|
74 |
|
75 if (null != cnl) |
|
76 { |
|
77 int n = cnl.getCurrentNode(); |
|
78 if(n == DTM.NULL) |
|
79 { |
|
80 if(cnl.getCurrentPos() == 0) |
|
81 return 0; |
|
82 |
|
83 // Then I think we're in a sort. See sort21.xsl. So the iterator has |
|
84 // already been spent, and is not on the node we're processing. |
|
85 // It's highly possible that this is an issue for other context-list |
|
86 // functions. Shouldn't be a problem for last(), and it shouldn't be |
|
87 // a problem for current(). |
|
88 try |
|
89 { |
|
90 cnl = cnl.cloneWithReset(); |
|
91 } |
|
92 catch(CloneNotSupportedException cnse) |
|
93 { |
|
94 throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(cnse); |
|
95 } |
|
96 int currentNode = xctxt.getContextNode(); |
|
97 // System.out.println("currentNode: "+currentNode); |
|
98 while(DTM.NULL != (n = cnl.nextNode())) |
|
99 { |
|
100 if(n == currentNode) |
|
101 break; |
|
102 } |
|
103 } |
|
104 // System.out.println("n: "+n); |
|
105 // System.out.println("FuncPosition- cnl.getCurrentPos(): "+cnl.getCurrentPos()); |
|
106 return cnl.getCurrentPos(); |
|
107 } |
|
108 |
|
109 // System.out.println("FuncPosition - out of guesses: -1"); |
|
110 return -1; |
|
111 } |
|
112 |
|
113 /** |
|
114 * Execute the function. The function must return |
|
115 * a valid object. |
|
116 * @param xctxt The current execution context. |
|
117 * @return A valid XObject. |
|
118 * |
|
119 * @throws javax.xml.transform.TransformerException |
|
120 */ |
|
121 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException |
|
122 { |
|
123 double pos = (double) getPositionInContextNodeList(xctxt); |
|
124 |
|
125 return new XNumber(pos); |
|
126 } |
|
127 |
|
128 /** |
|
129 * No arguments to process, so this does nothing. |
|
130 */ |
|
131 public void fixupVariables(java.util.Vector vars, int globalsSize) |
|
132 { |
|
133 // no-op |
|
134 } |
|
135 } |