KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.tinytree;
2 import com.icl.saxon.pattern.NodeTest;
3 import com.icl.saxon.om.NodeInfo;
4 import com.icl.saxon.om.AxisEnumeration;
5
6 /**
7 * Enumerate the following axis starting at a given node.
8 * The start node must not be a namespace or attribute node.
9 */

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

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