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: DTMAxisIteratorBase.java,v 1.11 2004/02/16 23:06:11 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.dtm.ref;
  20. import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  21. /**
  22. * This class serves as a default base for implementations of mutable
  23. * DTMAxisIterators.
  24. */
  25. public abstract class DTMAxisIteratorBase implements DTMAxisIterator
  26. {
  27. /** The position of the last node within the iteration, as defined by XPath.
  28. * Note that this is _not_ the node's handle within the DTM. Also, don't
  29. * confuse it with the current (most recently returned) position.
  30. */
  31. protected int _last = -1;
  32. /** The position of the current node within the iteration, as defined by XPath.
  33. * Note that this is _not_ the node's handle within the DTM!
  34. */
  35. protected int _position = 0;
  36. /** The position of the marked node within the iteration;
  37. * a saved itaration state that we may want to come back to.
  38. * Note that only one mark is maintained; there is no stack.
  39. */
  40. protected int _markedNode;
  41. /** The handle to the start, or root, of the iteration.
  42. * Set this to END to construct an empty iterator.
  43. */
  44. protected int _startNode = DTMAxisIterator.END;
  45. /** True if the start node should be considered part of the iteration.
  46. * False will cause it to be skipped.
  47. */
  48. protected boolean _includeSelf = false;
  49. /** True if this iteration can be restarted. False otherwise (eg, if
  50. * we are iterating over a stream that can not be re-scanned, or if
  51. * the iterator was produced by cloning another iterator.)
  52. */
  53. protected boolean _isRestartable = true;
  54. /**
  55. * Get start to END should 'close' the iterator,
  56. * i.e. subsequent call to next() should return END.
  57. *
  58. * @return The root node of the iteration.
  59. */
  60. public int getStartNode()
  61. {
  62. return _startNode;
  63. }
  64. /**
  65. * @return A DTMAxisIterator which has been reset to the start node,
  66. * which may or may not be the same as this iterator.
  67. * */
  68. public DTMAxisIterator reset()
  69. {
  70. final boolean temp = _isRestartable;
  71. _isRestartable = true;
  72. setStartNode(_startNode);
  73. _isRestartable = temp;
  74. return this;
  75. }
  76. /**
  77. * Set the flag to include the start node in the iteration.
  78. *
  79. *
  80. * @return This default method returns just returns this DTMAxisIterator,
  81. * after setting the flag.
  82. * (Returning "this" permits C++-style chaining of
  83. * method calls into a single expression.)
  84. */
  85. public DTMAxisIterator includeSelf()
  86. {
  87. _includeSelf = true;
  88. return this;
  89. }
  90. /** Returns the position of the last node within the iteration, as
  91. * defined by XPath. In a forward iterator, I believe this equals the number of nodes which this
  92. * iterator will yield. In a reverse iterator, I believe it should return
  93. * 1 (since the "last" is the first produced.)
  94. *
  95. * This may be an expensive operation when called the first time, since
  96. * it may have to iterate through a large part of the document to produce
  97. * its answer.
  98. *
  99. * @return The number of nodes in this iterator (forward) or 1 (reverse).
  100. */
  101. public int getLast()
  102. {
  103. if (_last == -1) // Not previously established
  104. {
  105. // Note that we're doing both setMark() -- which saves _currentChild
  106. // -- and explicitly saving our position counter (number of nodes
  107. // yielded so far).
  108. //
  109. // %REVIEW% Should position also be saved by setMark()?
  110. // (It wasn't in the XSLTC version, but I don't understand why not.)
  111. final int temp = _position; // Save state
  112. setMark();
  113. reset(); // Count the nodes found by this iterator
  114. do
  115. {
  116. _last++;
  117. }
  118. while (next() != END);
  119. gotoMark(); // Restore saved state
  120. _position = temp;
  121. }
  122. return _last;
  123. }
  124. /**
  125. * @return The position of the current node within the set, as defined by
  126. * XPath. Note that this is one-based, not zero-based.
  127. */
  128. public int getPosition()
  129. {
  130. return _position == 0 ? 1 : _position;
  131. }
  132. /**
  133. * @return true if this iterator has a reversed axis, else false
  134. */
  135. public boolean isReverse()
  136. {
  137. return false;
  138. }
  139. /**
  140. * Returns a deep copy of this iterator. Cloned iterators may not be
  141. * restartable. The iterator being cloned may or may not become
  142. * non-restartable as a side effect of this operation.
  143. *
  144. * @return a deep copy of this iterator.
  145. */
  146. public DTMAxisIterator cloneIterator()
  147. {
  148. try
  149. {
  150. final DTMAxisIteratorBase clone = (DTMAxisIteratorBase) super.clone();
  151. clone._isRestartable = false;
  152. // return clone.reset();
  153. return clone;
  154. }
  155. catch (CloneNotSupportedException e)
  156. {
  157. throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
  158. }
  159. }
  160. /**
  161. * Do any final cleanup that is required before returning the node that was
  162. * passed in, and then return it. The intended use is
  163. * <br />
  164. * <code>return returnNode(node);</code>
  165. *
  166. * %REVIEW% If we're calling it purely for side effects, should we really
  167. * be bothering with a return value? Something like
  168. * <br />
  169. * <code> accept(node); return node; </code>
  170. * <br />
  171. * would probably optimize just about as well and avoid questions
  172. * about whether what's returned could ever be different from what's
  173. * passed in.
  174. *
  175. * @param node Node handle which iteration is about to yield.
  176. *
  177. * @return The node handle passed in. */
  178. protected final int returnNode(final int node)
  179. {
  180. _position++;
  181. return node;
  182. }
  183. /**
  184. * Reset the position to zero. NOTE that this does not change the iteration
  185. * state, only the position number associated with that state.
  186. *
  187. * %REVIEW% Document when this would be used?
  188. *
  189. * @return This instance.
  190. */
  191. protected final DTMAxisIterator resetPosition()
  192. {
  193. _position = 0;
  194. return this;
  195. }
  196. /**
  197. * Returns true if all the nodes in the iteration well be returned in document
  198. * order.
  199. *
  200. * @return true as a default.
  201. */
  202. public boolean isDocOrdered()
  203. {
  204. return true;
  205. }
  206. /**
  207. * Returns the axis being iterated, if it is known.
  208. *
  209. * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
  210. * types.
  211. */
  212. public int getAxis()
  213. {
  214. return -1;
  215. }
  216. public void setRestartable(boolean isRestartable) {
  217. _isRestartable = isRestartable;
  218. }
  219. /**
  220. * Return the node at the given position.
  221. *
  222. * @param position The position
  223. * @return The node at the given position.
  224. */
  225. public int getNodeByPosition(int position)
  226. {
  227. if (position > 0) {
  228. final int pos = isReverse() ? getLast() - position + 1
  229. : position;
  230. int node;
  231. while ((node = next()) != DTMAxisIterator.END) {
  232. if (pos == getPosition()) {
  233. return node;
  234. }
  235. }
  236. }
  237. return END;
  238. }
  239. }