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