KickJava   Java API By Example, From Geeks To Geeks.

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


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.NodeInfo;
5 import net.sf.saxon.om.SequenceIterator;
6 import net.sf.saxon.pattern.NodeTest;
7
8 /**
9 * This class enumerates the ancestor:: or ancestor-or-self:: axes,
10 * starting at a given node. The start node will never be the root.
11 */

12
13 final class AncestorEnumeration extends AxisIteratorImpl {
14
15     private TinyNodeImpl startNode;
16     private NodeTest test;
17     private boolean includeSelf;
18
19     public AncestorEnumeration(TinyNodeImpl node, NodeTest nodeTest, boolean includeSelf) {
20         test = nodeTest;
21         this.startNode = node;
22         this.includeSelf = includeSelf;
23         current = startNode;
24     }
25
26     public Item next() {
27         if (position==0 && includeSelf && test.matches(startNode)) {
28             current = startNode;
29             position = 1;
30             return current;
31         } else {
32             NodeInfo node = ((NodeInfo)current).getParent();
33             while (node != null && !test.matches(node)) {
34                 node = node.getParent();
35             }
36             current = node;
37             if (node == null) {
38                 position = -1;
39             } else {
40                 position++;
41             }
42             return current;
43         }
44     }
45
46     /**
47     * Get another enumeration of the same nodes
48     */

49
50     public SequenceIterator getAnother() {
51         return new AncestorEnumeration(startNode, test, includeSelf);
52     }
53
54 }
55
56 //
57
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
58
// you may not use this file except in compliance with the License. You may obtain a copy of the
59
// License at http://www.mozilla.org/MPL/
60
//
61
// Software distributed under the License is distributed on an "AS IS" basis,
62
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
63
// See the License for the specific language governing rights and limitations under the License.
64
//
65
// The Original Code is: all this file.
66
//
67
// The Initial Developer of the Original Code is Michael H. Kay.
68
//
69
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
70
//
71
// Contributor(s): none.
72
//
73
Popular Tags