01: package net.sf.saxon.tinytree;
02:
03: import net.sf.saxon.om.AxisIteratorImpl;
04: import net.sf.saxon.om.Item;
05: import net.sf.saxon.om.NodeInfo;
06: import net.sf.saxon.om.SequenceIterator;
07: import net.sf.saxon.pattern.NodeTest;
08:
09: /**
10: * This class enumerates the ancestor:: or ancestor-or-self:: axes,
11: * starting at a given node. The start node will never be the root.
12: */
13:
14: final class AncestorEnumeration extends AxisIteratorImpl {
15:
16: private TinyNodeImpl startNode;
17: private NodeTest test;
18: private boolean includeSelf;
19:
20: public AncestorEnumeration(TinyNodeImpl node, NodeTest nodeTest,
21: boolean includeSelf) {
22: test = nodeTest;
23: this .startNode = node;
24: this .includeSelf = includeSelf;
25: current = startNode;
26: }
27:
28: public Item next() {
29: if (position == 0 && includeSelf && test.matches(startNode)) {
30: current = startNode;
31: position = 1;
32: return current;
33: } else {
34: NodeInfo node = ((NodeInfo) current).getParent();
35: while (node != null && !test.matches(node)) {
36: node = node.getParent();
37: }
38: current = node;
39: if (node == null) {
40: position = -1;
41: } else {
42: position++;
43: }
44: return current;
45: }
46: }
47:
48: /**
49: * Get another enumeration of the same nodes
50: */
51:
52: public SequenceIterator getAnother() {
53: return new AncestorEnumeration(startNode, test, includeSelf);
54: }
55:
56: }
57:
58: //
59: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
60: // you may not use this file except in compliance with the License. You may obtain a copy of the
61: // License at http://www.mozilla.org/MPL/
62: //
63: // Software distributed under the License is distributed on an "AS IS" basis,
64: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
65: // See the License for the specific language governing rights and limitations under the License.
66: //
67: // The Original Code is: all this file.
68: //
69: // The Initial Developer of the Original Code is Michael H. Kay.
70: //
71: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
72: //
73: // Contributor(s): none.
74: //
|