Class | Line # | Actions | ||||
---|---|---|---|---|---|---|
AttributeListImpl | 66 | 34 | 20 | 56 |
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 org.xml.sax.AttributeList; | |
9 | ||
10 | import java.util.Vector; | |
11 | ||
12 | ||
13 | /**e | |
14 | * Default implementation for AttributeList. | |
15 | * | |
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> | |
22 | * | |
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> | |
27 | * | |
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> | |
34 | * | |
35 | * <pre> | |
36 | * private AttributeList myatts; | |
37 | * | |
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> | |
46 | * | |
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> | |
52 | * | |
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 | |
65 | */ | |
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 | */ | |
79 | 0 | ![]() |
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 | */ | |
94 | 0 | ![]() |
95 | { | |
96 | 0 | 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 | */ | |
114 | 0 | ![]() |
115 | { | |
116 | 0 | int count = atts.getLength(); |
117 | ||
118 | 0 | clear(); |
119 | ||
120 | 0 | for (int i = 0; i < count; i++) { |
121 | 0 | 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 | */ | |
139 | 0 | ![]() |
140 | { | |
141 | 0 | names.addElement(name); |
142 | 0 | types.addElement(type); |
143 | 0 | values.addElement(value); |
144 | } | |
145 | ||
146 | ||
147 | /** | |
148 | * Remove an attribute from the list. | |
149 | * | |
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> | |
154 | * | |
155 | * <p>If the requested attribute is not in the list, this is | |
156 | * a no-op.</p> | |
157 | * | |
158 | * @param name The attribute name. | |
159 | * @see #addAttribute | |
160 | */ | |
161 | 0 | ![]() |
162 | { | |
163 | 0 | int i = names.indexOf(name); |
164 | ||
165 | 0 | if (i >= 0) { |
166 | 0 | names.removeElementAt(i); |
167 | 0 | types.removeElementAt(i); |
168 | 0 | values.removeElementAt(i); |
169 | } | |
170 | } | |
171 | ||
172 | ||
173 | /** | |
174 | * Clear the attribute list. | |
175 | * | |
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> | |
180 | * | |
181 | * @see org.xml.sax.DocumentHandler#startElement | |
182 | */ | |
183 | 0 | ![]() |
184 | { | |
185 | 0 | names.removeAllElements(); |
186 | 0 | types.removeAllElements(); |
187 | 0 | values.removeAllElements(); |
188 | } | |
189 | ||
190 | ||
191 | ||
192 | //////////////////////////////////////////////////////////////////// | |
193 | // Implementation of org.xml.sax.AttributeList | |
194 | //////////////////////////////////////////////////////////////////// | |
195 | ||
196 | ||
197 | /** | |
198 | * Return the number of attributes in the list. | |
199 | * | |
200 | * @return The number of attributes in the list. | |
201 | * @see org.xml.sax.AttributeList#getLength | |
202 | */ | |
203 | 0 | ![]() |
204 | public int getLength () | |
205 | { | |
206 | 0 | return names.size(); |
207 | } | |
208 | ||
209 | ||
210 | /** | |
211 | * Get the name of an attribute (by position). | |
212 | * | |
213 | * @param i The position of the attribute in the list. | |
214 | * @return The attribute name as a string, or null if there | |
215 | * is no attribute at that position. | |
216 | * @see org.xml.sax.AttributeList#getName(int) | |
217 | */ | |
218 | 0 | ![]() |
219 | public String getName (int i) | |
220 | { | |
221 | 0 | if (i < 0) { |
222 | 0 | return null; |
223 | } | |
224 | 0 | try { |
225 | 0 | return (String)names.elementAt(i); |
226 | } catch (ArrayIndexOutOfBoundsException e) { | |
227 | 0 | return null; |
228 | } | |
229 | } | |
230 | ||
231 | ||
232 | /** | |
233 | * Get the type of an attribute (by position). | |
234 | * | |
235 | * @param i The position of the attribute in the list. | |
236 | * @return The attribute type as a string ("NMTOKEN" for an | |
237 | * enumeration, and "CDATA" if no declaration was | |
238 | * read), or null if there is no attribute at | |
239 | * that position. | |
240 | * @see org.xml.sax.AttributeList#getType(int) | |
241 | */ | |
242 | 0 | ![]() |
243 | public String getType (int i) | |
244 | { | |
245 | 0 | if (i < 0) { |
246 | 0 | return null; |
247 | } | |
248 | 0 | try { |
249 | 0 | return (String)types.elementAt(i); |
250 | } catch (ArrayIndexOutOfBoundsException e) { | |
251 | 0 | return null; |
252 | } | |
253 | } | |
254 | ||
255 | ||
256 | /** | |
257 | * Get the value of an attribute (by position). | |
258 | * | |
259 | * @param i The position of the attribute in the list. | |
260 | * @return The attribute value as a string, or null if | |
261 | * there is no attribute at that position. | |
262 | * @see org.xml.sax.AttributeList#getValue(int) | |
263 | */ | |
264 | 0 | ![]() |
265 | public String getValue (int i) | |
266 | { | |
267 | 0 | if (i < 0) { |
268 | 0 | return null; |
269 | } | |
270 | 0 | try { |
271 | 0 | return (String)values.elementAt(i); |
272 | } catch (ArrayIndexOutOfBoundsException e) { | |
273 | 0 | return null; |
274 | } | |
275 | } | |
276 | ||
277 | ||
278 | /** | |
279 | * Get the type of an attribute (by name). | |
280 | * | |
281 | * @param name The attribute name. | |
282 | * @return The attribute type as a string ("NMTOKEN" for an | |
283 | * enumeration, and "CDATA" if no declaration was | |
284 | * read). | |
285 | * @see org.xml.sax.AttributeList#getType(java.lang.String) | |
286 | */ | |
287 | 0 | ![]() |
288 | public String getType (String name) | |
289 | { | |
290 | 0 | return getType(names.indexOf(name)); |
291 | } | |
292 | ||
293 | ||
294 | /** | |
295 | * Get the value of an attribute (by name). | |
296 | * | |
297 | * @param name The attribute name. | |
298 | * @see org.xml.sax.AttributeList#getValue(java.lang.String) | |
299 | */ | |
300 | 0 | ![]() |
301 | public String getValue (String name) | |
302 | { | |
303 | 0 | return getValue(names.indexOf(name)); |
304 | } | |
305 | ||
306 | ||
307 | ||
308 | //////////////////////////////////////////////////////////////////// | |
309 | // Internal state. | |
310 | //////////////////////////////////////////////////////////////////// | |
311 | ||
312 | Vector names = new Vector(); | |
313 | Vector types = new Vector(); | |
314 | Vector values = new Vector(); | |
315 | ||
316 | } | |
317 | ||
318 | // end of AttributeListImpl.java |