KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > xml > sax > AttributesImpl


1 package javolution.xml.sax;
2
3 import j2me.lang.CharSequence;
4 import javolution.lang.Reusable;
5 import javolution.util.FastComparator;
6 import javolution.util.FastTable;
7 import javolution.xml.sax.Attributes;
8
9 /**
10  * This class provides a default implementation of the {@link Attributes}
11  * interface.
12  * It is a more generic version of <code>org.xml.sax.AttributesImpl</code> with
13  * <code>String</code> replaced by <code>CharSequence</code>.
14  *
15  * @author <a HREF="mailto:sax@megginson.com">David Megginson</a>
16  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
17  * @author <a HREF="mailto:javolution@arakelian.com">Gregory Arakelian </a>
18  */

19 public final class AttributesImpl implements Attributes, Reusable {
20
21     /**
22      * Holds the entries increment between each attribute.
23      * For each attribute, we store the namespace URI, the local name, the
24      * prefix, the qualified or q-name, the XML data type and the value.
25      */

26     private static final int INCR = 6;
27
28     /**
29      * Holds the current number of attributes.
30      */

31     private int _length;
32
33     /**
34      * Holds the entries.
35      */

36     private FastTable _entries = new FastTable();
37
38     ////////////////////////////////////////////////////////////////////
39
// Constructors.
40
////////////////////////////////////////////////////////////////////
41

42     /**
43      * Default constructor.
44      */

45     public AttributesImpl() {
46     }
47
48     ////////////////////////////////////////////////////////////////////
49
// Implementation of org.xml.sax.Attributes.
50
////////////////////////////////////////////////////////////////////
51

52     /**
53      * Returns the number of attributes in the list.
54      *
55      * @return the number of attributes in the list.
56      */

57     public int getLength() {
58         return _length;
59     }
60
61     /**
62      * Return an attribute namespace URI.
63      *
64      * @param index the attribute index (zero-based).
65      * @return the namespace URI, the empty string if none is available,
66      * or null if the index is out of range.
67      */

68     public CharSequence getURI(int index) {
69         if (index >= 0 && index < _length)
70             return (CharSequence) _entries.get(index * INCR + URI_OFFSET);
71         return null;
72     }
73
74     /**
75      * Return an attribute local name.
76      *
77      * @param index the attribute index (zero-based).
78      * @return The attribute local name, the empty string if none is
79      * available, or null if the index if out of range.
80      */

81     public CharSequence getLocalName(int index) {
82         if (index >= 0 && index < _length)
83             return (CharSequence) _entries
84                     .get(index * INCR + LOCAL_NAME_OFFSET);
85         return null;
86     }
87
88     /**
89      * Return an attribute prefix.
90      *
91      * @param index the attribute index (zero-based).
92      * @return the attribute prefix, the empty string if none is
93      * available, or null if the index is out of bounds.
94      */

95     public CharSequence getPrefix(int index) {
96         if (index >= 0 && index < _length)
97             return (CharSequence) _entries.get(index * INCR + PREFIX_OFFSET);
98         return null;
99     }
100
101     /**
102      * Return an attribute qualified (prefixed) name.
103      *
104      * @param index the attribute index (zero-based).
105      * @return the attribute qualified name, the empty string if none is
106      * available, or null if the index is out of bounds.
107      */

108     public CharSequence getQName(int index) {
109         if (index >= 0 && index < _length)
110             return (CharSequence) _entries.get(index * INCR + QNAME_OFFSET);
111         return null;
112     }
113
114     /**
115      * Return an attribute type by index.
116      *
117      * @param index the attribute index (zero-based).
118      * @return the attribute type, "CDATA" if the type is unknown, or null if
119      * the index is out of bounds.
120      */

121     public String getType(int index) {
122         if (index >= 0 && index < _length)
123             return (String) _entries.get(index * INCR + TYPE_OFFSET);
124         return null;
125
126     }
127
128     /**
129      * Return an attribute value by index.
130      *
131      * @param index he attribute index (zero-based).
132      * @return the attribute value or null if the index is out of bounds.
133      */

134     public CharSequence getValue(int index) {
135         if (index >= 0 && index < _length)
136             return (CharSequence) _entries.get(index * INCR + VALUE_OFFSET);
137         return null;
138     }
139
140     /**
141      * Look up an attribute index by namespace name.
142      * In many cases, it will be more efficient to look up the name once and use
143      * the index query methods rather than using the name query methods
144      * repeatedly.
145      *
146      * @param uri the attribute namespace URI, or the empty string if none is
147      * available.
148      * @param localName the attribute local name.
149      * @return the attribute index, or -1 if none matches.
150      */

151     public int getIndex(CharSequence uri, CharSequence localName) {
152         for (int i = 0; i < _length; i++) {
153             if (FastComparator.LEXICAL.areEqual(_entries.get(i * INCR
154                     + URI_OFFSET), uri)
155                     && FastComparator.LEXICAL.areEqual(_entries.get(i * INCR
156                             + LOCAL_NAME_OFFSET), localName))
157                 return i;
158         }
159         return -1;
160     }
161
162     /**
163      * Equivalent to {@link #getIndex(CharSequence, CharSequence)}
164      * (for J2ME compatibility).
165      */

166     public int getIndex(String uri, String localName) {
167         for (int i = 0; i < _length; i++) {
168             if (FastComparator.LEXICAL.areEqual(_entries.get(i * INCR
169                     + URI_OFFSET), uri)
170                     && FastComparator.LEXICAL.areEqual(_entries.get(i * INCR
171                             + LOCAL_NAME_OFFSET), localName))
172                 return i;
173         }
174         return -1;
175     }
176
177     /**
178      * Look up an attribute index by qualified (prefixed) name.
179      *
180      * @param qName the qualified name.
181      * @return the attribute index, or -1 if none matches.
182      */

183     public int getIndex(CharSequence qName) {
184         for (int i = 0; i < _length; i++) {
185             if (FastComparator.LEXICAL.areEqual(_entries.get(i * INCR
186                     + QNAME_OFFSET), qName))
187                 return i;
188         }
189         return -1;
190     }
191
192     /**
193      * Equivalent to {@link #getIndex(CharSequence)} (for J2ME compatibility).
194      */

195     public int getIndex(String qName) {
196         for (int i = 0; i < _length; i++) {
197             if (FastComparator.LEXICAL.areEqual(_entries.get(i * INCR
198                     + QNAME_OFFSET), qName))
199                 return i;
200         }
201         return -1;
202     }
203
204     /**
205      * Look up an attribute type by namespace-qualified name.
206      *
207      * @param uri the namespace URI, or the empty string for a name with no
208      * explicit Namespace URI.
209      * @param localName he local name.
210      * @return the attribute type, or null if there is no matching attribute.
211      */

212     public String getType(CharSequence uri, CharSequence localName) {
213         final int index = getIndex(uri, localName);
214         if (index >= 0)
215             return (String) _entries.get(index * INCR + TYPE_OFFSET);
216         return null;
217     }
218
219     /**
220      * Equivalent to {@link #getType(CharSequence, CharSequence)}
221      * (for J2ME compatibility).
222      */

223     public String getType(String uri, String localName) {
224         final int index = getIndex(uri, localName);
225         if (index >= 0)
226             return (String) _entries.get(index * INCR + TYPE_OFFSET);
227         return null;
228     }
229
230     /**
231      * Look up an attribute type by qualified (prefixed) name.
232      *
233      * @param qName the qualified name.
234      * @return the attribute type, or null if there is no matching attribute.
235      */

236     public String getType(CharSequence qName) {
237         final int index = getIndex(qName);
238         if (index >= 0)
239             return (String) _entries.get(index * INCR + TYPE_OFFSET);
240         return null;
241     }
242
243     /**
244      * Equivalent to {@link #getType(CharSequence)} (for J2ME compatibility).
245      */

246     public String getType(String qName) {
247         final int index = getIndex(qName);
248         if (index >= 0)
249             return (String) _entries.get(index * INCR + TYPE_OFFSET);
250         return null;
251     }
252
253     /**
254      * Look up an attribute value by namespace-qualified name.
255      *
256      * @param uri the namespace URI, or the empty string for a name with no
257      * explicit namespace URI.
258      * @param localName the local name.
259      * @return the attribute's value, or null if there is no matching attribute.
260      */

261     public CharSequence getValue(CharSequence uri, CharSequence localName) {
262         final int index = getIndex(uri, localName);
263         if (index >= 0)
264             return (CharSequence) _entries.get(index * INCR + VALUE_OFFSET);
265         return null;
266     }
267
268     /**
269      * Equivalent to {@link #getValue(CharSequence, CharSequence)}
270      * (for J2ME compatibility).
271      */

272     public CharSequence getValue(String uri, String localName) {
273         final int index = getIndex(uri, localName);
274         if (index >= 0)
275             return (CharSequence) _entries.get(index * INCR + VALUE_OFFSET);
276         return null;
277     }
278
279     /**
280      * Look up an attribute value by qualified (prefixed) name.
281      *
282      * @param qName the qualified name.
283      * @return the attribute value, or null if there is no matching attribute.
284      */

285     public CharSequence getValue(CharSequence qName) {
286         final int index = getIndex(qName);
287         if (index >= 0)
288             return (CharSequence) _entries.get(index * INCR + VALUE_OFFSET);
289         return null;
290     }
291
292     /**
293      * Equivalent to {@link #getValue(CharSequence)} (for J2ME compatibility).
294      */

295     public CharSequence getValue(String qName) {
296         final int index = getIndex(qName);
297         if (index >= 0)
298             return (CharSequence) _entries.get(index * INCR + VALUE_OFFSET);
299         return null;
300     }
301
302     ////////////////////////////////////////////////////////////////////
303
// Manipulators.
304
////////////////////////////////////////////////////////////////////
305

306     /**
307      * Clear the attribute list for reuse.
308      */

309     public void reset() {
310         _entries.clear();
311         _length = 0;
312     }
313
314     /**
315      * Adds an attribute to the end of the attribute list.
316      *
317      * @param uri the namespace URI or an empty sequence.
318      * @param localName the local name or an empty sequence.
319      * @param prefix the prefix or an empty sequence.
320      * @param qName the qualified (prefixed) name or an empty sequence.
321      * @param type the attribute type as a string.
322      * @param value the attribute value.
323      * @return the attribute index.
324      */

325     public int addAttribute(CharSequence uri, CharSequence localName,
326             CharSequence prefix, CharSequence qName, String type,
327             CharSequence value) {
328         _entries.addLast(uri);
329         _entries.addLast(localName);
330         _entries.addLast(prefix);
331         _entries.addLast(qName);
332         _entries.addLast(type);
333         _entries.addLast(value);
334         return _length++;
335     }
336
337     private static final int URI_OFFSET = 0;
338
339     private static final int LOCAL_NAME_OFFSET = 1;
340
341     private static final int PREFIX_OFFSET = 2;
342
343     private static final int QNAME_OFFSET = 3;
344
345     private static final int TYPE_OFFSET = 4;
346
347     private static final int VALUE_OFFSET = 5;
348
349     /**
350      * Removes an attribute from the list.
351      *
352      * @param index the index of the attribute (zero-based).
353      * @throws IndexOutOfBoundsException if <code>(index < 0) ||
354      * (index >= length())</code>
355      */

356     public void removeAttribute(int index) {
357         final int i = index * INCR;
358         _entries.removeRange(i, i + INCR);
359         _length--;
360     }
361
362     /**
363      * Sets the namespace URI of a specific attribute.
364      *
365      * @param index the index of the attribute (zero-based).
366      * @param uri the attribute's namespace URI, or the empty string for none.
367      * @throws IndexOutOfBoundsException if <code>(index < 0) ||
368      * (index >= length())</code>
369      */

370     public void setURI(int index, CharSequence uri) {
371         _entries.set(index * INCR + URI_OFFSET, uri);
372     }
373
374     /**
375      * Sets the local name of a specific attribute.
376      *
377      * @param index the index of the attribute (zero-based).
378      * @param localName the attribute local name, or the empty string for none.
379      * @throws IndexOutOfBoundsException if <code>(index < 0) ||
380      * (index >= length())</code>
381      */

382     public void setLocalName(int index, CharSequence localName) {
383         _entries.set(index * INCR + LOCAL_NAME_OFFSET, localName);
384     }
385
386     /**
387      * Sets the prefix of a specific attribute.
388      *
389      * @param index the index of the attribute (zero-based).
390      * @param prefix the attribute prefix, or the empty string for none.
391      * @throws IndexOutOfBoundsException if <code>(index < 0) ||
392      * (index >= length())</code>
393      */

394     public void setPrefix(int index, CharSequence prefix) {
395         _entries.set(index * INCR + PREFIX_OFFSET, prefix);
396     }
397
398     /**
399      * Sets the qualified name of a specific attribute.
400      *
401      * @param index the index of the attribute (zero-based).
402      * @param qName the attribute qualified name, or the empty string for
403      * none.
404      * @throws IndexOutOfBoundsException if <code>(index < 0) ||
405      * (index >= length())</code>
406      */

407     public void setQName(int index, CharSequence qName) {
408         _entries.set(index * INCR + QNAME_OFFSET, qName);
409     }
410
411     /**
412      * Sets the type of a specific attribute.
413      *
414      * @param index the index of the attribute (zero-based).
415      * @param type the attribute type.
416      * @throws IndexOutOfBoundsException if <code>(index < 0) ||
417      * (index >= length())</code>
418      */

419     public void setType(int index, String type) {
420         _entries.set(index * INCR + TYPE_OFFSET, type);
421     }
422
423     /**
424      * Sets the value of a specific attribute.
425      *
426      * @param index the index of the attribute (zero-based).
427      * @param value the attribute value.
428      * @throws IndexOutOfBoundsException if <code>(index < 0) ||
429      * (index >= length())</code>
430      */

431     public void setValue(int index, CharSequence value) {
432         _entries.set(index * INCR + VALUE_OFFSET, value);
433     }
434
435 }
Popular Tags