elementnode2.java
来自「java jdk 1.4的源码」· Java 代码 · 共 624 行 · 第 1/2 页
JAVA
624 行
throw new IllegalStateException ( getMessage ("EN-002")); out.write (tagStart, 0, 1); // "<" out.write (qName); if (attributes != null) attributes.writeXml (context); // // Write empty nodes as "<EMPTY />" to make sure version 3 // and 4 web browsers can read empty tag output as HTML. // XML allows "<EMPTY/>" too, of course. // if (!hasChildNodes ()) out.write (tagEnd, 0, 3); // " />" else { out.write (tagEnd, 2, 1); // ">" writeChildrenXml (context); out.write (tagStart, 0, 2); // "</" out.write (qName); out.write (tagEnd, 2, 1); // ">" } } /** * Assigns the name of the element's ID attribute; only one attribute * may have the ID type. XML supports a kind of validatable internal * linking using ID attributes, with IDREF attributes identifying * specific nodes (and IDREFS attributes identifying sets of them). */ public void setIdAttributeName (String attName) { if (readonly) throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR); idAttributeName = attName; } /** * Returns the name of the element's ID attribute, if one is known. */ public String getIdAttributeName () { return idAttributeName; } public void setUserObject (Object userObject) { this.userObject = userObject; } public Object getUserObject () { return userObject; } // DOM support /** <b>DOM:</b> Returns the ELEMENT_NODE node type. */ public short getNodeType () { return ELEMENT_NODE; } /** <b>DOM:</b> Returns the name of the XML tag for this element. */ public String getTagName () { return qName; } /** * Returns <code>true</code> when an attribute with a given name is * specified on this element or has a default value, <code>false</code> * otherwise. * @since DOM Level 2 */ public boolean hasAttribute(String name) { return getAttributeNode(name) != null; } /** * Returns <code>true</code> when an attribute with a given local name * and namespace URI is specified on this element or has a default * value, <code>false</code> otherwise. * @since DOM Level 2 */ public boolean hasAttributeNS(String namespaceURI, String localName) { return getAttributeNodeNS(namespaceURI, localName) != null; } /** <b>DOM:</b> Returns the value of the named attribute, or an empty * string */ public String getAttribute (String name) { return (attributes == null) ? "" : attributes.getValue (name); } /** * Retrieves an attribute value by local name and namespace URI. * @since DOM Level 2 */ public String getAttributeNS(String namespaceURI, String localName) { if (attributes == null) { return ""; } Attr attr = getAttributeNodeNS(namespaceURI, localName); if (attr == null) { return ""; } return attr.getValue(); } /** * Retrieves an <code>Attr</code> node by local name and namespace URI. * @since DOM Level 2 */ public Attr getAttributeNodeNS(String namespaceURI, String localName) { if (localName == null) { return null; } if (attributes == null) { return null; } for (int i = 0; ; i++) { AttributeNode attr = (AttributeNode) attributes.item(i); if (attr == null) { return null; } if (localName.equals(attr.getLocalName()) && (attr.getNamespaceURI() == namespaceURI || attr.getNamespaceURI().equals(namespaceURI))) { return attr; } } } /** * <b>DOM:</b> Assigns or modifies the value of the specified attribute. */ public void setAttribute (String name, String value) throws DOMException { NodeBase att; // Common superclass of all Attr nodes if (readonly) throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR); if (!XmlNames.isName(name)) { throw new DomEx(DOMException.INVALID_CHARACTER_ERR); } if (attributes == null) attributes = new AttributeSet (this); if ((att = (NodeBase) attributes.getNamedItem (name)) != null) att.setNodeValue (value); else { att = new AttributeNode1(name, value, true, null); att.setOwnerDocument ((XmlDocument) getOwnerDocument ()); /* "ownerElement" should be null before calling "setNamedItem" */ attributes.setNamedItem (att); } } /** * <b>DOM2:</b> * @since DOM Level 2 */ public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException { AttributeNode.checkArguments(namespaceURI, qualifiedName); Attr attr = getAttributeNodeNS(namespaceURI, XmlNames.getLocalPart(qualifiedName)); if (attr == null) { AttributeNode newAttr = new AttributeNode(namespaceURI, qualifiedName, value, true, null); newAttr.setOwnerDocument((XmlDocument)getOwnerDocument()); setAttributeNodeNS(newAttr); } else { attr.setValue(value); attr.setPrefix(XmlNames.getPrefix(qualifiedName)); } } /** * <b>DOM2:</b> * @since DOM Level 2 */ public Attr setAttributeNodeNS(Attr newAttr) throws DOMException { if (readonly) { throw new DomEx(DomEx.NO_MODIFICATION_ALLOWED_ERR); } if (newAttr.getOwnerDocument() != getOwnerDocument()) { throw new DomEx(DomEx.WRONG_DOCUMENT_ERR); } if (attributes == null) { attributes = new AttributeSet(this); } // Note: ownerElement of newAttr is both checked and set in the // following call to AttributeSet.setNamedItemNS(Node) return (Attr)attributes.setNamedItemNS(newAttr); } /** <b>DOM:</b> Remove the named attribute. */ public void removeAttribute (String name) throws DOMException { if (readonly) throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR); if (attributes == null) { return; } try { attributes.removeNamedItem (name); } catch (DOMException x) { // DOM2 does not allow a NOT_FOUND_ERR exception to be thrown if (x.code != DOMException.NOT_FOUND_ERR) { throw x; } } } /** * <b>DOM2:</b> * @since DOM Level 2 */ public void removeAttributeNS(String namespaceURI, String localName) throws DOMException { if (readonly) { throw new DomEx(DomEx.NO_MODIFICATION_ALLOWED_ERR); } try { attributes.removeNamedItemNS(namespaceURI, localName); } catch (DOMException x) { // DOM2 does not allow a NOT_FOUND_ERR exception to be thrown if (x.code != DOMException.NOT_FOUND_ERR) { throw x; } } } /** <b>DOM:</b> returns the attribute */ public Attr getAttributeNode (String name) { if (attributes != null) return (Attr) attributes.getNamedItem (name); else return null; } /** <b>DOM:</b> assigns the attribute */ public Attr setAttributeNode (Attr newAttr) throws DOMException { if (readonly) throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR); if (!(newAttr instanceof AttributeNode)) throw new DomEx (DomEx.WRONG_DOCUMENT_ERR); if (attributes == null) attributes = new AttributeSet (this); // Note: ownerElement of newAttr is both checked and set in the // following call to AttributeSet.setNamedItem(Node) return (Attr) attributes.setNamedItem(newAttr); } /** <b>DOM:</b> removes the attribute with the same name as this one */ public Attr removeAttributeNode (Attr oldAttr) throws DOMException { if (isReadonly ()) throw new DomEx (DomEx.NO_MODIFICATION_ALLOWED_ERR); Attr attr = getAttributeNode (oldAttr.getNodeName ()); if (attr == null) throw new DomEx (DomEx.NOT_FOUND_ERR); removeAttribute (attr.getNodeName ()); return attr; } /** * Creates a new unparented node whose attributes are the same as * this node's attributes; if <em>deep</em> is true, the children * of this node are cloned as children of the new node. */ public Node cloneNode (boolean deep) { try { ElementNode2 retval = makeClone(); if (deep) { for (int i = 0; true; i++) { Node node = item (i); if (node == null) break; retval.appendChild (node.cloneNode (true)); } } return retval; } catch (DOMException e) { throw new RuntimeException (getMessage ("EN-001")); } } /** * Convenience method to construct a non-prettyprinting XML write * context and call writeXml with it. Subclasses may choose to * to override this method to generate non-XML text, * * @param out where to emit the XML content of this node */ public void write (Writer out) throws IOException { writeXml (new XmlWriteContext (out)); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?