KickJava   Java API By Example, From Geeks To Geeks.

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


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.om.NodeInfo;
6 import net.sf.saxon.pattern.NameTest;
7 import net.sf.saxon.pattern.NodeTest;
8 import net.sf.saxon.type.Type;
9 import net.sf.saxon.value.UntypedAtomicValue;
10 import net.sf.saxon.style.StandardNames;
11
12 /**
13 * AttributeEnumeration is an iterator over all the attribute nodes of an Element.
14 */

15
16 final class AttributeEnumeration extends AxisIteratorImpl {
17
18     private TinyTree tree;
19     private int element;
20     private NodeTest nodeTest;
21     private int index;
22
23     /**
24     * Constructor. Note: this constructor will only be called if the relevant node
25     * is an element and if it has one or more attributes. Otherwise an EmptyEnumeration
26     * will be constructed instead.
27     * @param tree: the containing TinyTree
28     * @param element: the node number of the element whose attributes are required
29     * @param nodeTest: condition to be applied to the names of the attributes selected
30     */

31
32     AttributeEnumeration(TinyTree tree, int element, NodeTest nodeTest) {
33
34         this.nodeTest = nodeTest;
35         this.tree = tree;
36         this.element = element;
37         index = tree.alpha[element];
38     }
39
40     /**
41     * Get the next node in the iteration.
42     */

43
44     public Item next() {
45         while (true) {
46             if (index >= tree.numberOfAttributes || tree.attParent[index] != element) {
47                 index = Integer.MAX_VALUE;
48                 current = null;
49                 position = -1;
50                 return null;
51             }
52             int typeCode = tree.getAttributeAnnotation(index);
53             if ((typeCode & NodeInfo.IS_DTD_TYPE) != 0) {
54                 typeCode = StandardNames.XDT_UNTYPED_ATOMIC;
55             }
56             if (nodeTest.matches(Type.ATTRIBUTE, tree.attCode[index], typeCode)) {
57                 position++;
58                 int nodeNr = index++;
59                 if (nodeTest instanceof NameTest) {
60                     // there can only be one match, so abandon the search after this node
61
index = Integer.MAX_VALUE;
62                 }
63                 if (isAtomizing() && typeCode == StandardNames.XDT_UNTYPED_ATOMIC) {
64                     // optimization: avoid creating the Node object if not needed
65
current = new UntypedAtomicValue(tree.attValue[nodeNr]);
66                     return current;
67                 } else {
68                     current = tree.getAttributeNode(nodeNr);
69                     return current;
70                 }
71             }
72             index++;
73         }
74     }
75
76     /**
77     * Get another iteration over the same nodes
78     */

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