Home | History | Annotate | Download | only in axes
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: OneStepIteratorForward.java 469314 2006-10-30 23:31:59Z minchau $
     20  */
     21 package org.apache.xpath.axes;
     22 
     23 import org.apache.xml.dtm.DTM;
     24 import org.apache.xml.dtm.DTMFilter;
     25 import org.apache.xpath.Expression;
     26 import org.apache.xpath.compiler.Compiler;
     27 import org.apache.xpath.compiler.OpMap;
     28 
     29 /**
     30  * This class implements a general iterator for
     31  * those LocationSteps with only one step, and perhaps a predicate,
     32  * that only go forward (i.e. it can not be used with ancestors,
     33  * preceding, etc.)
     34  * @see org.apache.xpath.axes#ChildTestIterator
     35  * @xsl.usage advanced
     36  */
     37 public class OneStepIteratorForward extends ChildTestIterator
     38 {
     39     static final long serialVersionUID = -1576936606178190566L;
     40   /** The traversal axis from where the nodes will be filtered. */
     41   protected int m_axis = -1;
     42 
     43   /**
     44    * Create a OneStepIterator object.
     45    *
     46    * @param compiler A reference to the Compiler that contains the op map.
     47    * @param opPos The position within the op map, which contains the
     48    * location path expression for this itterator.
     49    *
     50    * @throws javax.xml.transform.TransformerException
     51    */
     52   OneStepIteratorForward(Compiler compiler, int opPos, int analysis)
     53           throws javax.xml.transform.TransformerException
     54   {
     55     super(compiler, opPos, analysis);
     56     int firstStepPos = OpMap.getFirstChildPos(opPos);
     57 
     58     m_axis = WalkerFactory.getAxisFromStep(compiler, firstStepPos);
     59 
     60   }
     61 
     62   /**
     63    * Create a OneStepIterator object that will just traverse the self axes.
     64    *
     65    * @param axis One of the org.apache.xml.dtm.Axis integers.
     66    *
     67    * @throws javax.xml.transform.TransformerException
     68    */
     69   public OneStepIteratorForward(int axis)
     70   {
     71     super(null);
     72 
     73     m_axis = axis;
     74     int whatToShow = DTMFilter.SHOW_ALL;
     75     initNodeTest(whatToShow);
     76   }
     77 
     78 
     79 
     80 
     81   /**
     82    * Initialize the context values for this expression
     83    * after it is cloned.
     84    *
     85    * @param context The XPath runtime context for this
     86    * transformation.
     87    */
     88   public void setRoot(int context, Object environment)
     89   {
     90     super.setRoot(context, environment);
     91     m_traverser = m_cdtm.getAxisTraverser(m_axis);
     92 
     93   }
     94 
     95 //  /**
     96 //   * Return the first node out of the nodeset, if this expression is
     97 //   * a nodeset expression.  This is the default implementation for
     98 //   * nodesets.
     99 //   * <p>WARNING: Do not mutate this class from this function!</p>
    100 //   * @param xctxt The XPath runtime context.
    101 //   * @return the first node out of the nodeset, or DTM.NULL.
    102 //   */
    103 //  public int asNode(XPathContext xctxt)
    104 //    throws javax.xml.transform.TransformerException
    105 //  {
    106 //    if(getPredicateCount() > 0)
    107 //      return super.asNode(xctxt);
    108 //
    109 //    int current = xctxt.getCurrentNode();
    110 //
    111 //    DTM dtm = xctxt.getDTM(current);
    112 //    DTMAxisTraverser traverser = dtm.getAxisTraverser(m_axis);
    113 //
    114 //    String localName = getLocalName();
    115 //    String namespace = getNamespace();
    116 //    int what = m_whatToShow;
    117 //
    118 //    // System.out.println("what: ");
    119 //    // NodeTest.debugWhatToShow(what);
    120 //    if(DTMFilter.SHOW_ALL == what
    121 //       || ((DTMFilter.SHOW_ELEMENT & what) == 0)
    122 //       || localName == NodeTest.WILD
    123 //       || namespace == NodeTest.WILD)
    124 //    {
    125 //      return traverser.first(current);
    126 //    }
    127 //    else
    128 //    {
    129 //      int type = getNodeTypeTest(what);
    130 //      int extendedType = dtm.getExpandedTypeID(namespace, localName, type);
    131 //      return traverser.first(current, extendedType);
    132 //    }
    133 //  }
    134 
    135   /**
    136    * Get the next node via getFirstAttribute && getNextAttribute.
    137    */
    138   protected int getNextNode()
    139   {
    140     m_lastFetched = (DTM.NULL == m_lastFetched)
    141                      ? m_traverser.first(m_context)
    142                      : m_traverser.next(m_context, m_lastFetched);
    143     return m_lastFetched;
    144   }
    145 
    146   /**
    147    * Returns the axis being iterated, if it is known.
    148    *
    149    * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
    150    * types.
    151    */
    152   public int getAxis()
    153   {
    154     return m_axis;
    155   }
    156 
    157   /**
    158    * @see Expression#deepEquals(Expression)
    159    */
    160   public boolean deepEquals(Expression expr)
    161   {
    162   	if(!super.deepEquals(expr))
    163   		return false;
    164 
    165   	if(m_axis != ((OneStepIteratorForward)expr).m_axis)
    166   		return false;
    167 
    168   	return true;
    169   }
    170 
    171 
    172 }
    173