001: package net.sf.saxon.tinytree;
002:
003: import net.sf.saxon.om.AxisIteratorImpl;
004: import net.sf.saxon.om.Item;
005: import net.sf.saxon.om.SequenceIterator;
006: import net.sf.saxon.pattern.NodeTest;
007: import net.sf.saxon.style.StandardNames;
008:
009: /**
010: * This class supports both the descendant:: and descendant-or-self:: axes, which are
011: * identical except for the route to the first candidate node.
012: * It enumerates descendants of the specified node.
013: * The calling code must ensure that the start node is not an attribute or namespace node.
014: */
015:
016: final class DescendantEnumeration extends AxisIteratorImpl {
017:
018: private TinyTree tree;
019: private TinyNodeImpl startNode;
020: private boolean includeSelf;
021: private int nextNodeNr;
022: private int startDepth;
023: private NodeTest test;
024:
025: /**
026: * Create an iterator over the descendant axis
027: * @param doc the containing TinyTree
028: * @param node the node whose descendants are required
029: * @param nodeTest test to be satisfied by each returned node
030: * @param includeSelf true if the start node is to be included
031: */
032:
033: DescendantEnumeration(TinyTree doc, TinyNodeImpl node,
034: NodeTest nodeTest, boolean includeSelf) {
035: tree = doc;
036: startNode = node;
037: this .includeSelf = includeSelf;
038: test = nodeTest;
039: nextNodeNr = node.nodeNr;
040: startDepth = doc.depth[nextNodeNr];
041: }
042:
043: public Item next() {
044: if (position == 0 && includeSelf && test.matches(startNode)) {
045: current = startNode;
046: position++;
047: return current;
048: }
049:
050: do {
051: nextNodeNr++;
052: try {
053: if (tree.depth[nextNodeNr] <= startDepth) {
054: nextNodeNr = -1;
055: current = null;
056: position = -1;
057: return null;
058: }
059: } catch (ArrayIndexOutOfBoundsException e) {
060: // this shouldn't happen. If it does happen, it means the tree wasn't properly closed
061: // during construction (there is no stopper node at the end). In this case, we'll recover
062: // by returning end-of sequence
063: nextNodeNr = -1;
064: current = null;
065: position = -1;
066: return null;
067: }
068: } while (!test.matches(tree, nextNodeNr));
069:
070: position++;
071: if (isAtomizing()
072: && tree.getTypeAnnotation(nextNodeNr) == StandardNames.XDT_UNTYPED) {
073: current = tree.getUntypedAtomicValue(nextNodeNr);
074: } else {
075: current = tree.getNode(nextNodeNr);
076: }
077:
078: return current;
079: }
080:
081: /**
082: * Get another enumeration of the same nodes
083: */
084:
085: public SequenceIterator getAnother() {
086: return new DescendantEnumeration(tree, startNode, test,
087: includeSelf);
088: }
089: }
090:
091: //
092: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
093: // you may not use this file except in compliance with the License. You may obtain a copy of the
094: // License at http://www.mozilla.org/MPL/
095: //
096: // Software distributed under the License is distributed on an "AS IS" basis,
097: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
098: // See the License for the specific language governing rights and limitations under the License.
099: //
100: // The Original Code is: all this file.
101: //
102: // The Initial Developer of the Original Code is Michael H. Kay.
103: //
104: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
105: //
106: // Contributor(s): none.
107: //
|