1 // AttributesImpl.java - default implementation of Attributes.
\r
2 // Written by David Megginson, sax@megginson.com
\r
3 // NO WARRANTY! This class is in the public domain.
\r
5 // $Id: AttributesImpl.java,v 1.2 2001/05/31 16:03:17 garyp Exp $
\r
8 package org.xml.sax.helpers;
\r
10 import org.xml.sax.Attributes;
\r
14 * Default implementation of the Attributes interface.
\r
17 * <em>This module, both source code and documentation, is in the
\r
18 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
\r
21 * <p>This class provides a default implementation of the SAX2
\r
22 * {@link org.xml.sax.Attributes Attributes} interface, with the
\r
23 * addition of manipulators so that the list can be modified or
\r
26 * <p>There are two typical uses of this class:</p>
\r
29 * <li>to take a persistent snapshot of an Attributes object
\r
30 * in a {@link org.xml.sax.ContentHandler#startElement startElement} event; or</li>
\r
31 * <li>to construct or modify an Attributes object in a SAX2 driver or filter.</li>
\r
34 * <p>This class replaces the now-deprecated SAX1 {@link
\r
35 * org.xml.sax.helpers.AttributeListImpl AttributeListImpl}
\r
36 * class; in addition to supporting the updated Attributes
\r
37 * interface rather than the deprecated {@link org.xml.sax.AttributeList
\r
38 * AttributeList} interface, it also includes a much more efficient
\r
39 * implementation using a single array rather than a set of Vectors.</p>
\r
42 * @author David Megginson,
\r
43 * <a href="mailto:sax@megginson.com">sax@megginson.com</a>
\r
46 public class AttributesImpl implements Attributes
\r
50 ////////////////////////////////////////////////////////////////////
\r
52 ////////////////////////////////////////////////////////////////////
\r
56 * Construct a new, empty AttributesImpl object.
\r
58 public AttributesImpl ()
\r
66 * Copy an existing Attributes object.
\r
68 * <p>This constructor is especially useful inside a
\r
69 * {@link org.xml.sax.ContentHandler#startElement startElement} event.</p>
\r
71 * @param atts The existing Attributes object.
\r
73 public AttributesImpl (Attributes atts)
\r
75 setAttributes(atts);
\r
80 ////////////////////////////////////////////////////////////////////
\r
81 // Implementation of org.xml.sax.Attributes.
\r
82 ////////////////////////////////////////////////////////////////////
\r
86 * Return the number of attributes in the list.
\r
88 * @return The number of attributes in the list.
\r
89 * @see org.xml.sax.Attributes#getLength
\r
91 public int getLength ()
\r
98 * Return an attribute's Namespace URI.
\r
100 * @param index The attribute's index (zero-based).
\r
101 * @return The Namespace URI, the empty string if none is
\r
102 * available, or null if the index is out of range.
\r
103 * @see org.xml.sax.Attributes#getURI
\r
105 public String getURI (int index)
\r
107 if (index >= 0 && index < length) {
\r
108 return data[index*5];
\r
116 * Return an attribute's local name.
\r
118 * @param index The attribute's index (zero-based).
\r
119 * @return The attribute's local name, the empty string if
\r
120 * none is available, or null if the index if out of range.
\r
121 * @see org.xml.sax.Attributes#getLocalName
\r
123 public String getLocalName (int index)
\r
125 if (index >= 0 && index < length) {
\r
126 return data[index*5+1];
\r
134 * Return an attribute's qualified (prefixed) name.
\r
136 * @param index The attribute's index (zero-based).
\r
137 * @return The attribute's qualified name, the empty string if
\r
138 * none is available, or null if the index is out of bounds.
\r
139 * @see org.xml.sax.Attributes#getQName
\r
141 public String getQName (int index)
\r
143 if (index >= 0 && index < length) {
\r
144 return data[index*5+2];
\r
152 * Return an attribute's type by index.
\r
154 * @param index The attribute's index (zero-based).
\r
155 * @return The attribute's type, "CDATA" if the type is unknown, or null
\r
156 * if the index is out of bounds.
\r
157 * @see org.xml.sax.Attributes#getType(int)
\r
159 public String getType (int index)
\r
161 if (index >= 0 && index < length) {
\r
162 return data[index*5+3];
\r
170 * Return an attribute's value by index.
\r
172 * @param index The attribute's index (zero-based).
\r
173 * @return The attribute's value or null if the index is out of bounds.
\r
174 * @see org.xml.sax.Attributes#getValue(int)
\r
176 public String getValue (int index)
\r
178 if (index >= 0 && index < length) {
\r
179 return data[index*5+4];
\r
187 * Look up an attribute's index by Namespace name.
\r
189 * <p>In many cases, it will be more efficient to look up the name once and
\r
190 * use the index query methods rather than using the name query methods
\r
193 * @param uri The attribute's Namespace URI, or the empty
\r
194 * string if none is available.
\r
195 * @param localName The attribute's local name.
\r
196 * @return The attribute's index, or -1 if none matches.
\r
197 * @see org.xml.sax.Attributes#getIndex(java.lang.String,java.lang.String)
\r
199 public int getIndex (String uri, String localName)
\r
201 int max = length * 5;
\r
202 for (int i = 0; i < max; i += 5) {
\r
203 if (data[i].equals(uri) && data[i+1].equals(localName)) {
\r
212 * Look up an attribute's index by qualified (prefixed) name.
\r
214 * @param qName The qualified name.
\r
215 * @return The attribute's index, or -1 if none matches.
\r
216 * @see org.xml.sax.Attributes#getIndex(java.lang.String)
\r
218 public int getIndex (String qName)
\r
220 int max = length * 5;
\r
221 for (int i = 0; i < max; i += 5) {
\r
222 if (data[i+2].equals(qName)) {
\r
231 * Look up an attribute's type by Namespace-qualified name.
\r
233 * @param uri The Namespace URI, or the empty string for a name
\r
234 * with no explicit Namespace URI.
\r
235 * @param localName The local name.
\r
236 * @return The attribute's type, or null if there is no
\r
237 * matching attribute.
\r
238 * @see org.xml.sax.Attributes#getType(java.lang.String,java.lang.String)
\r
240 public String getType (String uri, String localName)
\r
242 int max = length * 5;
\r
243 for (int i = 0; i < max; i += 5) {
\r
244 if (data[i].equals(uri) && data[i+1].equals(localName)) {
\r
253 * Look up an attribute's type by qualified (prefixed) name.
\r
255 * @param qName The qualified name.
\r
256 * @return The attribute's type, or null if there is no
\r
257 * matching attribute.
\r
258 * @see org.xml.sax.Attributes#getType(java.lang.String)
\r
260 public String getType (String qName)
\r
262 int max = length * 5;
\r
263 for (int i = 0; i < max; i += 5) {
\r
264 if (data[i+2].equals(qName)) {
\r
273 * Look up an attribute's value by Namespace-qualified name.
\r
275 * @param uri The Namespace URI, or the empty string for a name
\r
276 * with no explicit Namespace URI.
\r
277 * @param localName The local name.
\r
278 * @return The attribute's value, or null if there is no
\r
279 * matching attribute.
\r
280 * @see org.xml.sax.Attributes#getValue(java.lang.String,java.lang.String)
\r
282 public String getValue (String uri, String localName)
\r
284 int max = length * 5;
\r
285 for (int i = 0; i < max; i += 5) {
\r
286 if (data[i].equals(uri) && data[i+1].equals(localName)) {
\r
295 * Look up an attribute's value by qualified (prefixed) name.
\r
297 * @param qName The qualified name.
\r
298 * @return The attribute's value, or null if there is no
\r
299 * matching attribute.
\r
300 * @see org.xml.sax.Attributes#getValue(java.lang.String)
\r
302 public String getValue (String qName)
\r
304 int max = length * 5;
\r
305 for (int i = 0; i < max; i += 5) {
\r
306 if (data[i+2].equals(qName)) {
\r
315 ////////////////////////////////////////////////////////////////////
\r
317 ////////////////////////////////////////////////////////////////////
\r
321 * Clear the attribute list for reuse.
\r
323 * <p>Note that no memory is actually freed by this call:
\r
324 * the current arrays are kept so that they can be
\r
327 public void clear ()
\r
334 * Copy an entire Attributes object.
\r
336 * <p>It may be more efficient to reuse an existing object
\r
337 * rather than constantly allocating new ones.</p>
\r
339 * @param atts The attributes to copy.
\r
341 public void setAttributes (Attributes atts)
\r
344 length = atts.getLength();
\r
345 data = new String[length*5];
\r
346 for (int i = 0; i < length; i++) {
\r
347 data[i*5] = atts.getURI(i);
\r
348 data[i*5+1] = atts.getLocalName(i);
\r
349 data[i*5+2] = atts.getQName(i);
\r
350 data[i*5+3] = atts.getType(i);
\r
351 data[i*5+4] = atts.getValue(i);
\r
357 * Add an attribute to the end of the list.
\r
359 * <p>For the sake of speed, this method does no checking
\r
360 * to see if the attribute is already in the list: that is
\r
361 * the responsibility of the application.</p>
\r
363 * @param uri The Namespace URI, or the empty string if
\r
364 * none is available or Namespace processing is not
\r
366 * @param localName The local name, or the empty string if
\r
367 * Namespace processing is not being performed.
\r
368 * @param qName The qualified (prefixed) name, or the empty string
\r
369 * if qualified names are not available.
\r
370 * @param type The attribute type as a string.
\r
371 * @param value The attribute value.
\r
373 public void addAttribute (String uri, String localName, String qName,
\r
374 String type, String value)
\r
376 ensureCapacity(length+1);
\r
377 data[length*5] = uri;
\r
378 data[length*5+1] = localName;
\r
379 data[length*5+2] = qName;
\r
380 data[length*5+3] = type;
\r
381 data[length*5+4] = value;
\r
387 * Set an attribute in the list.
\r
389 * <p>For the sake of speed, this method does no checking
\r
390 * for name conflicts or well-formedness: such checks are the
\r
391 * responsibility of the application.</p>
\r
393 * @param index The index of the attribute (zero-based).
\r
394 * @param uri The Namespace URI, or the empty string if
\r
395 * none is available or Namespace processing is not
\r
397 * @param localName The local name, or the empty string if
\r
398 * Namespace processing is not being performed.
\r
399 * @param qName The qualified name, or the empty string
\r
400 * if qualified names are not available.
\r
401 * @param type The attribute type as a string.
\r
402 * @param value The attribute value.
\r
403 * @exception java.lang.ArrayIndexOutOfBoundsException When the
\r
404 * supplied index does not point to an attribute
\r
407 public void setAttribute (int index, String uri, String localName,
\r
408 String qName, String type, String value)
\r
410 if (index >= 0 && index < length) {
\r
411 data[index*5] = uri;
\r
412 data[index*5+1] = localName;
\r
413 data[index*5+2] = qName;
\r
414 data[index*5+3] = type;
\r
415 data[index*5+4] = value;
\r
423 * Remove an attribute from the list.
\r
425 * @param index The index of the attribute (zero-based).
\r
426 * @exception java.lang.ArrayIndexOutOfBoundsException When the
\r
427 * supplied index does not point to an attribute
\r
430 public void removeAttribute (int index)
\r
432 if (index >= 0 && index < length) {
\r
433 data[index*5] = null;
\r
434 data[index*5+1] = null;
\r
435 data[index*5+2] = null;
\r
436 data[index*5+3] = null;
\r
437 data[index*5+4] = null;
\r
438 if (index < length - 1) {
\r
439 System.arraycopy(data, (index+1)*5, data, index*5,
\r
440 (length-index-1)*5);
\r
450 * Set the Namespace URI of a specific attribute.
\r
452 * @param index The index of the attribute (zero-based).
\r
453 * @param uri The attribute's Namespace URI, or the empty
\r
455 * @exception java.lang.ArrayIndexOutOfBoundsException When the
\r
456 * supplied index does not point to an attribute
\r
459 public void setURI (int index, String uri)
\r
461 if (index >= 0 && index < length) {
\r
462 data[index*5] = uri;
\r
470 * Set the local name of a specific attribute.
\r
472 * @param index The index of the attribute (zero-based).
\r
473 * @param localName The attribute's local name, or the empty
\r
475 * @exception java.lang.ArrayIndexOutOfBoundsException When the
\r
476 * supplied index does not point to an attribute
\r
479 public void setLocalName (int index, String localName)
\r
481 if (index >= 0 && index < length) {
\r
482 data[index*5+1] = localName;
\r
490 * Set the qualified name of a specific attribute.
\r
492 * @param index The index of the attribute (zero-based).
\r
493 * @param qName The attribute's qualified name, or the empty
\r
495 * @exception java.lang.ArrayIndexOutOfBoundsException When the
\r
496 * supplied index does not point to an attribute
\r
499 public void setQName (int index, String qName)
\r
501 if (index >= 0 && index < length) {
\r
502 data[index*5+2] = qName;
\r
510 * Set the type of a specific attribute.
\r
512 * @param index The index of the attribute (zero-based).
\r
513 * @param type The attribute's type.
\r
514 * @exception java.lang.ArrayIndexOutOfBoundsException When the
\r
515 * supplied index does not point to an attribute
\r
518 public void setType (int index, String type)
\r
520 if (index >= 0 && index < length) {
\r
521 data[index*5+3] = type;
\r
529 * Set the value of a specific attribute.
\r
531 * @param index The index of the attribute (zero-based).
\r
532 * @param value The attribute's value.
\r
533 * @exception java.lang.ArrayIndexOutOfBoundsException When the
\r
534 * supplied index does not point to an attribute
\r
537 public void setValue (int index, String value)
\r
539 if (index >= 0 && index < length) {
\r
540 data[index*5+4] = value;
\r
548 ////////////////////////////////////////////////////////////////////
\r
549 // Internal methods.
\r
550 ////////////////////////////////////////////////////////////////////
\r
554 * Ensure the internal array's capacity.
\r
556 * @param n The minimum number of attributes that the array must
\r
559 private void ensureCapacity (int n)
\r
561 if (n > 0 && data == null) {
\r
562 data = new String[25];
\r
565 int max = data.length;
\r
566 if (max >= n * 5) {
\r
571 while (max < n * 5) {
\r
574 String newData[] = new String[max];
\r
575 System.arraycopy(data, 0, newData, 0, length*5);
\r
581 * Report a bad array index in a manipulator.
\r
583 * @param index The index to report.
\r
584 * @exception java.lang.ArrayIndexOutOfBoundsException Always.
\r
586 private void badIndex (int index)
\r
587 throws ArrayIndexOutOfBoundsException
\r
590 "Attempt to modify attribute at illegal index: " + index;
\r
591 throw new ArrayIndexOutOfBoundsException(msg);
\r
596 ////////////////////////////////////////////////////////////////////
\r
598 ////////////////////////////////////////////////////////////////////
\r
605 // end of AttributesImpl.java
\r