KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > tinytree > PrecedingEnumeration


1 package net.sf.saxon.tinytree;
2 import net.sf.saxon.om.AxisIteratorImpl;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.pattern.NodeTest;
6
7 /**
8 * Enumerate all the nodes on the preceding axis from a given start node.
9 * The calling code ensures that the start node is not a root, attribute,
10 * or namespace node. As well as the standard XPath preceding axis, this
11 * class also implements a Saxon-specific "preceding-or-ancestor" axis
12 * which returns ancestor nodes as well as preceding nodes. This is used
13 * when performing xsl:number level="any".
14 */

15
16 final class PrecedingEnumeration extends AxisIteratorImpl {
17
18     private TinyTree tree;
19     private TinyNodeImpl startNode;
20     private NodeTest test;
21     private int nextAncestorDepth;
22     private boolean includeAncestors;
23
24     public PrecedingEnumeration(TinyTree doc, TinyNodeImpl node,
25                                 NodeTest nodeTest, boolean includeAncestors) {
26
27         this.includeAncestors = includeAncestors;
28         test = nodeTest;
29         tree = doc;
30         startNode = node;
31         current = startNode;
32         nextAncestorDepth = doc.depth[node.nodeNr] - 1;
33     }
34
35     public Item next() {
36         int nextNodeNr = ((TinyNodeImpl)current).nodeNr;
37         while (true) {
38             nextNodeNr--;
39             if (!includeAncestors) {
40                 // skip over ancestor elements
41
while (nextAncestorDepth >= 0 && tree.depth[nextNodeNr] == nextAncestorDepth) {
42                     if (nextAncestorDepth-- <= 0) { // bug 1121528
43
current = null;
44                         position = -1;
45                         return null;
46                     };
47                     nextNodeNr--;
48                 }
49             } else if (tree.depth[nextNodeNr] == 0) {
50                 current = null;
51                 position = -1;
52                 return null;
53             }
54             if (test.matches(tree, nextNodeNr)) {
55                 position++;
56                 current = tree.getNode(nextNodeNr);
57                 return current;
58             }
59             if (tree.depth[nextNodeNr] == 0) {
60                 current = null;
61                 position = -1;
62                 return null;
63             }
64         }
65     }
66
67     /**
68     * Get another enumeration of the same nodes
69     */

70
71     public SequenceIterator getAnother() {
72         return new PrecedingEnumeration(tree, startNode, test, includeAncestors);
73     }
74 }
75
76
77
78
79 //
80
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
81
// you may not use this file except in compliance with the License. You may obtain a copy of the
82
// License at http://www.mozilla.org/MPL/
83
//
84
// Software distributed under the License is distributed on an "AS IS" basis,
85
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
86
// See the License for the specific language governing rights and limitations under the License.
87
//
88
// The Original Code is: all this file.
89
//
90
// The Initial Developer of the Original Code is Michael H. Kay.
91
//
92
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
93
//
94
// Contributor(s): none.
95
//
96
Popular Tags