KickJava   Java API By Example, From Geeks To Geeks.

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


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 all the nodes on the preceding axis from a given start node.
8 * The calling code ensures that the start node is not a root, attribute,
9 * or namespace node. As well as the standard XPath preceding axis, this
10 * class also implements a Saxon-specific "preceding-or-ancestor" axis
11 * which returns ancestor nodes as well as preceding nodes. This is used
12 * when performing xsl:number level="any".
13 */

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

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