Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/org/xml/sax/ext/Attributes2Impl.java
48576 views
/*1* Copyright (c) 2004, 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// Attributes2Impl.java - extended AttributesImpl26// http://www.saxproject.org27// Public Domain: no warranty.28// $Id: Attributes2Impl.java,v 1.3 2005/02/24 11:20:18 gg156739 Exp $2930package org.xml.sax.ext;3132import org.xml.sax.Attributes;33import org.xml.sax.helpers.AttributesImpl;343536/**37* SAX2 extension helper for additional Attributes information,38* implementing the {@link Attributes2} interface.39*40* <blockquote>41* <em>This module, both source code and documentation, is in the42* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>43* </blockquote>44*45* <p>This is not part of core-only SAX2 distributions.</p>46*47* <p>The <em>specified</em> flag for each attribute will always48* be true, unless it has been set to false in the copy constructor49* or using {@link #setSpecified}.50* Similarly, the <em>declared</em> flag for each attribute will51* always be false, except for defaulted attributes (<em>specified</em>52* is false), non-CDATA attributes, or when it is set to true using53* {@link #setDeclared}.54* If you change an attribute's type by hand, you may need to modify55* its <em>declared</em> flag to match.56* </p>57*58* @since SAX 2.0 (extensions 1.1 alpha)59* @author David Brownell60*/61public class Attributes2Impl extends AttributesImpl implements Attributes262{63private boolean declared [];64private boolean specified [];656667/**68* Construct a new, empty Attributes2Impl object.69*/70public Attributes2Impl () {71specified = null;72declared = null;73}747576/**77* Copy an existing Attributes or Attributes2 object.78* If the object implements Attributes2, values of the79* <em>specified</em> and <em>declared</em> flags for each80* attribute are copied.81* Otherwise the flag values are defaulted to assume no DTD was used,82* unless there is evidence to the contrary (such as attributes with83* type other than CDATA, which must have been <em>declared</em>).84*85* <p>This constructor is especially useful inside a86* {@link org.xml.sax.ContentHandler#startElement startElement} event.</p>87*88* @param atts The existing Attributes object.89*/90public Attributes2Impl (Attributes atts)91{92super (atts);93}949596////////////////////////////////////////////////////////////////////97// Implementation of Attributes298////////////////////////////////////////////////////////////////////99100101/**102* Returns the current value of the attribute's "declared" flag.103*/104// javadoc mostly from interface105public boolean isDeclared (int index)106{107if (index < 0 || index >= getLength ())108throw new ArrayIndexOutOfBoundsException (109"No attribute at index: " + index);110return declared [index];111}112113114/**115* Returns the current value of the attribute's "declared" flag.116*/117// javadoc mostly from interface118public boolean isDeclared (String uri, String localName)119{120int index = getIndex (uri, localName);121122if (index < 0)123throw new IllegalArgumentException (124"No such attribute: local=" + localName125+ ", namespace=" + uri);126return declared [index];127}128129130/**131* Returns the current value of the attribute's "declared" flag.132*/133// javadoc mostly from interface134public boolean isDeclared (String qName)135{136int index = getIndex (qName);137138if (index < 0)139throw new IllegalArgumentException (140"No such attribute: " + qName);141return declared [index];142}143144145/**146* Returns the current value of an attribute's "specified" flag.147*148* @param index The attribute index (zero-based).149* @return current flag value150* @exception java.lang.ArrayIndexOutOfBoundsException When the151* supplied index does not identify an attribute.152*/153public boolean isSpecified (int index)154{155if (index < 0 || index >= getLength ())156throw new ArrayIndexOutOfBoundsException (157"No attribute at index: " + index);158return specified [index];159}160161162/**163* Returns the current value of an attribute's "specified" flag.164*165* @param uri The Namespace URI, or the empty string if166* the name has no Namespace URI.167* @param localName The attribute's local name.168* @return current flag value169* @exception java.lang.IllegalArgumentException When the170* supplied names do not identify an attribute.171*/172public boolean isSpecified (String uri, String localName)173{174int index = getIndex (uri, localName);175176if (index < 0)177throw new IllegalArgumentException (178"No such attribute: local=" + localName179+ ", namespace=" + uri);180return specified [index];181}182183184/**185* Returns the current value of an attribute's "specified" flag.186*187* @param qName The XML qualified (prefixed) name.188* @return current flag value189* @exception java.lang.IllegalArgumentException When the190* supplied name does not identify an attribute.191*/192public boolean isSpecified (String qName)193{194int index = getIndex (qName);195196if (index < 0)197throw new IllegalArgumentException (198"No such attribute: " + qName);199return specified [index];200}201202203////////////////////////////////////////////////////////////////////204// Manipulators205////////////////////////////////////////////////////////////////////206207208/**209* Copy an entire Attributes object. The "specified" flags are210* assigned as true, and "declared" flags as false (except when211* an attribute's type is not CDATA),212* unless the object is an Attributes2 object.213* In that case those flag values are all copied.214*215* @see AttributesImpl#setAttributes216*/217public void setAttributes (Attributes atts)218{219int length = atts.getLength ();220221super.setAttributes (atts);222declared = new boolean [length];223specified = new boolean [length];224225if (atts instanceof Attributes2) {226Attributes2 a2 = (Attributes2) atts;227for (int i = 0; i < length; i++) {228declared [i] = a2.isDeclared (i);229specified [i] = a2.isSpecified (i);230}231} else {232for (int i = 0; i < length; i++) {233declared [i] = !"CDATA".equals (atts.getType (i));234specified [i] = true;235}236}237}238239240/**241* Add an attribute to the end of the list, setting its242* "specified" flag to true. To set that flag's value243* to false, use {@link #setSpecified}.244*245* <p>Unless the attribute <em>type</em> is CDATA, this attribute246* is marked as being declared in the DTD. To set that flag's value247* to true for CDATA attributes, use {@link #setDeclared}.248*249* @see AttributesImpl#addAttribute250*/251public void addAttribute (String uri, String localName, String qName,252String type, String value)253{254super.addAttribute (uri, localName, qName, type, value);255256257int length = getLength ();258if(specified==null)259{260specified = new boolean[length];261declared = new boolean[length];262} else if (length > specified.length) {263boolean newFlags [];264265newFlags = new boolean [length];266System.arraycopy (declared, 0, newFlags, 0, declared.length);267declared = newFlags;268269newFlags = new boolean [length];270System.arraycopy (specified, 0, newFlags, 0, specified.length);271specified = newFlags;272}273274specified [length - 1] = true;275declared [length - 1] = !"CDATA".equals (type);276}277278279// javadoc entirely from superclass280public void removeAttribute (int index)281{282int origMax = getLength () - 1;283284super.removeAttribute (index);285if (index != origMax) {286System.arraycopy (declared, index + 1, declared, index,287origMax - index);288System.arraycopy (specified, index + 1, specified, index,289origMax - index);290}291}292293294/**295* Assign a value to the "declared" flag of a specific attribute.296* This is normally needed only for attributes of type CDATA,297* including attributes whose type is changed to or from CDATA.298*299* @param index The index of the attribute (zero-based).300* @param value The desired flag value.301* @exception java.lang.ArrayIndexOutOfBoundsException When the302* supplied index does not identify an attribute.303* @see #setType304*/305public void setDeclared (int index, boolean value)306{307if (index < 0 || index >= getLength ())308throw new ArrayIndexOutOfBoundsException (309"No attribute at index: " + index);310declared [index] = value;311}312313314/**315* Assign a value to the "specified" flag of a specific attribute.316* This is the only way this flag can be cleared, except clearing317* by initialization with the copy constructor.318*319* @param index The index of the attribute (zero-based).320* @param value The desired flag value.321* @exception java.lang.ArrayIndexOutOfBoundsException When the322* supplied index does not identify an attribute.323*/324public void setSpecified (int index, boolean value)325{326if (index < 0 || index >= getLength ())327throw new ArrayIndexOutOfBoundsException (328"No attribute at index: " + index);329specified [index] = value;330}331}332333334