001: // Attributes2Impl.java - extended AttributesImpl
002: // http://www.saxproject.org
003: // Public Domain: no warranty.
004: // $Id: Attributes2Impl.java,v 1.3 2002/02/01 20:06:20 db Exp $
005:
006: package org.xml.sax.ext;
007:
008: import org.xml.sax.Attributes;
009: import org.xml.sax.helpers.AttributesImpl;
010:
011: /**
012: * SAX2 extension helper for additional Attributes information,
013: * implementing the {@link Attributes2} interface.
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: * </blockquote>
019: *
020: * <p>This is not part of core-only SAX2 distributions.</p>
021: *
022: * <p>The <em>specified</em> flag for each attribute will always
023: * be true, unless it has been set to false in the copy constructor
024: * or using {@link #setSpecified}. </p>
025: *
026: * @since SAX 2.0 (extensions 1.1 alpha)
027: * @author David Brownell
028: * @version TBS
029: */
030: public class Attributes2Impl extends AttributesImpl implements
031: Attributes2 {
032: private boolean flags[];
033:
034: /**
035: * Construct a new, empty Attributes2Impl object.
036: */
037: public Attributes2Impl() {
038: }
039:
040: /**
041: * Copy an existing Attributes or Attributes2 object.
042: * If the object implements Attributes2, values of the
043: * <em>specified</em> flag for each attribute are copied,
044: * otherwise the flag values are set to <em>true</em>.
045: *
046: * <p>This constructor is especially useful inside a
047: * {@link org.xml.sax.ContentHandler#startElement startElement} event.</p>
048: *
049: * @param atts The existing Attributes object.
050: */
051: public Attributes2Impl(Attributes atts) {
052: super (atts);
053: }
054:
055: ////////////////////////////////////////////////////////////////////
056: // Implementation of Attributes2
057: ////////////////////////////////////////////////////////////////////
058:
059: /**
060: * Returns the current value of an attribute's "specified" flag.
061: *
062: * @param index The attribute index (zero-based).
063: * @return current flag value
064: * @exception java.lang.ArrayIndexOutOfBoundsException When the
065: * supplied index does not identify an attribute.
066: */
067: public boolean isSpecified(int index) {
068: if (index < 0 || index >= getLength())
069: throw new ArrayIndexOutOfBoundsException(
070: "No attribute at index: " + index);
071: return flags[index];
072: }
073:
074: /**
075: * Returns the current value of an attribute's "specified" flag.
076: *
077: * @param uri The Namespace URI, or the empty string if
078: * the name has no Namespace URI.
079: * @param localName The attribute's local name.
080: * @return current flag value
081: * @exception java.lang.IllegalArgumentException When the
082: * supplied names do not identify an attribute.
083: */
084: public boolean isSpecified(String uri, String localName) {
085: int index = getIndex(uri, localName);
086:
087: if (index < 0)
088: throw new IllegalArgumentException(
089: "No such attribute: local=" + localName
090: + ", namespace=" + uri);
091: return flags[index];
092: }
093:
094: /**
095: * Returns the current value of an attribute's "specified" flag.
096: *
097: * @param qName The XML 1.0 qualified name.
098: * @return current flag value
099: * @exception java.lang.IllegalArgumentException When the
100: * supplied name does not identify an attribute.
101: */
102: public boolean isSpecified(String qName) {
103: int index = getIndex(qName);
104:
105: if (index < 0)
106: throw new IllegalArgumentException("No such attribute: "
107: + qName);
108: return flags[index];
109: }
110:
111: ////////////////////////////////////////////////////////////////////
112: // Manipulators
113: ////////////////////////////////////////////////////////////////////
114:
115: /**
116: * Copy an entire Attributes object. The "specified" flags are
117: * assigned as true, unless the object is an Attributes2 object
118: * in which case those values are copied.
119: *
120: * @see AttributesImpl#setAttributes
121: */
122: public void setAttributes(Attributes atts) {
123: int length = atts.getLength();
124:
125: super .setAttributes(atts);
126: flags = new boolean[length];
127:
128: if (atts instanceof Attributes2) {
129: Attributes2 a2 = (Attributes2) atts;
130: for (int i = 0; i < length; i++)
131: flags[i] = a2.isSpecified(i);
132: } else {
133: for (int i = 0; i < length; i++)
134: flags[i] = true;
135: }
136:
137: }
138:
139: /**
140: * Add an attribute to the end of the list, setting its
141: * "specified" flag to true. To set that flag's value
142: * to false, use {@link #setSpecified}.
143: *
144: * @see AttributesImpl#addAttribute
145: */
146: public void addAttribute(String uri, String localName,
147: String qName, String type, String value) {
148: super .addAttribute(uri, localName, qName, type, value);
149:
150: int length = getLength();
151:
152: if (length < flags.length) {
153: boolean newFlags[] = new boolean[length];
154: System.arraycopy(flags, 0, newFlags, 0, flags.length);
155: flags = newFlags;
156: }
157:
158: flags[length - 1] = true;
159: }
160:
161: // javadoc entirely from superclass
162: public void removeAttribute(int index) {
163: int origMax = getLength() - 1;
164:
165: super .removeAttribute(index);
166: if (index != origMax)
167: System.arraycopy(flags, index + 1, flags, index, origMax
168: - index);
169: }
170:
171: /**
172: * Assign a value to the "specified" flag of a specific attribute.
173: * This is the only way this flag can be cleared, except clearing
174: * by initialization with the copy constructor.
175: *
176: * @param index The index of the attribute (zero-based).
177: * @param value The desired flag value.
178: * @exception java.lang.ArrayIndexOutOfBoundsException When the
179: * supplied index does not identify an attribute.
180: */
181: public void setSpecified(int index, boolean value) {
182: if (index < 0 || index >= getLength())
183: throw new ArrayIndexOutOfBoundsException(
184: "No attribute at index: " + index);
185: flags[index] = value;
186: }
187: }
|