001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: FuncLast.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xpath.functions;
022    
023    import org.apache.xml.dtm.DTMIterator;
024    import org.apache.xpath.XPathContext;
025    import org.apache.xpath.axes.SubContextList;
026    import org.apache.xpath.compiler.Compiler;
027    import org.apache.xpath.objects.XNumber;
028    import org.apache.xpath.objects.XObject;
029    
030    
031    /**
032     * Execute the Last() function.
033     * @xsl.usage advanced
034     */
035    public class FuncLast extends Function
036    {
037        static final long serialVersionUID = 9205812403085432943L;
038      
039      private boolean m_isTopLevel;
040      
041      /**
042       * Figure out if we're executing a toplevel expression.
043       * If so, we can't be inside of a predicate. 
044       */
045      public void postCompileStep(Compiler compiler)
046      {
047        m_isTopLevel = compiler.getLocationPathDepth() == -1;
048      }
049    
050      /**
051       * Get the position in the current context node list.
052       *
053       * @param xctxt non-null reference to XPath runtime context.
054       *
055       * @return The number of nodes in the list.
056       *
057       * @throws javax.xml.transform.TransformerException
058       */
059      public int getCountOfContextNodeList(XPathContext xctxt)
060              throws javax.xml.transform.TransformerException
061      {
062    
063        // assert(null != m_contextNodeList, "m_contextNodeList must be non-null");
064        // If we're in a predicate, then this will return non-null.
065        SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList();
066    
067        // System.out.println("iter: "+iter);
068        if (null != iter)
069          return iter.getLastPos(xctxt);
070    
071        DTMIterator cnl = xctxt.getContextNodeList();
072        int count;
073        if(null != cnl)
074        {
075          count = cnl.getLength();
076          // System.out.println("count: "+count); 
077        }
078        else
079          count = 0;   
080        return count;
081      }
082    
083      /**
084       * Execute the function.  The function must return
085       * a valid object.
086       * @param xctxt The current execution context.
087       * @return A valid XObject.
088       *
089       * @throws javax.xml.transform.TransformerException
090       */
091      public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
092      {
093        XNumber xnum = new XNumber((double) getCountOfContextNodeList(xctxt));
094        // System.out.println("last: "+xnum.num());
095        return xnum;
096      }
097      
098      /**
099       * No arguments to process, so this does nothing.
100       */
101      public void fixupVariables(java.util.Vector vars, int globalsSize)
102      {
103        // no-op
104      }
105    
106    }