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