1/*
2 * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26// SAX default implementation for AttributeList.
27// http://www.saxproject.org
28// No warranty; no copyright -- use this as you will.
29// $Id: AttributeListImpl.java,v 1.2 2004/11/03 22:53:08 jsuttor Exp $
30
31package org.xml.sax.helpers;
32
33import org.xml.sax.AttributeList;
34
35import java.util.Vector;
36
37
38/**
39 * Default implementation for AttributeList.
40 *
41 * <blockquote>
42 * <em>This module, both source code and documentation, is in the
43 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
44 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
45 * for further information.
46 * </blockquote>
47 *
48 * <p>AttributeList implements the deprecated SAX1 {@link
49 * org.xml.sax.AttributeList AttributeList} interface, and has been
50 * replaced by the new SAX2 {@link org.xml.sax.helpers.AttributesImpl
51 * AttributesImpl} interface.</p>
52 *
53 * <p>This class provides a convenience implementation of the SAX
54 * {@link org.xml.sax.AttributeList AttributeList} interface.  This
55 * implementation is useful both for SAX parser writers, who can use
56 * it to provide attributes to the application, and for SAX application
57 * writers, who can use it to create a persistent copy of an element's
58 * attribute specifications:</p>
59 *
60 * <pre>
61 * private AttributeList myatts;
62 *
63 * public void startElement (String name, AttributeList atts)
64 * {
65 *              // create a persistent copy of the attribute list
66 *              // for use outside this method
67 *   myatts = new AttributeListImpl(atts);
68 *   [...]
69 * }
70 * </pre>
71 *
72 * <p>Please note that SAX parsers are not required to use this
73 * class to provide an implementation of AttributeList; it is
74 * supplied only as an optional convenience.  In particular,
75 * parser writers are encouraged to invent more efficient
76 * implementations.</p>
77 *
78 * @deprecated This class implements a deprecated interface,
79 *             {@link org.xml.sax.AttributeList AttributeList};
80 *             that interface has been replaced by
81 *             {@link org.xml.sax.Attributes Attributes},
82 *             which is implemented in the
83 *             {@link org.xml.sax.helpers.AttributesImpl
84 *            AttributesImpl} helper class.
85 * @since 1.4, SAX 1.0
86 * @author David Megginson
87 * @see org.xml.sax.AttributeList
88 * @see org.xml.sax.DocumentHandler#startElement
89 */
90public class AttributeListImpl implements AttributeList
91{
92
93    /**
94     * Create an empty attribute list.
95     *
96     * <p>This constructor is most useful for parser writers, who
97     * will use it to create a single, reusable attribute list that
98     * can be reset with the clear method between elements.</p>
99     *
100     * @see #addAttribute
101     * @see #clear
102     */
103    public AttributeListImpl ()
104    {
105    }
106
107
108    /**
109     * Construct a persistent copy of an existing attribute list.
110     *
111     * <p>This constructor is most useful for application writers,
112     * who will use it to create a persistent copy of an existing
113     * attribute list.</p>
114     *
115     * @param atts The attribute list to copy
116     * @see org.xml.sax.DocumentHandler#startElement
117     */
118    public AttributeListImpl (AttributeList atts)
119    {
120        setAttributeList(atts);
121    }
122
123
124
125    ////////////////////////////////////////////////////////////////////
126    // Methods specific to this class.
127    ////////////////////////////////////////////////////////////////////
128
129
130    /**
131     * Set the attribute list, discarding previous contents.
132     *
133     * <p>This method allows an application writer to reuse an
134     * attribute list easily.</p>
135     *
136     * @param atts The attribute list to copy.
137     */
138    public void setAttributeList (AttributeList atts)
139    {
140        int count = atts.getLength();
141
142        clear();
143
144        for (int i = 0; i < count; i++) {
145            addAttribute(atts.getName(i), atts.getType(i), atts.getValue(i));
146        }
147    }
148
149
150    /**
151     * Add an attribute to an attribute list.
152     *
153     * <p>This method is provided for SAX parser writers, to allow them
154     * to build up an attribute list incrementally before delivering
155     * it to the application.</p>
156     *
157     * @param name The attribute name.
158     * @param type The attribute type ("NMTOKEN" for an enumeration).
159     * @param value The attribute value (must not be null).
160     * @see #removeAttribute
161     * @see org.xml.sax.DocumentHandler#startElement
162     */
163    public void addAttribute (String name, String type, String value)
164    {
165        names.addElement(name);
166        types.addElement(type);
167        values.addElement(value);
168    }
169
170
171    /**
172     * Remove an attribute from the list.
173     *
174     * <p>SAX application writers can use this method to filter an
175     * attribute out of an AttributeList.  Note that invoking this
176     * method will change the length of the attribute list and
177     * some of the attribute's indices.</p>
178     *
179     * <p>If the requested attribute is not in the list, this is
180     * a no-op.</p>
181     *
182     * @param name The attribute name.
183     * @see #addAttribute
184     */
185    public void removeAttribute (String name)
186    {
187        int i = names.indexOf(name);
188
189        if (i >= 0) {
190            names.removeElementAt(i);
191            types.removeElementAt(i);
192            values.removeElementAt(i);
193        }
194    }
195
196
197    /**
198     * Clear the attribute list.
199     *
200     * <p>SAX parser writers can use this method to reset the attribute
201     * list between DocumentHandler.startElement events.  Normally,
202     * it will make sense to reuse the same AttributeListImpl object
203     * rather than allocating a new one each time.</p>
204     *
205     * @see org.xml.sax.DocumentHandler#startElement
206     */
207    public void clear ()
208    {
209        names.removeAllElements();
210        types.removeAllElements();
211        values.removeAllElements();
212    }
213
214
215
216    ////////////////////////////////////////////////////////////////////
217    // Implementation of org.xml.sax.AttributeList
218    ////////////////////////////////////////////////////////////////////
219
220
221    /**
222     * Return the number of attributes in the list.
223     *
224     * @return The number of attributes in the list.
225     * @see org.xml.sax.AttributeList#getLength
226     */
227    public int getLength ()
228    {
229        return names.size();
230    }
231
232
233    /**
234     * Get the name of an attribute (by position).
235     *
236     * @param i The position of the attribute in the list.
237     * @return The attribute name as a string, or null if there
238     *         is no attribute at that position.
239     * @see org.xml.sax.AttributeList#getName(int)
240     */
241    public String getName (int i)
242    {
243        if (i < 0) {
244            return null;
245        }
246        try {
247            return (String)names.elementAt(i);
248        } catch (ArrayIndexOutOfBoundsException e) {
249            return null;
250        }
251    }
252
253
254    /**
255     * Get the type of an attribute (by position).
256     *
257     * @param i The position of the attribute in the list.
258     * @return The attribute type as a string ("NMTOKEN" for an
259     *         enumeration, and "CDATA" if no declaration was
260     *         read), or null if there is no attribute at
261     *         that position.
262     * @see org.xml.sax.AttributeList#getType(int)
263     */
264    public String getType (int i)
265    {
266        if (i < 0) {
267            return null;
268        }
269        try {
270            return (String)types.elementAt(i);
271        } catch (ArrayIndexOutOfBoundsException e) {
272            return null;
273        }
274    }
275
276
277    /**
278     * Get the value of an attribute (by position).
279     *
280     * @param i The position of the attribute in the list.
281     * @return The attribute value as a string, or null if
282     *         there is no attribute at that position.
283     * @see org.xml.sax.AttributeList#getValue(int)
284     */
285    public String getValue (int i)
286    {
287        if (i < 0) {
288            return null;
289        }
290        try {
291            return (String)values.elementAt(i);
292        } catch (ArrayIndexOutOfBoundsException e) {
293            return null;
294        }
295    }
296
297
298    /**
299     * Get the type of an attribute (by name).
300     *
301     * @param name The attribute name.
302     * @return The attribute type as a string ("NMTOKEN" for an
303     *         enumeration, and "CDATA" if no declaration was
304     *         read).
305     * @see org.xml.sax.AttributeList#getType(java.lang.String)
306     */
307    public String getType (String name)
308    {
309        return getType(names.indexOf(name));
310    }
311
312
313    /**
314     * Get the value of an attribute (by name).
315     *
316     * @param name The attribute name.
317     * @see org.xml.sax.AttributeList#getValue(java.lang.String)
318     */
319    public String getValue (String name)
320    {
321        return getValue(names.indexOf(name));
322    }
323
324
325
326    ////////////////////////////////////////////////////////////////////
327    // Internal state.
328    ////////////////////////////////////////////////////////////////////
329
330    Vector names = new Vector();
331    Vector types = new Vector();
332    Vector values = new Vector();
333
334}
335
336// end of AttributeListImpl.java
337