domdocument.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,388 行 · 第 1/3 页
JAVA
1,388 行
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) { Node root = getDocumentElement(); return (root == null) ? false : root.isDefaultNamespace(namespaceURI); } public String lookupNamespaceURI(String prefix) { Node root = getDocumentElement(); return (root == null) ? null : root.lookupNamespaceURI(prefix); } public String getBaseURI() { return getDocumentURI(); /* Node root = getDocumentElement(); if (root != null) { NamedNodeMap attrs = root.getAttributes(); Node xmlBase = attrs.getNamedItemNS(XMLConstants.XML_NS_URI, "base"); if (xmlBase != null) { return xmlBase.getNodeValue(); } } return systemId; */ } public String getDocumentURI() { return systemId; } public void setDocumentURI(String documentURI) { systemId = documentURI; } public Node adoptNode(Node source) { switch (source.getNodeType()) { case DOCUMENT_NODE: case DOCUMENT_TYPE_NODE: throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR); case ENTITY_NODE: case NOTATION_NODE: throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); } if (source instanceof DomNode) { DomNode src = (DomNode) source; DomNode dst = src; if (dst.parent != null) { dst = (DomNode) dst.cloneNode(true); } dst.setOwner(this); src.notifyUserDataHandlers(UserDataHandler.NODE_ADOPTED, src, dst); return dst; } return null; } public DOMConfiguration getDomConfig() { if (config == null) { config = new DomDocumentConfiguration(); } return config; } public void normalizeDocument() { boolean save = building; building = true; normalizeNode(this); building = save; } void normalizeNode(DomNode node) { node.normalize(); if (config != null) { switch (node.nodeType) { case CDATA_SECTION_NODE: if (!config.cdataSections) { // replace CDATA section with text node Text text = createTextNode(node.getNodeValue()); node.parent.insertBefore(text, node); node.parent.removeChild(node); // merge adjacent text nodes String data = text.getWholeText(); node = (DomNode) text.replaceWholeText(data); } else if (config.splitCdataSections) { String value = node.getNodeValue(); int i = value.indexOf("]]>"); while (i != -1) { Node node2 = createCDATASection(value.substring(0, i)); node.parent.insertBefore(node2, node); value = value.substring(i + 3); node.setNodeValue(value); i = value.indexOf("]]>"); } } break; case COMMENT_NODE: if (!config.comments) { node.parent.removeChild(node); } break; case TEXT_NODE: if (!config.elementContentWhitespace && ((Text) node).isElementContentWhitespace()) { node.parent.removeChild(node); } break; case ENTITY_REFERENCE_NODE: if (!config.entities) { for (DomNode ctx = node.first; ctx != null; ) { DomNode ctxNext = ctx.next; node.parent.insertBefore(ctx, node); ctx = ctxNext; } node.parent.removeChild(node); } break; case ELEMENT_NODE: if (!config.namespaceDeclarations) { DomNamedNodeMap attrs = (DomNamedNodeMap) node.getAttributes(); boolean aro = attrs.readonly; attrs.readonly = false; // Ensure we can delete if necessary int len = attrs.getLength(); for (int i = 0; i < len; i++) { Node attr = attrs.item(i); String namespace = attr.getNamespaceURI(); if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespace)) { attrs.removeNamedItemNS(namespace, attr.getLocalName()); i--; len--; } } attrs.readonly = aro; } break; } } for (DomNode ctx = node.first; ctx != null; ) { DomNode ctxNext = ctx.next; normalizeNode(ctx); ctx = ctxNext; } } public Node renameNode(Node n, String namespaceURI, String qualifiedName) throws DOMException { if (n instanceof DomNsNode) { DomNsNode src = (DomNsNode) n; if (src == null) { throw new DomDOMException(DOMException.NOT_FOUND_ERR); } if (src.owner != this) { throw new DomDOMException(DOMException.WRONG_DOCUMENT_ERR, null, src, 0); } boolean xml11 = "1.1".equals(version); checkName(qualifiedName, xml11); int ci = qualifiedName.indexOf(':'); if ("".equals(namespaceURI)) { namespaceURI = null; } if (namespaceURI != null) { checkNCName(qualifiedName, xml11); String prefix = (ci == -1) ? "" : qualifiedName.substring(0, ci); if (XMLConstants.XML_NS_PREFIX.equals(prefix) && !XMLConstants.XML_NS_URI.equals(namespaceURI)) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "xml namespace must be " + XMLConstants.XML_NS_URI, src, 0); } else if (src.nodeType == ATTRIBUTE_NODE && (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) || XMLConstants.XMLNS_ATTRIBUTE.equals(qualifiedName)) && !XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI)) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "xmlns namespace must be " + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, src, 0); } if (XMLConstants.XML_NS_URI.equals(namespaceURI) && !XMLConstants.XML_NS_PREFIX.equals(prefix)) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "xml namespace must be " + XMLConstants.XML_NS_URI, src, 0); } else if (src.nodeType == ATTRIBUTE_NODE && XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(namespaceURI) && !(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) || XMLConstants.XMLNS_ATTRIBUTE.equals(qualifiedName))) { throw new DomDOMException(DOMException.NAMESPACE_ERR, "xmlns namespace must be " + XMLConstants.XMLNS_ATTRIBUTE_NS_URI, src, 0); } } src.setNodeName(qualifiedName); src.setNamespaceURI(namespaceURI); src.notifyUserDataHandlers(UserDataHandler.NODE_RENAMED, src, src); // TODO MutationNameEvents // DOMElementNameChanged or DOMAttributeNameChanged return src; } throw new DomDOMException(DOMException.NOT_SUPPORTED_ERR, null, n, 0); } // -- XPathEvaluator -- public XPathExpression createExpression(String expression, XPathNSResolver resolver) throws XPathException, DOMException { return new DomXPathExpression(this, expression, resolver); } public XPathNSResolver createNSResolver(Node nodeResolver) { return new DomXPathNSResolver(nodeResolver); } public Object evaluate(String expression, Node contextNode, XPathNSResolver resolver, short type, Object result) throws XPathException, DOMException { XPathExpression xpe = new DomXPathExpression(this, expression, resolver); return xpe.evaluate(contextNode, type, result); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?