KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.tinytree;
2
3 import net.sf.saxon.om.AttributeCollection;
4 import net.sf.saxon.om.NamePool;
5 import net.sf.saxon.event.LocationProvider;
6 import net.sf.saxon.style.StandardNames;
7
8 /**
9  * An implementation of the AttributeCollection interface based directly on the
10  * TinyTree data structure.
11  */

12
13 public class TinyAttributeCollection implements AttributeCollection {
14
15     int element;
16     TinyTree tree;
17     int firstAttribute;
18
19     public TinyAttributeCollection(TinyTree tree, int element) {
20         this.tree = tree;
21         this.element = element;
22         this.firstAttribute = tree.alpha[element];
23     }
24
25     /**
26      * Set the location provider. This must be set if the methods getSystemId() and getLineNumber()
27      * are to be used to get location information for an attribute.
28      */

29
30     public void setLocationProvider(LocationProvider provider) {
31         //
32
}
33
34     /**
35      * Return the number of attributes in the list.
36      *
37      * @return The number of attributes in the list.
38      */

39
40     public int getLength() {
41         int i = firstAttribute;
42         while (i < tree.numberOfAttributes && tree.attParent[i] == element) {
43             i++;
44         }
45         return i - firstAttribute;
46     }
47
48     /**
49      * Get the namecode of an attribute (by position).
50      *
51      * @param index The position of the attribute in the list.
52      * @return The display name of the attribute as a string, or null if there
53      * is no attribute at that position.
54      */

55
56     public int getNameCode(int index) {
57         return tree.attCode[firstAttribute + index];
58     }
59
60     /**
61      * Get the type annotation of an attribute (by position).
62      *
63      * @param index The position of the attribute in the list.
64      * @return The type annotation of the attribute as the fingerprint of the type name.
65      * The bit {@link net.sf.saxon.om.NodeInfo.IS_DTD_TYPE} represents a DTD-derived type.
66      */

67
68     public int getTypeAnnotation(int index) {
69         if (tree.attTypeCode == null) {
70             return StandardNames.XDT_UNTYPED_ATOMIC;
71         };
72         return tree.attTypeCode[firstAttribute + index];
73     }
74
75     /**
76      * Get the locationID of an attribute (by position)
77      *
78      * @param index The position of the attribute in the list.
79      * @return The location identifier of the attribute. This can be supplied
80      * to a {@link net.sf.saxon.event.LocationProvider} in order to obtain the
81      * actual system identifier and line number of the relevant location
82      */

83
84     public int getLocationId(int index) {
85         return 0;
86     }
87
88     /**
89      * Get the systemId part of the location of an attribute, at a given index.
90      * <p/>
91      * <p>Attribute location information is not available from a SAX parser, so this method
92      * is not useful for getting the location of an attribute in a source document. However,
93      * in a Saxon result document, the location information represents the location in the
94      * stylesheet of the instruction used to generate this attribute, which is useful for
95      * debugging.</p>
96      *
97      * @param index the required attribute
98      * @return the systemId of the location of the attribute
99      */

100
101     public String JavaDoc getSystemId(int index) {
102         return tree.getSystemId(element);
103     }
104
105     /**
106      * Get the line number part of the location of an attribute, at a given index.
107      * <p/>
108      * <p>Attribute location information is not available from a SAX parser, so this method
109      * is not useful for getting the location of an attribute in a source document. However,
110      * in a Saxon result document, the location information represents the location in the
111      * stylesheet of the instruction used to generate this attribute, which is useful for
112      * debugging.</p>
113      *
114      * @param index the required attribute
115      * @return the line number of the location of the attribute
116      */

117
118     public int getLineNumber(int index) {
119         return -1;
120     }
121
122     /**
123      * Get the properties of an attribute (by position)
124      *
125      * @param index The position of the attribute in the list.
126      * @return The properties of the attribute. This is a set
127      * of bit-settings defined in class {@link net.sf.saxon.event.ReceiverOptions}. The
128      * most interesting of these is {{@link net.sf.saxon.event.ReceiverOptions#DEFAULTED_ATTRIBUTE},
129      * which indicates an attribute that was added to an element as a result of schema validation.
130      */

131
132     public int getProperties(int index) {
133         return 0;
134     }
135
136     /**
137      * Get the prefix of the name of an attribute (by position).
138      *
139      * @param index The position of the attribute in the list.
140      * @return The prefix of the attribute name as a string, or null if there
141      * is no attribute at that position. Returns "" for an attribute that
142      * has no prefix.
143      */

144
145     public String JavaDoc getPrefix(int index) {
146         return tree.getNamePool().getPrefix(getNameCode(index));
147     }
148
149     /**
150      * Get the lexical QName of an attribute (by position).
151      *
152      * @param index The position of the attribute in the list.
153      * @return The lexical QName of the attribute as a string, or null if there
154      * is no attribute at that position.
155      */

156
157     public String JavaDoc getQName(int index) {
158         return tree.getNamePool().getDisplayName(getNameCode(index));
159     }
160
161     /**
162      * Get the local name of an attribute (by position).
163      *
164      * @param index The position of the attribute in the list.
165      * @return The local name of the attribute as a string, or null if there
166      * is no attribute at that position.
167      */

168
169     public String JavaDoc getLocalName(int index) {
170         return tree.getNamePool().getLocalName(getNameCode(index));
171     }
172
173     /**
174      * Get the namespace URI of an attribute (by position).
175      *
176      * @param index The position of the attribute in the list.
177      * @return The local name of the attribute as a string, or null if there
178      * is no attribute at that position.
179      */

180
181     public String JavaDoc getURI(int index) {
182         return tree.getNamePool().getURI(getNameCode(index));
183     }
184
185     /**
186      * Get the index of an attribute (by name).
187      *
188      * @param uri The namespace uri of the attribute.
189      * @param localname The local name of the attribute.
190      * @return The index position of the attribute
191      */

192
193     public int getIndex(String JavaDoc uri, String JavaDoc localname) {
194         int fingerprint = tree.getNamePool().getFingerprint(uri, localname);
195         return getIndexByFingerprint(fingerprint);
196     }
197
198     /**
199      * Get the index, given the fingerprint
200      */

201
202     public int getIndexByFingerprint(int fingerprint) {
203         int i = firstAttribute;
204         while (tree.attParent[i] == element) {
205             if ((tree.attCode[i] & NamePool.FP_MASK) == fingerprint) {
206                  return i - firstAttribute;
207             }
208             i++;
209         }
210         return -1;
211     }
212
213     /**
214      * Get the attribute value using its fingerprint
215      */

216
217     public String JavaDoc getValueByFingerprint(int fingerprint) {
218         return getValue(getIndexByFingerprint(fingerprint));
219     }
220
221     /**
222      * Get the value of an attribute (by name).
223      *
224      * @param uri The namespace uri of the attribute.
225      * @param localname The local name of the attribute.
226      * @return The index position of the attribute
227      */

228
229     public String JavaDoc getValue(String JavaDoc uri, String JavaDoc localname) {
230         return getValue(getIndex(uri, localname));
231     }
232
233     /**
234      * Get the value of an attribute (by position).
235      *
236      * @param index The position of the attribute in the list.
237      * @return The attribute value as a string, or null if
238      * there is no attribute at that position.
239      */

240
241     public String JavaDoc getValue(int index) {
242         return tree.attValue[firstAttribute + index].toString();
243     }
244
245     /**
246      * Determine whether a given attribute has the is-ID property set
247      */

248
249     public boolean isId(int index) {
250         return ((getTypeAnnotation(index) & NamePool.FP_MASK) == StandardNames.XS_ID) ||
251                 ((getNameCode(index) & NamePool.FP_MASK) == StandardNames.XML_ID);
252     }
253 }
254
Popular Tags