xmlattributesimpl.java

来自「JAVA 所有包」· Java 代码 · 共 1,168 行 · 第 1/3 页

JAVA
1,168
字号
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights  * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment:   *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written  *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    nor may "Apache" appear in their name, without prior written *    permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, International * Business Machines, Inc., http://www.apache.org.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package com.sun.org.apache.xerces.internal.util;import com.sun.xml.internal.stream.XMLBufferListener;import com.sun.org.apache.xerces.internal.xni.Augmentations;import com.sun.org.apache.xerces.internal.xni.QName;import com.sun.org.apache.xerces.internal.xni.XMLAttributes;import com.sun.org.apache.xerces.internal.xni.XMLString;/** * The XMLAttributesImpl class is an implementation of the XMLAttributes * interface which defines a collection of attributes for an element.  * In the parser, the document source would scan the entire start element  * and collect the attributes. The attributes are communicated to the * document handler in the startElement method. * <p> * The attributes are read-write so that subsequent stages in the document * pipeline can modify the values or change the attributes that are * propogated to the next stage. * * @see com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler#startElement * * @author Andy Clark, IBM  * @author Elena Litani, IBM * @author Michael Glavassevich, IBM * * @version $Id: XMLAttributesImpl.java,v 1.3 2005/09/26 13:03:01 sunithareddy Exp $ */public class XMLAttributesImplimplements XMLAttributes, XMLBufferListener {    //    // Constants    //        /** Default table size. */    protected static final int TABLE_SIZE = 101;        /**      * Threshold at which an instance is treated     * as a large attribute list.     */    protected static final int SIZE_LIMIT = 20;        //    // Data    //    // features    /** Namespaces. */    protected boolean fNamespaces = true;    // data    /**      * Usage count for the attribute table view.      * Incremented each time all attributes are removed     * when the attribute table view is in use.     */    protected int fLargeCount = 1;        /** Attribute count. */    protected int fLength;    /** Attribute information. */    protected Attribute[] fAttributes = new Attribute[4];    /**      * Hashtable of attribute information.      * Provides an alternate view of the attribute specification.      */    protected Attribute[] fAttributeTableView;        /**     * Tracks whether each chain in the hash table is stale     * with respect to the current state of this object.     * A chain is stale if its state is not the same as the number     * of times the attribute table view has been used.     */    protected int[] fAttributeTableViewChainState;        /**     * Actual number of buckets in the table view.     */    protected int fTableViewBuckets;        /**     * Indicates whether the table view contains consistent data.     */    protected boolean fIsTableViewConsistent;    //    // Constructors    //    /** Default constructor. */    public XMLAttributesImpl() {        this(TABLE_SIZE);    }        /**     * @param tableSize initial size of table view     */    public XMLAttributesImpl(int tableSize) {        fTableViewBuckets = tableSize;        for (int i = 0; i < fAttributes.length; i++) {            fAttributes[i] = new Attribute();        }    } // <init>()    //    // Public methods    //    /**      * Sets whether namespace processing is being performed. This state     * is needed to return the correct value from the getLocalName method.     *     * @param namespaces True if namespace processing is turned on.     *     * @see #getLocalName     */    public void setNamespaces(boolean namespaces) {        fNamespaces = namespaces;    } // setNamespaces(boolean)    //    // XMLAttributes methods    //    /**     * Adds an attribute. The attribute's non-normalized value of the     * attribute will have the same value as the attribute value until     * set using the <code>setNonNormalizedValue</code> method. Also,     * the added attribute will be marked as specified in the XML instance     * document unless set otherwise using the <code>setSpecified</code>     * method.     * <p>     * <strong>Note:</strong> If an attribute of the same name already     * exists, the old values for the attribute are replaced by the new     * values.     *      * @param name  The attribute name.     * @param type  The attribute type. The type name is determined by     *                  the type specified for this attribute in the DTD.     *                  For example: "CDATA", "ID", "NMTOKEN", etc. However,     *                  attributes of type enumeration will have the type     *                  value specified as the pipe ('|') separated list of     *                  the enumeration values prefixed by an open      *                  parenthesis and suffixed by a close parenthesis.     *                  For example: "(true|false)".     * @param value The attribute value.     *      * @return Returns the attribute index.     *     * @see #setNonNormalizedValue     * @see #setSpecified     */    public int addAttribute(QName name, String type, String value) {      return addAttribute(name,type,value,null);    }    public int addAttribute(QName name, String type, String value,XMLString valueCache) {        int index;        if (fLength < SIZE_LIMIT) {            index = name.uri != null && !name.uri.equals("")                 ? getIndexFast(name.uri, name.localpart)                : getIndexFast(name.rawname);                if (index == -1) {                index = fLength;                if (fLength++ == fAttributes.length) {                    Attribute[] attributes = new Attribute[fAttributes.length + 4];                    System.arraycopy(fAttributes, 0, attributes, 0, fAttributes.length);                    for (int i = fAttributes.length; i < attributes.length; i++) {                        attributes[i] = new Attribute();                    }                    fAttributes = attributes;                }            }        }        else if (name.uri == null ||             name.uri.length() == 0 ||             (index = getIndexFast(name.uri, name.localpart)) == -1) {                        /**             * If attributes were removed from the list after the table             * becomes in use this isn't reflected in the table view. It's             * assumed that once a user starts removing attributes they're              * not likely to add more. We only make the view consistent if             * the user of this class adds attributes, removes them, and             * then adds more.             */            if (!fIsTableViewConsistent || fLength == SIZE_LIMIT) {                prepareAndPopulateTableView();                fIsTableViewConsistent = true;            }            int bucket = getTableViewBucket(name.rawname);                     // The chain is stale.             // This must be a unique attribute.            if (fAttributeTableViewChainState[bucket] != fLargeCount) {                index = fLength;                if (fLength++ == fAttributes.length) {                    Attribute[] attributes = new Attribute[fAttributes.length << 1];                    System.arraycopy(fAttributes, 0, attributes, 0, fAttributes.length);                    for (int i = fAttributes.length; i < attributes.length; i++) {                        attributes[i] = new Attribute();                    }                    fAttributes = attributes;                }                            // Update table view.                fAttributeTableViewChainState[bucket] = fLargeCount;                fAttributes[index].next = null;                fAttributeTableView[bucket] = fAttributes[index];            }            // This chain is active.             // We need to check if any of the attributes has the same rawname.            else {                // Search the table.                Attribute found = fAttributeTableView[bucket];                while (found != null) {                    if (found.name.rawname == name.rawname) {                        break;                    }                    found = found.next;                }                // This attribute is unique.                if (found == null) {                    index = fLength;                    if (fLength++ == fAttributes.length) {                        Attribute[] attributes = new Attribute[fAttributes.length << 1];                        System.arraycopy(fAttributes, 0, attributes, 0, fAttributes.length);                        for (int i = fAttributes.length; i < attributes.length; i++) {                            attributes[i] = new Attribute();                        }                        fAttributes = attributes;                    }                                     // Update table view                    fAttributes[index].next = fAttributeTableView[bucket];                    fAttributeTableView[bucket] = fAttributes[index];                }                // Duplicate. We still need to find the index.                else {                    index = getIndexFast(name.rawname);                }            }        }                  // set values        Attribute attribute = fAttributes[index];        attribute.name.setValues(name);        attribute.type = type;        attribute.value = value;        attribute.xmlValue = valueCache;        attribute.nonNormalizedValue = value;        attribute.specified = false;        // clear augmentations        if(attribute.augs != null)            attribute.augs.removeAllItems();                return index;    } // addAttribute(QName,String,XMLString)    /**      * Removes all of the attributes. This method will also remove all     * entities associated to the attributes.     */    public void removeAllAttributes() {        fLength = 0;    } // removeAllAttributes()    /**     * Removes the attribute at the specified index.     * <p>     * <strong>Note:</strong> This operation changes the indexes of all     * attributes following the attribute at the specified index.     *      * @param attrIndex The attribute index.     */    public void removeAttributeAt(int attrIndex) {        fIsTableViewConsistent = false;        if (attrIndex < fLength - 1) {            Attribute removedAttr = fAttributes[attrIndex];            System.arraycopy(fAttributes, attrIndex + 1,                             fAttributes, attrIndex, fLength - attrIndex - 1);            // Make the discarded Attribute object available for re-use            // by tucking it after the Attributes that are still in use            fAttributes[fLength-1] = removedAttr;        }        fLength--;    } // removeAttributeAt(int)    /**     * Sets the name of the attribute at the specified index.     *      * @param attrIndex The attribute index.     * @param attrName  The new attribute name.     */    public void setName(int attrIndex, QName attrName) {        fAttributes[attrIndex].name.setValues(attrName);    } // setName(int,QName)    /**     * Sets the fields in the given QName structure with the values     * of the attribute name at the specified index.     *      * @param attrIndex The attribute index.     * @param attrName  The attribute name structure to fill in.     */    public void getName(int attrIndex, QName attrName) {        attrName.setValues(fAttributes[attrIndex].name);    } // getName(int,QName)    /**     * Sets the type of the attribute at the specified index.     *      * @param attrIndex The attribute index.     * @param attrType  The attribute type. The type name is determined by     *                  the type specified for this attribute in the DTD.     *                  For example: "CDATA", "ID", "NMTOKEN", etc. However,     *                  attributes of type enumeration will have the type     *                  value specified as the pipe ('|') separated list of     *                  the enumeration values prefixed by an open      *                  parenthesis and suffixed by a close parenthesis.     *                  For example: "(true|false)".     */    public void setType(int attrIndex, String attrType) {        fAttributes[attrIndex].type = attrType;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?