KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.tree;
2 import net.sf.saxon.om.*;
3 import net.sf.saxon.pattern.NameTest;
4 import net.sf.saxon.pattern.NodeTest;
5 import net.sf.saxon.type.Type;
6
7 /**
8 * AttributeEnumeration is an enumeration of all the attribute nodes of an Element.
9 */

10
11 final class AttributeEnumeration extends AxisIteratorImpl implements LookaheadIterator {
12
13     private ElementImpl element;
14     private NodeTest nodeTest;
15     private NodeInfo next;
16     private int index;
17     private int length;
18
19     /**
20     * Constructor
21     * @param node: the element whose attributes are required. This may be any type of node,
22     * but if it is not an element the enumeration will be empty
23     * @param nodeTest: condition to be applied to the names of the attributes selected
24     */

25
26     public AttributeEnumeration(NodeImpl node, NodeTest nodeTest) {
27
28         this.nodeTest = nodeTest;
29
30         if (node.getNodeKind()==Type.ELEMENT) {
31             element = (ElementImpl)node;
32             AttributeCollection attlist = element.getAttributeList();
33             index = 0;
34
35             if (nodeTest instanceof NameTest) {
36                 NameTest test = (NameTest)nodeTest;
37                 index = attlist.getIndexByFingerprint(test.getFingerprint());
38
39                 if (index<0) {
40                     next = null;
41                 } else {
42                     next = new AttributeImpl(element, index);
43                     index = 0;
44                     length = 0; // force iteration to select one node only
45
}
46
47             } else {
48                 index = 0;
49                 length = attlist.getLength();
50                 advance();
51             }
52         }
53         else { // if it's not an element, or if we're not looking for attributes,
54
// then there's nothing to find
55
next = null;
56             index = 0;
57             length = 0;
58         }
59     }
60
61     /**
62     * Test if there are mode nodes still to come.
63     * ("elements" is used here in the sense of the Java enumeration class, not in the XML sense)
64     */

65
66     public boolean hasNext() {
67         return next != null;
68     }
69
70     /**
71     * Get the next node in the iteration, or null if there are no more.
72     */

73
74     public Item next() {
75         if (next == null) {
76             current = null;
77             position = -1;
78             return null;
79         } else {
80             current = next;
81             position++;
82             advance();
83             return current;
84         }
85     }
86
87     /**
88     * Move to the next node in the enumeration.
89     */

90
91     private void advance() {
92         do {
93             if (index<length) {
94                 next = new AttributeImpl(element, index);
95                 index++;
96             } else {
97                 next = null;
98                 return;
99             }
100         } while (!nodeTest.matches(next));
101     }
102
103     /**
104     * Get another enumeration of the same nodes
105     */

106
107     public SequenceIterator getAnother() {
108         return new AttributeEnumeration(element, nodeTest);
109     }
110
111     /**
112      * Get properties of this iterator, as a bit-significant integer.
113      *
114      * @return the properties of this iterator. This will be some combination of
115      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
116      * and {@link LOOKAHEAD}. It is always
117      * acceptable to return the value zero, indicating that there are no known special properties.
118      * It is acceptable for the properties of the iterator to change depending on its state.
119      */

120
121     public int getProperties() {
122         return LOOKAHEAD;
123     }
124 }
125
126
127
128 //
129
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
130
// you may not use this file except in compliance with the License. You may obtain a copy of the
131
// License at http://www.mozilla.org/MPL/
132
//
133
// Software distributed under the License is distributed on an "AS IS" basis,
134
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
135
// See the License for the specific language governing rights and limitations under the License.
136
//
137
// The Original Code is: all this file.
138
//
139
// The Initial Developer of the Original Code is Michael H. Kay.
140
//
141
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
142
//
143
// Contributor(s): none.
144
//
145
Popular Tags