KickJava   Java API By Example, From Geeks To Geeks.

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


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
7 /**
8 * Iterate over the following axis starting at a given node.
9 * The start node must not be a namespace or attribute node.
10 */

11
12 final class FollowingEnumeration extends AxisIteratorImpl {
13
14     private TinyTree tree;
15     private TinyNodeImpl startNode;
16     private NodeTest test;
17     private boolean includeDescendants;
18
19     /**
20      * Create an iterator over the following axis
21      * @param doc the containing TinyTree
22      * @param node the start node. If the actual start was an attribute or namespace node, this will
23      * be the parent element of that attribute or namespace
24      * @param nodeTest condition that all the returned nodes must satisfy
25      * @param includeDescendants true if descendants of the start node are to be included. This will
26      * be false if the actual start was an element node, true if it was an attribute or namespace node
27      * (since the children of their parent follow the attribute or namespace in document order).
28      */

29
30     public FollowingEnumeration(TinyTree doc, TinyNodeImpl node,
31                                  NodeTest nodeTest, boolean includeDescendants) {
32         tree = doc;
33         test = nodeTest;
34         startNode = node;
35         this.includeDescendants = includeDescendants;
36     }
37
38     public Item next() {
39         int nodeNr;
40         if (position == 0) {
41             // first time call
42
nodeNr = startNode.nodeNr;
43             int depth = tree.depth[nodeNr];
44
45             // skip the descendant nodes if any
46
if (includeDescendants) {
47                 nodeNr++;
48             } else {
49                 do {
50                     nodeNr++;
51                     if (tree.depth[nodeNr] == 0) {
52                         current = null;
53                         position = -1;
54                         return null;
55                     }
56                 } while (tree.depth[nodeNr] > depth);
57             }
58         } else {
59             nodeNr = ((TinyNodeImpl)current).nodeNr + 1;
60         }
61
62         while (true) {
63             if (tree.depth[nodeNr] == 0) {
64                 current = null;
65                 position = -1;
66                 return null;
67             }
68             if (test.matches(tree, nodeNr)) {
69                 position++;
70                 current = tree.getNode(nodeNr);
71                 return current;
72             }
73             nodeNr++;
74         }
75     }
76
77     /**
78     * Get another enumeration of the same nodes
79     */

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