package com.quadcap.text.sax;
/* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
*
* This software is distributed under the Quadcap Free Software License.
* This software may be used or modified for any purpose, personal or
* commercial. Open Source redistributions are permitted. Commercial
* redistribution of larger works derived from, or works which bundle
* this software requires a "Commercial Redistribution License"; see
* http://www.quadcap.com/purchase.
*
* Redistributions qualify as "Open Source" under one of the following terms:
*
* Redistributions are made at no charge beyond the reasonable cost of
* materials and delivery.
*
* Redistributions are accompanied by a copy of the Source Code or by an
* irrevocable offer to provide a copy of the Source Code for up to three
* years at the cost of materials and delivery. Such redistributions
* must allow further use, modification, and redistribution of the Source
* Code under substantially the same terms as this license.
*
* Redistributions of source code must retain the copyright notices as they
* appear in each source code file, these license terms, and the
* disclaimer/limitation of liability set forth as paragraph 6 below.
*
* Redistributions in binary form must reproduce this Copyright Notice,
* these license terms, and the disclaimer/limitation of liability set
* forth as paragraph 6 below, in the documentation and/or other materials
* provided with the distribution.
*
* The Software is provided on an "AS IS" basis. No warranty is
* provided that the Software is free of defects, or fit for a
* particular purpose.
*
* Limitation of Liability. Quadcap Software shall not be liable
* for any damages suffered by the Licensee or any third party resulting
* from use of the Software.
*/
/**
* AttributeList implementation; uses arrays of Strings internally.
*
* @author Stan Bailes
*/
public class AttributeList implements org.xml.sax.AttributeList {
int length = 0;
String[] name = new String[8];
String[] value = new String[8];
String[] type = new String[8];
/**
* Return the number of attributes in this list.
*
* <p>The SAX parser may provide attributes in any
* arbitrary order, regardless of the order in which they were
* declared or specified. The number of attributes may be
* zero.</p>
*
* @return The number of attributes in the list.
*/
public final int getLength() {
return length;
}
/**
* Return the name of an attribute in this list (by position).
*
* <p>The names must be unique: the SAX parser shall not include the
* same attribute twice. Attributes without values (those declared
* #IMPLIED without a value specified in the start tag) will be
* omitted from the list.</p>
*
* <p>If the attribute name has a namespace prefix, the prefix
* will still be attached.</p>
*
* @param i The index of the attribute in the list (starting at 0).
* @return The name of the indexed attribute, or null
* if the index is out of range.
* @see #getLength
*/
public final String getName(int i) {
try {
return name[i];
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Return the type of an attribute in the list (by position).
*
* <p>The attribute type is one of the strings "CDATA", "ID",
* "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES",
* or "NOTATION" (always in upper case).</p>
*
* <p>If the parser has not read a declaration for the attribute,
* or if the parser does not report attribute types, then it must
* return the value "CDATA" as stated in the XML 1.0 Recommentation
* (clause 3.3.3, "Attribute-Value Normalization").</p>
*
* <p>For an enumerated attribute that is not a notation, the
* parser will report the type as "NMTOKEN".</p>
*
* @param i The index of the attribute in the list (starting at 0).
* @return The attribute type as a string, or
* null if the index is out of range.
* @see #getLength
* @see #getType(java.lang.String)
*/
public final String getType(int i) {
try {
return type[i];
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Return the value of an attribute in the list (by position).
*
* <p>If the attribute value is a list of tokens (IDREFS,
* ENTITIES, or NMTOKENS), the tokens will be concatenated
* into a single string separated by whitespace.</p>
*
* @param i The index of the attribute in the list (starting at 0).
* @return The attribute value as a string, or
* null if the index is out of range.
* @see #getLength
* @see #getValue(java.lang.String)
*/
public final String getValue (int i) {
try {
return value[i];
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Return the type of an attribute in the list (by name).
*
* <p>The return value is the same as the return value for
* getType(int).</p>
*
* <p>If the attribute name has a namespace prefix in the document,
* the application must include the prefix here.</p>
*
* @param name The name of the attribute.
* @return The attribute type as a string, or null if no
* such attribute exists.
* @see #getType(int)
*/
public final String getType(String name) {
return getType(getAttribute(name));
}
/**
* Return the value of an attribute in the list (by name).
*
* <p>The return value is the same as the return value for
* getValue(int).</p>
*
* <p>If the attribute name has a namespace prefix in the document,
* the application must include the prefix here.</p>
*
* @param i The index of the attribute in the list.
* @return The attribute value as a string, or null if
* no such attribute exists.
* @see #getValue(int)
*/
public final String getValue(String n) {
return getValue(getAttribute(n));
}
final int getAttribute(String n) {
for (int i = 0; i < length; i++) {
if (name[i].equals(n)) return i;
}
return -1;
}
final String[] resize(String[] v, int len) {
String[] n = new String[len];
System.arraycopy(v, 0, n, 0, v.length);
return n;
}
final void addAttribute(String n, String t, String v) {
if (length >= name.length) {
name = resize(name, length + (length >> 2) + 4);
type = resize(type, length + (length >> 2) + 4);
value = resize(value, length + (length >> 2) + 4);
}
name[length] = n;
type[length] = t;
value[length] = v;
length++;
}
final void clear() {
length = 0;
}
public static AttributeList copy(org.xml.sax.AttributeList a) {
AttributeList c = new AttributeList();
for (int i = 0; i < a.getLength(); i++) {
c.addAttribute(a.getName(i), a.getType(i), a.getValue(i));
}
return c;
}
public static String toString(org.xml.sax.AttributeList attributes) {
StringBuffer sb = new StringBuffer();
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
sb.append(' ');
sb.append(attributes.getName(i));
sb.append("=\"");
sb.append(attributes.getValue(i));
sb.append("\"");
}
}
return sb.toString();
}
public String toString() {
return toString(this);
}
}
|