001: package com.quadcap.text.sax;
002:
003: /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: /**
042: * AttributeList implementation; uses arrays of Strings internally.
043: *
044: * @author Stan Bailes
045: */
046: public class AttributeList implements org.xml.sax.AttributeList {
047: int length = 0;
048: String[] name = new String[8];
049: String[] value = new String[8];
050: String[] type = new String[8];
051:
052: /**
053: * Return the number of attributes in this list.
054: *
055: * <p>The SAX parser may provide attributes in any
056: * arbitrary order, regardless of the order in which they were
057: * declared or specified. The number of attributes may be
058: * zero.</p>
059: *
060: * @return The number of attributes in the list.
061: */
062: public final int getLength() {
063: return length;
064: }
065:
066: /**
067: * Return the name of an attribute in this list (by position).
068: *
069: * <p>The names must be unique: the SAX parser shall not include the
070: * same attribute twice. Attributes without values (those declared
071: * #IMPLIED without a value specified in the start tag) will be
072: * omitted from the list.</p>
073: *
074: * <p>If the attribute name has a namespace prefix, the prefix
075: * will still be attached.</p>
076: *
077: * @param i The index of the attribute in the list (starting at 0).
078: * @return The name of the indexed attribute, or null
079: * if the index is out of range.
080: * @see #getLength
081: */
082: public final String getName(int i) {
083: try {
084: return name[i];
085: } catch (ArrayIndexOutOfBoundsException e) {
086: return null;
087: }
088: }
089:
090: /**
091: * Return the type of an attribute in the list (by position).
092: *
093: * <p>The attribute type is one of the strings "CDATA", "ID",
094: * "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES",
095: * or "NOTATION" (always in upper case).</p>
096: *
097: * <p>If the parser has not read a declaration for the attribute,
098: * or if the parser does not report attribute types, then it must
099: * return the value "CDATA" as stated in the XML 1.0 Recommentation
100: * (clause 3.3.3, "Attribute-Value Normalization").</p>
101: *
102: * <p>For an enumerated attribute that is not a notation, the
103: * parser will report the type as "NMTOKEN".</p>
104: *
105: * @param i The index of the attribute in the list (starting at 0).
106: * @return The attribute type as a string, or
107: * null if the index is out of range.
108: * @see #getLength
109: * @see #getType(java.lang.String)
110: */
111: public final String getType(int i) {
112: try {
113: return type[i];
114: } catch (ArrayIndexOutOfBoundsException e) {
115: return null;
116: }
117: }
118:
119: /**
120: * Return the value of an attribute in the list (by position).
121: *
122: * <p>If the attribute value is a list of tokens (IDREFS,
123: * ENTITIES, or NMTOKENS), the tokens will be concatenated
124: * into a single string separated by whitespace.</p>
125: *
126: * @param i The index of the attribute in the list (starting at 0).
127: * @return The attribute value as a string, or
128: * null if the index is out of range.
129: * @see #getLength
130: * @see #getValue(java.lang.String)
131: */
132: public final String getValue(int i) {
133: try {
134: return value[i];
135: } catch (ArrayIndexOutOfBoundsException e) {
136: return null;
137: }
138: }
139:
140: /**
141: * Return the type of an attribute in the list (by name).
142: *
143: * <p>The return value is the same as the return value for
144: * getType(int).</p>
145: *
146: * <p>If the attribute name has a namespace prefix in the document,
147: * the application must include the prefix here.</p>
148: *
149: * @param name The name of the attribute.
150: * @return The attribute type as a string, or null if no
151: * such attribute exists.
152: * @see #getType(int)
153: */
154: public final String getType(String name) {
155: return getType(getAttribute(name));
156: }
157:
158: /**
159: * Return the value of an attribute in the list (by name).
160: *
161: * <p>The return value is the same as the return value for
162: * getValue(int).</p>
163: *
164: * <p>If the attribute name has a namespace prefix in the document,
165: * the application must include the prefix here.</p>
166: *
167: * @param i The index of the attribute in the list.
168: * @return The attribute value as a string, or null if
169: * no such attribute exists.
170: * @see #getValue(int)
171: */
172: public final String getValue(String n) {
173: return getValue(getAttribute(n));
174: }
175:
176: final int getAttribute(String n) {
177: for (int i = 0; i < length; i++) {
178: if (name[i].equals(n))
179: return i;
180: }
181: return -1;
182: }
183:
184: final String[] resize(String[] v, int len) {
185: String[] n = new String[len];
186: System.arraycopy(v, 0, n, 0, v.length);
187: return n;
188: }
189:
190: final void addAttribute(String n, String t, String v) {
191: if (length >= name.length) {
192: name = resize(name, length + (length >> 2) + 4);
193: type = resize(type, length + (length >> 2) + 4);
194: value = resize(value, length + (length >> 2) + 4);
195: }
196: name[length] = n;
197: type[length] = t;
198: value[length] = v;
199: length++;
200: }
201:
202: final void clear() {
203: length = 0;
204: }
205:
206: public static AttributeList copy(org.xml.sax.AttributeList a) {
207: AttributeList c = new AttributeList();
208: for (int i = 0; i < a.getLength(); i++) {
209: c.addAttribute(a.getName(i), a.getType(i), a.getValue(i));
210: }
211: return c;
212: }
213:
214: public static String toString(org.xml.sax.AttributeList attributes) {
215: StringBuffer sb = new StringBuffer();
216: if (attributes != null) {
217: for (int i = 0; i < attributes.getLength(); i++) {
218: sb.append(' ');
219: sb.append(attributes.getName(i));
220: sb.append("=\"");
221: sb.append(attributes.getValue(i));
222: sb.append("\"");
223: }
224: }
225: return sb.toString();
226: }
227:
228: public String toString() {
229: return toString(this);
230: }
231: }
|