286N/A/*
286N/A * Copyright (c) 2004, 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// Attributes2Impl.java - extended AttributesImpl
286N/A// http://www.saxproject.org
286N/A// Public Domain: no warranty.
286N/A// $Id: Attributes2Impl.java,v 1.3 2005/02/24 11:20:18 gg156739 Exp $
286N/A
286N/Apackage org.xml.sax.ext;
286N/A
286N/Aimport org.xml.sax.Attributes;
286N/Aimport org.xml.sax.helpers.AttributesImpl;
286N/A
286N/A
286N/A/**
286N/A * SAX2 extension helper for additional Attributes information,
286N/A * implementing the {@link Attributes2} 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 * </blockquote>
286N/A *
286N/A * <p>This is not part of core-only SAX2 distributions.</p>
286N/A *
286N/A * <p>The <em>specified</em> flag for each attribute will always
286N/A * be true, unless it has been set to false in the copy constructor
286N/A * or using {@link #setSpecified}.
286N/A * Similarly, the <em>declared</em> flag for each attribute will
286N/A * always be false, except for defaulted attributes (<em>specified</em>
286N/A * is false), non-CDATA attributes, or when it is set to true using
286N/A * {@link #setDeclared}.
286N/A * If you change an attribute's type by hand, you may need to modify
286N/A * its <em>declared</em> flag to match.
286N/A * </p>
286N/A *
286N/A * @since SAX 2.0 (extensions 1.1 alpha)
286N/A * @author David Brownell
286N/A */
286N/Apublic class Attributes2Impl extends AttributesImpl implements Attributes2
286N/A{
286N/A private boolean declared [];
286N/A private boolean specified [];
286N/A
286N/A
286N/A /**
286N/A * Construct a new, empty Attributes2Impl object.
286N/A */
286N/A public Attributes2Impl () {
286N/A specified = null;
286N/A declared = null;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Copy an existing Attributes or Attributes2 object.
286N/A * If the object implements Attributes2, values of the
286N/A * <em>specified</em> and <em>declared</em> flags for each
286N/A * attribute are copied.
286N/A * Otherwise the flag values are defaulted to assume no DTD was used,
286N/A * unless there is evidence to the contrary (such as attributes with
286N/A * type other than CDATA, which must have been <em>declared</em>).
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 Attributes2Impl (Attributes atts)
286N/A {
286N/A super (atts);
286N/A }
286N/A
286N/A
286N/A ////////////////////////////////////////////////////////////////////
286N/A // Implementation of Attributes2
286N/A ////////////////////////////////////////////////////////////////////
286N/A
286N/A
286N/A /**
286N/A * Returns the current value of the attribute's "declared" flag.
286N/A */
286N/A // javadoc mostly from interface
286N/A public boolean isDeclared (int index)
286N/A {
286N/A if (index < 0 || index >= getLength ())
286N/A throw new ArrayIndexOutOfBoundsException (
286N/A "No attribute at index: " + index);
286N/A return declared [index];
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Returns the current value of the attribute's "declared" flag.
286N/A */
286N/A // javadoc mostly from interface
286N/A public boolean isDeclared (String uri, String localName)
286N/A {
286N/A int index = getIndex (uri, localName);
286N/A
286N/A if (index < 0)
286N/A throw new IllegalArgumentException (
286N/A "No such attribute: local=" + localName
286N/A + ", namespace=" + uri);
286N/A return declared [index];
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Returns the current value of the attribute's "declared" flag.
286N/A */
286N/A // javadoc mostly from interface
286N/A public boolean isDeclared (String qName)
286N/A {
286N/A int index = getIndex (qName);
286N/A
286N/A if (index < 0)
286N/A throw new IllegalArgumentException (
286N/A "No such attribute: " + qName);
286N/A return declared [index];
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Returns the current value of an attribute's "specified" flag.
286N/A *
286N/A * @param index The attribute index (zero-based).
286N/A * @return current flag value
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException When the
286N/A * supplied index does not identify an attribute.
286N/A */
286N/A public boolean isSpecified (int index)
286N/A {
286N/A if (index < 0 || index >= getLength ())
286N/A throw new ArrayIndexOutOfBoundsException (
286N/A "No attribute at index: " + index);
286N/A return specified [index];
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Returns the current value of an attribute's "specified" flag.
286N/A *
286N/A * @param uri The Namespace URI, or the empty string if
286N/A * the name has no Namespace URI.
286N/A * @param localName The attribute's local name.
286N/A * @return current flag value
286N/A * @exception java.lang.IllegalArgumentException When the
286N/A * supplied names do not identify an attribute.
286N/A */
286N/A public boolean isSpecified (String uri, String localName)
286N/A {
286N/A int index = getIndex (uri, localName);
286N/A
286N/A if (index < 0)
286N/A throw new IllegalArgumentException (
286N/A "No such attribute: local=" + localName
286N/A + ", namespace=" + uri);
286N/A return specified [index];
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Returns the current value of an attribute's "specified" flag.
286N/A *
286N/A * @param qName The XML qualified (prefixed) name.
286N/A * @return current flag value
286N/A * @exception java.lang.IllegalArgumentException When the
286N/A * supplied name does not identify an attribute.
286N/A */
286N/A public boolean isSpecified (String qName)
286N/A {
286N/A int index = getIndex (qName);
286N/A
286N/A if (index < 0)
286N/A throw new IllegalArgumentException (
286N/A "No such attribute: " + qName);
286N/A return specified [index];
286N/A }
286N/A
286N/A
286N/A ////////////////////////////////////////////////////////////////////
286N/A // Manipulators
286N/A ////////////////////////////////////////////////////////////////////
286N/A
286N/A
286N/A /**
286N/A * Copy an entire Attributes object. The "specified" flags are
286N/A * assigned as true, and "declared" flags as false (except when
286N/A * an attribute's type is not CDATA),
286N/A * unless the object is an Attributes2 object.
286N/A * In that case those flag values are all copied.
286N/A *
286N/A * @see AttributesImpl#setAttributes
286N/A */
286N/A public void setAttributes (Attributes atts)
286N/A {
286N/A int length = atts.getLength ();
286N/A
286N/A super.setAttributes (atts);
286N/A declared = new boolean [length];
286N/A specified = new boolean [length];
286N/A
286N/A if (atts instanceof Attributes2) {
286N/A Attributes2 a2 = (Attributes2) atts;
286N/A for (int i = 0; i < length; i++) {
286N/A declared [i] = a2.isDeclared (i);
286N/A specified [i] = a2.isSpecified (i);
286N/A }
286N/A } else {
286N/A for (int i = 0; i < length; i++) {
286N/A declared [i] = !"CDATA".equals (atts.getType (i));
286N/A specified [i] = true;
286N/A }
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Add an attribute to the end of the list, setting its
286N/A * "specified" flag to true. To set that flag's value
286N/A * to false, use {@link #setSpecified}.
286N/A *
286N/A * <p>Unless the attribute <em>type</em> is CDATA, this attribute
286N/A * is marked as being declared in the DTD. To set that flag's value
286N/A * to true for CDATA attributes, use {@link #setDeclared}.
286N/A *
286N/A * @see AttributesImpl#addAttribute
286N/A */
286N/A public void addAttribute (String uri, String localName, String qName,
286N/A String type, String value)
286N/A {
286N/A super.addAttribute (uri, localName, qName, type, value);
286N/A
286N/A
286N/A int length = getLength ();
286N/A if(specified==null)
286N/A {
286N/A specified = new boolean[length];
286N/A declared = new boolean[length];
286N/A } else if (length > specified.length) {
286N/A boolean newFlags [];
286N/A
286N/A newFlags = new boolean [length];
286N/A System.arraycopy (declared, 0, newFlags, 0, declared.length);
286N/A declared = newFlags;
286N/A
286N/A newFlags = new boolean [length];
286N/A System.arraycopy (specified, 0, newFlags, 0, specified.length);
286N/A specified = newFlags;
286N/A }
286N/A
286N/A specified [length - 1] = true;
286N/A declared [length - 1] = !"CDATA".equals (type);
286N/A }
286N/A
286N/A
286N/A // javadoc entirely from superclass
286N/A public void removeAttribute (int index)
286N/A {
286N/A int origMax = getLength () - 1;
286N/A
286N/A super.removeAttribute (index);
286N/A if (index != origMax) {
286N/A System.arraycopy (declared, index + 1, declared, index,
286N/A origMax - index);
286N/A System.arraycopy (specified, index + 1, specified, index,
286N/A origMax - index);
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Assign a value to the "declared" flag of a specific attribute.
286N/A * This is normally needed only for attributes of type CDATA,
286N/A * including attributes whose type is changed to or from CDATA.
286N/A *
286N/A * @param index The index of the attribute (zero-based).
286N/A * @param value The desired flag value.
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException When the
286N/A * supplied index does not identify an attribute.
286N/A * @see #setType
286N/A */
286N/A public void setDeclared (int index, boolean value)
286N/A {
286N/A if (index < 0 || index >= getLength ())
286N/A throw new ArrayIndexOutOfBoundsException (
286N/A "No attribute at index: " + index);
286N/A declared [index] = value;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Assign a value to the "specified" flag of a specific attribute.
286N/A * This is the only way this flag can be cleared, except clearing
286N/A * by initialization with the copy constructor.
286N/A *
286N/A * @param index The index of the attribute (zero-based).
286N/A * @param value The desired flag value.
286N/A * @exception java.lang.ArrayIndexOutOfBoundsException When the
286N/A * supplied index does not identify an attribute.
286N/A */
286N/A public void setSpecified (int index, boolean value)
286N/A {
286N/A if (index < 0 || index >= getLength ())
286N/A throw new ArrayIndexOutOfBoundsException (
286N/A "No attribute at index: " + index);
286N/A specified [index] = value;
286N/A }
286N/A}