elementimpl.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,165 行 · 第 1/3 页

JAVA
1,165
字号
     * @param Attr      The Attr node to add to the attribute list. When      *                  the Node has no namespaceURI, this method behaves      *                  like setAttributeNode.     * @return Attr     If the newAttr attribute replaces an existing attribute     *                  with the same local name and namespace URI, the *     *                  previously existing Attr node is returned, otherwise     *                  null is returned.     * @throws          WRONG_DOCUMENT_ERR: Raised if newAttr     *                  was created from a different document than the one that     *                  created the element.     *     * @throws          NO_MODIFICATION_ALLOWED_ERR: Raised if     *                  this node is readonly.     *     * @throws          INUSE_ATTRIBUTE_ERR: Raised if newAttr is     *                  already an attribute of another Element object. The     *                  DOM user must explicitly clone Attr nodes to re-use     *                  them in other elements.     * @since WD-DOM-Level-2-19990923     */    public Attr setAttributeNodeNS(Attr newAttr)        throws DOMException        {        if (needsSyncData()) {            synchronizeData();        }        if (ownerDocument.errorChecking) {            if (isReadOnly()) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);    		    throw new DOMException(                                     DOMException.NO_MODIFICATION_ALLOWED_ERR,                                      msg);            }            if (newAttr.getOwnerDocument() != ownerDocument) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);                throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);            }        }        if (attributes == null) {            attributes = new AttributeMap(this, null);        }    	// This will throw INUSE if necessary    	return (Attr) attributes.setNamedItemNS(newAttr);    } // setAttributeNodeNS(Attr):Attr    /**      * NON-DOM: sets attribute node for this element      */    protected int setXercesAttributeNode (Attr attr){        if (needsSyncData()) {            synchronizeData();        }                if (attributes == null) {            attributes = new AttributeMap(this, null);        }        return attributes.addItem(attr);    }        /**      * NON-DOM: get inded of an attribute      */    protected int getXercesAttribute(String namespaceURI, String localName){        if (needsSyncData()) {            synchronizeData();        }        if (attributes == null) {            return -1;        }        return attributes.getNamedItemIndex(namespaceURI, localName);    }         /**     * Introduced in DOM Level 2.     */    public boolean hasAttributes() {        if (needsSyncData()) {            synchronizeData();        }        return (attributes != null && attributes.getLength() != 0);    }    /**     * Introduced in DOM Level 2.     */    public boolean hasAttribute(String name) {        return getAttributeNode(name) != null;    }    /**     * Introduced in DOM Level 2.     */    public boolean hasAttributeNS(String namespaceURI, String localName) {        return getAttributeNodeNS(namespaceURI, localName) != null;    }    /**     * Introduced in DOM Level 2. <p>     *     * Returns a NodeList of all the Elements with a given local name and     * namespace URI in the order in which they would be encountered in a     * preorder traversal of the Document tree, starting from this node.     *     * @param namespaceURI The namespace URI of the elements to match     *                     on. The special value "*" matches all     *                     namespaces. When it is null or an empty     *                     string, this method behaves like     *                     getElementsByTagName.     * @param localName    The local name of the elements to match on.     *                     The special value "*" matches all local names.     * @return NodeList    A new NodeList object containing all the matched     *                     Elements.     * @since WD-DOM-Level-2-19990923     */    public NodeList getElementsByTagNameNS(String namespaceURI,                                           String localName) {    	return new DeepNodeListImpl(this, namespaceURI, localName);    }    /**     * DOM Level 3 WD- Experimental.     * Override inherited behavior from NodeImpl and ParentNode to check on     * attributes     */    public boolean isEqualNode(Node arg) {        if (!super.isEqualNode(arg)) {            return false;        }        boolean hasAttrs = hasAttributes();        if (hasAttrs != ((Element) arg).hasAttributes()) {            return false;        }        if (hasAttrs) {            NamedNodeMap map1 = getAttributes();            NamedNodeMap map2 = ((Element) arg).getAttributes();            int len = map1.getLength();            if (len != map2.getLength()) {                return false;            }            for (int i = 0; i < len; i++) {                Node n1 = map1.item(i);                if (n1.getLocalName() == null) { // DOM Level 1 Node                    Node n2 = map2.getNamedItem(n1.getNodeName());                    if (n2 == null || !((NodeImpl) n1).isEqualNode(n2)) {                        return false;                    }                }                else {                    Node n2 = map2.getNamedItemNS(n1.getNamespaceURI(),                                                  n1.getLocalName());                    if (n2 == null || !((NodeImpl) n1).isEqualNode(n2)) {                        return false;                    }                }            }        }        return true;    }        /**     * DOM Level 3: register the given attribute node as an ID attribute     */    public void setIdAttributeNode(Attr at, boolean makeId) {        if (needsSyncData()) {            synchronizeData();        }        if (ownerDocument.errorChecking) {            if (isReadOnly()) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);                throw new DOMException(                                     DOMException.NO_MODIFICATION_ALLOWED_ERR,                                      msg);            }                    if (at.getOwnerElement() != this) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);                throw new DOMException(DOMException.NOT_FOUND_ERR, msg);            }        }        ((AttrImpl) at).isIdAttribute(makeId);        if (!makeId) {            ownerDocument.removeIdentifier(at.getValue());        }        else {            ownerDocument.putIdentifier(at.getValue(), this);        }    }        /**     * DOM Level 3: register the given attribute node as an ID attribute     */    public void setIdAttribute(String name, boolean makeId) {        if (needsSyncData()) {            synchronizeData();        }        Attr at = getAttributeNode(name);				if( at == null){       		String msg = DOMMessageFormatter.formatMessage(									DOMMessageFormatter.DOM_DOMAIN, 									"NOT_FOUND_ERR", null);            throw new DOMException(DOMException.NOT_FOUND_ERR, msg);		}        		if (ownerDocument.errorChecking) {            if (isReadOnly()) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);                throw new DOMException(                                     DOMException.NO_MODIFICATION_ALLOWED_ERR,                                      msg);            }                    if (at.getOwnerElement() != this) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);                throw new DOMException(DOMException.NOT_FOUND_ERR, msg);            }        }                ((AttrImpl) at).isIdAttribute(makeId);        if (!makeId) {            ownerDocument.removeIdentifier(at.getValue());        }        else {            ownerDocument.putIdentifier(at.getValue(), this);        }    }    /**     * DOM Level 3: register the given attribute node as an ID attribute     */    public void setIdAttributeNS(String namespaceURI, String localName,                                    boolean makeId) {        if (needsSyncData()) {            synchronizeData();        }        //if namespace uri is empty string, set it to 'null'        if (namespaceURI != null) {            namespaceURI = (namespaceURI.length() == 0)? null : namespaceURI;                    }        Attr at = getAttributeNodeNS(namespaceURI, localName);				if( at == null){       		String msg = DOMMessageFormatter.formatMessage(									DOMMessageFormatter.DOM_DOMAIN, 									"NOT_FOUND_ERR", null);            throw new DOMException(DOMException.NOT_FOUND_ERR, msg);		}       		if (ownerDocument.errorChecking) {            if (isReadOnly()) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);                throw new DOMException(                                     DOMException.NO_MODIFICATION_ALLOWED_ERR,                                      msg);            }                    if (at.getOwnerElement() != this) {                String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);                throw new DOMException(DOMException.NOT_FOUND_ERR, msg);            }        }        ((AttrImpl) at).isIdAttribute(makeId);        if (!makeId) {            ownerDocument.removeIdentifier(at.getValue());        }        else {            ownerDocument.putIdentifier(at.getValue(), this);        }   }           /**    * NON-DOM: setting type used by the DOM parser    * @see NodeImpl#setReadOnly    */   public void setType(TypeInfo type) {       this.type = type;   }       /**    * Method getSchemaTypeInfo.    * @return TypeInfo    */   public TypeInfo getSchemaTypeInfo(){       if (needsSyncData()) {           synchronizeData();       }       return type;   }    //    // Public methods    //    /**     * NON-DOM: Subclassed to flip the attributes' readonly switch as well.     * @see NodeImpl#setReadOnly     */    public void setReadOnly(boolean readOnly, boolean deep) {    	super.setReadOnly(readOnly,deep);        if (attributes != null) {            attributes.setReadOnly(readOnly,true);        }    }    //    // Protected methods    //    /** Synchronizes the data (name and value) for fast nodes. */    protected void synchronizeData() {        // no need to sync in the future        needsSyncData(false);        // we don't want to generate any event for this so turn them off        boolean orig = ownerDocument.getMutationEvents();        ownerDocument.setMutationEvents(false);        // attributes        setupDefaultAttributes();        // set mutation events flag back to its original value        ownerDocument.setMutationEvents(orig);    } // synchronizeData()    // support for DOM Level 3 renameNode method    // @param el The element from which to take the attributes    void moveSpecifiedAttributes(ElementImpl el) {        if (needsSyncData()) {            synchronizeData();        }        if (el.hasAttributes()) {            if (attributes == null) {                attributes = new AttributeMap(this, null);            }            attributes.moveSpecifiedAttributes(el.attributes);        }    }    /** Setup the default attributes. */    protected void setupDefaultAttributes() {        NamedNodeMapImpl defaults = getDefaultAttributes();        if (defaults != null) {            attributes = new AttributeMap(this, defaults);        }    }    /** Reconcile default attributes. */    protected void reconcileDefaultAttributes() {        if (attributes != null) {            NamedNodeMapImpl defaults = getDefaultAttributes();            attributes.reconcileDefaults(defaults);        }    }    /** Get the default attributes. */    protected NamedNodeMapImpl getDefaultAttributes() {    	DocumentTypeImpl doctype =            (DocumentTypeImpl) ownerDocument.getDoctype();    	if (doctype == null) {            return null;        }        ElementDefinitionImpl eldef =            (ElementDefinitionImpl)doctype.getElements()                                               .getNamedItem(getNodeName());        if (eldef == null) {            return null;        }        return (NamedNodeMapImpl) eldef.getAttributes();    } // getDefaultAttributes()} // class ElementImpl

⌨️ 快捷键说明

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