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