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