KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > tree > TreeEnumeration


1 package com.icl.saxon.tree;
2 import com.icl.saxon.om.NodeInfo;
3 import com.icl.saxon.om.AxisEnumeration;
4 import com.icl.saxon.pattern.NodeTest;
5
6
7 abstract class TreeEnumeration implements AxisEnumeration {
8
9     protected NodeImpl start;
10     protected NodeImpl next;
11     protected NodeTest nodeTest;
12     protected int last=-1;
13
14     /**
15     * Create an axis enumeration for a given type and name of node, from a given
16     * origin node
17     */

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

30
31     protected boolean conforms(NodeImpl node) {
32         if (node==null) return true;
33         return nodeTest.matches(node);
34     }
35
36     /**
37     * Advance along the axis until a node is found that matches the required criteria
38     */

39
40     protected final void advance() {
41         do {
42             step();
43         } while (!conforms(next));
44     }
45
46     /**
47     * Advance one step along the axis: the resulting node might not meet the required
48     * criteria for inclusion
49     */

50
51     protected abstract void step();
52
53     /**
54     * Determine if there are more nodes to be returned
55     */

56
57     public final boolean hasMoreElements() {
58         return next!=null;
59     }
60
61     /**
62     * Return the next node in the enumeration
63     */

64
65     public final NodeInfo nextElement() {
66         NodeInfo n = next;
67         advance();
68         return n;
69     }
70
71     /**
72     * Determine if the nodes are guaranteed to be sorted in document order
73     */

74
75     public boolean isSorted() {
76         return false; // unless otherwise specified
77
}
78
79     /**
80     * Determine if the nodes are guaranteed to be sorted in reverse document order
81     */

82
83     public boolean isReverseSorted() {
84         return !isSorted();
85     }
86
87     /**
88     * Determine if the nodes are guaranteed to be peers (i.e. no node is a descendant of
89     * another node)
90     */

91
92     public boolean isPeer() {
93         return false; // unless otherwise specified
94
}
95
96     /**
97     * Count the number of nodes in the enumeration. This is used to support
98     * finding the last() position. Note that it must be used on a "clean"
99     * enumeration: the enumeration must be positioned at the start, and is left
100     * positioned at the end.
101     */

102
103     protected int count() {
104         int i=0;
105         while (hasMoreElements()) {
106             nextElement();
107             i++;
108         }
109         return i;
110     }
111 }
112
113
114 //
115
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
116
// you may not use this file except in compliance with the License. You may obtain a copy of the
117
// License at http://www.mozilla.org/MPL/
118
//
119
// Software distributed under the License is distributed on an "AS IS" basis,
120
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
121
// See the License for the specific language governing rights and limitations under the License.
122
//
123
// The Original Code is: all this file.
124
//
125
// The Initial Developer of the Original Code is
126
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
127
//
128
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
129
//
130
// Contributor(s): none.
131
//
132
Popular Tags