KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > tinytree > DescendantEnumeration


1 package net.sf.saxon.tinytree;
2 import net.sf.saxon.om.AxisIteratorImpl;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.pattern.NodeTest;
6 import net.sf.saxon.style.StandardNames;
7
8 /**
9 * This class supports both the descendant:: and descendant-or-self:: axes, which are
10 * identical except for the route to the first candidate node.
11 * It enumerates descendants of the specified node.
12 * The calling code must ensure that the start node is not an attribute or namespace node.
13 */

14
15 final class DescendantEnumeration extends AxisIteratorImpl {
16
17     private TinyTree tree;
18     private TinyNodeImpl startNode;
19     private boolean includeSelf;
20     private int nextNodeNr;
21     private int startDepth;
22     private NodeTest test;
23
24     /**
25      * Create an iterator over the descendant axis
26      * @param doc the containing TinyTree
27      * @param node the node whose descendants are required
28      * @param nodeTest test to be satisfied by each returned node
29      * @param includeSelf true if the start node is to be included
30      */

31
32     DescendantEnumeration(TinyTree doc, TinyNodeImpl node, NodeTest nodeTest, boolean includeSelf) {
33         tree = doc;
34         startNode = node;
35         this.includeSelf = includeSelf;
36         test = nodeTest;
37         nextNodeNr = node.nodeNr;
38         startDepth = doc.depth[nextNodeNr];
39     }
40
41     public Item next() {
42         if (position==0 && includeSelf && test.matches(startNode)) {
43             current = startNode;
44             position++;
45             return current;
46         }
47
48         do {
49             nextNodeNr++;
50             try {
51                 if (tree.depth[nextNodeNr] <= startDepth) {
52                     nextNodeNr = -1;
53                     current = null;
54                     position = -1;
55                     return null;
56                 }
57             } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
58                 // this shouldn't happen. If it does happen, it means the tree wasn't properly closed
59
// during construction (there is no stopper node at the end). In this case, we'll recover
60
// by returning end-of sequence
61
nextNodeNr = -1;
62                 current = null;
63                 position = -1;
64                 return null;
65             }
66         } while (!test.matches(tree, nextNodeNr));
67
68         position++;
69         if (isAtomizing() && tree.getTypeAnnotation(nextNodeNr) == StandardNames.XDT_UNTYPED) {
70             current = tree.getUntypedAtomicValue(nextNodeNr);
71         } else {
72             current = tree.getNode(nextNodeNr);
73         }
74
75         return current;
76     }
77
78     /**
79     * Get another enumeration of the same nodes
80     */

81
82     public SequenceIterator getAnother() {
83         return new DescendantEnumeration(tree, startNode, test, includeSelf);
84     }
85 }
86
87
88 //
89
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
90
// you may not use this file except in compliance with the License. You may obtain a copy of the
91
// License at http://www.mozilla.org/MPL/
92
//
93
// Software distributed under the License is distributed on an "AS IS" basis,
94
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
95
// See the License for the specific language governing rights and limitations under the License.
96
//
97
// The Original Code is: all this file.
98
//
99
// The Initial Developer of the Original Code is Michael H. Kay.
100
//
101
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
102
//
103
// Contributor(s): none.
104
//
105
Popular Tags