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