001 /*
002 * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 // SAX default implementation for AttributeList.
027 // http://www.saxproject.org
028 // No warranty; no copyright -- use this as you will.
029 // $Id: AttributeListImpl.java,v 1.2 2004/11/03 22:53:08 jsuttor Exp $
030 package org.xml.sax.helpers;
031
032 import org.xml.sax.AttributeList;
033
034 import java.util.Vector;
035
036 /**
037 * Default implementation for AttributeList.
038 *
039 * <blockquote>
040 * <em>This module, both source code and documentation, is in the
041 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
042 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
043 * for further information.
044 * </blockquote>
045 *
046 * <p>AttributeList implements the deprecated SAX1 {@link
047 * org.xml.sax.AttributeList AttributeList} interface, and has been
048 * replaced by the new SAX2 {@link org.xml.sax.helpers.AttributesImpl
049 * AttributesImpl} interface.</p>
050 *
051 * <p>This class provides a convenience implementation of the SAX
052 * {@link org.xml.sax.AttributeList AttributeList} interface. This
053 * implementation is useful both for SAX parser writers, who can use
054 * it to provide attributes to the application, and for SAX application
055 * writers, who can use it to create a persistent copy of an element's
056 * attribute specifications:</p>
057 *
058 * <pre>
059 * private AttributeList myatts;
060 *
061 * public void startElement (String name, AttributeList atts)
062 * {
063 * // create a persistent copy of the attribute list
064 * // for use outside this method
065 * myatts = new AttributeListImpl(atts);
066 * [...]
067 * }
068 * </pre>
069 *
070 * <p>Please note that SAX parsers are not required to use this
071 * class to provide an implementation of AttributeList; it is
072 * supplied only as an optional convenience. In particular,
073 * parser writers are encouraged to invent more efficient
074 * implementations.</p>
075 *
076 * @deprecated This class implements a deprecated interface,
077 * {@link org.xml.sax.AttributeList AttributeList};
078 * that interface has been replaced by
079 * {@link org.xml.sax.Attributes Attributes},
080 * which is implemented in the
081 * {@link org.xml.sax.helpers.AttributesImpl
082 * AttributesImpl} helper class.
083 * @since SAX 1.0
084 * @author David Megginson
085 * @version 2.0.1 (sax2r2)
086 * @see org.xml.sax.AttributeList
087 * @see org.xml.sax.DocumentHandler#startElement
088 */
089 public class AttributeListImpl implements AttributeList {
090
091 /**
092 * Create an empty attribute list.
093 *
094 * <p>This constructor is most useful for parser writers, who
095 * will use it to create a single, reusable attribute list that
096 * can be reset with the clear method between elements.</p>
097 *
098 * @see #addAttribute
099 * @see #clear
100 */
101 public AttributeListImpl() {
102 }
103
104 /**
105 * Construct a persistent copy of an existing attribute list.
106 *
107 * <p>This constructor is most useful for application writers,
108 * who will use it to create a persistent copy of an existing
109 * attribute list.</p>
110 *
111 * @param atts The attribute list to copy
112 * @see org.xml.sax.DocumentHandler#startElement
113 */
114 public AttributeListImpl(AttributeList atts) {
115 setAttributeList(atts);
116 }
117
118 ////////////////////////////////////////////////////////////////////
119 // Methods specific to this class.
120 ////////////////////////////////////////////////////////////////////
121
122 /**
123 * Set the attribute list, discarding previous contents.
124 *
125 * <p>This method allows an application writer to reuse an
126 * attribute list easily.</p>
127 *
128 * @param atts The attribute list to copy.
129 */
130 public void setAttributeList(AttributeList atts) {
131 int count = atts.getLength();
132
133 clear();
134
135 for (int i = 0; i < count; i++) {
136 addAttribute(atts.getName(i), atts.getType(i), atts
137 .getValue(i));
138 }
139 }
140
141 /**
142 * Add an attribute to an attribute list.
143 *
144 * <p>This method is provided for SAX parser writers, to allow them
145 * to build up an attribute list incrementally before delivering
146 * it to the application.</p>
147 *
148 * @param name The attribute name.
149 * @param type The attribute type ("NMTOKEN" for an enumeration).
150 * @param value The attribute value (must not be null).
151 * @see #removeAttribute
152 * @see org.xml.sax.DocumentHandler#startElement
153 */
154 public void addAttribute(String name, String type, String value) {
155 names.addElement(name);
156 types.addElement(type);
157 values.addElement(value);
158 }
159
160 /**
161 * Remove an attribute from the list.
162 *
163 * <p>SAX application writers can use this method to filter an
164 * attribute out of an AttributeList. Note that invoking this
165 * method will change the length of the attribute list and
166 * some of the attribute's indices.</p>
167 *
168 * <p>If the requested attribute is not in the list, this is
169 * a no-op.</p>
170 *
171 * @param name The attribute name.
172 * @see #addAttribute
173 */
174 public void removeAttribute(String name) {
175 int i = names.indexOf(name);
176
177 if (i >= 0) {
178 names.removeElementAt(i);
179 types.removeElementAt(i);
180 values.removeElementAt(i);
181 }
182 }
183
184 /**
185 * Clear the attribute list.
186 *
187 * <p>SAX parser writers can use this method to reset the attribute
188 * list between DocumentHandler.startElement events. Normally,
189 * it will make sense to reuse the same AttributeListImpl object
190 * rather than allocating a new one each time.</p>
191 *
192 * @see org.xml.sax.DocumentHandler#startElement
193 */
194 public void clear() {
195 names.removeAllElements();
196 types.removeAllElements();
197 values.removeAllElements();
198 }
199
200 ////////////////////////////////////////////////////////////////////
201 // Implementation of org.xml.sax.AttributeList
202 ////////////////////////////////////////////////////////////////////
203
204 /**
205 * Return the number of attributes in the list.
206 *
207 * @return The number of attributes in the list.
208 * @see org.xml.sax.AttributeList#getLength
209 */
210 public int getLength() {
211 return names.size();
212 }
213
214 /**
215 * Get the name of an attribute (by position).
216 *
217 * @param i The position of the attribute in the list.
218 * @return The attribute name as a string, or null if there
219 * is no attribute at that position.
220 * @see org.xml.sax.AttributeList#getName(int)
221 */
222 public String getName(int i) {
223 if (i < 0) {
224 return null;
225 }
226 try {
227 return (String) names.elementAt(i);
228 } catch (ArrayIndexOutOfBoundsException e) {
229 return null;
230 }
231 }
232
233 /**
234 * Get the type of an attribute (by position).
235 *
236 * @param i The position of the attribute in the list.
237 * @return The attribute type as a string ("NMTOKEN" for an
238 * enumeration, and "CDATA" if no declaration was
239 * read), or null if there is no attribute at
240 * that position.
241 * @see org.xml.sax.AttributeList#getType(int)
242 */
243 public String getType(int i) {
244 if (i < 0) {
245 return null;
246 }
247 try {
248 return (String) types.elementAt(i);
249 } catch (ArrayIndexOutOfBoundsException e) {
250 return null;
251 }
252 }
253
254 /**
255 * Get the value of an attribute (by position).
256 *
257 * @param i The position of the attribute in the list.
258 * @return The attribute value as a string, or null if
259 * there is no attribute at that position.
260 * @see org.xml.sax.AttributeList#getValue(int)
261 */
262 public String getValue(int i) {
263 if (i < 0) {
264 return null;
265 }
266 try {
267 return (String) values.elementAt(i);
268 } catch (ArrayIndexOutOfBoundsException e) {
269 return null;
270 }
271 }
272
273 /**
274 * Get the type of an attribute (by name).
275 *
276 * @param name The attribute name.
277 * @return The attribute type as a string ("NMTOKEN" for an
278 * enumeration, and "CDATA" if no declaration was
279 * read).
280 * @see org.xml.sax.AttributeList#getType(java.lang.String)
281 */
282 public String getType(String name) {
283 return getType(names.indexOf(name));
284 }
285
286 /**
287 * Get the value of an attribute (by name).
288 *
289 * @param name The attribute name.
290 * @see org.xml.sax.AttributeList#getValue(java.lang.String)
291 */
292 public String getValue(String name) {
293 return getValue(names.indexOf(name));
294 }
295
296 ////////////////////////////////////////////////////////////////////
297 // Internal state.
298 ////////////////////////////////////////////////////////////////////
299
300 Vector names = new Vector();
301 Vector types = new Vector();
302 Vector values = new Vector();
303
304 }
305
306 // end of AttributeListImpl.java
|