19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the  "License");
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License.
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License.
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: FuncPosition.java 468655 2006-10-28 07:12:06Z minchau $
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xpath.functions;
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTM;
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTMIterator;
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.XPathContext;
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.axes.SubContextList;
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.compiler.Compiler;
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.objects.XNumber;
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.objects.XObject;
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/**
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Execute the Position() function.
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @xsl.usage advanced
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class FuncPosition extends Function
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    static final long serialVersionUID = -9092846348197271582L;
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  private boolean m_isTopLevel;
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Figure out if we're executing a toplevel expression.
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * If so, we can't be inside of a predicate.
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void postCompileStep(Compiler compiler)
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_isTopLevel = compiler.getLocationPathDepth() == -1;
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Get the position in the current context node list.
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param xctxt Runtime XPath context.
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return The current position of the itteration in the context node list,
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *         or -1 if there is no active context node list.
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public int getPositionInContextNodeList(XPathContext xctxt)
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // System.out.println("FuncPosition- entry");
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // If we're in a predicate, then this will return non-null.
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    SubContextList iter = m_isTopLevel ? null : xctxt.getSubContextList();
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (null != iter)
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      int prox = iter.getProximityPosition(xctxt);
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // System.out.println("FuncPosition- prox: "+prox);
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      return prox;
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    DTMIterator cnl = xctxt.getContextNodeList();
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (null != cnl)
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      int n = cnl.getCurrentNode();
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      if(n == DTM.NULL)
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if(cnl.getCurrentPos() == 0)
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          return 0;
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // Then I think we're in a sort.  See sort21.xsl. So the iterator has
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // already been spent, and is not on the node we're processing.
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // It's highly possible that this is an issue for other context-list
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // functions.  Shouldn't be a problem for last(), and it shouldn't be
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // a problem for current().
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        try
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        {
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          cnl = cnl.cloneWithReset();
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        catch(CloneNotSupportedException cnse)
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        {
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          throw new org.apache.xml.utils.WrappedRuntimeException(cnse);
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        int currentNode = xctxt.getContextNode();
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // System.out.println("currentNode: "+currentNode);
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        while(DTM.NULL != (n = cnl.nextNode()))
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        {
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          if(n == currentNode)
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            break;
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // System.out.println("n: "+n);
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // System.out.println("FuncPosition- cnl.getCurrentPos(): "+cnl.getCurrentPos());
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      return cnl.getCurrentPos();
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // System.out.println("FuncPosition - out of guesses: -1");
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return -1;
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Execute the function.  The function must return
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * a valid object.
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param xctxt The current execution context.
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return A valid XObject.
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws javax.xml.transform.TransformerException
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    double pos = (double) getPositionInContextNodeList(xctxt);
1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return new XNumber(pos);
1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * No arguments to process, so this does nothing.
1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void fixupVariables(java.util.Vector vars, int globalsSize)
1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // no-op
1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
135