1 /* 2 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 // SAX default implementation for AttributeList. 27 // http://www.saxproject.org 28 // No warranty; no copyright -- use this as you will. 29 // $Id: AttributeListImpl.java,v 1.2 2004/11/03 22:53:08 jsuttor Exp $ 30 31 package org.xml.sax.helpers; 32 33 import java.util.ArrayList; 34 import java.util.List; 35 import org.xml.sax.AttributeList; 36 37 38 /** 39 * Default implementation for AttributeList. 40 * 41 * <blockquote> 42 * <em>This module, both source code and documentation, is in the 43 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> 44 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a> 45 * for further information. 46 * </blockquote> 47 * 48 * <p>AttributeList implements the deprecated SAX1 {@link 49 * org.xml.sax.AttributeList AttributeList} interface, and has been 50 * replaced by the new SAX2 {@link org.xml.sax.helpers.AttributesImpl 51 * AttributesImpl} interface.</p> 52 * 53 * <p>This class provides a convenience implementation of the SAX 54 * {@link org.xml.sax.AttributeList AttributeList} interface. This 55 * implementation is useful both for SAX parser writers, who can use 56 * it to provide attributes to the application, and for SAX application 57 * writers, who can use it to create a persistent copy of an element's 58 * attribute specifications:</p> 59 * 60 * <pre> 61 * private AttributeList myatts; 62 * 63 * public void startElement (String name, AttributeList atts) 64 * { 65 * // create a persistent copy of the attribute list 66 * // for use outside this method 67 * myatts = new AttributeListImpl(atts); 68 * [...] 69 * } 70 * </pre> 71 * 72 * <p>Please note that SAX parsers are not required to use this 73 * class to provide an implementation of AttributeList; it is 74 * supplied only as an optional convenience. In particular, 75 * parser writers are encouraged to invent more efficient 76 * implementations.</p> 77 * 78 * @deprecated This class implements a deprecated interface, 79 * {@link org.xml.sax.AttributeList AttributeList}; 80 * that interface has been replaced by 81 * {@link org.xml.sax.Attributes Attributes}, 82 * which is implemented in the 83 * {@link org.xml.sax.helpers.AttributesImpl 84 * AttributesImpl} helper class. 85 * @since 1.4, SAX 1.0 86 * @author David Megginson 87 * @see org.xml.sax.AttributeList 88 * @see org.xml.sax.DocumentHandler#startElement 89 */ 90 @Deprecated(since="1.5") 91 public class AttributeListImpl implements AttributeList 92 { 93 94 /** 95 * Create an empty attribute list. 96 * 97 * <p>This constructor is most useful for parser writers, who 98 * will use it to create a single, reusable attribute list that 99 * can be reset with the clear method between elements.</p> 100 * 101 * @see #addAttribute 102 * @see #clear 103 */ 104 public AttributeListImpl () 105 { 106 } 107 108 109 /** 110 * Construct a persistent copy of an existing attribute list. 111 * 112 * <p>This constructor is most useful for application writers, 113 * who will use it to create a persistent copy of an existing 114 * attribute list.</p> 115 * 116 * @param atts The attribute list to copy 117 * @see org.xml.sax.DocumentHandler#startElement 118 */ 119 public AttributeListImpl (AttributeList atts) 120 { 121 setAttributeList(atts); 122 } 123 124 125 126 //////////////////////////////////////////////////////////////////// 127 // Methods specific to this class. 128 //////////////////////////////////////////////////////////////////// 129 130 131 /** 132 * Set the attribute list, discarding previous contents. 133 * 134 * <p>This method allows an application writer to reuse an 135 * attribute list easily.</p> 136 * 137 * @param atts The attribute list to copy. 138 */ 139 public void setAttributeList (AttributeList atts) 140 { 141 int count = atts.getLength(); 142 143 clear(); 144 145 for (int i = 0; i < count; i++) { 146 addAttribute(atts.getName(i), atts.getType(i), atts.getValue(i)); 147 } 148 } 149 150 151 /** 152 * Add an attribute to an attribute list. 153 * 154 * <p>This method is provided for SAX parser writers, to allow them 155 * to build up an attribute list incrementally before delivering 156 * it to the application.</p> 157 * 158 * @param name The attribute name. 159 * @param type The attribute type ("NMTOKEN" for an enumeration). 160 * @param value The attribute value (must not be null). 161 * @see #removeAttribute 162 * @see org.xml.sax.DocumentHandler#startElement 163 */ 164 public void addAttribute (String name, String type, String value) 165 { 166 names.add(name); 167 types.add(type); 168 values.add(value); 169 } 170 171 172 /** 173 * Remove an attribute from the list. 174 * 175 * <p>SAX application writers can use this method to filter an 176 * attribute out of an AttributeList. Note that invoking this 177 * method will change the length of the attribute list and 178 * some of the attribute's indices.</p> 179 * 180 * <p>If the requested attribute is not in the list, this is 181 * a no-op.</p> 182 * 183 * @param name The attribute name. 184 * @see #addAttribute 185 */ 186 public void removeAttribute (String name) 187 { 188 int i = names.indexOf(name); 189 190 if (i >= 0) { 191 names.remove(i); 192 types.remove(i); 193 values.remove(i); 194 } 195 } 196 197 198 /** 199 * Clear the attribute list. 200 * 201 * <p>SAX parser writers can use this method to reset the attribute 202 * list between DocumentHandler.startElement events. Normally, 203 * it will make sense to reuse the same AttributeListImpl object 204 * rather than allocating a new one each time.</p> 205 * 206 * @see org.xml.sax.DocumentHandler#startElement 207 */ 208 public void clear () 209 { 210 names.clear(); 211 types.clear(); 212 values.clear(); 213 } 214 215 216 217 //////////////////////////////////////////////////////////////////// 218 // Implementation of org.xml.sax.AttributeList 219 //////////////////////////////////////////////////////////////////// 220 221 222 /** 223 * Return the number of attributes in the list. 224 * 225 * @return The number of attributes in the list. 226 * @see org.xml.sax.AttributeList#getLength 227 */ 228 public int getLength () 229 { 230 return names.size(); 231 } 232 233 234 /** 235 * Get the name of an attribute (by position). 236 * 237 * @param i The position of the attribute in the list. 238 * @return The attribute name as a string, or null if there 239 * is no attribute at that position. 240 * @see org.xml.sax.AttributeList#getName(int) 241 */ 242 public String getName (int i) 243 { 244 if (i < 0) { 245 return null; 246 } 247 try { 248 return names.get(i); 249 } catch (IndexOutOfBoundsException e) { 250 return null; 251 } 252 } 253 254 255 /** 256 * Get the type of an attribute (by position). 257 * 258 * @param i The position of the attribute in the list. 259 * @return The attribute type as a string ("NMTOKEN" for an 260 * enumeration, and "CDATA" if no declaration was 261 * read), or null if there is no attribute at 262 * that position. 263 * @see org.xml.sax.AttributeList#getType(int) 264 */ 265 public String getType (int i) 266 { 267 if (i < 0) { 268 return null; 269 } 270 try { 271 return types.get(i); 272 } catch (IndexOutOfBoundsException e) { 273 return null; 274 } 275 } 276 277 278 /** 279 * Get the value of an attribute (by position). 280 * 281 * @param i The position of the attribute in the list. 282 * @return The attribute value as a string, or null if 283 * there is no attribute at that position. 284 * @see org.xml.sax.AttributeList#getValue(int) 285 */ 286 public String getValue (int i) 287 { 288 if (i < 0) { 289 return null; 290 } 291 try { 292 return values.get(i); 293 } catch (IndexOutOfBoundsException e) { 294 return null; 295 } 296 } 297 298 299 /** 300 * Get the type of an attribute (by name). 301 * 302 * @param name The attribute name. 303 * @return The attribute type as a string ("NMTOKEN" for an 304 * enumeration, and "CDATA" if no declaration was 305 * read). 306 * @see org.xml.sax.AttributeList#getType(java.lang.String) 307 */ 308 public String getType (String name) 309 { 310 return getType(names.indexOf(name)); 311 } 312 313 314 /** 315 * Get the value of an attribute (by name). 316 * 317 * @param name The attribute name. 318 * @see org.xml.sax.AttributeList#getValue(java.lang.String) 319 */ 320 public String getValue (String name) 321 { 322 return getValue(names.indexOf(name)); 323 } 324 325 326 327 //////////////////////////////////////////////////////////////////// 328 // Internal state. 329 //////////////////////////////////////////////////////////////////// 330 331 List<String> names = new ArrayList<>(); 332 List<String> types = new ArrayList<>(); 333 List<String> values = new ArrayList<>(); 334 335 } 336 337 // end of AttributeListImpl.java