001: package net.sf.saxon.tree;
002:
003: import net.sf.saxon.om.AxisIterator;
004: import net.sf.saxon.om.Item;
005: import net.sf.saxon.om.LookaheadIterator;
006: import net.sf.saxon.pattern.NodeTest;
007:
008: abstract class TreeEnumeration implements AxisIterator,
009: LookaheadIterator {
010:
011: protected NodeImpl start;
012: protected NodeImpl next;
013: protected NodeTest nodeTest;
014: protected NodeImpl current = null;
015: protected int position = 0;
016:
017: //protected int last = -1;
018:
019: /**
020: * Create an axis enumeration for a given type and name of node, from a given
021: * origin node
022: * @param origin the node from which the axis originates
023: * @param nodeTest test to be satisfied by the returned nodes, or null if all nodes
024: * are to be returned.
025: */
026:
027: public TreeEnumeration(NodeImpl origin, NodeTest nodeTest) {
028: next = origin;
029: start = origin;
030: this .nodeTest = nodeTest;
031: }
032:
033: /**
034: * Test whether a node conforms to the node type and name constraints.
035: * Note that this returns true if the supplied node is null, this is a way of
036: * terminating a loop.
037: */
038:
039: protected boolean conforms(NodeImpl node) {
040: if (node == null || nodeTest == null) {
041: return true;
042: }
043: return nodeTest.matches(node);
044: }
045:
046: /**
047: * Advance along the axis until a node is found that matches the required criteria
048: */
049:
050: protected final void advance() {
051: do {
052: step();
053: } while (!conforms(next));
054: }
055:
056: /**
057: * Advance one step along the axis: the resulting node might not meet the required
058: * criteria for inclusion
059: */
060:
061: protected abstract void step();
062:
063: /**
064: * Determine whether there are more items to come. Note that this operation
065: * is stateless and it is not necessary (or usual) to call it before calling
066: * next(). It is used only when there is an explicit need to tell if we
067: * are at the last element.
068: *
069: * @return true if there are more items in the sequence
070: */
071:
072: public boolean hasNext() {
073: return next != null;
074: }
075:
076: /**
077: * Return the next node in the enumeration
078: */
079:
080: public final Item next() {
081: if (next == null) {
082: current = null;
083: position = -1;
084: return null;
085: } else {
086: current = next;
087: position++;
088: advance();
089: return current;
090: }
091: }
092:
093: /**
094: * Return the current Item
095: */
096:
097: public final Item current() {
098: return current;
099: }
100:
101: /**
102: * Return the current position
103: */
104:
105: public final int position() {
106: return position;
107: }
108:
109: /**
110: * Get properties of this iterator, as a bit-significant integer.
111: *
112: * @return the properties of this iterator. This will be some combination of
113: * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
114: * and {@link LOOKAHEAD}. It is always
115: * acceptable to return the value zero, indicating that there are no known special properties.
116: * It is acceptable for the properties of the iterator to change depending on its state.
117: */
118:
119: public int getProperties() {
120: return LOOKAHEAD;
121: }
122:
123: /**
124: * Indicate that any nodes returned in the sequence will be atomized. This
125: * means that if it wishes to do so, the implementation can return the typed
126: * values of the nodes rather than the nodes themselves. The implementation
127: * is free to ignore this hint.
128: * @param atomizing true if the caller of this iterator will atomize any
129: * nodes that are returned, and is therefore willing to accept the typed
130: * value of the nodes instead of the nodes themselves.
131: */
132:
133: //public void setIsAtomizing(boolean atomizing) {}
134: }
135:
136: //
137: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
138: // you may not use this file except in compliance with the License. You may obtain a copy of the
139: // License at http://www.mozilla.org/MPL/
140: //
141: // Software distributed under the License is distributed on an "AS IS" basis,
142: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
143: // See the License for the specific language governing rights and limitations under the License.
144: //
145: // The Original Code is: all this file.
146: //
147: // The Initial Developer of the Original Code is Michael H. Kay.
148: //
149: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
150: //
151: // Contributor(s): none.
152: //
|