KickJava   Java API By Example, From Geeks To Geeks.

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


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 the preceding-sibling axis.
8 * The starting node must be an element, text node, comment, or processing instruction:
9 * to ensure this, construct the enumeration using NodeInfo#getEnumeration()
10 */

11     
12 final class PrecedingSiblingEnumeration implements AxisEnumeration {
13
14     TinyDocumentImpl document;
15     TinyNodeImpl startNode;
16     int nextNodeNr;
17     int depth;
18     NodeTest test;
19     TinyNodeImpl parentNode;
20     int last = -1;
21
22     protected PrecedingSiblingEnumeration(TinyDocumentImpl doc, TinyNodeImpl node,
23                               NodeTest nodeTest) {
24         document = doc;
25         document.ensurePriorIndex();
26         test = nodeTest;
27         startNode = node;
28         nextNodeNr = node.nodeNr;
29         depth = doc.depth[nextNodeNr];
30         parentNode = node.parent; // doesn't matter if this is null (unknown)
31
advance();
32     }
33
34     public boolean hasMoreElements() {
35         return nextNodeNr >= 0;
36     }
37
38     public NodeInfo nextElement() {
39         TinyNodeImpl node = document.getNode(nextNodeNr);
40         node.setParentNode(parentNode);
41         advance();
42         return node;
43     }
44
45     private void advance() {
46         do {
47             nextNodeNr = document.prior[nextNodeNr];
48         } while ( nextNodeNr >= 0 &&
49                 !test.matches(document.nodeType[nextNodeNr],
50                               document.nameCode[nextNodeNr]));
51 /*
52         for (int i=nextNodeNr-1; i>=0; i--) {
53             int ndepth = document.depth[i];
54             if (ndepth>=depth) {
55                 if (ndepth==depth &&
56                     test.matches(document.nodeType[i],
57                                   document.nameCode[i] & 0xfffff)) {
58                     nextNodeNr = i;
59                     return;
60                 }
61             } else {
62                 nextNodeNr = -1;
63                 return;
64             }
65         }
66 */

67     }
68
69     public boolean isSorted() {
70         return false;
71     }
72
73     public boolean isReverseSorted() {
74         return true;
75     }
76     
77     public boolean isPeer() {
78         return true;
79     }
80
81     /**
82     * Get the last position, that is the number of nodes in the enumeration
83     */

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