coredocumentimpl.java
来自「JAVA 所有包」· Java 代码 · 共 1,776 行 · 第 1/5 页
JAVA
1,776 行
/** * Since a Document may contain at most one top-level Element child, * and at most one DocumentType declaraction, we need to subclass our * add-children methods to implement this constraint. * Since appendChild() is implemented as insertBefore(,null), * altering the latter fixes both. * <p> * While I'm doing so, I've taken advantage of the opportunity to * cache documentElement and docType so we don't have to * search for them. * * REVISIT: According to the spec it is not allowed to alter neither the * document element nor the document type in any way */ public Node insertBefore(Node newChild, Node refChild) throws DOMException { // Only one such child permitted int type = newChild.getNodeType(); if (errorChecking) { if((type == Node.ELEMENT_NODE && docElement != null) || (type == Node.DOCUMENT_TYPE_NODE && docType != null)) { String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null); throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg); } } // Adopt orphan doctypes if (newChild.getOwnerDocument() == null && newChild instanceof DocumentTypeImpl) { ((DocumentTypeImpl) newChild).ownerDocument = this; } super.insertBefore(newChild,refChild); // If insert succeeded, cache the kid appropriately if (type == Node.ELEMENT_NODE) { docElement = (ElementImpl)newChild; } else if (type == Node.DOCUMENT_TYPE_NODE) { docType = (DocumentTypeImpl)newChild; } return newChild; } // insertBefore(Node,Node):Node /** * Since insertBefore caches the docElement (and, currently, docType), * removeChild has to know how to undo the cache * * REVISIT: According to the spec it is not allowed to alter neither the * document element nor the document type in any way */ public Node removeChild(Node oldChild) throws DOMException { super.removeChild(oldChild); // If remove succeeded, un-cache the kid appropriately int type = oldChild.getNodeType(); if(type == Node.ELEMENT_NODE) { docElement = null; } else if (type == Node.DOCUMENT_TYPE_NODE) { docType = null; } return oldChild; } // removeChild(Node):Node /** * Since we cache the docElement (and, currently, docType), * replaceChild has to update the cache * * REVISIT: According to the spec it is not allowed to alter neither the * document element nor the document type in any way */ public Node replaceChild(Node newChild, Node oldChild) throws DOMException { // Adopt orphan doctypes if (newChild.getOwnerDocument() == null && newChild instanceof DocumentTypeImpl) { ((DocumentTypeImpl) newChild).ownerDocument = this; } if (errorChecking &&((docType != null && oldChild.getNodeType() != Node.DOCUMENT_TYPE_NODE && newChild.getNodeType() == Node.DOCUMENT_TYPE_NODE) || (docElement != null && oldChild.getNodeType() != Node.ELEMENT_NODE && newChild.getNodeType() == Node.ELEMENT_NODE))) { throw new DOMException( DOMException.HIERARCHY_REQUEST_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null)); } super.replaceChild(newChild, oldChild); int type = oldChild.getNodeType(); if(type == Node.ELEMENT_NODE) { docElement = (ElementImpl)newChild; } else if (type == Node.DOCUMENT_TYPE_NODE) { docType = (DocumentTypeImpl)newChild; } return oldChild; } // replaceChild(Node,Node):Node /* * Get Node text content * @since DOM Level 3 */ public String getTextContent() throws DOMException { return null; } /* * Set Node text content * @since DOM Level 3 */ public void setTextContent(String textContent) throws DOMException { // no-op } /** * @since DOM Level 3 */ public Object getFeature(String feature, String version) { boolean anyVersion = version == null || version.length() == 0; // if a plus sign "+" is prepended to any feature name, implementations // are considered in which the specified feature may not be directly // castable DOMImplementation.getFeature(feature, version). Without a // plus, only features whose interfaces are directly castable are // considered. if ((feature.equalsIgnoreCase("+XPath")) && (anyVersion || version.equals("3.0"))) { // If an XPathEvaluator was created previously // return it otherwise create a new one. if (fXPathEvaluator != null) { return fXPathEvaluator; } try { Class xpathClass = ObjectFactory.findProviderClass( "com.sun.org.apache.xpath.internal.domapi.XPathEvaluatorImpl", ObjectFactory.findClassLoader(), true); Constructor xpathClassConstr = xpathClass.getConstructor(new Class[] { Document.class }); // Check if the DOM XPath implementation implements // the interface org.w3c.dom.XPathEvaluator Class interfaces[] = xpathClass.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { if (interfaces[i].getName().equals( "org.w3c.dom.xpath.XPathEvaluator")) { fXPathEvaluator = xpathClassConstr.newInstance(new Object[] { this }); return fXPathEvaluator; } } return null; } catch (Exception e) { return null; } } return super.getFeature(feature, version); } // // Document methods // // factory methods /** * Factory method; creates an Attribute having this Document as its * OwnerDoc. * * @param name The name of the attribute. Note that the attribute's value is * _not_ established at the factory; remember to set it! * * @throws DOMException(INVALID_NAME_ERR) * if the attribute name is not acceptable. */ public Attr createAttribute(String name) throws DOMException { if (errorChecking && !isXMLName(name,xml11Version)) { String msg = DOMMessageFormatter.formatMessage( DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); throw new DOMException(DOMException.INVALID_CHARACTER_ERR, msg); } return new AttrImpl(this, name); } // createAttribute(String):Attr /** * Factory method; creates a CDATASection having this Document as * its OwnerDoc. * * @param data The initial contents of the CDATA * * @throws DOMException(NOT_SUPPORTED_ERR) for HTML documents. (HTML * not yet implemented.) */ public CDATASection createCDATASection(String data) throws DOMException { return new CDATASectionImpl(this, data); } /** * Factory method; creates a Comment having this Document as its * OwnerDoc. * * @param data The initial contents of the Comment. */ public Comment createComment(String data) { return new CommentImpl(this, data); } /** * Factory method; creates a DocumentFragment having this Document * as its OwnerDoc. */ public DocumentFragment createDocumentFragment() { return new DocumentFragmentImpl(this); } /** * Factory method; creates an Element having this Document * as its OwnerDoc. * * @param tagName The name of the element type to instantiate. For * XML, this is case-sensitive. For HTML, the tagName parameter may * be provided in any case, but it must be mapped to the canonical * uppercase form by the DOM implementation. * * @throws DOMException(INVALID_NAME_ERR) if the tag name is not * acceptable. */ public Element createElement(String tagName) throws DOMException { if (errorChecking && !isXMLName(tagName,xml11Version)) { String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); throw new DOMException(DOMException.INVALID_CHARACTER_ERR, msg); } return new ElementImpl(this, tagName); } // createElement(String):Element /** * Factory method; creates an EntityReference having this Document * as its OwnerDoc. * * @param name The name of the Entity we wish to refer to * * @throws DOMException(NOT_SUPPORTED_ERR) for HTML documents, where * nonstandard entities are not permitted. (HTML not yet * implemented.) */ public EntityReference createEntityReference(String name) throws DOMException { if (errorChecking && !isXMLName(name,xml11Version)) { String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); throw new DOMException(DOMException.INVALID_CHARACTER_ERR, msg); } return new EntityReferenceImpl(this, name); } // createEntityReference(String):EntityReference /** * Factory method; creates a ProcessingInstruction having this Document * as its OwnerDoc. * * @param target The target "processor channel" * @param data Parameter string to be passed to the target. * * @throws DOMException(INVALID_NAME_ERR) if the target name is not * acceptable. * * @throws DOMException(NOT_SUPPORTED_ERR) for HTML documents. (HTML * not yet implemented.) */ public ProcessingInstruction createProcessingInstruction(String target, String data) throws DOMException { if (errorChecking && !isXMLName(target,xml11Version)) { String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); throw new DOMException(DOMException.INVALID_CHARACTER_ERR, msg); } return new ProcessingInstructionImpl(this, target, data); } // createProcessingInstruction(String,String):ProcessingInstruction /** * Factory method; creates a Text node having this Document as its * OwnerDoc. * * @param data The initial contents of the Text. */ public Text createTextNode(String data) { return new TextImpl(this, data); } // other document methods /** * For XML, this provides access to the Document Type Definition. * For HTML documents, and XML documents which don't specify a DTD, * it will be null. */ public DocumentType getDoctype() { if (needsSyncChildren()) { synchronizeChildren(); } return docType; } /** * Convenience method, allowing direct access to the child node * which is considered the root of the actual document content. For * HTML, where it is legal to have more than one Element at the top * level of the document, we pick the one with the tagName * "HTML". For XML there should be only one top-level * * (HTML not yet supported.) */ public Element getDocumentElement() { if (needsSyncChildren()) { synchronizeChildren(); } return docElement; } /** * Return a <em>live</em> collection of all descendent Elements (not just * immediate children) having the specified tag name. * * @param tagname The type of Element we want to gather. "*" will be * taken as a wildcard, meaning "all elements in the document." * * @see DeepNodeListImpl */ public NodeList getElementsByTagName(String tagname) { return new DeepNodeListImpl(this,tagname); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?