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.6 2002/01/30 20:52:22 dbrownell Exp $
5 
6 package org.xml.sax.helpers;
7 
8 import java.util.ArrayList;
9 import org.xml.sax.AttributeList;
10 
11 
12 /**
13  * Default implementation for AttributeList.
14  *
15  * <blockquote>
16  * <em>This module, both source code and documentation, is in the
17  * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
18  * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
19  * for further information.
20  * </blockquote>
21  *
22  * <p>AttributeList implements the deprecated SAX1 {@link
23  * org.xml.sax.AttributeList AttributeList} interface, and has been
24  * replaced by the new SAX2 {@link org.xml.sax.helpers.AttributesImpl
25  * AttributesImpl} interface.</p>
26  *
27  * <p>This class provides a convenience implementation of the SAX
28  * {@link org.xml.sax.AttributeList AttributeList} interface.  This
29  * implementation is useful both for SAX parser writers, who can use
30  * it to provide attributes to the application, and for SAX application
31  * writers, who can use it to create a persistent copy of an element's
32  * attribute specifications:</p>
33  *
34  * <pre>
35  * private AttributeList myatts;
36  *
37  * public void startElement (String name, AttributeList atts)
38  * {
39  *              // create a persistent copy of the attribute list
40  *              // for use outside this method
41  *   myatts = new AttributeListImpl(atts);
42  *   [...]
43  * }
44  * </pre>
45  *
46  * <p>Please note that SAX parsers are not required to use this
47  * class to provide an implementation of AttributeList; it is
48  * supplied only as an optional convenience.  In particular,
49  * parser writers are encouraged to invent more efficient
50  * implementations.</p>
51  *
52  * @deprecated This class implements a deprecated interface,
53  *             {@link org.xml.sax.AttributeList AttributeList};
54  *             that interface has been replaced by
55  *             {@link org.xml.sax.Attributes Attributes},
56  *             which is implemented in the
57  *             {@link org.xml.sax.helpers.AttributesImpl
58  *            AttributesImpl} helper class.
59  * @since SAX 1.0
60  * @author David Megginson
61  * @version 2.0.1 (sax2r2)
62  * @see org.xml.sax.AttributeList
63  * @see org.xml.sax.DocumentHandler#startElement
64  */
65 @Deprecated
66 public class AttributeListImpl implements AttributeList
67 {
68 
69     /**
70      * Create an empty attribute list.
71      *
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>
75      *
76      * @see #addAttribute
77      * @see #clear
78      */
AttributeListImpl()79     public AttributeListImpl ()
80     {
81     }
82 
83 
84     /**
85      * Construct a persistent copy of an existing attribute list.
86      *
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>
90      *
91      * @param atts The attribute list to copy
92      * @see org.xml.sax.DocumentHandler#startElement
93      */
AttributeListImpl(AttributeList atts)94     public AttributeListImpl (AttributeList atts)
95     {
96     setAttributeList(atts);
97     }
98 
99 
100 
101     ////////////////////////////////////////////////////////////////////
102     // Methods specific to this class.
103     ////////////////////////////////////////////////////////////////////
104 
105 
106     /**
107      * Set the attribute list, discarding previous contents.
108      *
109      * <p>This method allows an application writer to reuse an
110      * attribute list easily.</p>
111      *
112      * @param atts The attribute list to copy.
113      */
setAttributeList(AttributeList atts)114     public void setAttributeList (AttributeList atts)
115     {
116     int count = atts.getLength();
117 
118     clear();
119 
120     for (int i = 0; i < count; i++) {
121         addAttribute(atts.getName(i), atts.getType(i), atts.getValue(i));
122     }
123     }
124 
125 
126     /**
127      * Add an attribute to an attribute list.
128      *
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>
132      *
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
138      */
addAttribute(String name, String type, String value)139     public void addAttribute(String name, String type, String value) {
140         names.add(name);
141         types.add(type);
142         values.add(value);
143     }
144 
145 
146     /**
147      * Remove an attribute from the list.
148      *
149      * <p>SAX application writers can use this method to filter an
150      * attribute out of an AttributeList.  Note that invoking this
151      * method will change the length of the attribute list and
152      * some of the attribute's indices.</p>
153      *
154      * <p>If the requested attribute is not in the list, this is
155      * a no-op.</p>
156      *
157      * @param name The attribute name.
158      * @see #addAttribute
159      */
removeAttribute(String name)160     public void removeAttribute(String name) {
161         int i = names.indexOf(name);
162         if (i != -1) {
163             names.remove(i);
164             types.remove(i);
165             values.remove(i);
166         }
167     }
168 
169 
170     /**
171      * Clear the attribute list.
172      *
173      * <p>SAX parser writers can use this method to reset the attribute
174      * list between DocumentHandler.startElement events.  Normally,
175      * it will make sense to reuse the same AttributeListImpl object
176      * rather than allocating a new one each time.</p>
177      *
178      * @see org.xml.sax.DocumentHandler#startElement
179      */
clear()180     public void clear() {
181         names.clear();
182         types.clear();
183         values.clear();
184     }
185 
186 
187 
188     ////////////////////////////////////////////////////////////////////
189     // Implementation of org.xml.sax.AttributeList
190     ////////////////////////////////////////////////////////////////////
191 
192 
193     /**
194      * Return the number of attributes in the list.
195      *
196      * @return The number of attributes in the list.
197      * @see org.xml.sax.AttributeList#getLength
198      */
getLength()199     public int getLength() {
200         return names.size();
201     }
202 
203 
204     /**
205      * Get the name of an attribute (by position).
206      *
207      * @param i The position of the attribute in the list.
208      * @return The attribute name as a string, or null if there
209      *         is no attribute at that position.
210      * @see org.xml.sax.AttributeList#getName(int)
211      */
getName(int i)212     public String getName(int i) {
213         if (i < 0 || i >= names.size()) {
214             return null;
215         }
216         return names.get(i);
217     }
218 
219 
220     /**
221      * Get the type of an attribute (by position).
222      *
223      * @param i The position of the attribute in the list.
224      * @return The attribute type as a string ("NMTOKEN" for an
225      *         enumeration, and "CDATA" if no declaration was
226      *         read), or null if there is no attribute at
227      *         that position.
228      * @see org.xml.sax.AttributeList#getType(int)
229      */
getType(int i)230     public String getType(int i) {
231         if (i < 0 || i >= types.size()) {
232             return null;
233         }
234         return types.get(i);
235     }
236 
237 
238     /**
239      * Get the value of an attribute (by position).
240      *
241      * @param i The position of the attribute in the list.
242      * @return The attribute value as a string, or null if
243      *         there is no attribute at that position.
244      * @see org.xml.sax.AttributeList#getValue(int)
245      */
getValue(int i)246     public String getValue(int i) {
247         if (i < 0 || i >= values.size()) {
248             return null;
249         }
250         return values.get(i);
251     }
252 
253 
254     /**
255      * Get the type of an attribute (by name).
256      *
257      * @param name The attribute name.
258      * @return The attribute type as a string ("NMTOKEN" for an
259      *         enumeration, and "CDATA" if no declaration was
260      *         read).
261      * @see org.xml.sax.AttributeList#getType(java.lang.String)
262      */
getType(String name)263     public String getType(String name) {
264         return getType(names.indexOf(name));
265     }
266 
267 
268     /**
269      * Get the value of an attribute (by name).
270      *
271      * @param name The attribute name.
272      * @return the named attribute's value or null, if the attribute does not
273      *         exist.
274      * @see org.xml.sax.AttributeList#getValue(java.lang.String)
275      */
getValue(String name)276     public String getValue(String name) {
277         return getValue(names.indexOf(name));
278     }
279 
280 
281 
282     ////////////////////////////////////////////////////////////////////
283     // Internal state.
284     ////////////////////////////////////////////////////////////////////
285 
286     private ArrayList<String> names = new ArrayList<String>();
287     private ArrayList<String> types = new ArrayList<String>();
288     private ArrayList<String> values = new ArrayList<String>();
289 
290 }
291 
292 // end of AttributeListImpl.java
293