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 */ 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 */ 94 public AttributeListImpl (AttributeList atts) 95 { 96 setAttributeList(atts); 97 } 98 99 100 101 //////////////////////////////////////////////////////////////////// 103 // Methods specific to this class. 104 //////////////////////////////////////////////////////////////////// 105 106 107 /** 108 * Set the attribute list, discarding previous contents. 109 * 110 * <p>This method allows an application writer to reuse an 111 * attribute list easily.</p> 112 * 113 * @param atts The attribute list to copy. 114 */ 115 public void setAttributeList (AttributeList atts) 116 { 117 int count = atts.getLength(); 118 119 clear(); 120 121 for (int i = 0; i < count; i++) { 122 addAttribute(atts.getName(i), atts.getType(i), atts.getValue(i)); 123 } 124 } 125 126 127 /** 128 * Add an attribute to an attribute list. 129 * 130 * <p>This method is provided for SAX parser writers, to allow them 131 * to build up an attribute list incrementally before delivering 132 * it to the application.</p> 133 * 134 * @param name The attribute name. 135 * @param type The attribute type ("NMTOKEN" for an enumeration). 136 * @param value The attribute value (must not be null). 137 * @see #removeAttribute 138 * @see org.xml.sax.DocumentHandler#startElement 139 */ 140 public void addAttribute(String name, String type, String value) { 141 names.add(name); 142 types.add(type); 143 values.add(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 public void removeAttribute(String name) { 162 int i = names.indexOf(name); 163 if (i != -1) { 164 names.remove(i); 165 types.remove(i); 166 values.remove(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 names.clear(); 183 types.clear(); 184 values.clear(); 185 } 186 187 188 189 //////////////////////////////////////////////////////////////////// 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 return names.size(); 203 } 204 205 206 /** 207 * Get the name of an attribute (by position). 208 * 209 * @param i The position of the attribute in the list. 210 * @return The attribute name as a string, or null if there 211 * is no attribute at that position. 212 * @see org.xml.sax.AttributeList#getName(int) 213 */ 214 public String getName(int i) { 215 if (i < 0 || i >= names.size()) { 216 return null; 217 } 218 return names.get(i); 219 } 220 221 222 /** 223 * Get the type of an attribute (by position). 224 * 225 * @param i The position of the attribute in the list. 226 * @return The attribute type as a string ("NMTOKEN" for an 227 * enumeration, and "CDATA" if no declaration was 228 * read), or null if there is no attribute at 229 * that position. 230 * @see org.xml.sax.AttributeList#getType(int) 231 */ 232 public String getType(int i) { 233 if (i < 0 || i >= types.size()) { 234 return null; 235 } 236 return types.get(i); 237 } 238 239 240 /** 241 * Get the value of an attribute (by position). 242 * 243 * @param i The position of the attribute in the list. 244 * @return The attribute value as a string, or null if 245 * there is no attribute at that position. 246 * @see org.xml.sax.AttributeList#getValue(int) 247 */ 248 public String getValue(int i) { 249 if (i < 0 || i >= values.size()) { 250 return null; 251 } 252 return values.get(i); 253 } 254 255 256 /** 257 * Get the type of an attribute (by name). 258 * 259 * @param name The attribute name. 260 * @return The attribute type as a string ("NMTOKEN" for an 261 * enumeration, and "CDATA" if no declaration was 262 * read). 263 * @see org.xml.sax.AttributeList#getType(java.lang.String) 264 */ 265 public String getType(String name) { 266 return getType(names.indexOf(name)); 267 } 268 269 270 /** 271 * Get the value of an attribute (by name). 272 * 273 * @param name The attribute name. 274 * @return the named attribute's value or null, if the attribute does not 275 * exist. 276 * @see org.xml.sax.AttributeList#getValue(java.lang.String) 277 */ 278 public String getValue(String name) { 279 return getValue(names.indexOf(name)); 280 } 281 282 283 284 //////////////////////////////////////////////////////////////////// 286 // Internal state. 287 //////////////////////////////////////////////////////////////////// 288 289 private ArrayList<String> names = new ArrayList<String>(); 290 private ArrayList<String> types = new ArrayList<String>(); 291 private ArrayList<String> values = new ArrayList<String>(); 292 293 } 294 295 // end of AttributeListImpl.java 296