1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.dtm.ref;
  58. import org.apache.xml.dtm.*;
  59. /**
  60. * This class serves as a default base for implementations of DTMAxisIterators.
  61. */
  62. public abstract class DTMAxisIteratorBase implements DTMAxisIterator
  63. {
  64. /** The position of the last node within the iteration, as defined by XPath.
  65. * Note that this is _not_ the node's handle within the DTM. Also, don't
  66. * confuse it with the current (most recently returned) position.
  67. */
  68. private int _last = -1;
  69. /** The position of the current node within the iteration, as defined by XPath.
  70. * Note that this is _not_ the node's handle within the DTM!
  71. */
  72. private int _position = 0;
  73. /** The position of the marked node within the iteration;
  74. * a saved itaration state that we may want to come back to.
  75. * Note that only one mark is maintained; there is no stack.
  76. */
  77. protected int _markedNode;
  78. /** The handle to the start, or root, of the iteration.
  79. * Set this to END to construct an empty iterator.
  80. */
  81. protected int _startNode = DTMAxisIterator.END;
  82. /** True if the start node should be considered part of the iteration.
  83. * False will cause it to be skipped.
  84. */
  85. protected boolean _includeSelf = false;
  86. /** True if this iteration can be restarted. False otherwise (eg, if
  87. * we are iterating over a stream that can not be re-scanned, or if
  88. * the iterator was produced by cloning another iterator.)
  89. */
  90. protected boolean _isRestartable = true;
  91. /**
  92. * Get start to END should 'close' the iterator,
  93. * i.e. subsequent call to next() should return END.
  94. *
  95. * @return The root node of the iteration.
  96. */
  97. public int getStartNode()
  98. {
  99. return _startNode;
  100. }
  101. /**
  102. * @return A DTMAxisIterator which has been reset to the start node,
  103. * which may or may not be the same as this iterator.
  104. * */
  105. public DTMAxisIterator reset()
  106. {
  107. final boolean temp = _isRestartable;
  108. _isRestartable = true;
  109. setStartNode(_startNode);
  110. _isRestartable = temp;
  111. return this;
  112. }
  113. /**
  114. * Set the flag to include the start node in the iteration.
  115. *
  116. *
  117. * @return This default method returns just returns this DTMAxisIterator,
  118. * after setting the flag.
  119. * (Returning "this" permits C++-style chaining of
  120. * method calls into a single expression.)
  121. */
  122. public DTMAxisIterator includeSelf()
  123. {
  124. _includeSelf = true;
  125. return this;
  126. }
  127. /** Returns the position of the last node within the iteration, as
  128. * defined by XPath. In a forward iterator, I believe this equals the number of nodes which this
  129. * iterator will yield. In a reverse iterator, I believe it should return
  130. * 1 (since the "last" is the first produced.)
  131. *
  132. * This may be an expensive operation when called the first time, since
  133. * it may have to iterate through a large part of the document to produce
  134. * its answer.
  135. *
  136. * @return The number of nodes in this iterator (forward) or 1 (reverse).
  137. */
  138. public int getLast()
  139. {
  140. if (_last == -1) // Not previously established
  141. {
  142. // Note that we're doing both setMark() -- which saves _currentChild
  143. // -- and explicitly saving our position counter (number of nodes
  144. // yielded so far).
  145. //
  146. // %REVIEW% Should position also be saved by setMark()?
  147. // (It wasn't in the XSLTC version, but I don't understand why not.)
  148. final int temp = _position; // Save state
  149. setMark();
  150. reset(); // Count the nodes found by this iterator
  151. do
  152. {
  153. _last++;
  154. }
  155. while (next() != END);
  156. gotoMark(); // Restore saved state
  157. _position = temp;
  158. }
  159. return _last;
  160. }
  161. /**
  162. * @return The position of the current node within the set, as defined by
  163. * XPath. Note that this is one-based, not zero-based.
  164. */
  165. public int getPosition()
  166. {
  167. return _position == 0 ? 1 : _position;
  168. }
  169. /**
  170. * @return true if this iterator has a reversed axis, else false
  171. */
  172. public boolean isReverse()
  173. {
  174. return false;
  175. }
  176. /**
  177. * Returns a deep copy of this iterator. Cloned iterators may not be
  178. * restartable. The iterator being cloned may or may not become
  179. * non-restartable as a side effect of this operation.
  180. *
  181. * @return a deep copy of this iterator.
  182. */
  183. public DTMAxisIterator cloneIterator()
  184. {
  185. try
  186. {
  187. final DTMAxisIteratorBase clone = (DTMAxisIteratorBase) super.clone();
  188. // clone._isRestartable = false;
  189. // return clone.reset();
  190. return clone;
  191. }
  192. catch (CloneNotSupportedException e)
  193. {
  194. throw new org.apache.xml.utils.WrappedRuntimeException(e);
  195. }
  196. }
  197. /**
  198. * Do any final cleanup that is required before returning the node that was
  199. * passed in, and then return it. The intended use is
  200. * <br />
  201. * <code>return returnNode(node);</code>
  202. *
  203. * %REVIEW% If we're calling it purely for side effects, should we really
  204. * be bothering with a return value? Something like
  205. * <br />
  206. * <code> accept(node); return node; </code>
  207. * <br />
  208. * would probably optimize just about as well and avoid questions
  209. * about whether what's returned could ever be different from what's
  210. * passed in.
  211. *
  212. * @param node Node handle which iteration is about to yield.
  213. *
  214. * @return The node handle passed in. */
  215. protected final int returnNode(final int node)
  216. {
  217. _position++;
  218. return node;
  219. }
  220. /**
  221. * Reset the position to zero. NOTE that this does not change the iteration
  222. * state, only the position number associated with that state.
  223. *
  224. * %REVIEW% Document when this would be used?
  225. *
  226. * @return This instance.
  227. */
  228. protected final DTMAxisIterator resetPosition()
  229. {
  230. _position = 0;
  231. return this;
  232. }
  233. /**
  234. * Returns true if all the nodes in the iteration well be returned in document
  235. * order.
  236. *
  237. * @return true as a default.
  238. */
  239. public boolean isDocOrdered()
  240. {
  241. return true;
  242. }
  243. /**
  244. * Returns the axis being iterated, if it is known.
  245. *
  246. * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
  247. * types.
  248. */
  249. public int getAxis()
  250. {
  251. return -1;
  252. }
  253. }