KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.tinytree;
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 TinyDocumentImpl doc;
15     private int element;
16     private NodeTest nodeTest;
17     private int index;
18     private int last = -1;
19
20     /**
21     * Constructor. Note: this constructor will only be called if the relevant node
22     * is an element and if it has one or more attributes. Otherwise an EmptyEnumeration
23     * will be constructed instead.
24     * @param node: the element whose attributes are required. This may be any type of node,
25     * but if it is not an element the enumeration will be empty
26     * @param nodeType: the type of node required. This may be any type of node,
27     * but if it is not an attribute the enumeration will be empty
28     * @param nameTest: condition to be applied to the names of the attributes selected
29     */

30
31     protected AttributeEnumeration(TinyDocumentImpl doc, int element, NodeTest nodeTest) {
32
33         this.nodeTest = nodeTest;
34         this.doc = doc;
35         this.element = element;
36         
37         index = doc.offset[element];
38         advance();
39     }
40
41     /**
42     * Test if there are mode nodes still to come.
43     * ("elements" is used here in the sense of the Java enumeration class, not in the XML sense)
44     */

45
46     public boolean hasMoreElements() {
47         return index >=0;
48     }
49
50     /**
51     * Get the next node in the enumeration.
52     * ("elements" is used here in the sense of the Java enumeration class, not in the XML sense)
53     */

54
55     public NodeInfo nextElement() {
56         int node = index++;
57         if (nodeTest instanceof NameTest) {
58             // there can only be one match, so abandon the search now
59
index = -1;
60         } else {
61             advance();
62         }
63         return doc.getAttributeNode(node);
64     }
65
66     /**
67     * Move to the next node in the enumeration.
68     */

69
70     private void advance() {
71         do {
72             if (index >= doc.numberOfAttributes || doc.attParent[index] != element) {
73                 index = -1;
74                 return;
75             }
76             if (nodeTest.matches(NodeInfo.ATTRIBUTE, doc.attCode[index])) {
77                 return;
78             }
79             index++;
80         } while (true);
81     }
82
83     public boolean isSorted() {
84         return true; // in the sense that there is no need to sort them again
85
}
86
87     public boolean isReverseSorted() {
88         return false;
89     }
90
91     public boolean isPeer() {
92         return true;
93     }
94
95     /**
96     * Get the last position, that is the number of nodes in the enumeration
97     */

98
99     public int getLastPosition() {
100         if (last>=0) return last;
101         AttributeEnumeration enum =
102             new AttributeEnumeration(doc, element, nodeTest);
103         last = 0;
104         while (enum.hasMoreElements()) {
105             enum.nextElement();
106             last++;
107         }
108         return last;
109     }
110 }
111
112
113
114 //
115
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
116
// you may not use this file except in compliance with the License. You may obtain a copy of the
117
// License at http://www.mozilla.org/MPL/
118
//
119
// Software distributed under the License is distributed on an "AS IS" basis,
120
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
121
// See the License for the specific language governing rights and limitations under the License.
122
//
123
// The Original Code is: all this file.
124
//
125
// The Initial Developer of the Original Code is
126
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
127
//
128
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
129
//
130
// Contributor(s): none.
131
//
132
Popular Tags