attributemap.java

来自「JAVA 所有包」· Java 代码 · 共 567 行 · 第 1/2 页

JAVA
567
字号
/* * Copyright 2000-2002,2004 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.sun.org.apache.xerces.internal.dom;import java.util.Vector;import org.w3c.dom.DOMException;import org.w3c.dom.Node;/** * AttributeMap inherits from NamedNodeMapImpl and extends it to deal with the * specifics of storing attributes. These are: * <ul> *  <li>managing ownership of attribute nodes *  <li>managing default attributes        *  <li>firing mutation events * </ul> * <p> * This class doesn't directly support mutation events, however, it notifies * the document when mutations are performed so that the document class do so. *  * @xerces.internal * * @version $Id: AttributeMap.java,v 1.2.6.1 2005/08/30 11:30:38 sunithareddy Exp $ */public class AttributeMap extends NamedNodeMapImpl {        /** Serialization version. */    static final long serialVersionUID = 8872606282138665383L;    //    // Constructors    //    /** Constructs a named node map. */    protected AttributeMap(ElementImpl ownerNode, NamedNodeMapImpl defaults) {        super(ownerNode);        if (defaults != null) {            // initialize map with the defaults            cloneContent(defaults);            if (nodes != null) {                hasDefaults(true);            }        }    }    /**     * Adds an attribute using its nodeName attribute.     * @see org.w3c.dom.NamedNodeMap#setNamedItem     * @return If the new Node replaces an existing node the replaced Node is     *      returned, otherwise null is returned.      * @param arg      *      An Attr node to store in this map.     * @exception org.w3c.dom.DOMException The exception description.     */    public Node setNamedItem(Node arg)    throws DOMException {                boolean errCheck = ownerNode.ownerDocument().errorChecking;        if (errCheck) {            if (isReadOnly()) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);                throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);            }            if (arg.getOwnerDocument() != ownerNode.ownerDocument()) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);                throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);            }            if (arg.getNodeType() != Node.ATTRIBUTE_NODE) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);                throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);            }        }        AttrImpl argn = (AttrImpl)arg;                if (argn.isOwned()){            if (errCheck && argn.getOwnerElement() != ownerNode) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INUSE_ATTRIBUTE_ERR", null);                throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR, msg);            }             // replacing an Attribute with itself does nothing            return arg;        }                        // set owner        argn.ownerNode = ownerNode;        argn.isOwned(true);                int i = findNamePoint(arg.getNodeName(),0);        AttrImpl previous = null;        if (i >= 0) {            previous = (AttrImpl) nodes.elementAt(i);            nodes.setElementAt(arg,i);            previous.ownerNode = ownerNode.ownerDocument();            previous.isOwned(false);            // make sure it won't be mistaken with defaults in case it's reused            previous.isSpecified(true);        } else {            i = -1 - i; // Insert point (may be end of list)            if (null == nodes) {                nodes = new Vector(5, 10);            }            nodes.insertElementAt(arg, i);        }                // notify document        ownerNode.ownerDocument().setAttrNode(argn, previous);                // If the new attribute is not normalized,        // the owning element is inherently not normalized.        if (!argn.isNormalized()) {            ownerNode.isNormalized(false);        }        return previous;            } // setNamedItem(Node):Node    /**     * Adds an attribute using its namespaceURI and localName.     * @see org.w3c.dom.NamedNodeMap#setNamedItem     * @return If the new Node replaces an existing node the replaced Node is     *      returned, otherwise null is returned.      * @param arg A node to store in a named node map.     */    public Node setNamedItemNS(Node arg)    throws DOMException {                boolean errCheck = ownerNode.ownerDocument().errorChecking;        if (errCheck) {             if (isReadOnly()) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);                throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);                        }            if(arg.getOwnerDocument() != ownerNode.ownerDocument()) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);                throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);            }            if (arg.getNodeType() != Node.ATTRIBUTE_NODE) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);                throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);            }        }        AttrImpl argn = (AttrImpl)arg;                if (argn.isOwned()){            if (errCheck && argn.getOwnerElement() != ownerNode) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INUSE_ATTRIBUTE_ERR", null);                throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR, msg);            }             // replacing an Attribute with itself does nothing            return arg;        }                // set owner        argn.ownerNode = ownerNode;        argn.isOwned(true);                int i = findNamePoint(argn.getNamespaceURI(), argn.getLocalName());        AttrImpl previous = null;        if (i >= 0) {            previous = (AttrImpl) nodes.elementAt(i);            nodes.setElementAt(arg,i);            previous.ownerNode = ownerNode.ownerDocument();            previous.isOwned(false);            // make sure it won't be mistaken with defaults in case it's reused            previous.isSpecified(true);        } else {            // If we can't find by namespaceURI, localName, then we find by            // nodeName so we know where to insert.            i = findNamePoint(arg.getNodeName(),0);            if (i >=0) {                previous = (AttrImpl) nodes.elementAt(i);                nodes.insertElementAt(arg,i);            } else {                i = -1 - i; // Insert point (may be end of list)                if (null == nodes) {                    nodes = new Vector(5, 10);                }                nodes.insertElementAt(arg, i);            }        }        //    	changed(true);                // notify document        ownerNode.ownerDocument().setAttrNode(argn, previous);                // If the new attribute is not normalized,        // the owning element is inherently not normalized.        if (!argn.isNormalized()) {            ownerNode.isNormalized(false);        }        return previous;            } // setNamedItemNS(Node):Node       /**     * Removes an attribute specified by name.     * @param name     *      The name of a node to remove. If the     *      removed attribute is known to have a default value, an     *      attribute immediately appears containing the default value     *      as well as the corresponding namespace URI, local name,     *      and prefix when applicable.     * @return The node removed from the map if a node with such a name exists.     * @throws              NOT_FOUND_ERR: Raised if there is no node named     *                      name in the map.     */    /***/    public Node removeNamedItem(String name)        throws DOMException {        return internalRemoveNamedItem(name, true);    }    /**     * Same as removeNamedItem except that it simply returns null if the     * specified name is not found.     */    Node safeRemoveNamedItem(String name) {        return internalRemoveNamedItem(name, false);    }    /**     * NON-DOM: Remove the node object     *      * NOTE: Specifically removes THIS NODE -- not the node with this     * name, nor the node with these contents. If node does not belong to     * this named node map, we throw a DOMException.     *      * @param item       The node to remove     * @param addDefault true -- magically add default attribute     * @return Removed node     * @exception DOMException     */    protected Node removeItem(Node item, boolean addDefault)        throws DOMException {        int index = -1;        if (nodes != null) {            for (int i = 0; i < nodes.size(); i++) {                if (nodes.elementAt(i) == item) {                    index = i;                    break;                }            }        }        if (index < 0) {            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);            throw new DOMException(DOMException.NOT_FOUND_ERR, msg);        }                return remove((AttrImpl)item, index, addDefault);    }    /**     * Internal removeNamedItem method allowing to specify whether an exception     * must be thrown if the specified name is not found.     */    final protected Node internalRemoveNamedItem(String name, boolean raiseEx){        if (isReadOnly()) {                            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);                throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);                    }        int i = findNamePoint(name,0);        if (i < 0) {            if (raiseEx) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);                throw new DOMException(DOMException.NOT_FOUND_ERR, msg);

⌨️ 快捷键说明

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