KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > functions > FuncPosition


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: FuncPosition.java,v 1.12 2004/02/17 04:34:00 minchau Exp $
18  */

19 package org.apache.xpath.functions;
20
21 import org.apache.xml.dtm.DTM;
22 import org.apache.xml.dtm.DTMIterator;
23 import org.apache.xpath.XPathContext;
24 import org.apache.xpath.axes.SubContextList;
25 import org.apache.xpath.compiler.Compiler;
26 import org.apache.xpath.objects.XNumber;
27 import org.apache.xpath.objects.XObject;
28
29 /**
30  * Execute the Position() function.
31  * @xsl.usage advanced
32  */

33 public class FuncPosition extends Function
34 {
35   private boolean m_isTopLevel;
36   
37   /**
38    * Figure out if we're executing a toplevel expression.
39    * If so, we can't be inside of a predicate.
40    */

41   public void postCompileStep(Compiler JavaDoc compiler)
42   {
43     m_isTopLevel = compiler.getLocationPathDepth() == -1;
44   }
45
46   /**
47    * Get the position in the current context node list.
48    *
49    * @param xctxt Runtime XPath context.
50    *
51    * @return The current position of the itteration in the context node list,
52    * or -1 if there is no active context node list.
53    */

54   public int getPositionInContextNodeList(XPathContext xctxt)
55   {
56
57     // System.out.println("FuncPosition- entry");
58
// If we're in a predicate, then this will return non-null.
59
SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList();
60
61     if (null != iter)
62     {
63       int prox = iter.getProximityPosition(xctxt);
64  
65       // System.out.println("FuncPosition- prox: "+prox);
66
return prox;
67     }
68
69     DTMIterator cnl = xctxt.getContextNodeList();
70
71     if (null != cnl)
72     {
73       int n = cnl.getCurrentNode();
74       if(n == DTM.NULL)
75       {
76         if(cnl.getCurrentPos() == 0)
77           return 0;
78           
79         // Then I think we're in a sort. See sort21.xsl. So the iterator has
80
// already been spent, and is not on the node we're processing.
81
// It's highly possible that this is an issue for other context-list
82
// functions. Shouldn't be a problem for last(), and it shouldn't be
83
// a problem for current().
84
try
85         {
86           cnl = cnl.cloneWithReset();
87         }
88         catch(CloneNotSupportedException JavaDoc cnse)
89         {
90           throw new org.apache.xml.utils.WrappedRuntimeException(cnse);
91         }
92         int currentNode = xctxt.getContextNode();
93         // System.out.println("currentNode: "+currentNode);
94
while(DTM.NULL != (n = cnl.nextNode()))
95         {
96           if(n == currentNode)
97             break;
98         }
99       }
100       // System.out.println("n: "+n);
101
// System.out.println("FuncPosition- cnl.getCurrentPos(): "+cnl.getCurrentPos());
102
return cnl.getCurrentPos();
103     }
104
105     // System.out.println("FuncPosition - out of guesses: -1");
106
return -1;
107   }
108
109   /**
110    * Execute the function. The function must return
111    * a valid object.
112    * @param xctxt The current execution context.
113    * @return A valid XObject.
114    *
115    * @throws javax.xml.transform.TransformerException
116    */

117   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException JavaDoc
118   {
119     double pos = (double) getPositionInContextNodeList(xctxt);
120     
121     return new XNumber(pos);
122   }
123   
124   /**
125    * No arguments to process, so this does nothing.
126    */

127   public void fixupVariables(java.util.Vector JavaDoc vars, int globalsSize)
128   {
129     // no-op
130
}
131 }
132
Popular Tags