KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > dtm > DTMIterator


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 /**
22
23  * <code>DTMIterators</code> are used to step through a (possibly
24  * filtered) set of nodes. Their API is modeled largely after the DOM
25  * NodeIterator.
26  *
27  * <p>A DTMIterator is a somewhat unusual type of iterator, in that it
28  * can serve both single node iteration and random access.</p>
29  *
30  * <p>The DTMIterator's traversal semantics, i.e. how it walks the tree,
31  * are specified when it is created, possibly and probably by an XPath
32  * <a HREF="http://www.w3.org/TR/xpath#NT-LocationPath>LocationPath</a> or
33  * a <a HREF="http://www.w3.org/TR/xpath#NT-UnionExpr">UnionExpr</a>.</p>
34  *
35  * <p>A DTMIterator is meant to be created once as a master static object, and
36  * then cloned many times for runtime use. Or the master object itself may
37  * be used for simpler use cases.</p>
38  *
39  * <p>At this time, we do not expect DTMIterator to emulate
40  * NodeIterator's "maintain relative position" semantics under
41  * document mutation. It's likely to respond more like the
42  * TreeWalker's "current node" semantics. However, since the base DTM
43  * is immutable, this issue currently makes no practical
44  * difference.</p>
45  *
46  * <p>State: In progress!!</p> */

47 public interface DTMIterator
48 {
49
50   // Constants returned by acceptNode, borrowed from the DOM Traversal chapter
51
// %REVIEW% Should we explicitly initialize them from, eg,
52
// org.w3c.dom.traversal.NodeFilter.FILTER_ACCEPT?
53

54   /**
55    * Accept the node.
56    */

57   public static final short FILTER_ACCEPT = 1;
58
59   /**
60    * Reject the node. Same behavior as FILTER_SKIP. (In the DOM these
61    * differ when applied to a TreeWalker but have the same result when
62    * applied to a NodeIterator).
63    */

64   public static final short FILTER_REJECT = 2;
65
66   /**
67    * Skip this single node.
68    */

69   public static final short FILTER_SKIP = 3;
70     
71   /**
72    * Get an instance of a DTM that "owns" a node handle. Since a node
73    * iterator may be passed without a DTMManager, this allows the
74    * caller to easily get the DTM using just the iterator.
75    *
76    * @param nodeHandle the nodeHandle.
77    *
78    * @return a non-null DTM reference.
79    */

80   public DTM getDTM(int nodeHandle);
81   
82   /**
83    * Get an instance of the DTMManager. Since a node
84    * iterator may be passed without a DTMManager, this allows the
85    * caller to easily get the DTMManager using just the iterator.
86    *
87    * @return a non-null DTMManager reference.
88    */

89   public DTMManager getDTMManager();
90
91   /**
92    * The root node of the <code>DTMIterator</code>, as specified when it
93    * was created. Note the root node is not the root node of the
94    * document tree, but the context node from where the iteration
95    * begins and ends.
96    *
97    * @return nodeHandle int Handle of the context node.
98    */

99   public int getRoot();
100
101   /**
102    * Reset the root node of the <code>DTMIterator</code>, overriding
103    * the value specified when it was created. Note the root node is
104    * not the root node of the document tree, but the context node from
105    * where the iteration begins.
106    *
107    * @param nodeHandle int Handle of the context node.
108    * @param environment The environment object.
109    * The environment in which this iterator operates, which should provide:
110    * <ul>
111    * <li>a node (the context node... same value as "root" defined below) </li>
112    * <li>a pair of non-zero positive integers (the context position and the context size) </li>
113    * <li>a set of variable bindings </li>
114    * <li>a function library </li>
115    * <li>the set of namespace declarations in scope for the expression.</li>
116    * <ul>
117    *
118    * <p>At this time the exact implementation of this environment is application
119    * dependent. Probably a proper interface will be created fairly soon.</p>
120    *
121    */

122   public void setRoot(int nodeHandle, Object JavaDoc environment);
123   
124   /**
125    * Reset the iterator to the start. After resetting, the next node returned
126    * will be the root node -- or, if that's filtered out, the first node
127    * within the root's subtree which is _not_ skipped by the filters.
128    */

129   public void reset();
130
131   /**
132    * This attribute determines which node types are presented via the
133    * iterator. The available set of constants is defined above.
134    * Nodes not accepted by
135    * <code>whatToShow</code> will be skipped, but their children may still
136    * be considered.
137    *
138    * @return one of the SHOW_XXX constants, or several ORed together.
139    */

140   public int getWhatToShow();
141
142   /**
143    * <p>The value of this flag determines whether the children of entity
144    * reference nodes are visible to the iterator. If false, they and
145    * their descendants will be rejected. Note that this rejection takes
146    * precedence over <code>whatToShow</code> and the filter. </p>
147    *
148    * <p> To produce a view of the document that has entity references
149    * expanded and does not expose the entity reference node itself, use
150    * the <code>whatToShow</code> flags to hide the entity reference node
151    * and set <code>expandEntityReferences</code> to true when creating the
152    * iterator. To produce a view of the document that has entity reference
153    * nodes but no entity expansion, use the <code>whatToShow</code> flags
154    * to show the entity reference node and set
155    * <code>expandEntityReferences</code> to false.</p>
156    *
157    * <p>NOTE: In Xalan's use of DTM we will generally have fully expanded
158    * entity references when the document tree was built, and thus this
159    * flag will have no effect.</p>
160    *
161    * @return true if entity references will be expanded. */

162   public boolean getExpandEntityReferences();
163
164   /**
165    * Returns the next node in the set and advances the position of the
166    * iterator in the set. After a <code>DTMIterator</code> has setRoot called,
167    * the first call to <code>nextNode()</code> returns that root or (if it
168    * is rejected by the filters) the first node within its subtree which is
169    * not filtered out.
170    * @return The next node handle in the set being iterated over, or
171    * <code>DTM.NULL</code> if there are no more members in that set.
172    */

173   public int nextNode();
174
175   /**
176    * Returns the previous node in the set and moves the position of the
177    * <code>DTMIterator</code> backwards in the set.
178    * @return The previous node handle in the set being iterated over,
179    * or <code>DTM.NULL</code> if there are no more members in that set.
180    */

181   public int previousNode();
182
183   /**
184    * Detaches the <code>DTMIterator</code> from the set which it iterated
185    * over, releasing any computational resources and placing the iterator
186    * in the INVALID state. After <code>detach</code> has been invoked,
187    * calls to <code>nextNode</code> or <code>previousNode</code> will
188    * raise a runtime exception.
189    */

190   public void detach();
191   
192   /**
193    * Specify if it's OK for detach to release the iterator for reuse.
194    *
195    * @param allowRelease true if it is OK for detach to release this iterator
196    * for pooling.
197    */

198   public void allowDetachToRelease(boolean allowRelease);
199
200   /**
201    * Get the current node in the iterator. Note that this differs from
202    * the DOM's NodeIterator, where the current position lies between two
203    * nodes (as part of the maintain-relative-position semantic).
204    *
205    * @return The current node handle, or -1.
206    */

207   public int getCurrentNode();
208
209   /**
210    * Tells if this NodeSetDTM is "fresh", in other words, if
211    * the first nextNode() that is called will return the
212    * first node in the set.
213    *
214    * @return true if the iteration of this list has not yet begun.
215    */

216   public boolean isFresh();
217
218   //========= Random Access ==========
219

220   /**
221    * If setShouldCacheNodes(true) is called, then nodes will
222    * be cached, enabling random access, and giving the ability to do
223    * sorts and the like. They are not cached by default.
224    *
225    * %REVIEW% Shouldn't the other random-access methods throw an exception
226    * if they're called on a DTMIterator with this flag set false?
227    *
228    * @param b true if the nodes should be cached.
229    */

230   public void setShouldCacheNodes(boolean b);
231   
232   /**
233    * Tells if this iterator can have nodes added to it or set via
234    * the <code>setItem(int node, int index)</code> method.
235    *
236    * @return True if the nodelist can be mutated.
237    */

238   public boolean isMutable();
239
240   /** Get the current position within the cached list, which is one
241    * less than the next nextNode() call will retrieve. i.e. if you
242    * call getCurrentPos() and the return is 0, the next fetch will
243    * take place at index 1.
244    *
245    * @return The position of the iteration.
246    */

247   public int getCurrentPos();
248
249   /**
250    * If an index is requested, NodeSetDTM will call this method
251    * to run the iterator to the index. By default this sets
252    * m_next to the index. If the index argument is -1, this
253    * signals that the iterator should be run to the end and
254    * completely fill the cache.
255    *
256    * @param index The index to run to, or -1 if the iterator should be run
257    * to the end.
258    */

259   public void runTo(int index);
260
261   /**
262    * Set the current position in the node set.
263    *
264    * @param i Must be a valid index.
265    */

266   public void setCurrentPos(int i);
267
268   /**
269    * Returns the <code>node handle</code> of an item in the collection. If
270    * <code>index</code> is greater than or equal to the number of nodes in
271    * the list, this returns <code>null</code>.
272    *
273    * @param index of the item.
274    * @return The node handle at the <code>index</code>th position in the
275    * <code>DTMIterator</code>, or <code>-1</code> if that is not a valid
276    * index.
277    */

278   public int item(int index);
279   
280   /**
281    * Sets the node at the specified index of this vector to be the
282    * specified node. The previous component at that position is discarded.
283    *
284    * <p>The index must be a value greater than or equal to 0 and less
285    * than the current size of the vector.
286    * The iterator must be in cached mode.</p>
287    *
288    * <p>Meant to be used for sorted iterators.</p>
289    *
290    * @param node Node to set
291    * @param index Index of where to set the node
292    */

293   public void setItem(int node, int index);
294   
295   /**
296    * The number of nodes in the list. The range of valid child node indices
297    * is 0 to <code>length-1</code> inclusive. Note that this requires running
298    * the iterator to completion, and presumably filling the cache.
299    *
300    * @return The number of nodes in the list.
301    */

302   public int getLength();
303     
304   //=========== Cloning operations. ============
305

306   /**
307    * Get a cloned Iterator that is reset to the start of the iteration.
308    *
309    * @return A clone of this iteration that has been reset.
310    *
311    * @throws CloneNotSupportedException
312    */

313   public DTMIterator cloneWithReset() throws CloneNotSupportedException JavaDoc;
314
315   /**
316    * Get a clone of this iterator, but don't reset the iteration in the
317    * process, so that it may be used from the current position.
318    *
319    * @return A clone of this object.
320    *
321    * @throws CloneNotSupportedException
322    */

323   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc;
324   
325   /**
326    * Returns true if all the nodes in the iteration well be returned in document
327    * order.
328    *
329    * @return true if all the nodes in the iteration well be returned in document
330    * order.
331    */

332   public boolean isDocOrdered();
333   
334   /**
335    * Returns the axis being iterated, if it is known.
336    *
337    * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
338    * types.
339    */

340   public int getAxis();
341
342 }
343
Popular Tags