001: /* Copyright 2000 - 2001 Quadcap Software. All rights reserved.
002: *
003: * This software is distributed under the Quadcap Free Software License.
004: * This software may be used or modified for any purpose, personal or
005: * commercial. Open Source redistributions are permitted. Commercial
006: * redistribution of larger works derived from, or works which bundle
007: * this software requires a "Commercial Redistribution License"; see
008: * http://www.quadcap.com/purchase.
009: *
010: * Redistributions qualify as "Open Source" under one of the following terms:
011: *
012: * Redistributions are made at no charge beyond the reasonable cost of
013: * materials and delivery.
014: *
015: * Redistributions are accompanied by a copy of the Source Code or by an
016: * irrevocable offer to provide a copy of the Source Code for up to three
017: * years at the cost of materials and delivery. Such redistributions
018: * must allow further use, modification, and redistribution of the Source
019: * Code under substantially the same terms as this license.
020: *
021: * Redistributions of source code must retain the copyright notices as they
022: * appear in each source code file, these license terms, and the
023: * disclaimer/limitation of liability set forth as paragraph 6 below.
024: *
025: * Redistributions in binary form must reproduce this Copyright Notice,
026: * these license terms, and the disclaimer/limitation of liability set
027: * forth as paragraph 6 below, in the documentation and/or other materials
028: * provided with the distribution.
029: *
030: * The Software is provided on an "AS IS" basis. No warranty is
031: * provided that the Software is free of defects, or fit for a
032: * particular purpose.
033: *
034: * Limitation of Liability. Quadcap Software shall not be liable
035: * for any damages suffered by the Licensee or any third party resulting
036: * from use of the Software.
037: */
038:
039: // SAX default implementation for AttributeList.
040: // No warranty; no copyright -- use this as you will.
041: // $Id: AttributeListImpl.java,v 1.3 2001/01/06 06:11:03 stan Exp $
042: package org.xml.sax.helpers;
043:
044: import org.xml.sax.AttributeList;
045:
046: import java.util.Vector;
047:
048: /**
049: * Convenience implementation for AttributeList.
050: *
051: * <p>This class provides a convenience implementation of the SAX
052: * AttributeList class. This implementation is useful both
053: * for SAX parser writers, who can use it to provide attributes
054: * to the application, and for SAX application writers, who can
055: * use it to create a persistent copy of an element's attribute
056: * specifications:</p>
057: *
058: * <pre>
059: * private AttributeList myatts;
060: *
061: * public void startElement (String name, AttributeList atts)
062: * {
063: * // create a persistent copy of the attribute list
064: * // for use outside this method
065: * myatts = new AttributeListImpl(atts);
066: * [...]
067: * }
068: * </pre>
069: *
070: * <p>Please note that SAX parsers are not required to use this
071: * class to provide an implementation of AttributeList; it is
072: * supplied only as an optional convenience. In particular,
073: * parser writers are encouraged to invent more efficient
074: * implementations.</p>
075: *
076: * @author David Megginson (ak117@freenet.carleton.ca)
077: * @version 1.0
078: * @see org.xml.sax.AttributeList
079: * @see org.xml.sax.DocumentHandler#startElement
080: */
081: public class AttributeListImpl implements AttributeList {
082:
083: /**
084: * Create an empty attribute list.
085: *
086: * <p>This constructor is most useful for parser writers, who
087: * will use it to create a single, reusable attribute list that
088: * can be reset with the clear method between elements.</p>
089: *
090: * @see #addAttribute
091: * @see #clear
092: */
093: public AttributeListImpl() {
094: }
095:
096: /**
097: * Construct a persistent copy of an existing attribute list.
098: *
099: * <p>This constructor is most useful for application writers,
100: * who will use it to create a persistent copy of an existing
101: * attribute list.</p>
102: *
103: * @param atts The attribute list to copy
104: * @see org.xml.sax.DocumentHandler#startElement
105: */
106: public AttributeListImpl(AttributeList atts) {
107: setAttributeList(atts);
108: }
109:
110: //////////////////////////////////////////////////////////////////////
111: // Methods specific to this class.
112: //////////////////////////////////////////////////////////////////////
113:
114: /**
115: * Set the attribute list, discarding previous contents.
116: *
117: * <p>This method allows an application writer to reuse an
118: * attribute list easily.</p>
119: *
120: * @param atts The attribute list to copy.
121: */
122: public void setAttributeList(AttributeList atts) {
123: int count = atts.getLength();
124:
125: clear();
126:
127: for (int i = 0; i < count; i++) {
128: addAttribute(atts.getName(i), atts.getType(i), atts
129: .getValue(i));
130: }
131: }
132:
133: /**
134: * Add an attribute to an attribute list.
135: *
136: * <p>This method is provided for SAX parser writers, to allow them
137: * to build up an attribute list incrementally before delivering
138: * it to the application.</p>
139: *
140: * @param name The attribute name.
141: * @param type The attribute type ("NMTOKEN" for an enumeration).
142: * @param value The attribute value (must not be null).
143: * @see #removeAttribute
144: * @see org.xml.sax.DocumentHandler#startElement
145: */
146: public void addAttribute(String name, String type, String value) {
147: names.addElement(name);
148: types.addElement(type);
149: values.addElement(value);
150: }
151:
152: /**
153: * Remove an attribute from the list.
154: *
155: * <p>SAX application writers can use this method to filter an
156: * attribute out of an AttributeList. Note that invoking this
157: * method will change the length of the attribute list and
158: * some of the attribute's indices.</p>
159: *
160: * <p>If the requested attribute is not in the list, this is
161: * a no-op.</p>
162: *
163: * @param name The attribute name.
164: * @see #addAttribute
165: */
166: public void removeAttribute(String name) {
167: int i = names.indexOf(name);
168:
169: if (i >= 0) {
170: names.removeElementAt(i);
171: types.removeElementAt(i);
172: values.removeElementAt(i);
173: }
174: }
175:
176: /**
177: * Clear the attribute list.
178: *
179: * <p>SAX parser writers can use this method to reset the attribute
180: * list between DocumentHandler.startElement events. Normally,
181: * it will make sense to reuse the same AttributeListImpl object
182: * rather than allocating a new one each time.</p>
183: *
184: * @see org.xml.sax.DocumentHandler#startElement
185: */
186: public void clear() {
187: names.removeAllElements();
188: types.removeAllElements();
189: values.removeAllElements();
190: }
191:
192: //////////////////////////////////////////////////////////////////////
193: // Implementation of org.xml.sax.AttributeList
194: //////////////////////////////////////////////////////////////////////
195:
196: /**
197: * Return the number of attributes in the list.
198: *
199: * @return The number of attributes in the list.
200: * @see org.xml.sax.AttributeList#getLength
201: */
202: public int getLength() {
203: return names.size();
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: try {
216: return (String) names.elementAt(i);
217: } catch (ArrayIndexOutOfBoundsException e) {
218: return null;
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: try {
234: return (String) types.elementAt(i);
235: } catch (ArrayIndexOutOfBoundsException e) {
236: return null;
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: try {
250: return (String) values.elementAt(i);
251: } catch (ArrayIndexOutOfBoundsException e) {
252: return null;
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: * Get the value of an attribute (by name).
271: *
272: * @param name The attribute name.
273: * @see org.xml.sax.AttributeList#getValue(java.lang.String)
274: */
275: public String getValue(String name) {
276: return getValue(names.indexOf(name));
277: }
278:
279: //////////////////////////////////////////////////////////////////////
280: // Internal state.
281: //////////////////////////////////////////////////////////////////////
282:
283: Vector names = new Vector();
284: Vector types = new Vector();
285: Vector values = new Vector();
286:
287: }
|