Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / external / sax / org / xml / sax / helpers / AttributeListImpl.java
blobb6186238bdbd80bda706eac3d30c25419548d5ec
1 // SAX default implementation for AttributeList.
2 // http://www.saxproject.org
3 // No warranty; no copyright -- use this as you will.
4 // $Id: AttributeListImpl.java,v 1.1 2005/02/02 00:41:53 tromey Exp $
6 package org.xml.sax.helpers;
8 import org.xml.sax.AttributeList;
10 import java.util.Vector;
13 /**
14 * Default implementation for AttributeList.
16 * <blockquote>
17 * <em>This module, both source code and documentation, is in the
18 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
19 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
20 * for further information.
21 * </blockquote>
23 * <p>AttributeList implements the deprecated SAX1 {@link
24 * org.xml.sax.AttributeList AttributeList} interface, and has been
25 * replaced by the new SAX2 {@link org.xml.sax.helpers.AttributesImpl
26 * AttributesImpl} interface.</p>
28 * <p>This class provides a convenience implementation of the SAX
29 * {@link org.xml.sax.AttributeList AttributeList} interface. This
30 * implementation is useful both for SAX parser writers, who can use
31 * it to provide attributes to the application, and for SAX application
32 * writers, who can use it to create a persistent copy of an element's
33 * attribute specifications:</p>
35 * <pre>
36 * private AttributeList myatts;
38 * public void startElement (String name, AttributeList atts)
39 * {
40 * // create a persistent copy of the attribute list
41 * // for use outside this method
42 * myatts = new AttributeListImpl(atts);
43 * [...]
44 * }
45 * </pre>
47 * <p>Please note that SAX parsers are not required to use this
48 * class to provide an implementation of AttributeList; it is
49 * supplied only as an optional convenience. In particular,
50 * parser writers are encouraged to invent more efficient
51 * implementations.</p>
53 * @deprecated This class implements a deprecated interface,
54 * {@link org.xml.sax.AttributeList AttributeList};
55 * that interface has been replaced by
56 * {@link org.xml.sax.Attributes Attributes},
57 * which is implemented in the
58 * {@link org.xml.sax.helpers.AttributesImpl
59 * AttributesImpl} helper class.
60 * @since SAX 1.0
61 * @author David Megginson
62 * @version 2.0.1 (sax2r2)
63 * @see org.xml.sax.AttributeList
64 * @see org.xml.sax.DocumentHandler#startElement
66 public class AttributeListImpl implements AttributeList
69 /**
70 * Create an empty attribute list.
72 * <p>This constructor is most useful for parser writers, who
73 * will use it to create a single, reusable attribute list that
74 * can be reset with the clear method between elements.</p>
76 * @see #addAttribute
77 * @see #clear
79 public AttributeListImpl ()
84 /**
85 * Construct a persistent copy of an existing attribute list.
87 * <p>This constructor is most useful for application writers,
88 * who will use it to create a persistent copy of an existing
89 * attribute list.</p>
91 * @param atts The attribute list to copy
92 * @see org.xml.sax.DocumentHandler#startElement
94 public AttributeListImpl (AttributeList atts)
96 setAttributeList(atts);
101 ////////////////////////////////////////////////////////////////////
102 // Methods specific to this class.
103 ////////////////////////////////////////////////////////////////////
107 * Set the attribute list, discarding previous contents.
109 * <p>This method allows an application writer to reuse an
110 * attribute list easily.</p>
112 * @param atts The attribute list to copy.
114 public void setAttributeList (AttributeList atts)
116 int count = atts.getLength();
118 clear();
120 for (int i = 0; i < count; i++) {
121 addAttribute(atts.getName(i), atts.getType(i), atts.getValue(i));
127 * Add an attribute to an attribute list.
129 * <p>This method is provided for SAX parser writers, to allow them
130 * to build up an attribute list incrementally before delivering
131 * it to the application.</p>
133 * @param name The attribute name.
134 * @param type The attribute type ("NMTOKEN" for an enumeration).
135 * @param value The attribute value (must not be null).
136 * @see #removeAttribute
137 * @see org.xml.sax.DocumentHandler#startElement
139 public void addAttribute (String name, String type, String value)
141 names.addElement(name);
142 types.addElement(type);
143 values.addElement(value);
148 * Remove an attribute from the list.
150 * <p>SAX application writers can use this method to filter an
151 * attribute out of an AttributeList. Note that invoking this
152 * method will change the length of the attribute list and
153 * some of the attribute's indices.</p>
155 * <p>If the requested attribute is not in the list, this is
156 * a no-op.</p>
158 * @param name The attribute name.
159 * @see #addAttribute
161 public void removeAttribute (String name)
163 int i = names.indexOf(name);
165 if (i >= 0) {
166 names.removeElementAt(i);
167 types.removeElementAt(i);
168 values.removeElementAt(i);
174 * Clear the attribute list.
176 * <p>SAX parser writers can use this method to reset the attribute
177 * list between DocumentHandler.startElement events. Normally,
178 * it will make sense to reuse the same AttributeListImpl object
179 * rather than allocating a new one each time.</p>
181 * @see org.xml.sax.DocumentHandler#startElement
183 public void clear ()
185 names.removeAllElements();
186 types.removeAllElements();
187 values.removeAllElements();
192 ////////////////////////////////////////////////////////////////////
193 // Implementation of org.xml.sax.AttributeList
194 ////////////////////////////////////////////////////////////////////
198 * Return the number of attributes in the list.
200 * @return The number of attributes in the list.
201 * @see org.xml.sax.AttributeList#getLength
203 public int getLength ()
205 return names.size();
210 * Get the name of an attribute (by position).
212 * @param i The position of the attribute in the list.
213 * @return The attribute name as a string, or null if there
214 * is no attribute at that position.
215 * @see org.xml.sax.AttributeList#getName(int)
217 public String getName (int i)
219 if (i < 0) {
220 return null;
222 try {
223 return (String)names.elementAt(i);
224 } catch (ArrayIndexOutOfBoundsException e) {
225 return null;
231 * Get the type of an attribute (by position).
233 * @param i The position of the attribute in the list.
234 * @return The attribute type as a string ("NMTOKEN" for an
235 * enumeration, and "CDATA" if no declaration was
236 * read), or null if there is no attribute at
237 * that position.
238 * @see org.xml.sax.AttributeList#getType(int)
240 public String getType (int i)
242 if (i < 0) {
243 return null;
245 try {
246 return (String)types.elementAt(i);
247 } catch (ArrayIndexOutOfBoundsException e) {
248 return null;
254 * Get the value of an attribute (by position).
256 * @param i The position of the attribute in the list.
257 * @return The attribute value as a string, or null if
258 * there is no attribute at that position.
259 * @see org.xml.sax.AttributeList#getValue(int)
261 public String getValue (int i)
263 if (i < 0) {
264 return null;
266 try {
267 return (String)values.elementAt(i);
268 } catch (ArrayIndexOutOfBoundsException e) {
269 return null;
275 * Get the type of an attribute (by name).
277 * @param name The attribute name.
278 * @return The attribute type as a string ("NMTOKEN" for an
279 * enumeration, and "CDATA" if no declaration was
280 * read).
281 * @see org.xml.sax.AttributeList#getType(java.lang.String)
283 public String getType (String name)
285 return getType(names.indexOf(name));
290 * Get the value of an attribute (by name).
292 * @param name The attribute name.
293 * @see org.xml.sax.AttributeList#getValue(java.lang.String)
295 public String getValue (String name)
297 return getValue(names.indexOf(name));
302 ////////////////////////////////////////////////////////////////////
303 // Internal state.
304 ////////////////////////////////////////////////////////////////////
306 Vector names = new Vector();
307 Vector types = new Vector();
308 Vector values = new Vector();
312 // end of AttributeListImpl.java