📄 domdocument.java
字号:
String data) { if (checkingCharacters) { boolean xml11 = "1.1".equals(version); checkName(target, xml11); if ("xml".equalsIgnoreCase(target)) { throw new DomDOMException(DOMException.SYNTAX_ERR, "illegal PI target name", this, 0); } checkChar(data, xml11); } return new DomProcessingInstruction(this, target, data); } /** * <b>DOM L1</b> * Returns a newly created attribute with the specified name. */ public Attr createAttribute(String name) { if (checkingCharacters) { checkName(name, "1.1".equals(version)); } if (name.startsWith("xml:")) { return createAttributeNS(XMLConstants.XML_NS_URI, name); } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || name.startsWith("xmlns:")) { return createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, name); } else { DomAttr ret = new DomAttr(this, null, name); ret.localName = null; return ret; } } /** * <b>DOM L2</b> * Returns a newly created attribute with the specified name * and namespace information. */ public Attr createAttributeNS(String namespaceURI, String name) { if (checkingCharacters) { checkNCName(name, "1.1".equals(version)); } if ("".equals(namespaceURI)) { namespaceURI = null; } if (name.startsWith ("xml:")) { if (namespaceURI == null) { namespaceURI = XMLConstants.XML_NS_URI; } else if (!XMLConstants.XML_NS_URI.equals(namespaceURI)) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "xml namespace is always " + XMLConstants.XML_NS_URI, this, 0); } } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || name.startsWith("xmlns:")) { if (namespaceURI == null) { namespaceURI = XMLConstants.XMLNS_ATTRIBUTE_NS_URI; } else if (!XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "xmlns namespace must be " + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, this, 0); } } else if (namespaceURI == null && name.indexOf(':') != -1) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "prefixed name needs a URI: " + name, this, 0); } return new DomAttr(this, namespaceURI, name); } /** * <b>DOM L1</b> * Returns a newly created reference to the specified entity. * The caller should populate this with the appropriate children * and then mark it as readonly. * * @see DomNode#makeReadonly */ public EntityReference createEntityReference(String name) { DomEntityReference ret = new DomEntityReference(this, name); DocumentType doctype = getDoctype(); if (doctype != null) { DomEntity ent = (DomEntity) doctype.getEntities().getNamedItem(name); if (ent != null) { for (DomNode ctx = ent.first; ctx != null; ctx = ctx.next) { ret.appendChild(ctx.cloneNode(true)); } } } ret.makeReadonly(); return ret; } /** * <b>DOM L2</b> * Makes a copy of the specified node, with all nodes "owned" by * this document and with children optionally copied. This type * of standard utility has become, well, a standard utility. * * <p> Note that EntityReference nodes created through this method (either * directly, or recursively) never have children, and that there is no * portable way to associate them with such children. * * <p> Note also that there is no requirement that the specified node * be associated with a different document. This differs from the * <em>cloneNode</em> operation in that the node itself is not given * an opportunity to participate, so that any information managed * by node subclasses will be lost. */ public Node importNode(Node src, boolean deep) { Node dst = null; switch (src.getNodeType()) { case TEXT_NODE: dst = createTextNode(src.getNodeValue()); break; case CDATA_SECTION_NODE: dst = createCDATASection(src.getNodeValue()); break; case COMMENT_NODE: dst = createComment(src.getNodeValue()); break; case PROCESSING_INSTRUCTION_NODE: dst = createProcessingInstruction(src.getNodeName(), src.getNodeValue()); break; case NOTATION_NODE: // NOTE: There's no standard way to create // these, or add them to a doctype. Useless. Notation notation = (Notation) src; dst = new DomNotation(this, notation.getNodeName(), notation.getPublicId(), notation.getSystemId()); break; case ENTITY_NODE: // NOTE: There's no standard way to create // these, or add them to a doctype. Useless. Entity entity = (Entity) src; dst = new DomEntity(this, entity.getNodeName(), entity.getPublicId(), entity.getSystemId(), entity.getNotationName()); if (deep) { for (Node ctx = src.getFirstChild(); ctx != null; ctx = ctx.getNextSibling()) { dst.appendChild(importNode(ctx, deep)); } } break; case ENTITY_REFERENCE_NODE: dst = createEntityReference(src.getNodeName()); break; case DOCUMENT_FRAGMENT_NODE: dst = new DomDocumentFragment(this); if (deep) { for (Node ctx = src.getFirstChild(); ctx != null; ctx = ctx.getNextSibling()) { dst.appendChild(importNode(ctx, deep)); } } break; case ATTRIBUTE_NODE: String attr_nsuri = src.getNamespaceURI(); if (attr_nsuri != null) { dst = createAttributeNS(attr_nsuri, src.getNodeName()); } else { dst = createAttribute(src.getNodeName()); } // this is _always_ done regardless of "deep" setting for (Node ctx = src.getFirstChild(); ctx != null; ctx = ctx.getNextSibling()) { dst.appendChild(importNode(ctx, false)); } break; case ELEMENT_NODE: String elem_nsuri = src.getNamespaceURI(); if (elem_nsuri != null) { dst = createElementNS(elem_nsuri, src.getNodeName()); } else { dst = createElement(src.getNodeName()); } NamedNodeMap srcAttrs = src.getAttributes(); NamedNodeMap dstAttrs = dst.getAttributes(); int len = srcAttrs.getLength(); for (int i = 0; i < len; i++) { Attr a = (Attr) srcAttrs.item(i); Attr dflt; // maybe update defaulted attributes dflt = (Attr) dstAttrs.getNamedItem(a.getNodeName()); if (dflt != null) { String newval = a.getNodeValue(); if (!dflt.getNodeValue().equals(newval) || a.getSpecified () == true) { dflt.setNodeValue (newval); } continue; } dstAttrs.setNamedItem((Attr) importNode(a, false)); } if (deep) { for (Node ctx = src.getFirstChild(); ctx != null; ctx = ctx.getNextSibling()) { dst.appendChild(importNode(ctx, true)); } } break; // can't import document or doctype nodes case DOCUMENT_NODE: case DOCUMENT_TYPE_NODE: // FALLTHROUGH // can't import unrecognized or nonstandard nodes default: throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, null, src, 0); } // FIXME cleanup a bit -- for deep copies, copy those // children in one place, here (code sharing is healthy) if (src instanceof DomNode) { ((DomNode) src).notifyUserDataHandlers(UserDataHandler.NODE_IMPORTED, src, dst); } return dst; } /** * <b>DOM L2 (Traversal)</b> * Returns a newly created node iterator. Don't forget to detach * this iterator when you're done using it! * * @see DomIterator */ public NodeIterator createNodeIterator(Node root, int whatToShow, NodeFilter filter, boolean expandEntities) { return new DomNodeIterator(root, whatToShow, filter, expandEntities, false); } public TreeWalker createTreeWalker(Node root, int whatToShow, NodeFilter filter, boolean expandEntities) { return new DomNodeIterator(root, whatToShow, filter, expandEntities, true); } // DOM Level 3 methods /** * DOM L3 */ public String getInputEncoding() { return inputEncoding; } public void setInputEncoding(String inputEncoding) { this.inputEncoding = inputEncoding; } /** * DOM L3 */ public String getXmlEncoding() { return encoding; } public void setXmlEncoding(String encoding) { this.encoding = encoding; } public boolean getXmlStandalone() { return standalone; } public void setXmlStandalone(boolean xmlStandalone) { standalone = xmlStandalone; } public String getXmlVersion() { return version; } public void setXmlVersion(String xmlVersion) { if (xmlVersion == null) { xmlVersion = "1.0"; } if ("1.0".equals(xmlVersion) || "1.1".equals(xmlVersion)) { version = xmlVersion; } else { throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); } } public boolean getStrictErrorChecking() { return checkingCharacters; } public void setStrictErrorChecking(boolean strictErrorChecking) { checkingCharacters = strictErrorChecking; } public String lookupPrefix(String namespaceURI) { Node root = getDocumentElement(); return (root == null) ? null : root.lookupPrefix(namespaceURI); } public boolean isDefaultNamespace(String namespaceURI) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -