2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 * $Id: DTMAxisIteratorBase.java 468653 2006-10-28 07:07:05Z minchau $
21 package org.apache.xml.dtm.ref;
23 import org.apache.xml.dtm.DTMAxisIterator;
26 * This class serves as a default base for implementations of mutable
29 public abstract class DTMAxisIteratorBase implements DTMAxisIterator
32 /** The position of the last node within the iteration, as defined by XPath.
33 * Note that this is _not_ the node's handle within the DTM. Also, don't
34 * confuse it with the current (most recently returned) position.
36 protected int _last = -1;
38 /** The position of the current node within the iteration, as defined by XPath.
39 * Note that this is _not_ the node's handle within the DTM!
41 protected int _position = 0;
43 /** The position of the marked node within the iteration;
44 * a saved itaration state that we may want to come back to.
45 * Note that only one mark is maintained; there is no stack.
47 protected int _markedNode;
49 /** The handle to the start, or root, of the iteration.
50 * Set this to END to construct an empty iterator.
52 protected int _startNode = DTMAxisIterator.END;
54 /** True if the start node should be considered part of the iteration.
55 * False will cause it to be skipped.
57 protected boolean _includeSelf = false;
59 /** True if this iteration can be restarted. False otherwise (eg, if
60 * we are iterating over a stream that can not be re-scanned, or if
61 * the iterator was produced by cloning another iterator.)
63 protected boolean _isRestartable = true;
66 * Get start to END should 'close' the iterator,
67 * i.e. subsequent call to next() should return END.
69 * @return The root node of the iteration.
71 public int getStartNode()
77 * @return A DTMAxisIterator which has been reset to the start node,
78 * which may or may not be the same as this iterator.
80 public DTMAxisIterator reset()
83 final boolean temp = _isRestartable;
85 _isRestartable = true;
87 setStartNode(_startNode);
89 _isRestartable = temp;
95 * Set the flag to include the start node in the iteration.
98 * @return This default method returns just returns this DTMAxisIterator,
99 * after setting the flag.
100 * (Returning "this" permits C++-style chaining of
101 * method calls into a single expression.)
103 public DTMAxisIterator includeSelf()
111 /** Returns the position of the last node within the iteration, as
112 * defined by XPath. In a forward iterator, I believe this equals the number of nodes which this
113 * iterator will yield. In a reverse iterator, I believe it should return
114 * 1 (since the "last" is the first produced.)
116 * This may be an expensive operation when called the first time, since
117 * it may have to iterate through a large part of the document to produce
120 * @return The number of nodes in this iterator (forward) or 1 (reverse).
125 if (_last == -1) // Not previously established
127 // Note that we're doing both setMark() -- which saves _currentChild
128 // -- and explicitly saving our position counter (number of nodes
131 // %REVIEW% Should position also be saved by setMark()?
132 // (It wasn't in the XSLTC version, but I don't understand why not.)
134 final int temp = _position; // Save state
137 reset(); // Count the nodes found by this iterator
142 while (next() != END);
144 gotoMark(); // Restore saved state
152 * @return The position of the current node within the set, as defined by
153 * XPath. Note that this is one-based, not zero-based.
155 public int getPosition()
157 return _position == 0 ? 1 : _position;
161 * @return true if this iterator has a reversed axis, else false
163 public boolean isReverse()
169 * Returns a deep copy of this iterator. Cloned iterators may not be
170 * restartable. The iterator being cloned may or may not become
171 * non-restartable as a side effect of this operation.
173 * @return a deep copy of this iterator.
175 public DTMAxisIterator cloneIterator()
180 final DTMAxisIteratorBase clone = (DTMAxisIteratorBase) super.clone();
182 clone._isRestartable = false;
184 // return clone.reset();
187 catch (CloneNotSupportedException e)
189 throw new org.apache.xml.utils.WrappedRuntimeException(e);
194 * Do any final cleanup that is required before returning the node that was
195 * passed in, and then return it. The intended use is
197 * <code>return returnNode(node);</code>
199 * %REVIEW% If we're calling it purely for side effects, should we really
200 * be bothering with a return value? Something like
202 * <code> accept(node); return node; </code>
204 * would probably optimize just about as well and avoid questions
205 * about whether what's returned could ever be different from what's
208 * @param node Node handle which iteration is about to yield.
210 * @return The node handle passed in. */
211 protected final int returnNode(final int node)
219 * Reset the position to zero. NOTE that this does not change the iteration
220 * state, only the position number associated with that state.
222 * %REVIEW% Document when this would be used?
224 * @return This instance.
226 protected final DTMAxisIterator resetPosition()
235 * Returns true if all the nodes in the iteration well be returned in document
238 * @return true as a default.
240 public boolean isDocOrdered()
246 * Returns the axis being iterated, if it is known.
248 * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
256 public void setRestartable(boolean isRestartable) {
257 _isRestartable = isRestartable;
261 * Return the node at the given position.
263 * @param position The position
264 * @return The node at the given position.
266 public int getNodeByPosition(int position)
269 final int pos = isReverse() ? getLast() - position + 1
272 while ((node = next()) != DTMAxisIterator.END) {
273 if (pos == getPosition()) {