xmldocument.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,456 行 · 第 1/4 页
JAVA
1,456 行
Node node; for (int i = 0; (node = item (i)) != null; i++) { if (node instanceof DocumentType) { // XXX recreate continue; } node = node.cloneNode (true); retval.changeNodeOwner (node); retval.appendChild (node); } } return retval; } /** * Changes the "owner document" of the given node, and all child * and associated attribute nodes, to be this document. If the * node has a parent, it is first removed from that parent. * <b>Obsolete</b> Use importNode method instead. Still useful for * internal implementation. * * @param node * @exception DOMException WRONG_DOCUMENT_ERROR when attempting * to change the owner for some other DOM implementation<P> * HIERARCHY_REQUEST_ERROR when the node is a document, document * type, entity, or notation; or when it is an attribute associated * with an element whose owner is not being (recursively) changed. */ final public void changeNodeOwner (Node node) throws DOMException { TreeWalker walker; NodeBase n; if (node.getOwnerDocument () == this) return; if (!(node instanceof NodeBase)) throw new DomEx (DomEx.WRONG_DOCUMENT_ERR); switch (node.getNodeType ()) { // Documents _are_ owners; can't switch identities case Node.DOCUMENT_NODE: // Entities, Notations only live in doctypes ... we // don't support changing their ownership at this time case Node.ENTITY_NODE: case Node.NOTATION_NODE: case Node.DOCUMENT_TYPE_NODE: throw new DomEx (DomEx.HIERARCHY_REQUEST_ERR); } // // If node is an attribute, its "scoped" by one element... // and if that scope hasn't been changed (i.e. if this isn't // a recursive call) we can't really fix anything! // if (node instanceof AttributeNode) { AttributeNode attr = (AttributeNode) node; Element scope = attr.getOwnerElement(); if (scope != null && scope.getOwnerDocument () != this) throw new DomEx (DomEx.HIERARCHY_REQUEST_ERR); } // unparent node if needed n = (NodeBase) node.getParentNode (); if (n != null) n.removeChild (node); // change any children (including self) for (walker = new TreeWalker (node), n = (NodeBase) walker.getCurrent (); n != null; n = (NodeBase) walker.getNext ()) { n.setOwnerDocument (this); // Elements have associated attributes, which must // also have owners changed. if (n instanceof Element) { NamedNodeMap list = n.getAttributes (); int length = list.getLength (); for (int i = 0; i < length; i++) changeNodeOwner (list.item (i)); } } } /** * Returns the <code>Element</code> whose <code>ID</code> is given by * <code>elementId</code>. * * @since DOM Level 2 */ public Element getElementById(String elementId) { return getElementExById(elementId); } /** * Returns the element whose ID is given by the parameter; or null * if no such element exists. This relies on elements to know the * name of their ID attribute, as will be currently be true only if * the document has been parsed from XML text with a DTD using the * <em>XmlDocumentBuilder</em> class, or if it has been constructed * using specialized DOM implementation classes which know the name * of their ID attribute. (XML allows only one ID attribute per * element, and different elements may use different names for their * ID attributes.) * * <P> This may be used to implement internal IDREF linkage, as well * as some kinds of <em>XPointer</em> linkage as used in current * drafts of <em>XLink</em>. * * @param id The value of the ID attribute which will be matched * by any element which is returned. * @deprecated As of DOM level 2, replaced by the method * Document.getElementById */ // Note: HTML DOM has getElementById() with "Element" return type public ElementEx getElementExById (String id) { if (id == null) throw new IllegalArgumentException (getMessage ("XD-000")); TreeWalker w = new TreeWalker (this); ElementEx element; while ((element = (ElementEx) w.getNextElement (null)) != null) { String idAttr = element.getIdAttributeName (); String value; if (idAttr == null) continue; value = element.getAttribute (idAttr); if (value.equals (id)) return element; } return null; } /** * @since DOM Level 2 */ public Node importNode(Node importedNode, boolean deep) throws DOMException { // First make a copy of the subtree, then change the ownerDocument // of the subtree. Node node = null; switch (importedNode.getNodeType()) { case ATTRIBUTE_NODE: node = importedNode.cloneNode(true); break; case DOCUMENT_FRAGMENT_NODE: if (deep) { node = importedNode.cloneNode(true); } else { node = new DocFragNode(); } break; case DOCUMENT_NODE: case DOCUMENT_TYPE_NODE: throw new DomEx(DomEx.NOT_SUPPORTED_ERR); case ELEMENT_NODE: node = ((ElementNode2) importedNode).createCopyForImportNode(deep); break; case ENTITY_NODE: node = importedNode.cloneNode(deep); break; case ENTITY_REFERENCE_NODE: node = importedNode.cloneNode(false); break; case NOTATION_NODE: case PROCESSING_INSTRUCTION_NODE: case TEXT_NODE: case CDATA_SECTION_NODE: case COMMENT_NODE: default: node = importedNode.cloneNode(false); break; } // Change the ownerDocument of subtree root and any children TreeWalker walker; NodeBase n; for (walker = new TreeWalker (node), n = (NodeBase) walker.getCurrent (); n != null; n = (NodeBase) walker.getNext ()) { n.setOwnerDocument (this); // Elements have associated attributes, which must // also have owners changed. if (n instanceof Element) { NamedNodeMap list = n.getAttributes (); int length = list.getLength (); for (int i = 0; i < length; i++) changeNodeOwner (list.item (i)); } } return node; } // // Represent document fragments other than the document itself. // (This class is primarily motivated for use with editors.) // static final class DocFragNode extends ParentNode implements DocumentFragment { // package private -- overrides base class method void checkChildType (int type) throws DOMException { switch (type) { case ELEMENT_NODE: case PROCESSING_INSTRUCTION_NODE: case COMMENT_NODE: case TEXT_NODE: case CDATA_SECTION_NODE: case ENTITY_REFERENCE_NODE: return; default: throw new DomEx (DomEx.HIERARCHY_REQUEST_ERR); } } public void writeXml (XmlWriteContext context) throws IOException { this.writeChildrenXml (context); } public Node getParentNode () { return null; } public void setParentNode (Node p) { if (p != null) throw new IllegalArgumentException (); } public short getNodeType () { return DOCUMENT_FRAGMENT_NODE; } public String getNodeName () { return ("#document-fragment"); } public Node cloneNode (boolean deep) { DocFragNode retval = new DocFragNode (); ((NodeBase)retval).setOwnerDocument ((XmlDocument)this.getOwnerDocument ()); if (deep) { Node node; for (int i = 0; (node = item (i)) != null; i++) { node = node.cloneNode (true); retval.appendChild (node); } } return retval; } } // // Represent entity references. // final static class EntityRefNode extends ParentNode implements EntityReference { private String entity; EntityRefNode (String name) { if (name == null) throw new IllegalArgumentException (getMessage ("XD-002")); entity = name; } // package private -- overrides base class method void checkChildType (int type) throws DOMException { switch (type) { case ELEMENT_NODE: case PROCESSING_INSTRUCTION_NODE: case COMMENT_NODE: case TEXT_NODE: case CDATA_SECTION_NODE: case ENTITY_REFERENCE_NODE: return; default: throw new DomEx (DomEx.HIERARCHY_REQUEST_ERR); } } public void writeXml (XmlWriteContext context) throws IOException { if (!context.isEntityDeclared (entity)) throw new IOException (getMessage ("XD-003", new Object[] { entity })); Writer out = context.getWriter (); out.write ('&'); out.write (entity); out.write (';'); } public short getNodeType () { return ENTITY_REFERENCE_NODE; } public String getNodeName () { return entity; } public Node cloneNode (boolean deep) { EntityRefNode retval = new EntityRefNode (entity); ((NodeBase)retval).setOwnerDocument(( XmlDocument)this.getOwnerDocument ()); if (deep) { Node node; for (int i = 0; (node = item (i)) != null; i++) { node = node.cloneNode (true); retval.appendChild (node); } // XXX //throw new RuntimeException (getMessage ("XD-001")); } return retval; } } class ExtWriteContext extends XmlWriteContext { ExtWriteContext (Writer out) { super (out); } ExtWriteContext (Writer out, int level) { super (out, level); } public boolean isEntityDeclared (String name) { if (super.isEntityDeclared (name)) return true; DocumentType doctype = getDoctype (); if (doctype == null) return false; else return doctype.getEntities ().getNamedItem (name) != null; } } static class Catalog extends MessageCatalog { Catalog () { super (Catalog.class); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?