KickJava   Java API By Example, From Geeks To Geeks.

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


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: FuncLast.java,v 1.15 2004/02/17 04:34:01 minchau Exp $
18  */

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

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

42   public void postCompileStep(Compiler JavaDoc compiler)
43   {
44     m_isTopLevel = compiler.getLocationPathDepth() == -1;
45   }
46
47   /**
48    * Get the position in the current context node list.
49    *
50    * @param xctxt non-null reference to XPath runtime context.
51    *
52    * @return The number of nodes in the list.
53    *
54    * @throws javax.xml.transform.TransformerException
55    */

56   public int getCountOfContextNodeList(XPathContext xctxt)
57           throws javax.xml.transform.TransformerException JavaDoc
58   {
59
60     // assert(null != m_contextNodeList, "m_contextNodeList must be non-null");
61
// If we're in a predicate, then this will return non-null.
62
SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList();
63
64     // System.out.println("iter: "+iter);
65
if (null != iter)
66       return iter.getLastPos(xctxt);
67
68     DTMIterator cnl = xctxt.getContextNodeList();
69     int count;
70     if(null != cnl)
71     {
72       count = cnl.getLength();
73       // System.out.println("count: "+count);
74
}
75     else
76       count = 0;
77     return count;
78   }
79
80   /**
81    * Execute the function. The function must return
82    * a valid object.
83    * @param xctxt The current execution context.
84    * @return A valid XObject.
85    *
86    * @throws javax.xml.transform.TransformerException
87    */

88   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException JavaDoc
89   {
90     XNumber xnum = new XNumber((double) getCountOfContextNodeList(xctxt));
91     // System.out.println("last: "+xnum.num());
92
return xnum;
93   }
94   
95   /**
96    * No arguments to process, so this does nothing.
97    */

98   public void fixupVariables(java.util.Vector JavaDoc vars, int globalsSize)
99   {
100     // no-op
101
}
102
103 }
104
Popular Tags