domdocument.java
来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 790 行 · 第 1/2 页
JAVA
790 行
if (checkingCharacters) verifyXmlName (name); if (name.startsWith ("xml:")) element = createElementNS (null, name); else element = new DomElement (this, null, name); defaultAttributes (element, name); return element; } /** * <b>DOM L2</b> * Returns a newly created element with the specified name * and namespace information. */ public Element createElementNS (String namespaceURI, String name) { if (checkingCharacters) verifyNamespaceName (name); if ("".equals (namespaceURI)) namespaceURI = null; if (name.startsWith ("xml:")) { if (namespaceURI != null && !xmlNamespace.equals (namespaceURI)) throw new DomEx (DomEx.NAMESPACE_ERR, "xml namespace is always " + xmlNamespace, this, 0); namespaceURI = xmlNamespace; } else if (name.startsWith ("xmlns:")) throw new DomEx (DomEx.NAMESPACE_ERR, "xmlns is reserved", this, 0); else if (namespaceURI == null && name.indexOf (':') != -1) throw new DomEx (DomEx.NAMESPACE_ERR, "prefixed name needs a URI", this, 0); Element element = new DomElement (this, namespaceURI, name); defaultAttributes (element, name); return element; } private void defaultAttributes (Element element, String name) { DomDoctype doctype = (DomDoctype) getDoctype (); DomDoctype.ElementInfo info; if (doctype == null) return; // default any attributes that need it info = doctype.getElementInfo (name); for (Enumeration e = info.keys (); e.hasMoreElements (); /* NOP */) { String attr = (String) e.nextElement (); DomAttr node = (DomAttr) createAttribute (attr); node.setValue ((String) info.get (attr)); node.setSpecified (false); element.setAttributeNode (node); } } /** * <b>DOM L1</b> * Returns a newly created document fragment. */ public DocumentFragment createDocumentFragment () { return new DomFragment (this); } /** * <b>DOM L1</b> * Returns a newly created text node with the specified value. */ public Text createTextNode (String value) { if (checkingCharacters) verifyXmlCharacters (value); return new DomText (this, value); } /** * Returns a newly created text node with the specified value. */ public Text createTextNode (char buf [], int off, int len) { if (checkingCharacters) verifyXmlCharacters (buf, off, len); return new DomText (this, buf, off, len); } /** * <b>DOM L1</b> * Returns a newly created comment node with the specified value. */ public Comment createComment (String value) { if (checkingCharacters) verifyXmlCharacters (value); return new DomComment (this, value); } /** * <b>DOM L1</b> * Returns a newly created CDATA section node with the specified value. */ public CDATASection createCDATASection (String value) { if (checkingCharacters) verifyXmlCharacters (value); return new DomCDATA (this, value); } /** * Returns a newly created CDATA section node with the specified value. */ public CDATASection createCDATASection (char buf [], int off, int len) { if (checkingCharacters) verifyXmlCharacters (buf, off, len); return new DomCDATA (this, buf, off, len); } /** * <b>DOM L1</b> * Returns a newly created processing instruction. */ public ProcessingInstruction createProcessingInstruction ( String target, String data ) { if (checkingCharacters) { verifyXmlName (target); verifyXmlCharacters (data); if ("xml".equalsIgnoreCase (target)) throw new DomEx (DomEx.SYNTAX_ERR, "illegal PI target name", this, 0); } return new DomPI (this, target, data); } /** * <b>DOM L1</b> * Returns a newly created attribute with the specified name. */ public Attr createAttribute (String name) { if (checkingCharacters) verifyXmlName (name); if (name.startsWith ("xml:") || name.startsWith ("xmlns:")) return createAttributeNS (null, name); else return new DomAttr (this, null, name); } /** * <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) verifyNamespaceName (name); if ("".equals (namespaceURI)) namespaceURI = null; if (name.startsWith ("xml:")) { if (namespaceURI == null) namespaceURI = xmlNamespace; else if (!xmlNamespace.equals (namespaceURI)) throw new DomEx (DomEx.NAMESPACE_ERR, "xml namespace is always " + xmlNamespace, this, 0); namespaceURI = xmlNamespace; } else if (name.startsWith ("xmlns:") || name.equals ("xmlns")) { if (!xmlnsURI.equals (namespaceURI)) throw new DomEx (DomEx.NAMESPACE_ERR, "xmlns is reserved", this, 0); namespaceURI = xmlnsURI; } else if (namespaceURI == null && name.indexOf (':') != -1) throw new DomEx (DomEx.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 retval; if (checkingCharacters) verifyXmlName (name); retval = new DomEntityReference (this, name); // // If we have such an entity, it's allowed that one arrange that // the children of this reference be "the same as" (in an undefined // sense of "same", clearly not identity) the children of the entity. // That can be immediate or deferred. It's also allowed that nothing // be done -- we take that option here. // retval.makeReadonly (); return retval; } /** * <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 copiedNode, boolean deep) { switch (copiedNode.getNodeType ()) { case TEXT_NODE: return createTextNode (copiedNode.getNodeValue ()); case CDATA_SECTION_NODE: return createCDATASection (copiedNode.getNodeValue ()); case COMMENT_NODE: return createComment (copiedNode.getNodeValue ()); case PROCESSING_INSTRUCTION_NODE: return createProcessingInstruction ( copiedNode.getNodeName (), copiedNode.getNodeValue ()); case NOTATION_NODE: { // NOTE: There's no standard way to create // these, or add them to a doctype. Useless. Notation node = (Notation) copiedNode; return new DomNotation (this, node.getNodeName (), node.getPublicId (), node.getSystemId ()); } case ENTITY_NODE: { // NOTE: There's no standard way to create // these, or add them to a doctype. Useless. Entity node = (Entity) copiedNode; // FIXME if "deep", can/should copy children! return new DomEntity (this, node.getNodeName (), node.getPublicId (), node.getSystemId (), node.getNotationName ()); } case ENTITY_REFERENCE_NODE: return createEntityReference (copiedNode.getNodeName ()); case DOCUMENT_FRAGMENT_NODE: { DocumentFragment node = createDocumentFragment (); if (deep) { NodeList kids = copiedNode.getChildNodes (); int len = kids.getLength (); for (int i = 0; i < len; i++) node.appendChild ( importNode (kids.item (i), deep)); } return node; } case ATTRIBUTE_NODE: { DomAttr retval; String name = copiedNode.getNodeName (); String ns = copiedNode.getNamespaceURI (); NodeList kids = copiedNode.getChildNodes (); int len = kids.getLength (); if (ns != null) retval = (DomAttr) createAttributeNS (ns, name); else retval = (DomAttr) createAttribute (name); // this is _always_ done regardless of "deep" setting for (int i = 0; i < len; i++) retval.appendChild (importNode (kids.item (i), false)); return retval; } case ELEMENT_NODE: { DomElement retval; String name = copiedNode.getNodeName (); String ns = copiedNode.getNamespaceURI (); NamedNodeMap attrs = copiedNode.getAttributes (); int len = attrs.getLength (); if (ns != null) retval = (DomElement) createElementNS (ns, name); else retval = (DomElement) createElement (name); for (int i = 0; i < len; i++) { Attr attr = (Attr) attrs.item (i); Attr dflt; // maybe update defaulted attributes dflt = retval.getAttributeNode (attr.getNodeName ()); if (dflt != null) { String newval = attr.getNodeValue (); if (!dflt.getNodeValue ().equals (newval) || attr.getSpecified () == true) dflt.setNodeValue (newval); continue; } retval.setAttributeNode ((Attr) importNode (attr, false)); } if (!deep) return retval; NodeList kids = copiedNode.getChildNodes (); len = kids.getLength (); for (int i = 0; i < len; i++) retval.appendChild (importNode (kids.item (i), true)); return retval; } // 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 DomEx (DomEx.NOT_SUPPORTED_ERR, null, copiedNode, 0); } // FIXME cleanup a bit -- for deep copies, copy those // children in one place, here (code sharing is healthy) } /** * <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 DomIterator (root, whatToShow, filter, expandEntities); } public TreeWalker createTreeWalker ( Node root, int whatToShow, NodeFilter filter, boolean entityReferenceExpansion ) {nyi (); // FIXME createTreeWalker return null; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?