KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.tinytree;
2 import com.icl.saxon.pattern.NodeTest;
3 import com.icl.saxon.om.AxisEnumeration;
4 import com.icl.saxon.om.NodeInfo;
5
6 /**
7 * This class enumerates the ancestor:: or ancestor-or-self:: axes,
8 * starting at a given node. The start node will never be the root.
9 */

10
11 final class AncestorEnumeration implements AxisEnumeration {
12
13     private int nextNodeNr;
14     private TinyDocumentImpl document;
15     private TinyNodeImpl node;
16     private NodeTest test;
17     private TinyNodeImpl first = null;
18     private boolean includeSelf;
19     private int last = -1;
20
21     public AncestorEnumeration(TinyDocumentImpl doc, TinyNodeImpl node,
22                                 NodeTest nodeTest, boolean includeSelf) {
23         document = doc;
24         test = nodeTest;
25         this.node = node;
26         this.includeSelf = includeSelf;
27         if (includeSelf && nodeTest.matches(node)) {
28             first = node;
29         }
30
31         // this code is designed to catch the case where the first node
32
// is an attribute or namespace node
33

34         TinyNodeImpl next = (TinyNodeImpl)node.getParent();
35         nextNodeNr = next.nodeNr;
36         if (!nodeTest.matches(next)) {
37             advance();
38         }
39     }
40
41     public boolean hasMoreElements() {
42         return first != null || nextNodeNr >= 0;
43     }
44
45     public NodeInfo nextElement() {
46         if (first!=null) {
47             NodeInfo n = first;
48             first = null;
49             return n;
50         } else {
51             TinyNodeImpl node = document.getNode(nextNodeNr);
52             advance();
53             return node;
54         }
55     }
56
57     private void advance() {
58         int parentDepth = document.depth[nextNodeNr] - 1;
59         do {
60             do {
61                 nextNodeNr--;
62                 if (nextNodeNr<0) return;
63             } while (document.depth[nextNodeNr] > parentDepth);
64             if (test.matches(document.nodeType[nextNodeNr],
65                               document.nameCode[nextNodeNr])) {
66                 return;
67             }
68             parentDepth--;
69         } while ( nextNodeNr >= 0);
70     }
71
72     public boolean isSorted() {
73         return false;
74     }
75
76     public boolean isReverseSorted() {
77         return true;
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         AncestorEnumeration enum =
91             new AncestorEnumeration(document, node, 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
105 //
106
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
107
// you may not use this file except in compliance with the License. You may obtain a copy of the
108
// License at http://www.mozilla.org/MPL/
109
//
110
// Software distributed under the License is distributed on an "AS IS" basis,
111
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
112
// See the License for the specific language governing rights and limitations under the License.
113
//
114
// The Original Code is: all this file.
115
//
116
// The Initial Developer of the Original Code is
117
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
118
//
119
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
120
//
121
// Contributor(s): none.
122
//
123
Popular Tags