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