1. package org.apache.xpath.axes;
  2. import javax.xml.transform.TransformerException;
  3. import org.apache.xml.dtm.DTM;
  4. import org.apache.xml.dtm.DTMFilter;
  5. import org.apache.xpath.Expression;
  6. import org.apache.xpath.compiler.Compiler;
  7. /**
  8. * <meta name="usage" content="advanced"/>
  9. * This class implements a general iterator for
  10. * those LocationSteps with only one step, and perhaps a predicate,
  11. * that only go forward (i.e. it can not be used with ancestors,
  12. * preceding, etc.)
  13. * @see org.apache.xpath.axes.WalkerFactory#newLocPathIterator
  14. */
  15. public class OneStepIteratorForward extends ChildTestIterator
  16. {
  17. /** The traversal axis from where the nodes will be filtered. */
  18. protected int m_axis = -1;
  19. /**
  20. * Create a OneStepIterator object.
  21. *
  22. * @param compiler A reference to the Compiler that contains the op map.
  23. * @param opPos The position within the op map, which contains the
  24. * location path expression for this itterator.
  25. *
  26. * @throws javax.xml.transform.TransformerException
  27. */
  28. OneStepIteratorForward(Compiler compiler, int opPos, int analysis)
  29. throws javax.xml.transform.TransformerException
  30. {
  31. super(compiler, opPos, analysis);
  32. int firstStepPos = compiler.getFirstChildPos(opPos);
  33. m_axis = WalkerFactory.getAxisFromStep(compiler, firstStepPos);
  34. }
  35. /**
  36. * Create a OneStepIterator object that will just traverse the self axes.
  37. *
  38. * @param axis One of the org.apache.xml.dtm.Axis integers.
  39. *
  40. * @throws javax.xml.transform.TransformerException
  41. */
  42. public OneStepIteratorForward(int axis)
  43. {
  44. super(null);
  45. m_axis = axis;
  46. int whatToShow = DTMFilter.SHOW_ALL;
  47. initNodeTest(whatToShow);
  48. }
  49. /**
  50. * Initialize the context values for this expression
  51. * after it is cloned.
  52. *
  53. * @param execContext The XPath runtime context for this
  54. * transformation.
  55. */
  56. public void setRoot(int context, Object environment)
  57. {
  58. super.setRoot(context, environment);
  59. m_traverser = m_cdtm.getAxisTraverser(m_axis);
  60. }
  61. // /**
  62. // * Return the first node out of the nodeset, if this expression is
  63. // * a nodeset expression. This is the default implementation for
  64. // * nodesets.
  65. // * <p>WARNING: Do not mutate this class from this function!</p>
  66. // * @param xctxt The XPath runtime context.
  67. // * @return the first node out of the nodeset, or DTM.NULL.
  68. // */
  69. // public int asNode(XPathContext xctxt)
  70. // throws javax.xml.transform.TransformerException
  71. // {
  72. // if(getPredicateCount() > 0)
  73. // return super.asNode(xctxt);
  74. //
  75. // int current = xctxt.getCurrentNode();
  76. //
  77. // DTM dtm = xctxt.getDTM(current);
  78. // DTMAxisTraverser traverser = dtm.getAxisTraverser(m_axis);
  79. //
  80. // String localName = getLocalName();
  81. // String namespace = getNamespace();
  82. // int what = m_whatToShow;
  83. //
  84. // // System.out.println("what: ");
  85. // // NodeTest.debugWhatToShow(what);
  86. // if(DTMFilter.SHOW_ALL == what
  87. // || ((DTMFilter.SHOW_ELEMENT & what) == 0)
  88. // || localName == NodeTest.WILD
  89. // || namespace == NodeTest.WILD)
  90. // {
  91. // return traverser.first(current);
  92. // }
  93. // else
  94. // {
  95. // int type = getNodeTypeTest(what);
  96. // int extendedType = dtm.getExpandedTypeID(namespace, localName, type);
  97. // return traverser.first(current, extendedType);
  98. // }
  99. // }
  100. /**
  101. * Get the next node via getFirstAttribute && getNextAttribute.
  102. */
  103. protected int getNextNode()
  104. {
  105. m_lastFetched = (DTM.NULL == m_lastFetched)
  106. ? m_traverser.first(m_context)
  107. : m_traverser.next(m_context, m_lastFetched);
  108. return m_lastFetched;
  109. }
  110. /**
  111. * Returns the axis being iterated, if it is known.
  112. *
  113. * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
  114. * types.
  115. */
  116. public int getAxis()
  117. {
  118. return m_axis;
  119. }
  120. /**
  121. * @see Expression#deepEquals(Expression)
  122. */
  123. public boolean deepEquals(Expression expr)
  124. {
  125. if(!super.deepEquals(expr))
  126. return false;
  127. if(m_axis != ((OneStepIteratorForward)expr).m_axis)
  128. return false;
  129. return true;
  130. }
  131. }