286N/A/*
286N/A * Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/A// AttributesImpl.java - default implementation of Attributes.
286N/A// http://www.saxproject.org
286N/A// Written by David Megginson
286N/A// NO WARRANTY! This class is in the public domain.
286N/A// $Id: AttributesImpl.java,v 1.2 2004/11/03 22:53:08 jsuttor Exp $
286N/A
286N/Apackage org.xml.sax.helpers;
286N/A
286N/Aimport org.xml.sax.Attributes;
286N/A
286N/A
286N/A/**
286N/A * Default implementation of the Attributes interface.
286N/A *
286N/A * <blockquote>
286N/A * <em>This module, both source code and documentation, is in the
286N/A * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
286N/A * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
286N/A * for further information.
286N/A * </blockquote>
286N/A *
286N/A * <p>This class provides a default implementation of the SAX2
286N/A * {@link org.xml.sax.Attributes Attributes} interface, with the
286N/A * addition of manipulators so that the list can be modified or
286N/A * reused.</p>
286N/A *
286N/A * <p>There are two typical uses of this class:</p>
286N/A *
286N/A * <ol>
286N/A * <li>to take a persistent snapshot of an Attributes object
286N/A * in a {@link org.xml.sax.ContentHandler#startElement startElement} event; or</li>
286N/A * <li>to construct or modify an Attributes object in a SAX2 driver or filter.</li>
286N/A * </ol>
286N/A *
286N/A * <p>This class replaces the now-deprecated SAX1 {@link
286N/A * org.xml.sax.helpers.AttributeListImpl AttributeListImpl}
286N/A * class; in addition to supporting the updated Attributes
286N/A * interface rather than the deprecated {@link org.xml.sax.AttributeList
286N/A * AttributeList} interface, it also includes a much more efficient
286N/A * implementation using a single array rather than a set of Vectors.</p>
286N/A *
286N/A * @since SAX 2.0
286N/A * @author David Megginson
286N/A */
286N/Apublic class AttributesImpl implements Attributes
286N/A{
286N/A
286N/A
286N/A ////////////////////////////////////////////////////////////////////
286N/A // Constructors.
286N/A ////////////////////////////////////////////////////////////////////
286N/A
286N/A
286N/A /**
286N/A * Construct a new, empty AttributesImpl object.
286N/A */
286N/A public AttributesImpl ()
286N/A {
286N/A length = 0;
286N/A data = null;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Copy an existing Attributes object.
286N/A *
286N/A * <p>This constructor is especially useful inside a
286N/A * {@link org.xml.sax.ContentHandler#startElement startElement} event.</p>
286N/A *
286N/A * @param atts The existing Attributes object.
286N/A */
286N/A public AttributesImpl (Attributes atts)
286N/A {
286N/A setAttributes(atts);
286N/A }
286N/A
286N/A
286N/A
286N/A ////////////////////////////////////////////////////////////////////
286N/A // Implementation of org.xml.sax.Attributes.
286N/A ////////////////////////////////////////////////////////////////////
286N/A
286N/A
286N/A /**
286N/A * Return the number of attributes in the list.
286N/A *
286N/A * @return The number of attributes in the list.
286N/A * @see org.xml.sax.Attributes#getLength
286N/A */
286N/A public int getLength ()
286N/A {
286N/A return length;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Return an attribute's Namespace URI.
286N/A *
286N/A * @param index The attribute's index (zero-based).
286N/A * @return The Namespace URI, the empty string if none is
286N/A * available, or null if the index is out of range.
286N/A * @see org.xml.sax.Attributes#getURI
286N/A */
286N/A public String getURI (int index)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A return data[index*5];
286N/A } else {
286N/A return null;
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Return an attribute's local name.
286N/A *
286N/A * @param index The attribute's index (zero-based).
286N/A * @return The attribute's local name, the empty string if
286N/A * none is available, or null if the index if out of range.
286N/A * @see org.xml.sax.Attributes#getLocalName
286N/A */
286N/A public String getLocalName (int index)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A return data[index*5+1];
286N/A } else {
286N/A return null;
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Return an attribute's qualified (prefixed) name.
286N/A *
286N/A * @param index The attribute's index (zero-based).
286N/A * @return The attribute's qualified name, the empty string if
286N/A * none is available, or null if the index is out of bounds.
286N/A * @see org.xml.sax.Attributes#getQName
286N/A */
286N/A public String getQName (int index)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A return data[index*5+2];
286N/A } else {
286N/A return null;
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Return an attribute's type by index.
286N/A *
286N/A * @param index The attribute's index (zero-based).
286N/A * @return The attribute's type, "CDATA" if the type is unknown, or null
286N/A * if the index is out of bounds.
286N/A * @see org.xml.sax.Attributes#getType(int)
286N/A */
286N/A public String getType (int index)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A return data[index*5+3];
286N/A } else {
286N/A return null;
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Return an attribute's value by index.
286N/A *
286N/A * @param index The attribute's index (zero-based).
286N/A * @return The attribute's value or null if the index is out of bounds.
286N/A * @see org.xml.sax.Attributes#getValue(int)
286N/A */
286N/A public String getValue (int index)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A return data[index*5+4];
286N/A } else {
286N/A return null;
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Look up an attribute's index by Namespace name.
286N/A *
286N/A * <p>In many cases, it will be more efficient to look up the name once and
286N/A * use the index query methods rather than using the name query methods
286N/A * repeatedly.</p>
286N/A *
286N/A * @param uri The attribute's Namespace URI, or the empty
286N/A * string if none is available.
286N/A * @param localName The attribute's local name.
286N/A * @return The attribute's index, or -1 if none matches.
286N/A * @see org.xml.sax.Attributes#getIndex(java.lang.String,java.lang.String)
286N/A */
286N/A public int getIndex (String uri, String localName)
286N/A {
286N/A int max = length * 5;
286N/A for (int i = 0; i < max; i += 5) {
286N/A if (data[i].equals(uri) && data[i+1].equals(localName)) {
286N/A return i / 5;
286N/A }
286N/A }
286N/A return -1;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Look up an attribute's index by qualified (prefixed) name.
286N/A *
286N/A * @param qName The qualified name.
286N/A * @return The attribute's index, or -1 if none matches.
286N/A * @see org.xml.sax.Attributes#getIndex(java.lang.String)
286N/A */
286N/A public int getIndex (String qName)
286N/A {
286N/A int max = length * 5;
286N/A for (int i = 0; i < max; i += 5) {
286N/A if (data[i+2].equals(qName)) {
286N/A return i / 5;
286N/A }
286N/A }
286N/A return -1;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Look up an attribute's type by Namespace-qualified name.
286N/A *
286N/A * @param uri The Namespace URI, or the empty string for a name
286N/A * with no explicit Namespace URI.
286N/A * @param localName The local name.
286N/A * @return The attribute's type, or null if there is no
286N/A * matching attribute.
286N/A * @see org.xml.sax.Attributes#getType(java.lang.String,java.lang.String)
286N/A */
286N/A public String getType (String uri, String localName)
286N/A {
286N/A int max = length * 5;
286N/A for (int i = 0; i < max; i += 5) {
286N/A if (data[i].equals(uri) && data[i+1].equals(localName)) {
286N/A return data[i+3];
286N/A }
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Look up an attribute's type by qualified (prefixed) name.
286N/A *
286N/A * @param qName The qualified name.
286N/A * @return The attribute's type, or null if there is no
286N/A * matching attribute.
286N/A * @see org.xml.sax.Attributes#getType(java.lang.String)
286N/A */
286N/A public String getType (String qName)
286N/A {
286N/A int max = length * 5;
286N/A for (int i = 0; i < max; i += 5) {
286N/A if (data[i+2].equals(qName)) {
286N/A return data[i+3];
286N/A }
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Look up an attribute's value by Namespace-qualified name.
286N/A *
286N/A * @param uri The Namespace URI, or the empty string for a name
286N/A * with no explicit Namespace URI.
286N/A * @param localName The local name.
286N/A * @return The attribute's value, or null if there is no
286N/A * matching attribute.
286N/A * @see org.xml.sax.Attributes#getValue(java.lang.String,java.lang.String)
286N/A */
286N/A public String getValue (String uri, String localName)
286N/A {
286N/A int max = length * 5;
286N/A for (int i = 0; i < max; i += 5) {
286N/A if (data[i].equals(uri) && data[i+1].equals(localName)) {
286N/A return data[i+4];
286N/A }
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Look up an attribute's value by qualified (prefixed) name.
286N/A *
286N/A * @param qName The qualified name.
286N/A * @return The attribute's value, or null if there is no
286N/A * matching attribute.
286N/A * @see org.xml.sax.Attributes#getValue(java.lang.String)
286N/A */
286N/A public String getValue (String qName)
286N/A {
286N/A int max = length * 5;
286N/A for (int i = 0; i < max; i += 5) {
286N/A if (data[i+2].equals(qName)) {
286N/A return data[i+4];
286N/A }
286N/A }
286N/A return null;
286N/A }
286N/A
286N/A
286N/A
286N/A ////////////////////////////////////////////////////////////////////
286N/A // Manipulators.
286N/A ////////////////////////////////////////////////////////////////////
286N/A
286N/A
286N/A /**
286N/A * Clear the attribute list for reuse.
286N/A *
286N/A * <p>Note that little memory is freed by this call:
286N/A * the current array is kept so it can be
286N/A * reused.</p>
286N/A */
286N/A public void clear ()
286N/A {
286N/A if (data != null) {
286N/A for (int i = 0; i < (length * 5); i++)
286N/A data [i] = null;
286N/A }
286N/A length = 0;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Copy an entire Attributes object.
286N/A *
286N/A * <p>It may be more efficient to reuse an existing object
286N/A * rather than constantly allocating new ones.</p>
286N/A *
286N/A * @param atts The attributes to copy.
286N/A */
286N/A public void setAttributes (Attributes atts)
286N/A {
286N/A clear();
286N/A length = atts.getLength();
286N/A if (length > 0) {
286N/A data = new String[length*5];
286N/A for (int i = 0; i < length; i++) {
286N/A data[i*5] = atts.getURI(i);
286N/A data[i*5+1] = atts.getLocalName(i);
286N/A data[i*5+2] = atts.getQName(i);
286N/A data[i*5+3] = atts.getType(i);
286N/A data[i*5+4] = atts.getValue(i);
286N/A }
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Add an attribute to the end of the list.
286N/A *
286N/A * <p>For the sake of speed, this method does no checking
286N/A * to see if the attribute is already in the list: that is
286N/A * the responsibility of the application.</p>
286N/A *
286N/A * @param uri The Namespace URI, or the empty string if
286N/A * none is available or Namespace processing is not
286N/A * being performed.
286N/A * @param localName The local name, or the empty string if
286N/A * Namespace processing is not being performed.
286N/A * @param qName The qualified (prefixed) name, or the empty string
286N/A * if qualified names are not available.
286N/A * @param type The attribute type as a string.
286N/A * @param value The attribute value.
286N/A */
286N/A public void addAttribute (String uri, String localName, String qName,
286N/A String type, String value)
286N/A {
286N/A ensureCapacity(length+1);
286N/A data[length*5] = uri;
286N/A data[length*5+1] = localName;
286N/A data[length*5+2] = qName;
286N/A data[length*5+3] = type;
286N/A data[length*5+4] = value;
286N/A length++;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set an attribute in the list.
286N/A *
286N/A * <p>For the sake of speed, this method does no checking
286N/A * for name conflicts or well-formedness: such checks are the
286N/A * responsibility of the application.</p>
286N/A *
286N/A * @param index The index of the attribute (zero-based).
286N/A * @param uri The Namespace URI, or the empty string if
286N/A * none is available or Namespace processing is not
286N/A * being performed.
286N/A * @param localName The local name, or the empty string if
286N/A * Namespace processing is not being performed.
286N/A * @param qName The qualified name, or the empty string
286N/A * if qualified names are not available.
286N/A * @param type The attribute type as a string.
286N/A * @param value The attribute value.
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException When the
286N/A * supplied index does not point to an attribute
286N/A * in the list.
286N/A */
286N/A public void setAttribute (int index, String uri, String localName,
286N/A String qName, String type, String value)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A data[index*5] = uri;
286N/A data[index*5+1] = localName;
286N/A data[index*5+2] = qName;
286N/A data[index*5+3] = type;
286N/A data[index*5+4] = value;
286N/A } else {
286N/A badIndex(index);
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Remove an attribute from the list.
286N/A *
286N/A * @param index The index of the attribute (zero-based).
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException When the
286N/A * supplied index does not point to an attribute
286N/A * in the list.
286N/A */
286N/A public void removeAttribute (int index)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A if (index < length - 1) {
286N/A System.arraycopy(data, (index+1)*5, data, index*5,
286N/A (length-index-1)*5);
286N/A }
286N/A index = (length - 1) * 5;
286N/A data [index++] = null;
286N/A data [index++] = null;
286N/A data [index++] = null;
286N/A data [index++] = null;
286N/A data [index] = null;
286N/A length--;
286N/A } else {
286N/A badIndex(index);
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set the Namespace URI of a specific attribute.
286N/A *
286N/A * @param index The index of the attribute (zero-based).
286N/A * @param uri The attribute's Namespace URI, or the empty
286N/A * string for none.
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException When the
286N/A * supplied index does not point to an attribute
286N/A * in the list.
286N/A */
286N/A public void setURI (int index, String uri)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A data[index*5] = uri;
286N/A } else {
286N/A badIndex(index);
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set the local name of a specific attribute.
286N/A *
286N/A * @param index The index of the attribute (zero-based).
286N/A * @param localName The attribute's local name, or the empty
286N/A * string for none.
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException When the
286N/A * supplied index does not point to an attribute
286N/A * in the list.
286N/A */
286N/A public void setLocalName (int index, String localName)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A data[index*5+1] = localName;
286N/A } else {
286N/A badIndex(index);
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set the qualified name of a specific attribute.
286N/A *
286N/A * @param index The index of the attribute (zero-based).
286N/A * @param qName The attribute's qualified name, or the empty
286N/A * string for none.
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException When the
286N/A * supplied index does not point to an attribute
286N/A * in the list.
286N/A */
286N/A public void setQName (int index, String qName)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A data[index*5+2] = qName;
286N/A } else {
286N/A badIndex(index);
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set the type of a specific attribute.
286N/A *
286N/A * @param index The index of the attribute (zero-based).
286N/A * @param type The attribute's type.
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException When the
286N/A * supplied index does not point to an attribute
286N/A * in the list.
286N/A */
286N/A public void setType (int index, String type)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A data[index*5+3] = type;
286N/A } else {
286N/A badIndex(index);
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Set the value of a specific attribute.
286N/A *
286N/A * @param index The index of the attribute (zero-based).
286N/A * @param value The attribute's value.
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException When the
286N/A * supplied index does not point to an attribute
286N/A * in the list.
286N/A */
286N/A public void setValue (int index, String value)
286N/A {
286N/A if (index >= 0 && index < length) {
286N/A data[index*5+4] = value;
286N/A } else {
286N/A badIndex(index);
286N/A }
286N/A }
286N/A
286N/A
286N/A
286N/A ////////////////////////////////////////////////////////////////////
286N/A // Internal methods.
286N/A ////////////////////////////////////////////////////////////////////
286N/A
286N/A
286N/A /**
286N/A * Ensure the internal array's capacity.
286N/A *
286N/A * @param n The minimum number of attributes that the array must
286N/A * be able to hold.
286N/A */
286N/A private void ensureCapacity (int n) {
286N/A if (n <= 0) {
286N/A return;
286N/A }
286N/A int max;
286N/A if (data == null || data.length == 0) {
286N/A max = 25;
286N/A }
286N/A else if (data.length >= n * 5) {
286N/A return;
286N/A }
286N/A else {
286N/A max = data.length;
286N/A }
286N/A while (max < n * 5) {
286N/A max *= 2;
286N/A }
286N/A
286N/A String newData[] = new String[max];
286N/A if (length > 0) {
286N/A System.arraycopy(data, 0, newData, 0, length*5);
286N/A }
286N/A data = newData;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Report a bad array index in a manipulator.
286N/A *
286N/A * @param index The index to report.
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException Always.
286N/A */
286N/A private void badIndex (int index)
286N/A throws ArrayIndexOutOfBoundsException
286N/A {
286N/A String msg =
286N/A "Attempt to modify attribute at illegal index: " + index;
286N/A throw new ArrayIndexOutOfBoundsException(msg);
286N/A }
286N/A
286N/A
286N/A
286N/A ////////////////////////////////////////////////////////////////////
286N/A // Internal state.
286N/A ////////////////////////////////////////////////////////////////////
286N/A
286N/A int length;
286N/A String data [];
286N/A
286N/A}
286N/A
286N/A// end of AttributesImpl.java