coredocumentimpl.java
来自「JAVA 所有包」· Java 代码 · 共 1,776 行 · 第 1/5 页
JAVA
1,776 行
/* * Copyright 1999-2002,2004,2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.sun.org.apache.xerces.internal.dom;import java.lang.reflect.Constructor;import java.util.Enumeration;import java.util.Hashtable;import com.sun.org.apache.xerces.internal.util.URI;import org.w3c.dom.DOMConfiguration;import org.w3c.dom.UserDataHandler;import com.sun.org.apache.xerces.internal.util.XMLChar;import com.sun.org.apache.xerces.internal.util.XML11Char;import com.sun.org.apache.xerces.internal.xni.NamespaceContext;import org.w3c.dom.Attr;import org.w3c.dom.CDATASection;import org.w3c.dom.Comment;import org.w3c.dom.DOMException;import org.w3c.dom.DOMImplementation;import org.w3c.dom.Document;import org.w3c.dom.DocumentFragment;import org.w3c.dom.DocumentType;import org.w3c.dom.Element;import org.w3c.dom.Entity;import org.w3c.dom.EntityReference;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Notation;import org.w3c.dom.ProcessingInstruction;import org.w3c.dom.Text;import org.w3c.dom.events.Event;import org.w3c.dom.events.EventListener;import org.w3c.dom.ls.DOMImplementationLS;import org.w3c.dom.ls.LSSerializer;/** * The Document interface represents the entire HTML or XML document. * Conceptually, it is the root of the document tree, and provides the * primary access to the document's data. * <P> * Since elements, text nodes, comments, processing instructions, * etc. cannot exist outside the context of a Document, the Document * interface also contains the factory methods needed to create these * objects. The Node objects created have a ownerDocument attribute * which associates them with the Document within whose context they * were created. * <p> * The CoreDocumentImpl class only implements the DOM Core. Additional modules * are supported by the more complete DocumentImpl subclass. * <p> * <b>Note:</b> When any node in the document is serialized, the * entire document is serialized along with it. * * @xerces.internal * * @author Arnaud Le Hors, IBM * @author Joe Kesselman, IBM * @author Andy Clark, IBM * @author Ralf Pfeiffer, IBM * @version $Id: CoreDocumentImpl.java,v 1.3 2005/09/26 13:02:11 sunithareddy Exp $ * @since PR-DOM-Level-1-19980818. */public class CoreDocumentImplextends ParentNode implements Document { /**TODO:: * 1. Change XML11Char method names similar to XMLChar. That will prevent lot * of dirty version checking code. * * 2. IMO during cloneNode qname/isXMLName check should not be made. */ // // Constants // /** Serialization version. */ static final long serialVersionUID = 0; // // Data // // document information /** Document type. */ protected DocumentTypeImpl docType; /** Document element. */ protected ElementImpl docElement; /** NodeListCache free list */ transient NodeListCache fFreeNLCache; /**Experimental DOM Level 3 feature: Document encoding */ protected String encoding; /**Experimental DOM Level 3 feature: Document actualEncoding */ protected String actualEncoding; /**Experimental DOM Level 3 feature: Document version */ protected String version; /**Experimental DOM Level 3 feature: Document standalone */ protected boolean standalone; /**Experimental DOM Level 3 feature: documentURI */ protected String fDocumentURI; //Revisit :: change to a better data structure. /** Table for user data attached to this document nodes. */ protected Hashtable userData; /** Identifiers. */ protected Hashtable identifiers; // DOM Level 3: normalizeDocument transient DOMNormalizer domNormalizer = null; transient DOMConfigurationImpl fConfiguration = null; // support of XPath API transient Object fXPathEvaluator = null; /** Table for quick check of child insertion. */ private final static int[] kidOK; /** * Number of alterations made to this document since its creation. * Serves as a "dirty bit" so that live objects such as NodeList can * recognize when an alteration has been made and discard its cached * state information. * <p> * Any method that alters the tree structure MUST cause or be * accompanied by a call to changed(), to inform it that any outstanding * NodeLists may have to be updated. * <p> * (Required because NodeList is simultaneously "live" and integer- * indexed -- a bad decision in the DOM's design.) * <p> * Note that changes which do not affect the tree's structure -- changing * the node's name, for example -- do _not_ have to call changed(). * <p> * Alternative implementation would be to use a cryptographic * Digest value rather than a count. This would have the advantage that * "harmless" changes (those producing equal() trees) would not force * NodeList to resynchronize. Disadvantage is that it's slightly more prone * to "false negatives", though that's the difference between "wildly * unlikely" and "absurdly unlikely". IF we start maintaining digests, * we should consider taking advantage of them. * * Note: This used to be done a node basis, so that we knew what * subtree changed. But since only DeepNodeList really use this today, * the gain appears to be really small compared to the cost of having * an int on every (parent) node plus having to walk up the tree all the * way to the root to mark the branch as changed everytime a node is * changed. * So we now have a single counter global to the document. It means that * some objects may flush their cache more often than necessary, but this * makes nodes smaller and only the document needs to be marked as changed. */ protected int changes = 0; // experimental /** Allow grammar access. */ protected boolean allowGrammarAccess; /** Bypass error checking. */ protected boolean errorChecking = true; //Did version change at any point when the document was created ? //this field helps us to optimize when normalizingDocument. protected boolean xmlVersionChanged = false ; /** The following are required for compareDocumentPosition */ // Document number. Documents are ordered across the implementation using // positive integer values. Documents are assigned numbers on demand. private int documentNumber=0; // Node counter and table. Used to assign numbers to nodes for this // document. Node number values are negative integers. Nodes are // assigned numbers on demand. private int nodeCounter = 0; private Hashtable nodeTable; private boolean xml11Version = false; //by default 1.0 // // Static initialization // static { kidOK = new int[13]; kidOK[DOCUMENT_NODE] = 1 << ELEMENT_NODE | 1 << PROCESSING_INSTRUCTION_NODE | 1 << COMMENT_NODE | 1 << DOCUMENT_TYPE_NODE; kidOK[DOCUMENT_FRAGMENT_NODE] = kidOK[ENTITY_NODE] = kidOK[ENTITY_REFERENCE_NODE] = kidOK[ELEMENT_NODE] = 1 << ELEMENT_NODE | 1 << PROCESSING_INSTRUCTION_NODE | 1 << COMMENT_NODE | 1 << TEXT_NODE | 1 << CDATA_SECTION_NODE | 1 << ENTITY_REFERENCE_NODE ; kidOK[ATTRIBUTE_NODE] = 1 << TEXT_NODE | 1 << ENTITY_REFERENCE_NODE; kidOK[DOCUMENT_TYPE_NODE] = kidOK[PROCESSING_INSTRUCTION_NODE] = kidOK[COMMENT_NODE] = kidOK[TEXT_NODE] = kidOK[CDATA_SECTION_NODE] = kidOK[NOTATION_NODE] = 0; } // static // // Constructors // /** * NON-DOM: Actually creating a Document is outside the DOM's spec, * since it has to operate in terms of a particular implementation. */ public CoreDocumentImpl() { this(false); } /** Constructor. */ public CoreDocumentImpl(boolean grammarAccess) { super(null); ownerDocument = this; allowGrammarAccess = grammarAccess; } /** * For DOM2 support. * The createDocument factory method is in DOMImplementation. */ public CoreDocumentImpl(DocumentType doctype) { this(doctype, false); } /** For DOM2 support. */ public CoreDocumentImpl(DocumentType doctype, boolean grammarAccess) { this(grammarAccess); if (doctype != null) { DocumentTypeImpl doctypeImpl; try { doctypeImpl = (DocumentTypeImpl) doctype; } catch (ClassCastException e) { String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null); throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg); } doctypeImpl.ownerDocument = this; appendChild(doctype); } } // // Node methods // // even though ownerDocument refers to this in this implementation // the DOM Level 2 spec says it must be null, so make it appear so final public Document getOwnerDocument() { return null; } /** Returns the node type. */ public short getNodeType() { return Node.DOCUMENT_NODE; } /** Returns the node name. */ public String getNodeName() { return "#document"; } /** * Deep-clone a document, including fixing ownerDoc for the cloned * children. Note that this requires bypassing the WRONG_DOCUMENT_ERR * protection. I've chosen to implement it by calling importNode * which is DOM Level 2. * * @return org.w3c.dom.Node * @param deep boolean, iff true replicate children */ public Node cloneNode(boolean deep) { CoreDocumentImpl newdoc = new CoreDocumentImpl(); callUserDataHandlers(this, newdoc, UserDataHandler.NODE_CLONED); cloneNode(newdoc, deep); return newdoc; } // cloneNode(boolean):Node /** * internal method to share code with subclass **/ protected void cloneNode(CoreDocumentImpl newdoc, boolean deep) { // clone the children by importing them if (needsSyncChildren()) { synchronizeChildren(); } if (deep) { Hashtable reversedIdentifiers = null; if (identifiers != null) { // Build a reverse mapping from element to identifier. reversedIdentifiers = new Hashtable(); Enumeration elementIds = identifiers.keys(); while (elementIds.hasMoreElements()) { Object elementId = elementIds.nextElement(); reversedIdentifiers.put(identifiers.get(elementId), elementId); } } // Copy children into new document. for (ChildNode kid = firstChild; kid != null; kid = kid.nextSibling) { newdoc.appendChild(newdoc.importNode(kid, true, true, reversedIdentifiers)); } } // experimental newdoc.allowGrammarAccess = allowGrammarAccess; newdoc.errorChecking = errorChecking; } // cloneNode(CoreDocumentImpl,boolean):void
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?