Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/org/xml/sax/helpers/AttributesImpl.java
48576 views
/*1* Copyright (c) 2000, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425// AttributesImpl.java - default implementation of Attributes.26// http://www.saxproject.org27// Written by David Megginson28// NO WARRANTY! This class is in the public domain.29// $Id: AttributesImpl.java,v 1.2 2004/11/03 22:53:08 jsuttor Exp $3031package org.xml.sax.helpers;3233import org.xml.sax.Attributes;343536/**37* Default implementation of the Attributes interface.38*39* <blockquote>40* <em>This module, both source code and documentation, is in the41* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>42* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>43* for further information.44* </blockquote>45*46* <p>This class provides a default implementation of the SAX247* {@link org.xml.sax.Attributes Attributes} interface, with the48* addition of manipulators so that the list can be modified or49* reused.</p>50*51* <p>There are two typical uses of this class:</p>52*53* <ol>54* <li>to take a persistent snapshot of an Attributes object55* in a {@link org.xml.sax.ContentHandler#startElement startElement} event; or</li>56* <li>to construct or modify an Attributes object in a SAX2 driver or filter.</li>57* </ol>58*59* <p>This class replaces the now-deprecated SAX1 {@link60* org.xml.sax.helpers.AttributeListImpl AttributeListImpl}61* class; in addition to supporting the updated Attributes62* interface rather than the deprecated {@link org.xml.sax.AttributeList63* AttributeList} interface, it also includes a much more efficient64* implementation using a single array rather than a set of Vectors.</p>65*66* @since SAX 2.067* @author David Megginson68*/69public class AttributesImpl implements Attributes70{717273////////////////////////////////////////////////////////////////////74// Constructors.75////////////////////////////////////////////////////////////////////767778/**79* Construct a new, empty AttributesImpl object.80*/81public AttributesImpl ()82{83length = 0;84data = null;85}868788/**89* Copy an existing Attributes object.90*91* <p>This constructor is especially useful inside a92* {@link org.xml.sax.ContentHandler#startElement startElement} event.</p>93*94* @param atts The existing Attributes object.95*/96public AttributesImpl (Attributes atts)97{98setAttributes(atts);99}100101102103////////////////////////////////////////////////////////////////////104// Implementation of org.xml.sax.Attributes.105////////////////////////////////////////////////////////////////////106107108/**109* Return the number of attributes in the list.110*111* @return The number of attributes in the list.112* @see org.xml.sax.Attributes#getLength113*/114public int getLength ()115{116return length;117}118119120/**121* Return an attribute's Namespace URI.122*123* @param index The attribute's index (zero-based).124* @return The Namespace URI, the empty string if none is125* available, or null if the index is out of range.126* @see org.xml.sax.Attributes#getURI127*/128public String getURI (int index)129{130if (index >= 0 && index < length) {131return data[index*5];132} else {133return null;134}135}136137138/**139* Return an attribute's local name.140*141* @param index The attribute's index (zero-based).142* @return The attribute's local name, the empty string if143* none is available, or null if the index if out of range.144* @see org.xml.sax.Attributes#getLocalName145*/146public String getLocalName (int index)147{148if (index >= 0 && index < length) {149return data[index*5+1];150} else {151return null;152}153}154155156/**157* Return an attribute's qualified (prefixed) name.158*159* @param index The attribute's index (zero-based).160* @return The attribute's qualified name, the empty string if161* none is available, or null if the index is out of bounds.162* @see org.xml.sax.Attributes#getQName163*/164public String getQName (int index)165{166if (index >= 0 && index < length) {167return data[index*5+2];168} else {169return null;170}171}172173174/**175* Return an attribute's type by index.176*177* @param index The attribute's index (zero-based).178* @return The attribute's type, "CDATA" if the type is unknown, or null179* if the index is out of bounds.180* @see org.xml.sax.Attributes#getType(int)181*/182public String getType (int index)183{184if (index >= 0 && index < length) {185return data[index*5+3];186} else {187return null;188}189}190191192/**193* Return an attribute's value by index.194*195* @param index The attribute's index (zero-based).196* @return The attribute's value or null if the index is out of bounds.197* @see org.xml.sax.Attributes#getValue(int)198*/199public String getValue (int index)200{201if (index >= 0 && index < length) {202return data[index*5+4];203} else {204return null;205}206}207208209/**210* Look up an attribute's index by Namespace name.211*212* <p>In many cases, it will be more efficient to look up the name once and213* use the index query methods rather than using the name query methods214* repeatedly.</p>215*216* @param uri The attribute's Namespace URI, or the empty217* string if none is available.218* @param localName The attribute's local name.219* @return The attribute's index, or -1 if none matches.220* @see org.xml.sax.Attributes#getIndex(java.lang.String,java.lang.String)221*/222public int getIndex (String uri, String localName)223{224int max = length * 5;225for (int i = 0; i < max; i += 5) {226if (data[i].equals(uri) && data[i+1].equals(localName)) {227return i / 5;228}229}230return -1;231}232233234/**235* Look up an attribute's index by qualified (prefixed) name.236*237* @param qName The qualified name.238* @return The attribute's index, or -1 if none matches.239* @see org.xml.sax.Attributes#getIndex(java.lang.String)240*/241public int getIndex (String qName)242{243int max = length * 5;244for (int i = 0; i < max; i += 5) {245if (data[i+2].equals(qName)) {246return i / 5;247}248}249return -1;250}251252253/**254* Look up an attribute's type by Namespace-qualified name.255*256* @param uri The Namespace URI, or the empty string for a name257* with no explicit Namespace URI.258* @param localName The local name.259* @return The attribute's type, or null if there is no260* matching attribute.261* @see org.xml.sax.Attributes#getType(java.lang.String,java.lang.String)262*/263public String getType (String uri, String localName)264{265int max = length * 5;266for (int i = 0; i < max; i += 5) {267if (data[i].equals(uri) && data[i+1].equals(localName)) {268return data[i+3];269}270}271return null;272}273274275/**276* Look up an attribute's type by qualified (prefixed) name.277*278* @param qName The qualified name.279* @return The attribute's type, or null if there is no280* matching attribute.281* @see org.xml.sax.Attributes#getType(java.lang.String)282*/283public String getType (String qName)284{285int max = length * 5;286for (int i = 0; i < max; i += 5) {287if (data[i+2].equals(qName)) {288return data[i+3];289}290}291return null;292}293294295/**296* Look up an attribute's value by Namespace-qualified name.297*298* @param uri The Namespace URI, or the empty string for a name299* with no explicit Namespace URI.300* @param localName The local name.301* @return The attribute's value, or null if there is no302* matching attribute.303* @see org.xml.sax.Attributes#getValue(java.lang.String,java.lang.String)304*/305public String getValue (String uri, String localName)306{307int max = length * 5;308for (int i = 0; i < max; i += 5) {309if (data[i].equals(uri) && data[i+1].equals(localName)) {310return data[i+4];311}312}313return null;314}315316317/**318* Look up an attribute's value by qualified (prefixed) name.319*320* @param qName The qualified name.321* @return The attribute's value, or null if there is no322* matching attribute.323* @see org.xml.sax.Attributes#getValue(java.lang.String)324*/325public String getValue (String qName)326{327int max = length * 5;328for (int i = 0; i < max; i += 5) {329if (data[i+2].equals(qName)) {330return data[i+4];331}332}333return null;334}335336337338////////////////////////////////////////////////////////////////////339// Manipulators.340////////////////////////////////////////////////////////////////////341342343/**344* Clear the attribute list for reuse.345*346* <p>Note that little memory is freed by this call:347* the current array is kept so it can be348* reused.</p>349*/350public void clear ()351{352if (data != null) {353for (int i = 0; i < (length * 5); i++)354data [i] = null;355}356length = 0;357}358359360/**361* Copy an entire Attributes object.362*363* <p>It may be more efficient to reuse an existing object364* rather than constantly allocating new ones.</p>365*366* @param atts The attributes to copy.367*/368public void setAttributes (Attributes atts)369{370clear();371length = atts.getLength();372if (length > 0) {373data = new String[length*5];374for (int i = 0; i < length; i++) {375data[i*5] = atts.getURI(i);376data[i*5+1] = atts.getLocalName(i);377data[i*5+2] = atts.getQName(i);378data[i*5+3] = atts.getType(i);379data[i*5+4] = atts.getValue(i);380}381}382}383384385/**386* Add an attribute to the end of the list.387*388* <p>For the sake of speed, this method does no checking389* to see if the attribute is already in the list: that is390* the responsibility of the application.</p>391*392* @param uri The Namespace URI, or the empty string if393* none is available or Namespace processing is not394* being performed.395* @param localName The local name, or the empty string if396* Namespace processing is not being performed.397* @param qName The qualified (prefixed) name, or the empty string398* if qualified names are not available.399* @param type The attribute type as a string.400* @param value The attribute value.401*/402public void addAttribute (String uri, String localName, String qName,403String type, String value)404{405ensureCapacity(length+1);406data[length*5] = uri;407data[length*5+1] = localName;408data[length*5+2] = qName;409data[length*5+3] = type;410data[length*5+4] = value;411length++;412}413414415/**416* Set an attribute in the list.417*418* <p>For the sake of speed, this method does no checking419* for name conflicts or well-formedness: such checks are the420* responsibility of the application.</p>421*422* @param index The index of the attribute (zero-based).423* @param uri The Namespace URI, or the empty string if424* none is available or Namespace processing is not425* being performed.426* @param localName The local name, or the empty string if427* Namespace processing is not being performed.428* @param qName The qualified name, or the empty string429* if qualified names are not available.430* @param type The attribute type as a string.431* @param value The attribute value.432* @exception java.lang.ArrayIndexOutOfBoundsException When the433* supplied index does not point to an attribute434* in the list.435*/436public void setAttribute (int index, String uri, String localName,437String qName, String type, String value)438{439if (index >= 0 && index < length) {440data[index*5] = uri;441data[index*5+1] = localName;442data[index*5+2] = qName;443data[index*5+3] = type;444data[index*5+4] = value;445} else {446badIndex(index);447}448}449450451/**452* Remove an attribute from the list.453*454* @param index The index of the attribute (zero-based).455* @exception java.lang.ArrayIndexOutOfBoundsException When the456* supplied index does not point to an attribute457* in the list.458*/459public void removeAttribute (int index)460{461if (index >= 0 && index < length) {462if (index < length - 1) {463System.arraycopy(data, (index+1)*5, data, index*5,464(length-index-1)*5);465}466index = (length - 1) * 5;467data [index++] = null;468data [index++] = null;469data [index++] = null;470data [index++] = null;471data [index] = null;472length--;473} else {474badIndex(index);475}476}477478479/**480* Set the Namespace URI of a specific attribute.481*482* @param index The index of the attribute (zero-based).483* @param uri The attribute's Namespace URI, or the empty484* string for none.485* @exception java.lang.ArrayIndexOutOfBoundsException When the486* supplied index does not point to an attribute487* in the list.488*/489public void setURI (int index, String uri)490{491if (index >= 0 && index < length) {492data[index*5] = uri;493} else {494badIndex(index);495}496}497498499/**500* Set the local name of a specific attribute.501*502* @param index The index of the attribute (zero-based).503* @param localName The attribute's local name, or the empty504* string for none.505* @exception java.lang.ArrayIndexOutOfBoundsException When the506* supplied index does not point to an attribute507* in the list.508*/509public void setLocalName (int index, String localName)510{511if (index >= 0 && index < length) {512data[index*5+1] = localName;513} else {514badIndex(index);515}516}517518519/**520* Set the qualified name of a specific attribute.521*522* @param index The index of the attribute (zero-based).523* @param qName The attribute's qualified name, or the empty524* string for none.525* @exception java.lang.ArrayIndexOutOfBoundsException When the526* supplied index does not point to an attribute527* in the list.528*/529public void setQName (int index, String qName)530{531if (index >= 0 && index < length) {532data[index*5+2] = qName;533} else {534badIndex(index);535}536}537538539/**540* Set the type of a specific attribute.541*542* @param index The index of the attribute (zero-based).543* @param type The attribute's type.544* @exception java.lang.ArrayIndexOutOfBoundsException When the545* supplied index does not point to an attribute546* in the list.547*/548public void setType (int index, String type)549{550if (index >= 0 && index < length) {551data[index*5+3] = type;552} else {553badIndex(index);554}555}556557558/**559* Set the value of a specific attribute.560*561* @param index The index of the attribute (zero-based).562* @param value The attribute's value.563* @exception java.lang.ArrayIndexOutOfBoundsException When the564* supplied index does not point to an attribute565* in the list.566*/567public void setValue (int index, String value)568{569if (index >= 0 && index < length) {570data[index*5+4] = value;571} else {572badIndex(index);573}574}575576577578////////////////////////////////////////////////////////////////////579// Internal methods.580////////////////////////////////////////////////////////////////////581582583/**584* Ensure the internal array's capacity.585*586* @param n The minimum number of attributes that the array must587* be able to hold.588*/589private void ensureCapacity (int n) {590if (n <= 0) {591return;592}593int max;594if (data == null || data.length == 0) {595max = 25;596}597else if (data.length >= n * 5) {598return;599}600else {601max = data.length;602}603while (max < n * 5) {604max *= 2;605}606607String newData[] = new String[max];608if (length > 0) {609System.arraycopy(data, 0, newData, 0, length*5);610}611data = newData;612}613614615/**616* Report a bad array index in a manipulator.617*618* @param index The index to report.619* @exception java.lang.ArrayIndexOutOfBoundsException Always.620*/621private void badIndex (int index)622throws ArrayIndexOutOfBoundsException623{624String msg =625"Attempt to modify attribute at illegal index: " + index;626throw new ArrayIndexOutOfBoundsException(msg);627}628629630631////////////////////////////////////////////////////////////////////632// Internal state.633////////////////////////////////////////////////////////////////////634635int length;636String data [];637638}639640// end of AttributesImpl.java641642643