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: OneStepIterator.java 469314 2006-10-30 23:31:59Z minchau $
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xpath.axes;
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTM;
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTMAxisIterator;
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTMFilter;
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTMIterator;
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.Expression;
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.XPathContext;
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.compiler.Compiler;
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.compiler.OpMap;
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/**
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class implements a general iterator for
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * those LocationSteps with only one step, and perhaps a predicate.
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see org.apache.xpath.axes#LocPathIterator
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @xsl.usage advanced
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class OneStepIterator extends ChildTestIterator
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    static final long serialVersionUID = 4623710779664998283L;
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** The traversal axis from where the nodes will be filtered. */
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  protected int m_axis = -1;
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /** The DTM inner traversal class, that corresponds to the super axis. */
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  protected DTMAxisIterator m_iterator;
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Create a OneStepIterator object.
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param compiler A reference to the Compiler that contains the op map.
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param opPos The position within the op map, which contains the
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * location path expression for this itterator.
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws javax.xml.transform.TransformerException
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  OneStepIterator(Compiler compiler, int opPos, int analysis)
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          throws javax.xml.transform.TransformerException
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    super(compiler, opPos, analysis);
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    int firstStepPos = OpMap.getFirstChildPos(opPos);
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_axis = WalkerFactory.getAxisFromStep(compiler, firstStepPos);
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Create a OneStepIterator object.
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param iterator The DTM iterator which this iterator will use.
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param axis One of Axis.Child, etc., or -1 if the axis is unknown.
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws javax.xml.transform.TransformerException
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public OneStepIterator(DTMAxisIterator iterator, int axis)
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          throws javax.xml.transform.TransformerException
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    super(null);
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_iterator = iterator;
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_axis = axis;
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    int whatToShow = DTMFilter.SHOW_ALL;
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    initNodeTest(whatToShow);
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Initialize the context values for this expression
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * after it is cloned.
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param context The XPath runtime context for this
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * transformation.
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void setRoot(int context, Object environment)
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    super.setRoot(context, environment);
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if(m_axis > -1)
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_iterator = m_cdtm.getAxisIterator(m_axis);
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    m_iterator.setStartNode(m_context);
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *  Detaches the iterator from the set which it iterated over, releasing
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * any computational resources and placing the iterator in the INVALID
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * state. After<code>detach</code> has been invoked, calls to
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * <code>nextNode</code> or<code>previousNode</code> will raise the
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * exception INVALID_STATE_ERR.
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void detach()
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if(m_allowDetach)
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      if(m_axis > -1)
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        m_iterator = null;
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // Always call the superclass detach last!
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      super.detach();
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Get the next node via getFirstAttribute && getNextAttribute.
1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  protected int getNextNode()
1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return m_lastFetched = m_iterator.next();
1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Get a cloned iterator.
1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return A new iterator that can be used without mutating this one.
1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @throws CloneNotSupportedException
1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public Object clone() throws CloneNotSupportedException
1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Do not access the location path itterator during this operation!
1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    OneStepIterator clone = (OneStepIterator) super.clone();
1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if(m_iterator != null)
1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      clone.m_iterator = m_iterator.cloneIterator();
1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return clone;
1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *  Get a cloned Iterator that is reset to the beginning
1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *  of the query.
1519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *  @return A cloned NodeIterator set of the start of the query.
1539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *  @throws CloneNotSupportedException
1559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public DTMIterator cloneWithReset() throws CloneNotSupportedException
1579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    OneStepIterator clone = (OneStepIterator) super.cloneWithReset();
1609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    clone.m_iterator = m_iterator;
1619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return clone;
1639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Tells if this is a reverse axes.  Overrides AxesWalker#isReverseAxes.
1699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return true for this class.
1719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public boolean isReverseAxes()
1739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return m_iterator.isReverse();
1759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Get the current sub-context position.  In order to do the
1799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * reverse axes count, for the moment this re-searches the axes
1809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * up to the predicate.  An optimization on this is to cache
1819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * the nodes searched, but, for the moment, this case is probably
1829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * rare enough that the added complexity isn't worth it.
1839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param predicateIndex The predicate index of the proximity position.
1859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
1869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return The pridicate index, or -1.
1879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  protected int getProximityPosition(int predicateIndex)
1899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if(!isReverseAxes())
1919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      return super.getProximityPosition(predicateIndex);
1929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // A negative predicate index seems to occur with
1949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // (preceding-sibling::*|following-sibling::*)/ancestor::*[position()]/*[position()]
1959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // -sb
1969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if(predicateIndex < 0)
1979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      return -1;
1989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (m_proximityPositions[predicateIndex] <= 0)
2009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
2019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      XPathContext xctxt = getXPathContext();
2029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      try
2039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
2049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        OneStepIterator clone = (OneStepIterator) this.clone();
2059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        int root = getRoot();
2079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        xctxt.pushCurrentNode(root);
2089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        clone.setRoot(root, xctxt);
2099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // clone.setPredicateCount(predicateIndex);
2119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        clone.m_predCount = predicateIndex;
2129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // Count 'em all
2149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        int count = 1;
2159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        int next;
2169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        while (DTM.NULL != (next = clone.nextNode()))
2189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        {
2199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          count++;
2209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
2219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        m_proximityPositions[predicateIndex] += count;
2239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
2249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      catch (CloneNotSupportedException cnse)
2259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
2269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // can't happen
2289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
2299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      finally
2309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
2319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        xctxt.popCurrentNode();
2329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
2339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
2349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return m_proximityPositions[predicateIndex];
2369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
2379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
2399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *  The number of nodes in the list. The range of valid child node indices
2409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * is 0 to <code>length-1</code> inclusive.
2419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
2429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return The number of nodes in the list, always greater or equal to zero.
2439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
2449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public int getLength()
2459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
2469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if(!isReverseAxes())
2479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      return super.getLength();
2489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Tell if this is being called from within a predicate.
2509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    boolean isPredicateTest = (this == m_execContext.getSubContextList());
2519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // And get how many total predicates are part of this step.
2539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    int predCount = getPredicateCount();
2549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // If we have already calculated the length, and the current predicate
2569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // is the first predicate, then return the length.  We don't cache
2579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // the anything but the length of the list to the first predicate.
2589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (-1 != m_length && isPredicateTest && m_predicateIndex < 1)
2599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson       return m_length;
2609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    int count = 0;
2629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    XPathContext xctxt = getXPathContext();
2649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    try
2659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
2669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      OneStepIterator clone = (OneStepIterator) this.cloneWithReset();
2679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      int root = getRoot();
2699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      xctxt.pushCurrentNode(root);
2709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      clone.setRoot(root, xctxt);
2719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      clone.m_predCount = m_predicateIndex;
2739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      int next;
2759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      while (DTM.NULL != (next = clone.nextNode()))
2779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
2789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        count++;
2799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
2809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
2819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    catch (CloneNotSupportedException cnse)
2829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
2839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson       // can't happen
2849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
2859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    finally
2869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
2879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      xctxt.popCurrentNode();
2889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
2899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (isPredicateTest && m_predicateIndex < 1)
2909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_length = count;
2919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return count;
2939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
2949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
2959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
2969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Count backwards one proximity position.
2979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
2989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param i The predicate index.
2999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
3009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  protected void countProximityPosition(int i)
3019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
3029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if(!isReverseAxes())
3039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      super.countProximityPosition(i);
3049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    else if (i < m_proximityPositions.length)
3059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_proximityPositions[i]--;
3069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
3079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
3099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Reset the iterator.
3109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
3119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void reset()
3129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
3139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    super.reset();
3159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if(null != m_iterator)
3169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_iterator.reset();
3179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
3189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
3209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Returns the axis being iterated, if it is known.
3219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
3229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
3239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * types.
3249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
3259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public int getAxis()
3269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
3279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return m_axis;
3289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
3299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
3319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @see Expression#deepEquals(Expression)
3329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
3339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public boolean deepEquals(Expression expr)
3349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
3359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  	if(!super.deepEquals(expr))
3369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  		return false;
3379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  	if(m_axis != ((OneStepIterator)expr).m_axis)
3399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  		return false;
3409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  	return true;
3429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
3439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
3459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
346