KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > tree > TreeEnumeration


1 package net.sf.saxon.tree;
2 import net.sf.saxon.om.AxisIterator;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.LookaheadIterator;
5 import net.sf.saxon.pattern.NodeTest;
6
7 abstract class TreeEnumeration implements AxisIterator, LookaheadIterator {
8
9     protected NodeImpl start;
10     protected NodeImpl next;
11     protected NodeTest nodeTest;
12     protected NodeImpl current = null;
13     protected int position = 0;
14     //protected int last = -1;
15

16     /**
17     * Create an axis enumeration for a given type and name of node, from a given
18     * origin node
19      * @param origin the node from which the axis originates
20      * @param nodeTest test to be satisfied by the returned nodes, or null if all nodes
21      * are to be returned.
22     */

23
24     public TreeEnumeration(NodeImpl origin, NodeTest nodeTest) {
25         next = origin;
26         start = origin;
27         this.nodeTest = nodeTest;
28     }
29
30     /**
31     * Test whether a node conforms to the node type and name constraints.
32     * Note that this returns true if the supplied node is null, this is a way of
33     * terminating a loop.
34     */

35
36     protected boolean conforms(NodeImpl node) {
37         if (node==null || nodeTest==null) {
38             return true;
39         }
40         return nodeTest.matches(node);
41     }
42
43     /**
44     * Advance along the axis until a node is found that matches the required criteria
45     */

46
47     protected final void advance() {
48         do {
49             step();
50         } while (!conforms(next));
51     }
52
53     /**
54     * Advance one step along the axis: the resulting node might not meet the required
55     * criteria for inclusion
56     */

57
58     protected abstract void step();
59
60     /**
61      * Determine whether there are more items to come. Note that this operation
62      * is stateless and it is not necessary (or usual) to call it before calling
63      * next(). It is used only when there is an explicit need to tell if we
64      * are at the last element.
65      *
66      * @return true if there are more items in the sequence
67      */

68
69     public boolean hasNext() {
70         return next != null;
71     }
72
73     /**
74     * Return the next node in the enumeration
75     */

76
77     public final Item next() {
78         if (next==null) {
79             current = null;
80             position = -1;
81             return null;
82         } else {
83             current = next;
84             position++;
85             advance();
86             return current;
87         }
88     }
89
90     /**
91     * Return the current Item
92     */

93
94     public final Item current() {
95         return current;
96     }
97
98     /**
99     * Return the current position
100     */

101
102     public final int position() {
103         return position;
104     }
105
106     /**
107      * Get properties of this iterator, as a bit-significant integer.
108      *
109      * @return the properties of this iterator. This will be some combination of
110      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
111      * and {@link LOOKAHEAD}. It is always
112      * acceptable to return the value zero, indicating that there are no known special properties.
113      * It is acceptable for the properties of the iterator to change depending on its state.
114      */

115
116     public int getProperties() {
117         return LOOKAHEAD;
118     }
119
120     /**
121      * Indicate that any nodes returned in the sequence will be atomized. This
122      * means that if it wishes to do so, the implementation can return the typed
123      * values of the nodes rather than the nodes themselves. The implementation
124      * is free to ignore this hint.
125      * @param atomizing true if the caller of this iterator will atomize any
126      * nodes that are returned, and is therefore willing to accept the typed
127      * value of the nodes instead of the nodes themselves.
128      */

129
130     //public void setIsAtomizing(boolean atomizing) {}
131
}
132
133
134 //
135
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
136
// you may not use this file except in compliance with the License. You may obtain a copy of the
137
// License at http://www.mozilla.org/MPL/
138
//
139
// Software distributed under the License is distributed on an "AS IS" basis,
140
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
141
// See the License for the specific language governing rights and limitations under the License.
142
//
143
// The Original Code is: all this file.
144
//
145
// The Initial Developer of the Original Code is Michael H. Kay.
146
//
147
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
148
//
149
// Contributor(s): none.
150
//
151
Popular Tags