01: /*
02: * Copyright 1999-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.16 2004/08/17 19:25:36 jycli 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: * Execute the Last() function.
30: * @xsl.usage advanced
31: */
32: public class FuncLast extends Function {
33: static final long serialVersionUID = 9205812403085432943L;
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 compiler) {
42: m_isTopLevel = compiler.getLocationPathDepth() == -1;
43: }
44:
45: /**
46: * Get the position in the current context node list.
47: *
48: * @param xctxt non-null reference to XPath runtime context.
49: *
50: * @return The number of nodes in the list.
51: *
52: * @throws javax.xml.transform.TransformerException
53: */
54: public int getCountOfContextNodeList(XPathContext xctxt)
55: throws javax.xml.transform.TransformerException {
56:
57: // assert(null != m_contextNodeList, "m_contextNodeList must be non-null");
58: // If we're in a predicate, then this will return non-null.
59: SubContextList iter = m_isTopLevel ? null : xctxt
60: .getSubContextList();
61:
62: // System.out.println("iter: "+iter);
63: if (null != iter)
64: return iter.getLastPos(xctxt);
65:
66: DTMIterator cnl = xctxt.getContextNodeList();
67: int count;
68: if (null != cnl) {
69: count = cnl.getLength();
70: // System.out.println("count: "+count);
71: } else
72: count = 0;
73: return count;
74: }
75:
76: /**
77: * Execute the function. The function must return
78: * a valid object.
79: * @param xctxt The current execution context.
80: * @return A valid XObject.
81: *
82: * @throws javax.xml.transform.TransformerException
83: */
84: public XObject execute(XPathContext xctxt)
85: throws javax.xml.transform.TransformerException {
86: XNumber xnum = new XNumber(
87: (double) getCountOfContextNodeList(xctxt));
88: // System.out.println("last: "+xnum.num());
89: return xnum;
90: }
91:
92: /**
93: * No arguments to process, so this does nothing.
94: */
95: public void fixupVariables(java.util.Vector vars, int globalsSize) {
96: // no-op
97: }
98:
99: }
|