1. // SAX default implementation for AttributeList.
  2. // No warranty; no copyright -- use this as you will.
  3. // $Id: AttributeListImpl.java,v 1.2 2001/08/01 06:43:19 tcng Exp $
  4. package org.xml.sax.helpers;
  5. import org.xml.sax.AttributeList;
  6. import java.util.Vector;
  7. /**
  8. * Default implementation for AttributeList.
  9. *
  10. * <blockquote>
  11. * <em>This module, both source code and documentation, is in the
  12. * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  13. * </blockquote>
  14. *
  15. * <p>AttributeList implements the deprecated SAX1 {@link
  16. * org.xml.sax.AttributeList AttributeList} interface, and has been
  17. * replaced by the new SAX2 {@link org.xml.sax.helpers.AttributesImpl
  18. * AttributesImpl} interface.</p>
  19. *
  20. * <p>This class provides a convenience implementation of the SAX
  21. * {@link org.xml.sax.AttributeList AttributeList} interface. This
  22. * implementation is useful both for SAX parser writers, who can use
  23. * it to provide attributes to the application, and for SAX application
  24. * writers, who can use it to create a persistent copy of an element's
  25. * attribute specifications:</p>
  26. *
  27. * <pre>
  28. * private AttributeList myatts;
  29. *
  30. * public void startElement (String name, AttributeList atts)
  31. * {
  32. * // create a persistent copy of the attribute list
  33. * // for use outside this method
  34. * myatts = new AttributeListImpl(atts);
  35. * [...]
  36. * }
  37. * </pre>
  38. *
  39. * <p>Please note that SAX parsers are not required to use this
  40. * class to provide an implementation of AttributeList; it is
  41. * supplied only as an optional convenience. In particular,
  42. * parser writers are encouraged to invent more efficient
  43. * implementations.</p>
  44. *
  45. * @deprecated This class implements a deprecated interface,
  46. * {@link org.xml.sax.AttributeList AttributeList};
  47. * that interface has been replaced by
  48. * {@link org.xml.sax.Attributes Attributes},
  49. * which is implemented in the
  50. * {@link org.xml.sax.helpers.AttributesImpl
  51. * AttributesImpl} helper class.
  52. * @since SAX 1.0
  53. * @author David Megginson,
  54. * <a href="mailto:sax@megginson.com">sax@megginson.com</a>
  55. * @version 2.0
  56. * @see org.xml.sax.AttributeList
  57. * @see org.xml.sax.DocumentHandler#startElement
  58. */
  59. public class AttributeListImpl implements AttributeList
  60. {
  61. /**
  62. * Create an empty attribute list.
  63. *
  64. * <p>This constructor is most useful for parser writers, who
  65. * will use it to create a single, reusable attribute list that
  66. * can be reset with the clear method between elements.</p>
  67. *
  68. * @see #addAttribute
  69. * @see #clear
  70. */
  71. public AttributeListImpl ()
  72. {
  73. }
  74. /**
  75. * Construct a persistent copy of an existing attribute list.
  76. *
  77. * <p>This constructor is most useful for application writers,
  78. * who will use it to create a persistent copy of an existing
  79. * attribute list.</p>
  80. *
  81. * @param atts The attribute list to copy
  82. * @see org.xml.sax.DocumentHandler#startElement
  83. */
  84. public AttributeListImpl (AttributeList atts)
  85. {
  86. setAttributeList(atts);
  87. }
  88. ////////////////////////////////////////////////////////////////////
  89. // Methods specific to this class.
  90. ////////////////////////////////////////////////////////////////////
  91. /**
  92. * Set the attribute list, discarding previous contents.
  93. *
  94. * <p>This method allows an application writer to reuse an
  95. * attribute list easily.</p>
  96. *
  97. * @param atts The attribute list to copy.
  98. */
  99. public void setAttributeList (AttributeList atts)
  100. {
  101. int count = atts.getLength();
  102. clear();
  103. for (int i = 0; i < count; i++) {
  104. addAttribute(atts.getName(i), atts.getType(i), atts.getValue(i));
  105. }
  106. }
  107. /**
  108. * Add an attribute to an attribute list.
  109. *
  110. * <p>This method is provided for SAX parser writers, to allow them
  111. * to build up an attribute list incrementally before delivering
  112. * it to the application.</p>
  113. *
  114. * @param name The attribute name.
  115. * @param type The attribute type ("NMTOKEN" for an enumeration).
  116. * @param value The attribute value (must not be null).
  117. * @see #removeAttribute
  118. * @see org.xml.sax.DocumentHandler#startElement
  119. */
  120. public void addAttribute (String name, String type, String value)
  121. {
  122. names.addElement(name);
  123. types.addElement(type);
  124. values.addElement(value);
  125. }
  126. /**
  127. * Remove an attribute from the list.
  128. *
  129. * <p>SAX application writers can use this method to filter an
  130. * attribute out of an AttributeList. Note that invoking this
  131. * method will change the length of the attribute list and
  132. * some of the attribute's indices.</p>
  133. *
  134. * <p>If the requested attribute is not in the list, this is
  135. * a no-op.</p>
  136. *
  137. * @param name The attribute name.
  138. * @see #addAttribute
  139. */
  140. public void removeAttribute (String name)
  141. {
  142. int i = names.indexOf(name);
  143. if (i >= 0) {
  144. names.removeElementAt(i);
  145. types.removeElementAt(i);
  146. values.removeElementAt(i);
  147. }
  148. }
  149. /**
  150. * Clear the attribute list.
  151. *
  152. * <p>SAX parser writers can use this method to reset the attribute
  153. * list between DocumentHandler.startElement events. Normally,
  154. * it will make sense to reuse the same AttributeListImpl object
  155. * rather than allocating a new one each time.</p>
  156. *
  157. * @see org.xml.sax.DocumentHandler#startElement
  158. */
  159. public void clear ()
  160. {
  161. names.removeAllElements();
  162. types.removeAllElements();
  163. values.removeAllElements();
  164. }
  165. ////////////////////////////////////////////////////////////////////
  166. // Implementation of org.xml.sax.AttributeList
  167. ////////////////////////////////////////////////////////////////////
  168. /**
  169. * Return the number of attributes in the list.
  170. *
  171. * @return The number of attributes in the list.
  172. * @see org.xml.sax.AttributeList#getLength
  173. */
  174. public int getLength ()
  175. {
  176. return names.size();
  177. }
  178. /**
  179. * Get the name of an attribute (by position).
  180. *
  181. * @param i The position of the attribute in the list.
  182. * @return The attribute name as a string, or null if there
  183. * is no attribute at that position.
  184. * @see org.xml.sax.AttributeList#getName(int)
  185. */
  186. public String getName (int i)
  187. {
  188. if (i < 0) {
  189. return null;
  190. }
  191. try {
  192. return (String)names.elementAt(i);
  193. } catch (ArrayIndexOutOfBoundsException e) {
  194. return null;
  195. }
  196. }
  197. /**
  198. * Get the type of an attribute (by position).
  199. *
  200. * @param i The position of the attribute in the list.
  201. * @return The attribute type as a string ("NMTOKEN" for an
  202. * enumeration, and "CDATA" if no declaration was
  203. * read), or null if there is no attribute at
  204. * that position.
  205. * @see org.xml.sax.AttributeList#getType(int)
  206. */
  207. public String getType (int i)
  208. {
  209. if (i < 0) {
  210. return null;
  211. }
  212. try {
  213. return (String)types.elementAt(i);
  214. } catch (ArrayIndexOutOfBoundsException e) {
  215. return null;
  216. }
  217. }
  218. /**
  219. * Get the value of an attribute (by position).
  220. *
  221. * @param i The position of the attribute in the list.
  222. * @return The attribute value as a string, or null if
  223. * there is no attribute at that position.
  224. * @see org.xml.sax.AttributeList#getValue(int)
  225. */
  226. public String getValue (int i)
  227. {
  228. if (i < 0) {
  229. return null;
  230. }
  231. try {
  232. return (String)values.elementAt(i);
  233. } catch (ArrayIndexOutOfBoundsException e) {
  234. return null;
  235. }
  236. }
  237. /**
  238. * Get the type of an attribute (by name).
  239. *
  240. * @param name The attribute name.
  241. * @return The attribute type as a string ("NMTOKEN" for an
  242. * enumeration, and "CDATA" if no declaration was
  243. * read).
  244. * @see org.xml.sax.AttributeList#getType(java.lang.String)
  245. */
  246. public String getType (String name)
  247. {
  248. return getType(names.indexOf(name));
  249. }
  250. /**
  251. * Get the value of an attribute (by name).
  252. *
  253. * @param name The attribute name.
  254. * @see org.xml.sax.AttributeList#getValue(java.lang.String)
  255. */
  256. public String getValue (String name)
  257. {
  258. return getValue(names.indexOf(name));
  259. }
  260. ////////////////////////////////////////////////////////////////////
  261. // Internal state.
  262. ////////////////////////////////////////////////////////////////////
  263. Vector names = new Vector();
  264. Vector types = new Vector();
  265. Vector values = new Vector();
  266. }
  267. // end of AttributeListImpl.java