KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > tree > AttributeEnumeration


1 package com.icl.saxon.tree;
2 //import com.icl.saxon.*;
3
import com.icl.saxon.om.NodeInfo;
4 import com.icl.saxon.om.AxisEnumeration;
5 import com.icl.saxon.pattern.NodeTest;
6 import com.icl.saxon.pattern.NameTest;
7
8 /**
9 * AttributeEnumeration is an enumeration of all the attribute nodes of an Element.
10 */

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

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

69
70     public boolean hasMoreElements() {
71         return next != null;
72     }
73
74     /**
75     * Get the next node in the enumeration.
76     * ("elements" is used here in the sense of the Java enumeration class, not in the XML sense)
77     */

78
79     public NodeInfo nextElement() {
80         NodeInfo node = next;
81         advance();
82         return node;
83     }
84
85     /**
86     * Move to the next node in the enumeration.
87     */

88
89     private void advance() {
90         do {
91             if (index<length) {
92                 next = new AttributeImpl(element, index);
93                 index++;
94             } else {
95                 next = null;
96                 return;
97             }
98         } while (!nodeTest.matches(next));
99     }
100
101     public boolean isSorted() {
102         return true; // in the sense that there is no need to sort them again
103
}
104
105     public boolean isReverseSorted() {
106         return false;
107     }
108
109     public boolean isPeer() {
110         return true;
111     }
112
113     /**
114     * Get the last position, that is the number of nodes in the enumeration
115     */

116
117     public int getLastPosition() {
118         if (last >= 0) return last;
119         AttributeEnumeration enum =
120             new AttributeEnumeration(element, nodeTest);
121         last=0;
122         while (enum.hasMoreElements()) {
123             enum.nextElement();
124             last++;
125         }
126         return last;
127     }
128 }
129
130
131
132 //
133
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
134
// you may not use this file except in compliance with the License. You may obtain a copy of the
135
// License at http://www.mozilla.org/MPL/
136
//
137
// Software distributed under the License is distributed on an "AS IS" basis,
138
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
139
// See the License for the specific language governing rights and limitations under the License.
140
//
141
// The Original Code is: all this file.
142
//
143
// The Initial Developer of the Original Code is
144
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
145
//
146
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
147
//
148
// Contributor(s): none.
149
//
150
Popular Tags