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;
  58. /**
  59. * <code>DTMIterators</code> are used to step through a (possibly
  60. * filtered) set of nodes. Their API is modeled largely after the DOM
  61. * NodeIterator.
  62. *
  63. * <p>A DTMIterator is a somewhat unusual type of iterator, in that it
  64. * can serve both single node iteration and random access.</p>
  65. *
  66. * <p>The DTMIterator's traversal semantics, i.e. how it walks the tree,
  67. * are specified when it is created, possibly and probably by an XPath
  68. * <a href="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
  69. * a <a href="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.</p>
  70. *
  71. * <p>A DTMIterator is meant to be created once as a master static object, and
  72. * then cloned many times for runtime use. Or the master object itself may
  73. * be used for simpler use cases.</p>
  74. *
  75. * <p>At this time, we do not expect DTMIterator to emulate
  76. * NodeIterator's "maintain relative position" semantics under
  77. * document mutation. It's likely to respond more like the
  78. * TreeWalker's "current node" semantics. However, since the base DTM
  79. * is immutable, this issue currently makes no practical
  80. * difference.</p>
  81. *
  82. * <p>State: In progress!!</p> */
  83. public interface DTMIterator
  84. {
  85. // Constants returned by acceptNode, borrowed from the DOM Traversal chapter
  86. // %REVIEW% Should we explicitly initialize them from, eg,
  87. // org.w3c.dom.traversal.NodeFilter.FILTER_ACCEPT?
  88. /**
  89. * Accept the node.
  90. */
  91. public static final short FILTER_ACCEPT = 1;
  92. /**
  93. * Reject the node. Same behavior as FILTER_SKIP. (In the DOM these
  94. * differ when applied to a TreeWalker but have the same result when
  95. * applied to a NodeIterator).
  96. */
  97. public static final short FILTER_REJECT = 2;
  98. /**
  99. * Skip this single node.
  100. */
  101. public static final short FILTER_SKIP = 3;
  102. /**
  103. * Get an instance of a DTM that "owns" a node handle. Since a node
  104. * iterator may be passed without a DTMManager, this allows the
  105. * caller to easily get the DTM using just the iterator.
  106. *
  107. * @param nodeHandle the nodeHandle.
  108. *
  109. * @return a non-null DTM reference.
  110. */
  111. public DTM getDTM(int nodeHandle);
  112. /**
  113. * Get an instance of the DTMManager. Since a node
  114. * iterator may be passed without a DTMManager, this allows the
  115. * caller to easily get the DTMManager using just the iterator.
  116. *
  117. * @return a non-null DTMManager reference.
  118. */
  119. public DTMManager getDTMManager();
  120. /**
  121. * The root node of the <code>DTMIterator</code>, as specified when it
  122. * was created. Note the root node is not the root node of the
  123. * document tree, but the context node from where the iteration
  124. * begins and ends.
  125. *
  126. * @return nodeHandle int Handle of the context node.
  127. */
  128. public int getRoot();
  129. /**
  130. * Reset the root node of the <code>DTMIterator</code>, overriding
  131. * the value specified when it was created. Note the root node is
  132. * not the root node of the document tree, but the context node from
  133. * where the iteration begins.
  134. *
  135. * @param nodeHandle int Handle of the context node.
  136. * @param environment The environment object.
  137. * The environment in which this iterator operates, which should provide:
  138. * <ul>
  139. * <li>a node (the context node... same value as "root" defined below) </li>
  140. * <li>a pair of non-zero positive integers (the context position and the context size) </li>
  141. * <li>a set of variable bindings </li>
  142. * <li>a function library </li>
  143. * <li>the set of namespace declarations in scope for the expression.</li>
  144. * <ul>
  145. *
  146. * <p>At this time the exact implementation of this environment is application
  147. * dependent. Probably a proper interface will be created fairly soon.</p>
  148. *
  149. */
  150. public void setRoot(int nodeHandle, Object environment);
  151. /**
  152. * Reset the iterator to the start. After resetting, the next node returned
  153. * will be the root node -- or, if that's filtered out, the first node
  154. * within the root's subtree which is _not_ skipped by the filters.
  155. */
  156. public void reset();
  157. /**
  158. * This attribute determines which node types are presented via the
  159. * iterator. The available set of constants is defined above.
  160. * Nodes not accepted by
  161. * <code>whatToShow</code> will be skipped, but their children may still
  162. * be considered.
  163. *
  164. * @return one of the SHOW_XXX constants, or several ORed together.
  165. */
  166. public int getWhatToShow();
  167. /**
  168. * <p>The value of this flag determines whether the children of entity
  169. * reference nodes are visible to the iterator. If false, they and
  170. * their descendants will be rejected. Note that this rejection takes
  171. * precedence over <code>whatToShow</code> and the filter. </p>
  172. *
  173. * <p> To produce a view of the document that has entity references
  174. * expanded and does not expose the entity reference node itself, use
  175. * the <code>whatToShow</code> flags to hide the entity reference node
  176. * and set <code>expandEntityReferences</code> to true when creating the
  177. * iterator. To produce a view of the document that has entity reference
  178. * nodes but no entity expansion, use the <code>whatToShow</code> flags
  179. * to show the entity reference node and set
  180. * <code>expandEntityReferences</code> to false.</p>
  181. *
  182. * <p>NOTE: In Xalan's use of DTM we will generally have fully expanded
  183. * entity references when the document tree was built, and thus this
  184. * flag will have no effect.</p>
  185. *
  186. * @return true if entity references will be expanded. */
  187. public boolean getExpandEntityReferences();
  188. /**
  189. * Returns the next node in the set and advances the position of the
  190. * iterator in the set. After a <code>DTMIterator</code> has setRoot called,
  191. * the first call to <code>nextNode()</code> returns that root or (if it
  192. * is rejected by the filters) the first node within its subtree which is
  193. * not filtered out.
  194. * @return The next node handle in the set being iterated over, or
  195. * -1 if there are no more members in that set.
  196. */
  197. public int nextNode();
  198. /**
  199. * Returns the previous node in the set and moves the position of the
  200. * <code>DTMIterator</code> backwards in the set.
  201. * @return The previous node handle in the set being iterated over,
  202. * or <code>null</code> if there are no more members in that set.
  203. */
  204. public int previousNode();
  205. /**
  206. * Detaches the <code>DTMIterator</code> from the set which it iterated
  207. * over, releasing any computational resources and placing the iterator
  208. * in the INVALID state. After <code>detach</code> has been invoked,
  209. * calls to <code>nextNode</code> or <code>previousNode</code> will
  210. * raise a runtime exception.
  211. */
  212. public void detach();
  213. /**
  214. * Specify if it's OK for detach to release the iterator for reuse.
  215. *
  216. * @param allowRelease true if it is OK for detach to release this iterator
  217. * for pooling.
  218. */
  219. public void allowDetachToRelease(boolean allowRelease);
  220. /**
  221. * Get the current node in the iterator. Note that this differs from
  222. * the DOM's NodeIterator, where the current position lies between two
  223. * nodes (as part of the maintain-relative-position semantic).
  224. *
  225. * @return The current node handle, or -1.
  226. */
  227. public int getCurrentNode();
  228. /**
  229. * Tells if this NodeSetDTM is "fresh", in other words, if
  230. * the first nextNode() that is called will return the
  231. * first node in the set.
  232. *
  233. * @return true if the iteration of this list has not yet begun.
  234. */
  235. public boolean isFresh();
  236. //========= Random Access ==========
  237. /**
  238. * If setShouldCacheNodes(true) is called, then nodes will
  239. * be cached, enabling random access, and giving the ability to do
  240. * sorts and the like. They are not cached by default.
  241. *
  242. * %REVIEW% Shouldn't the other random-access methods throw an exception
  243. * if they're called on a DTMIterator with this flag set false?
  244. *
  245. * @param b true if the nodes should be cached.
  246. */
  247. public void setShouldCacheNodes(boolean b);
  248. /**
  249. * Tells if this iterator can have nodes added to it or set via
  250. * the <code>setItem(int node, int index)</code> method.
  251. *
  252. * @return True if the nodelist can be mutated.
  253. */
  254. public boolean isMutable();
  255. /** Get the current position within the cached list, which is one
  256. * less than the next nextNode() call will retrieve. i.e. if you
  257. * call getCurrentPos() and the return is 0, the next fetch will
  258. * take place at index 1.
  259. *
  260. * @return The position of the iteration.
  261. */
  262. public int getCurrentPos();
  263. /**
  264. * If an index is requested, NodeSetDTM will call this method
  265. * to run the iterator to the index. By default this sets
  266. * m_next to the index. If the index argument is -1, this
  267. * signals that the iterator should be run to the end and
  268. * completely fill the cache.
  269. *
  270. * @param index The index to run to, or -1 if the iterator should be run
  271. * to the end.
  272. */
  273. public void runTo(int index);
  274. /**
  275. * Set the current position in the node set.
  276. *
  277. * @param i Must be a valid index.
  278. */
  279. public void setCurrentPos(int i);
  280. /**
  281. * Returns the <code>node handle</code> of an item in the collection. If
  282. * <code>index</code> is greater than or equal to the number of nodes in
  283. * the list, this returns <code>null</code>.
  284. *
  285. * @param index of the item.
  286. * @return The node handle at the <code>index</code>th position in the
  287. * <code>DTMIterator</code>, or <code>-1</code> if that is not a valid
  288. * index.
  289. */
  290. public int item(int index);
  291. /**
  292. * Sets the node at the specified index of this vector to be the
  293. * specified node. The previous component at that position is discarded.
  294. *
  295. * <p>The index must be a value greater than or equal to 0 and less
  296. * than the current size of the vector.
  297. * The iterator must be in cached mode.</p>
  298. *
  299. * <p>Meant to be used for sorted iterators.</p>
  300. *
  301. * @param node Node to set
  302. * @param index Index of where to set the node
  303. */
  304. public void setItem(int node, int index);
  305. /**
  306. * The number of nodes in the list. The range of valid child node indices
  307. * is 0 to <code>length-1</code> inclusive. Note that this requires running
  308. * the iterator to completion, and presumably filling the cache.
  309. *
  310. * @return The number of nodes in the list.
  311. */
  312. public int getLength();
  313. //=========== Cloning operations. ============
  314. /**
  315. * Get a cloned Iterator that is reset to the start of the iteration.
  316. *
  317. * @return A clone of this iteration that has been reset.
  318. *
  319. * @throws CloneNotSupportedException
  320. */
  321. public DTMIterator cloneWithReset() throws CloneNotSupportedException;
  322. /**
  323. * Get a clone of this iterator, but don't reset the iteration in the
  324. * process, so that it may be used from the current position.
  325. *
  326. * @return A clone of this object.
  327. *
  328. * @throws CloneNotSupportedException
  329. */
  330. public Object clone() throws CloneNotSupportedException;
  331. /**
  332. * Returns true if all the nodes in the iteration well be returned in document
  333. * order.
  334. *
  335. * @return true if all the nodes in the iteration well be returned in document
  336. * order.
  337. */
  338. public boolean isDocOrdered();
  339. /**
  340. * Returns the axis being iterated, if it is known.
  341. *
  342. * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
  343. * types.
  344. */
  345. public int getAxis();
  346. }