elementimpl.java

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

JAVA
1,163
字号
     * Attribute has a default value, it is immediately replaced thereby.     * <P>     * The default logic is actually implemented in NamedNodeMapImpl.     * PR-DOM-Level-1-19980818 doesn't fully address the DTD, so some     * of this behavior is likely to change in future versions. ?????     * <P>     * Note that this call "succeeds" even if no attribute by this name     * existed -- unlike removeAttributeNode, which will throw a not-found     * exception in that case.     *     * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if the node is     * readonly.     */    public void removeAttribute(String name) {    	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);        }        if (needsSyncData()) {            synchronizeData();        }        if (attributes == null) {            return;        }        attributes.safeRemoveNamedItem(name);    } // removeAttribute(String)    /**     * Remove the specified attribute/value pair. If the removed     * Attribute has a default value, it is immediately replaced.     * <p>     * NOTE: Specifically removes THIS NODE -- not the node with this     * name, nor the node with these contents. If the specific Attribute     * object passed in is not stored in this Element, we throw a     * DOMException.  If you really want to remove an attribute by name,     * use removeAttribute().     *     * @return the Attribute object that was removed.     * @throws DOMException(NOT_FOUND_ERR) if oldattr is not an attribute of     * this Element.     * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if the node is     * readonly.     */    public Attr removeAttributeNode(Attr oldAttr)        throws DOMException {    	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);        }        if (needsSyncData()) {            synchronizeData();        }        if (attributes == null) {            String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_FOUND_ERR", null);            throw new DOMException(DOMException.NOT_FOUND_ERR, msg);        }        return (Attr) attributes.removeItem(oldAttr, true);    } // removeAttributeNode(Attr):Attr    /**     * Add a new name/value pair, or replace the value of the existing     * attribute having that name.     *     * Note: this method supports only the simplest kind of Attribute,     * one whose value is a string contained in a single Text node.     * If you want to assert a more complex value (which XML permits,     * though HTML doesn't), see setAttributeNode().     *     * The attribute is created with specified=true, meaning it's an     * explicit value rather than inherited from the DTD as a default.     * Again, setAttributeNode can be used to achieve other results.     *     * @throws DOMException(INVALID_NAME_ERR) if the name is not acceptable.     * (Attribute factory will do that test for us.)     *     * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if the node is     * readonly.     */	public void setAttribute(String name, String value) {		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);		}		if (needsSyncData()) {			synchronizeData();		}		Attr newAttr = getAttributeNode(name);		if (newAttr == null) {			newAttr = getOwnerDocument().createAttribute(name);			if (attributes == null) {				attributes = new AttributeMap(this, null);			}			newAttr.setNodeValue(value);			attributes.setNamedItem(newAttr);		}		else {			newAttr.setNodeValue(value);		}	} // setAttribute(String,String)    /**     * Add a new attribute/value pair, or replace the value of the     * existing attribute with that name.     * <P>     * This method allows you to add an Attribute that has already been     * constructed, and hence avoids the limitations of the simple     * setAttribute() call. It can handle attribute values that have     * arbitrarily complex tree structure -- in particular, those which     * had entity references mixed into their text.     *     * @throws DOMException(INUSE_ATTRIBUTE_ERR) if the Attribute object     * has already been assigned to another Element.     */    public Attr setAttributeNode(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.setNamedItem(newAttr);    } // setAttributeNode(Attr):Attr    //    // DOM2: Namespace methods    //    /**     * Introduced in DOM Level 2. <p>     *     * Retrieves an attribute value by local name and namespace URI.     *     * @param namespaceURI     *                      The namespace URI of the attribute to     *                      retrieve.     * @param localName     The local name of the attribute to retrieve.     * @return String       The Attr value as a string, or empty string     *                      if that attribute     *                      does not have a specified or default value.     * @since WD-DOM-Level-2-19990923     */    public String getAttributeNS(String namespaceURI, String localName) {        if (needsSyncData()) {            synchronizeData();        }        if (attributes == null) {            return "";        }        Attr attr = (Attr)(attributes.getNamedItemNS(namespaceURI, localName));        return (attr == null) ? "" : attr.getValue();    } // getAttributeNS(String,String):String    /**     * Introduced in DOM Level 2. <p>     *     *  Adds a new attribute.     *  If the given namespaceURI is null or an empty string and the     *  qualifiedName has a prefix that is "xml", the new attribute is bound to     *  the predefined namespace "http://www.w3.org/XML/1998/namespace"     *  [Namespaces].  If an attribute with the same local name and namespace     *  URI is already present on the element, its prefix is changed to be the     *  prefix part of the qualifiedName, and its value is changed to be the     *  value parameter. This value is a simple string, it is not parsed as it     *  is being set. So any markup (such as syntax to be recognized as an     *  entity reference) is treated as literal text, and needs to be     *  appropriately escaped by the implementation when it is written out. In     *  order to assign an attribute value that contains entity references, the     *  user must create an Attr node plus any Text and EntityReference nodes,     *  build the appropriate subtree, and use setAttributeNodeNS or     *  setAttributeNode to assign it as the value of an attribute.     *     * @param namespaceURI      The namespace URI of the attribute to create     *                          or alter.     * @param qualifiedName     The qualified name of the attribute to create or     *                          alter.     * @param value             The value to set in string form.     * @throws                  INVALID_CHARACTER_ERR: Raised if the specified     *                          name contains an invalid character.     *     * @throws                  NO_MODIFICATION_ALLOWED_ERR: Raised if this     *                          node is readonly.     *     * @throws                  NAMESPACE_ERR: Raised if the qualifiedName     *                          has a prefix that is "xml" and the namespaceURI     *                          is neither null nor an empty string nor     *                          "http://www.w3.org/XML/1998/namespace", or if     *                          the qualifiedName has a prefix that is "xmlns"     *                          but the namespaceURI is neither null nor an     *                          empty string, or if if the qualifiedName has a     *                          prefix different from "xml" and "xmlns" and the     *                          namespaceURI is null or an empty string.     * @since WD-DOM-Level-2-19990923     */     public void setAttributeNS(String namespaceURI,String qualifiedName,		                          String value) {		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);		}		if (needsSyncData()) {			synchronizeData();		}		int index = qualifiedName.indexOf(':');		String prefix, localName;		if (index < 0) {			prefix = null;			localName = qualifiedName;		}		else {			prefix = qualifiedName.substring(0, index);			localName = qualifiedName.substring(index + 1);		}		Attr newAttr = getAttributeNodeNS(namespaceURI, localName);		if (newAttr == null) {            // REVISIT: this is not efficient, we are creating twice the same            //          strings for prefix and localName.			newAttr = getOwnerDocument().createAttributeNS(					namespaceURI,					qualifiedName);			if (attributes == null) {				attributes = new AttributeMap(this, null);			}			newAttr.setNodeValue(value);			attributes.setNamedItemNS(newAttr);		}		else {            if (newAttr instanceof AttrNSImpl){                // change prefix and value                ((AttrNSImpl)newAttr).name= (prefix!=null)?(prefix+":"+localName):localName;            }            else {                // This case may happen if user calls:                //      elem.setAttribute("name", "value");                //      elem.setAttributeNS(null, "name", "value");                // This case is not defined by the DOM spec, we choose                // to create a new attribute in this case and remove an old one from the tree                // note this might cause events to be propagated or user data to be lost                 newAttr = new AttrNSImpl((CoreDocumentImpl)getOwnerDocument(), namespaceURI, qualifiedName, localName);                attributes.setNamedItemNS(newAttr);            }			newAttr.setNodeValue(value);		}    } // setAttributeNS(String,String,String)    /**     * Introduced in DOM Level 2. <p>     *     * Removes an attribute by local name and namespace URI. If the removed     * attribute has a default value it is immediately replaced.     * The replacing attribute has the same namespace URI and local name,     * as well as the original prefix.<p>     *     * @param namespaceURI  The namespace URI of the attribute to remove.     *     * @param localName     The local name of the attribute to remove.     * @throws                  NO_MODIFICATION_ALLOWED_ERR: Raised if this     *                          node is readonly.     * @since WD-DOM-Level-2-19990923     */    public void removeAttributeNS(String namespaceURI, String localName) {    	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);        }        if (needsSyncData()) {            synchronizeData();        }        if (attributes == null) {            return;        }        attributes.safeRemoveNamedItemNS(namespaceURI, localName);    } // removeAttributeNS(String,String)    /**     * Retrieves an Attr node by local name and namespace URI.     *     * @param namespaceURI  The namespace URI of the attribute to     *                      retrieve.     * @param localName     The local name of the attribute to retrieve.     * @return Attr         The Attr node with the specified attribute     *                      local name and namespace     *                      URI or null if there is no such attribute.     * @since WD-DOM-Level-2-19990923     */    public Attr getAttributeNodeNS(String namespaceURI, String localName){        if (needsSyncData()) {            synchronizeData();        }        if (attributes == null) {            return null;        }        return (Attr)attributes.getNamedItemNS(namespaceURI, localName);    } // getAttributeNodeNS(String,String):Attr    /**     * Introduced in DOM Level 2. <p>     *     * Adds a new attribute. If an attribute with that local name and     * namespace URI is already present in the element, it is replaced     * by the new one.     *     * @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();

⌨️ 快捷键说明

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