attributemap.java

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

JAVA
567
字号
            } else {                return null;            }        }        return remove((AttrImpl)nodes.elementAt(i), i, true);    } // internalRemoveNamedItem(String,boolean):Node    private final Node remove(AttrImpl attr, int index,                              boolean addDefault) {        CoreDocumentImpl ownerDocument = ownerNode.ownerDocument();        String name = attr.getNodeName();        if (attr.isIdAttribute()) {            ownerDocument.removeIdentifier(attr.getValue());        }        if (hasDefaults() && addDefault) {            // If there's a default, add it instead            NamedNodeMapImpl defaults =                ((ElementImpl) ownerNode).getDefaultAttributes();            Node d;            if (defaults != null &&                (d = defaults.getNamedItem(name)) != null &&                findNamePoint(name, index+1) < 0) {                    NodeImpl clone = (NodeImpl)d.cloneNode(true);                    if (d.getLocalName() !=null){                            // we must rely on the name to find a default attribute                            // ("test:attr"), but while copying it from the DOCTYPE                            // we should not loose namespace URI that was assigned                            // to the attribute in the instance document.                            ((AttrNSImpl)clone).namespaceURI = attr.getNamespaceURI();                    }                     clone.ownerNode = ownerNode;                    clone.isOwned(true);                    clone.isSpecified(false);                                    nodes.setElementAt(clone, index);                    if (attr.isIdAttribute()) {                        ownerDocument.putIdentifier(clone.getNodeValue(),                                                (ElementImpl)ownerNode);                    }            } else {                nodes.removeElementAt(index);            }        } else {            nodes.removeElementAt(index);        }        //        changed(true);        // remove reference to owner        attr.ownerNode = ownerDocument;        attr.isOwned(false);        // make sure it won't be mistaken with defaults in case it's        // reused        attr.isSpecified(true);        attr.isIdAttribute(false);        // notify document        ownerDocument.removedAttrNode(attr, ownerNode, name);        return attr;    }        /**     * Introduced in DOM Level 2. <p>     * Removes an attribute specified by local name and namespace URI.     * @param namespaceURI     *                      The namespace URI of the node to remove.     *                      When it is null or an empty string, this     *                      method behaves like removeNamedItem.     * @param               The local name of the node to remove. If the     *                      removed attribute is known to have a default     *                      value, an attribute immediately appears     *                      containing the default value.     * @return Node         The node removed from the map if a node with such     *                      a local name and namespace URI exists.     * @throws              NOT_FOUND_ERR: Raised if there is no node named     *                      name in the map.     */    public Node removeNamedItemNS(String namespaceURI, String name)        throws DOMException {        return internalRemoveNamedItemNS(namespaceURI, name, true);    }    /**     * Same as removeNamedItem except that it simply returns null if the     * specified local name and namespace URI is not found.     */    Node safeRemoveNamedItemNS(String namespaceURI, String name) {        return internalRemoveNamedItemNS(namespaceURI, name, false);    }    /**     * Internal removeNamedItemNS method allowing to specify whether an     * exception must be thrown if the specified local name and namespace URI     * is not found.     */    final protected Node internalRemoveNamedItemNS(String namespaceURI,            String name,            boolean raiseEx) {                CoreDocumentImpl ownerDocument = ownerNode.ownerDocument();        if (ownerDocument.errorChecking && 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(namespaceURI, name);        if (i < 0) {            if (raiseEx) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);                throw new DOMException(DOMException.NOT_FOUND_ERR, msg);            } else {                return null;            }        }                AttrImpl n = (AttrImpl)nodes.elementAt(i);                if (n.isIdAttribute()) {            ownerDocument.removeIdentifier(n.getValue());        }        // If there's a default, add it instead        String nodeName = n.getNodeName();        if (hasDefaults()) {            NamedNodeMapImpl defaults = ((ElementImpl) ownerNode).getDefaultAttributes();            Node d;            if (defaults != null                    && (d = defaults.getNamedItem(nodeName)) != null)            {                int j = findNamePoint(nodeName,0);                if (j>=0 && findNamePoint(nodeName, j+1) < 0) {                    NodeImpl clone = (NodeImpl)d.cloneNode(true);                    clone.ownerNode = ownerNode;                    if (d.getLocalName() != null) {                        // we must rely on the name to find a default attribute                        // ("test:attr"), but while copying it from the DOCTYPE                        // we should not loose namespace URI that was assigned                        // to the attribute in the instance document.                        ((AttrNSImpl)clone).namespaceURI = namespaceURI;                    }                    clone.isOwned(true);                    clone.isSpecified(false);                    nodes.setElementAt(clone, i);                    if (clone.isIdAttribute()) {                        ownerDocument.putIdentifier(clone.getNodeValue(),                                 (ElementImpl)ownerNode);                    }                } else {                    nodes.removeElementAt(i);                }            } else {                nodes.removeElementAt(i);            }        } else {            nodes.removeElementAt(i);        }                //        changed(true);                // remove reference to owner        n.ownerNode = ownerDocument;        n.isOwned(false);        // make sure it won't be mistaken with defaults in case it's        // reused        n.isSpecified(true);        // update id table if needed        n.isIdAttribute(false);                // notify document        ownerDocument.removedAttrNode(n, ownerNode, name);                return n;            } // internalRemoveNamedItemNS(String,String,boolean):Node    //    // Public methods    //    /**     * Cloning a NamedNodeMap is a DEEP OPERATION; it always clones     * all the nodes contained in the map.     */         public NamedNodeMapImpl cloneMap(NodeImpl ownerNode) {        AttributeMap newmap =            new AttributeMap((ElementImpl) ownerNode, null);        newmap.hasDefaults(hasDefaults());        newmap.cloneContent(this);        return newmap;    } // cloneMap():AttributeMap    /**     * Override parent's method to set the ownerNode correctly     */    protected void cloneContent(NamedNodeMapImpl srcmap) {        Vector srcnodes = srcmap.nodes;        if (srcnodes != null) {            int size = srcnodes.size();            if (size != 0) {                if (nodes == null) {                    nodes = new Vector(size);                }                nodes.setSize(size);                for (int i = 0; i < size; ++i) {                    NodeImpl n = (NodeImpl) srcnodes.elementAt(i);                    NodeImpl clone = (NodeImpl) n.cloneNode(true);                    clone.isSpecified(n.isSpecified());                    nodes.setElementAt(clone, i);                    clone.ownerNode = ownerNode;                    clone.isOwned(true);                }            }        }    } // cloneContent():AttributeMap    /**     * Move specified attributes from the given map to this one     */    void moveSpecifiedAttributes(AttributeMap srcmap) {        int nsize = (srcmap.nodes != null) ? srcmap.nodes.size() : 0;        for (int i = nsize - 1; i >= 0; i--) {            AttrImpl attr = (AttrImpl) srcmap.nodes.elementAt(i);            if (attr.isSpecified()) {                srcmap.remove(attr, i, false);                if (attr.getLocalName() != null) {                    setNamedItem(attr);                }                else {                    setNamedItemNS(attr);                }            }        }    } // moveSpecifiedAttributes(AttributeMap):void    /**     * Get this AttributeMap in sync with the given "defaults" map.     * @param defaults The default attributes map to sync with.     */    protected void reconcileDefaults(NamedNodeMapImpl defaults) {        // remove any existing default        int nsize = (nodes != null) ? nodes.size() : 0;        for (int i = nsize - 1; i >= 0; i--) {            AttrImpl attr = (AttrImpl) nodes.elementAt(i);            if (!attr.isSpecified()) {                remove(attr, i, false);            }        }        // add the new defaults        if (defaults == null) {            return;        }        if (nodes == null || nodes.size() == 0) {            cloneContent(defaults);        }        else {            int dsize = defaults.nodes.size();            for (int n = 0; n < dsize; n++) {                AttrImpl d = (AttrImpl) defaults.nodes.elementAt(n);                int i = findNamePoint(d.getNodeName(), 0);                 if (i < 0) {            		i = -1 - i;                     NodeImpl clone = (NodeImpl) d.cloneNode(true);                    clone.ownerNode = ownerNode;                    clone.isOwned(true);                    clone.isSpecified(false);            		nodes.insertElementAt(clone, i);                }            }        }    } // reconcileDefaults()} // class AttributeMap

⌨️ 快捷键说明

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