coredocumentimpl.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,776 行 · 第 1/5 页
JAVA
1,776 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, International * Business Machines, Inc., http://www.apache.org. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package com.sun.org.apache.xerces.internal.dom;import java.io.Serializable;import java.util.Enumeration;import java.util.Hashtable;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. * * @author Arnaud Le Hors, IBM * @author Joe Kesselman, IBM * @author Andy Clark, IBM * @author Ralf Pfeiffer, IBM * @version $Id: CoreDocumentImpl.java,v 1.71 2004/04/26 14:44:56 venu 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
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?