KickJava   Java API By Example, From Geeks To Geeks.

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


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 child:: and following-sibling:: axes, which are
8 * identical except for the route to the first candidate node.
9 * It enumerates either the children or the following siblings of the specified node.
10 * In the case of children, the specified node must always
11 * be a node that has children: to ensure this, construct the enumeration
12 * using NodeInfo#getEnumeration()
13 */

14     
15 final class SiblingEnumeration implements AxisEnumeration {
16
17     TinyDocumentImpl document;
18     int nextNodeNr;
19     NodeTest test;
20     TinyNodeImpl startNode;
21     TinyNodeImpl parentNode;
22     boolean getChildren;
23     int last = -1;
24
25     protected SiblingEnumeration(TinyDocumentImpl doc, TinyNodeImpl node,
26                               NodeTest nodeTest, boolean getChildren) {
27         document = doc;
28         test = nodeTest;
29         startNode = node;
30         this.getChildren = getChildren;
31         if (getChildren) { // child:: axis
32
parentNode = node;
33         
34             // move to first child
35
nextNodeNr = node.nodeNr + 1;
36             
37         } else { // following-sibling:: axis
38
parentNode = (TinyNodeImpl)node.getParent();
39             
40             // move to next sibling
41
nextNodeNr = doc.next[node.nodeNr];
42         }
43         
44         // check if this matches the conditions
45
if (nextNodeNr >= 0) {
46             if (!nodeTest.matches(document.nodeType[nextNodeNr],
47                                   document.nameCode[nextNodeNr])) {
48                 advance();
49             }
50         }
51     }
52
53     public boolean hasMoreElements() {
54         return nextNodeNr >= 0;
55     }
56
57     public NodeInfo nextElement() {
58         TinyNodeImpl node = document.getNode(nextNodeNr);
59         node.setParentNode(parentNode);
60         advance();
61         return node;
62     }
63
64     private void advance() {
65         do {
66             nextNodeNr = document.next[nextNodeNr];
67         } while ( nextNodeNr >= 0 &&
68                 !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 true;
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         SiblingEnumeration enum =
91             new SiblingEnumeration(document, startNode, test, getChildren);
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