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