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