aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: // AttributesImpl.java - default implementation of Attributes. aoqi@0: // Written by David Megginson, sax@megginson.com aoqi@0: // NO WARRANTY! This class is in the public domain. aoqi@0: aoqi@0: // $Id: AttributesImpl.java,v 1.4 2002/09/29 02:55:48 okajima Exp $ aoqi@0: aoqi@0: //fixed bug at removeAttribute!! by Daisuke OKAJIMA 2002.4.21 aoqi@0: aoqi@0: package com.sun.tools.internal.jxc.gen.config; aoqi@0: aoqi@0: import org.xml.sax.Attributes; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Default implementation of the Attributes interface. aoqi@0: * aoqi@0: *
aoqi@0: * This module, both source code and documentation, is in the aoqi@0: * Public Domain, and comes with NO WARRANTY. aoqi@0: *
aoqi@0: * aoqi@0: *

This class provides a default implementation of the SAX2 aoqi@0: * {@link org.xml.sax.Attributes Attributes} interface, with the aoqi@0: * addition of manipulators so that the list can be modified or aoqi@0: * reused.

aoqi@0: * aoqi@0: *

There are two typical uses of this class:

aoqi@0: * aoqi@0: *
    aoqi@0: *
  1. to take a persistent snapshot of an Attributes object aoqi@0: * in a {@link org.xml.sax.ContentHandler#startElement startElement} event; or
  2. aoqi@0: *
  3. to construct or modify an Attributes object in a SAX2 driver or filter.
  4. aoqi@0: *
aoqi@0: * aoqi@0: *

This class replaces the now-deprecated SAX1 {@link aoqi@0: * org.xml.sax.helpers.AttributeListImpl AttributeListImpl} aoqi@0: * class; in addition to supporting the updated Attributes aoqi@0: * interface rather than the deprecated {@link org.xml.sax.AttributeList aoqi@0: * AttributeList} interface, it also includes a much more efficient aoqi@0: * implementation using a single array rather than a set of Vectors.

aoqi@0: * aoqi@0: *

aoqi@0: * Auto-generated, do not edit. aoqi@0: *

aoqi@0: * @since SAX 2.0 aoqi@0: * @author David Megginson, aoqi@0: * sax@megginson.com aoqi@0: * @version 2.0 aoqi@0: */ aoqi@0: public class AttributesImpl implements Attributes aoqi@0: { aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Constructors. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Construct a new, empty AttributesImpl object. aoqi@0: */ aoqi@0: public AttributesImpl () aoqi@0: { aoqi@0: length = 0; aoqi@0: data = null; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Copy an existing Attributes object. aoqi@0: * aoqi@0: *

This constructor is especially useful inside a aoqi@0: * {@link org.xml.sax.ContentHandler#startElement startElement} event.

aoqi@0: * aoqi@0: * @param atts The existing Attributes object. aoqi@0: */ aoqi@0: public AttributesImpl (Attributes atts) aoqi@0: { aoqi@0: setAttributes(atts); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Implementation of org.xml.sax.Attributes. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return the number of attributes in the list. aoqi@0: * aoqi@0: * @return The number of attributes in the list. aoqi@0: * @see org.xml.sax.Attributes#getLength aoqi@0: */ aoqi@0: public int getLength () aoqi@0: { aoqi@0: return length; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return an attribute's Namespace URI. aoqi@0: * aoqi@0: * @param index The attribute's index (zero-based). aoqi@0: * @return The Namespace URI, the empty string if none is aoqi@0: * available, or null if the index is out of range. aoqi@0: * @see org.xml.sax.Attributes#getURI aoqi@0: */ aoqi@0: public String getURI (int index) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: return data[index*5]; aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return an attribute's local name. aoqi@0: * aoqi@0: * @param index The attribute's index (zero-based). aoqi@0: * @return The attribute's local name, the empty string if aoqi@0: * none is available, or null if the index if out of range. aoqi@0: * @see org.xml.sax.Attributes#getLocalName aoqi@0: */ aoqi@0: public String getLocalName (int index) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: return data[index*5+1]; aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return an attribute's qualified (prefixed) name. aoqi@0: * aoqi@0: * @param index The attribute's index (zero-based). aoqi@0: * @return The attribute's qualified name, the empty string if aoqi@0: * none is available, or null if the index is out of bounds. aoqi@0: * @see org.xml.sax.Attributes#getQName aoqi@0: */ aoqi@0: public String getQName (int index) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: return data[index*5+2]; aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return an attribute's type by index. aoqi@0: * aoqi@0: * @param index The attribute's index (zero-based). aoqi@0: * @return The attribute's type, "CDATA" if the type is unknown, or null aoqi@0: * if the index is out of bounds. aoqi@0: * @see org.xml.sax.Attributes#getType(int) aoqi@0: */ aoqi@0: public String getType (int index) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: return data[index*5+3]; aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Return an attribute's value by index. aoqi@0: * aoqi@0: * @param index The attribute's index (zero-based). aoqi@0: * @return The attribute's value or null if the index is out of bounds. aoqi@0: * @see org.xml.sax.Attributes#getValue(int) aoqi@0: */ aoqi@0: public String getValue (int index) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: return data[index*5+4]; aoqi@0: } else { aoqi@0: return null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Look up an attribute's index by Namespace name. aoqi@0: * aoqi@0: *

In many cases, it will be more efficient to look up the name once and aoqi@0: * use the index query methods rather than using the name query methods aoqi@0: * repeatedly.

aoqi@0: * aoqi@0: * @param uri The attribute's Namespace URI, or the empty aoqi@0: * string if none is available. aoqi@0: * @param localName The attribute's local name. aoqi@0: * @return The attribute's index, or -1 if none matches. aoqi@0: * @see org.xml.sax.Attributes#getIndex(java.lang.String,java.lang.String) aoqi@0: */ aoqi@0: public int getIndex (String uri, String localName) aoqi@0: { aoqi@0: int max = length * 5; aoqi@0: for (int i = 0; i < max; i += 5) { aoqi@0: if (data[i].equals(uri) && data[i+1].equals(localName)) { aoqi@0: return i / 5; aoqi@0: } aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Look up an attribute's index by qualified (prefixed) name. aoqi@0: * aoqi@0: * @param qName The qualified name. aoqi@0: * @return The attribute's index, or -1 if none matches. aoqi@0: * @see org.xml.sax.Attributes#getIndex(java.lang.String) aoqi@0: */ aoqi@0: public int getIndex (String qName) aoqi@0: { aoqi@0: int max = length * 5; aoqi@0: for (int i = 0; i < max; i += 5) { aoqi@0: if (data[i+2].equals(qName)) { aoqi@0: return i / 5; aoqi@0: } aoqi@0: } aoqi@0: return -1; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Look up an attribute's type by Namespace-qualified name. aoqi@0: * aoqi@0: * @param uri The Namespace URI, or the empty string for a name aoqi@0: * with no explicit Namespace URI. aoqi@0: * @param localName The local name. aoqi@0: * @return The attribute's type, or null if there is no aoqi@0: * matching attribute. aoqi@0: * @see org.xml.sax.Attributes#getType(java.lang.String,java.lang.String) aoqi@0: */ aoqi@0: public String getType (String uri, String localName) aoqi@0: { aoqi@0: int max = length * 5; aoqi@0: for (int i = 0; i < max; i += 5) { aoqi@0: if (data[i].equals(uri) && data[i+1].equals(localName)) { aoqi@0: return data[i+3]; aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Look up an attribute's type by qualified (prefixed) name. aoqi@0: * aoqi@0: * @param qName The qualified name. aoqi@0: * @return The attribute's type, or null if there is no aoqi@0: * matching attribute. aoqi@0: * @see org.xml.sax.Attributes#getType(java.lang.String) aoqi@0: */ aoqi@0: public String getType (String qName) aoqi@0: { aoqi@0: int max = length * 5; aoqi@0: for (int i = 0; i < max; i += 5) { aoqi@0: if (data[i+2].equals(qName)) { aoqi@0: return data[i+3]; aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Look up an attribute's value by Namespace-qualified name. aoqi@0: * aoqi@0: * @param uri The Namespace URI, or the empty string for a name aoqi@0: * with no explicit Namespace URI. aoqi@0: * @param localName The local name. aoqi@0: * @return The attribute's value, or null if there is no aoqi@0: * matching attribute. aoqi@0: * @see org.xml.sax.Attributes#getValue(java.lang.String,java.lang.String) aoqi@0: */ aoqi@0: public String getValue (String uri, String localName) aoqi@0: { aoqi@0: int max = length * 5; aoqi@0: for (int i = 0; i < max; i += 5) { aoqi@0: if (data[i].equals(uri) && data[i+1].equals(localName)) { aoqi@0: return data[i+4]; aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Look up an attribute's value by qualified (prefixed) name. aoqi@0: * aoqi@0: * @param qName The qualified name. aoqi@0: * @return The attribute's value, or null if there is no aoqi@0: * matching attribute. aoqi@0: * @see org.xml.sax.Attributes#getValue(java.lang.String) aoqi@0: */ aoqi@0: public String getValue (String qName) aoqi@0: { aoqi@0: int max = length * 5; aoqi@0: for (int i = 0; i < max; i += 5) { aoqi@0: if (data[i+2].equals(qName)) { aoqi@0: return data[i+4]; aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Manipulators. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Clear the attribute list for reuse. aoqi@0: * aoqi@0: *

Note that no memory is actually freed by this call: aoqi@0: * the current arrays are kept so that they can be aoqi@0: * reused.

aoqi@0: */ aoqi@0: public void clear () aoqi@0: { aoqi@0: length = 0; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Copy an entire Attributes object. aoqi@0: * aoqi@0: *

It may be more efficient to reuse an existing object aoqi@0: * rather than constantly allocating new ones.

aoqi@0: * aoqi@0: * @param atts The attributes to copy. aoqi@0: */ aoqi@0: public void setAttributes (Attributes atts) aoqi@0: { aoqi@0: clear(); aoqi@0: length = atts.getLength(); aoqi@0: data = new String[length*5]; aoqi@0: for (int i = 0; i < length; i++) { aoqi@0: data[i*5] = atts.getURI(i); aoqi@0: data[i*5+1] = atts.getLocalName(i); aoqi@0: data[i*5+2] = atts.getQName(i); aoqi@0: data[i*5+3] = atts.getType(i); aoqi@0: data[i*5+4] = atts.getValue(i); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Add an attribute to the end of the list. aoqi@0: * aoqi@0: *

For the sake of speed, this method does no checking aoqi@0: * to see if the attribute is already in the list: that is aoqi@0: * the responsibility of the application.

aoqi@0: * aoqi@0: * @param uri The Namespace URI, or the empty string if aoqi@0: * none is available or Namespace processing is not aoqi@0: * being performed. aoqi@0: * @param localName The local name, or the empty string if aoqi@0: * Namespace processing is not being performed. aoqi@0: * @param qName The qualified (prefixed) name, or the empty string aoqi@0: * if qualified names are not available. aoqi@0: * @param type The attribute type as a string. aoqi@0: * @param value The attribute value. aoqi@0: */ aoqi@0: public void addAttribute (String uri, String localName, String qName, aoqi@0: String type, String value) aoqi@0: { aoqi@0: ensureCapacity(length+1); aoqi@0: data[length*5] = uri; aoqi@0: data[length*5+1] = localName; aoqi@0: data[length*5+2] = qName; aoqi@0: data[length*5+3] = type; aoqi@0: data[length*5+4] = value; aoqi@0: length++; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Set an attribute in the list. aoqi@0: * aoqi@0: *

For the sake of speed, this method does no checking aoqi@0: * for name conflicts or well-formedness: such checks are the aoqi@0: * responsibility of the application.

aoqi@0: * aoqi@0: * @param index The index of the attribute (zero-based). aoqi@0: * @param uri The Namespace URI, or the empty string if aoqi@0: * none is available or Namespace processing is not aoqi@0: * being performed. aoqi@0: * @param localName The local name, or the empty string if aoqi@0: * Namespace processing is not being performed. aoqi@0: * @param qName The qualified name, or the empty string aoqi@0: * if qualified names are not available. aoqi@0: * @param type The attribute type as a string. aoqi@0: * @param value The attribute value. aoqi@0: * @exception java.lang.ArrayIndexOutOfBoundsException When the aoqi@0: * supplied index does not point to an attribute aoqi@0: * in the list. aoqi@0: */ aoqi@0: public void setAttribute (int index, String uri, String localName, aoqi@0: String qName, String type, String value) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: data[index*5] = uri; aoqi@0: data[index*5+1] = localName; aoqi@0: data[index*5+2] = qName; aoqi@0: data[index*5+3] = type; aoqi@0: data[index*5+4] = value; aoqi@0: } else { aoqi@0: badIndex(index); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Remove an attribute from the list. aoqi@0: * aoqi@0: * @param index The index of the attribute (zero-based). aoqi@0: * @exception java.lang.ArrayIndexOutOfBoundsException When the aoqi@0: * supplied index does not point to an attribute aoqi@0: * in the list. aoqi@0: */ aoqi@0: public void removeAttribute (int index) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: if (index < length - 1) { aoqi@0: System.arraycopy(data, (index+1)*5, data, index*5, aoqi@0: (length-index-1)*5); aoqi@0: } aoqi@0: length--; aoqi@0: } else { aoqi@0: badIndex(index); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Set the Namespace URI of a specific attribute. aoqi@0: * aoqi@0: * @param index The index of the attribute (zero-based). aoqi@0: * @param uri The attribute's Namespace URI, or the empty aoqi@0: * string for none. aoqi@0: * @exception java.lang.ArrayIndexOutOfBoundsException When the aoqi@0: * supplied index does not point to an attribute aoqi@0: * in the list. aoqi@0: */ aoqi@0: public void setURI (int index, String uri) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: data[index*5] = uri; aoqi@0: } else { aoqi@0: badIndex(index); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Set the local name of a specific attribute. aoqi@0: * aoqi@0: * @param index The index of the attribute (zero-based). aoqi@0: * @param localName The attribute's local name, or the empty aoqi@0: * string for none. aoqi@0: * @exception java.lang.ArrayIndexOutOfBoundsException When the aoqi@0: * supplied index does not point to an attribute aoqi@0: * in the list. aoqi@0: */ aoqi@0: public void setLocalName (int index, String localName) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: data[index*5+1] = localName; aoqi@0: } else { aoqi@0: badIndex(index); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Set the qualified name of a specific attribute. aoqi@0: * aoqi@0: * @param index The index of the attribute (zero-based). aoqi@0: * @param qName The attribute's qualified name, or the empty aoqi@0: * string for none. aoqi@0: * @exception java.lang.ArrayIndexOutOfBoundsException When the aoqi@0: * supplied index does not point to an attribute aoqi@0: * in the list. aoqi@0: */ aoqi@0: public void setQName (int index, String qName) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: data[index*5+2] = qName; aoqi@0: } else { aoqi@0: badIndex(index); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Set the type of a specific attribute. aoqi@0: * aoqi@0: * @param index The index of the attribute (zero-based). aoqi@0: * @param type The attribute's type. aoqi@0: * @exception java.lang.ArrayIndexOutOfBoundsException When the aoqi@0: * supplied index does not point to an attribute aoqi@0: * in the list. aoqi@0: */ aoqi@0: public void setType (int index, String type) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: data[index*5+3] = type; aoqi@0: } else { aoqi@0: badIndex(index); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Set the value of a specific attribute. aoqi@0: * aoqi@0: * @param index The index of the attribute (zero-based). aoqi@0: * @param value The attribute's value. aoqi@0: * @exception java.lang.ArrayIndexOutOfBoundsException When the aoqi@0: * supplied index does not point to an attribute aoqi@0: * in the list. aoqi@0: */ aoqi@0: public void setValue (int index, String value) aoqi@0: { aoqi@0: if (index >= 0 && index < length) { aoqi@0: data[index*5+4] = value; aoqi@0: } else { aoqi@0: badIndex(index); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Internal methods. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Ensure the internal array's capacity. aoqi@0: * aoqi@0: * @param n The minimum number of attributes that the array must aoqi@0: * be able to hold. aoqi@0: */ aoqi@0: private void ensureCapacity (int n) aoqi@0: { aoqi@0: if (n > 0 && (data == null || data.length==0)) { aoqi@0: data = new String[25]; aoqi@0: } aoqi@0: aoqi@0: int max = data.length; aoqi@0: if (max >= n * 5) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: while (max < n * 5) { aoqi@0: max *= 2; aoqi@0: } aoqi@0: String newData[] = new String[max]; aoqi@0: System.arraycopy(data, 0, newData, 0, length*5); aoqi@0: data = newData; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Report a bad array index in a manipulator. aoqi@0: * aoqi@0: * @param index The index to report. aoqi@0: * @exception java.lang.ArrayIndexOutOfBoundsException Always. aoqi@0: */ aoqi@0: private void badIndex (int index) aoqi@0: throws ArrayIndexOutOfBoundsException aoqi@0: { aoqi@0: String msg = aoqi@0: "Attempt to modify attribute at illegal index: " + index; aoqi@0: throw new ArrayIndexOutOfBoundsException(msg); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: // Internal state. aoqi@0: //////////////////////////////////////////////////////////////////// aoqi@0: aoqi@0: int length; aoqi@0: String data []; aoqi@0: aoqi@0: } aoqi@0: aoqi@0: // end of AttributesImpl.java