KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > tinytree > DescendantEnumeration


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

12     
13 final class DescendantEnumeration implements AxisEnumeration {
14
15     TinyDocumentImpl document;
16     TinyNodeImpl startNode;
17     boolean includeSelf;
18     int nextNodeNr;
19     int startDepth;
20     NodeTest test;
21     int last = -1;
22     TinyNodeImpl parentNode;
23
24     protected DescendantEnumeration(TinyDocumentImpl doc, TinyNodeImpl node,
25                                     NodeTest nodeTest, boolean includeSelf) {
26         document = doc;
27         startNode = node;
28         this.includeSelf = includeSelf;
29         test = nodeTest;
30         nextNodeNr = node.nodeNr;
31         startDepth = doc.depth[nextNodeNr];
32         if (includeSelf) { // descendant-or-self:: axis
33
// no action
34
} else { // descendant:: axis
35
nextNodeNr++;
36             if (doc.depth[nextNodeNr] <= startDepth) {
37                 nextNodeNr = -1;
38             }
39         }
40         
41         // check if this matches the conditions
42
if (nextNodeNr >= 0 &&
43                 nextNodeNr < doc.numberOfNodes &&
44                 !nodeTest.matches(document.nodeType[nextNodeNr],
45                               document.nameCode[nextNodeNr])) {
46             advance();
47         }
48     }
49
50     public boolean hasMoreElements() {
51         return nextNodeNr >= 0;
52     }
53
54     public NodeInfo nextElement() {
55         TinyNodeImpl node = document.getNode(nextNodeNr);
56         advance();
57         return node;
58     }
59
60     private void advance() {
61         do {
62             nextNodeNr++;
63             if (nextNodeNr >= document.numberOfNodes ||
64                 document.depth[nextNodeNr] <= startDepth) {
65                 nextNodeNr = -1;
66                 return;
67             }
68         } while (!test.matches(document.nodeType[nextNodeNr],
69                                 document.nameCode[nextNodeNr]));
70     }
71
72     public boolean isSorted() {
73         return true;
74     }
75
76     public boolean isReverseSorted() {
77         return false;
78     }
79     
80     public boolean isPeer() {
81         return false;
82     }
83
84     /**
85     * Get the last position, that is the number of nodes in the enumeration
86     */

87
88     public int getLastPosition() {
89         if (last>=0) return last;
90         DescendantEnumeration enum =
91             new DescendantEnumeration(document, startNode, test, includeSelf);
92         last = 0;
93         while (enum.hasMoreElements()) {
94             enum.nextElement();
95             last++;
96         }
97         return last;
98     }
99
100 }
101
102
103 //
104
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
105
// you may not use this file except in compliance with the License. You may obtain a copy of the
106
// License at http://www.mozilla.org/MPL/
107
//
108
// Software distributed under the License is distributed on an "AS IS" basis,
109
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
110
// See the License for the specific language governing rights and limitations under the License.
111
//
112
// The Original Code is: all this file.
113
//
114
// The Initial Developer of the Original Code is
115
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
116
//
117
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
118
//
119
// Contributor(s): none.
120
//
121
Popular Tags