nodeimpl.java
来自「JAVA 所有包」· Java 代码 · 共 1,687 行 · 第 1/5 页
JAVA
1,687 行
/* * Copyright 1999-2002,2004 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.io.IOException;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.Hashtable;import org.w3c.dom.UserDataHandler;import org.w3c.dom.DOMException;import org.w3c.dom.Document;import org.w3c.dom.DocumentType;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.events.Event;import org.w3c.dom.events.EventListener;import org.w3c.dom.events.EventTarget;/** * NodeImpl provides the basic structure of a DOM tree. It is never used * directly, but instead is subclassed to add type and data * information, and additional methods, appropriate to each node of * the tree. Only its subclasses should be instantiated -- and those, * with the exception of Document itself, only through a specific * Document's factory methods. * <P> * The Node interface provides shared behaviors such as siblings and * children, both for consistancy and so that the most common tree * operations may be performed without constantly having to downcast * to specific node types. When there is no obvious mapping for one of * these queries, it will respond with null. * Note that the default behavior is that children are forbidden. To * permit them, the subclass ParentNode overrides several methods. * <P> * NodeImpl also implements NodeList, so it can return itself in * response to the getChildNodes() query. This eliminiates the need * for a separate ChildNodeList object. Note that this is an * IMPLEMENTATION DETAIL; applications should _never_ assume that * this identity exists. * <P> * All nodes in a single document must originate * in that document. (Note that this is much tighter than "must be * same implementation") Nodes are all aware of their ownerDocument, * and attempts to mismatch will throw WRONG_DOCUMENT_ERR. * <P> * However, to save memory not all nodes always have a direct reference * to their ownerDocument. When a node is owned by another node it relies * on its owner to store its ownerDocument. Parent nodes always store it * though, so there is never more than one level of indirection. * And when a node doesn't have an owner, ownerNode refers to its * ownerDocument. * <p> * This class doesn't directly support mutation events, however, it still * implements the EventTarget interface and forward all related calls to the * document so that the document class do so. * * @xerces.internal * * @author Arnaud Le Hors, IBM * @author Joe Kesselman, IBM * @version $Id: NodeImpl.java,v 1.2.6.1 2005/08/31 12:27:09 sunithareddy Exp $ * @since PR-DOM-Level-1-19980818. */public abstract class NodeImpl implements Node, NodeList, EventTarget, Cloneable, Serializable{ // // Constants // // TreePosition Constants. // Taken from DOM L3 Node interface. /** * The node precedes the reference node. */ public static final short TREE_POSITION_PRECEDING = 0x01; /** * The node follows the reference node. */ public static final short TREE_POSITION_FOLLOWING = 0x02; /** * The node is an ancestor of the reference node. */ public static final short TREE_POSITION_ANCESTOR = 0x04; /** * The node is a descendant of the reference node. */ public static final short TREE_POSITION_DESCENDANT = 0x08; /** * The two nodes have an equivalent position. This is the case of two * attributes that have the same <code>ownerElement</code>, and two * nodes that are the same. */ public static final short TREE_POSITION_EQUIVALENT = 0x10; /** * The two nodes are the same. Two nodes that are the same have an * equivalent position, though the reverse may not be true. */ public static final short TREE_POSITION_SAME_NODE = 0x20; /** * The two nodes are disconnected, they do not have any common ancestor. * This is the case of two nodes that are not in the same document. */ public static final short TREE_POSITION_DISCONNECTED = 0x00; // DocumentPosition public static final short DOCUMENT_POSITION_DISCONNECTED = 0x01; public static final short DOCUMENT_POSITION_PRECEDING = 0x02; public static final short DOCUMENT_POSITION_FOLLOWING = 0x04; public static final short DOCUMENT_POSITION_CONTAINS = 0x08; public static final short DOCUMENT_POSITION_IS_CONTAINED = 0x10; public static final short DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20; /** Serialization version. */ static final long serialVersionUID = -6316591992167219696L; // public /** Element definition node type. */ public static final short ELEMENT_DEFINITION_NODE = 21; // // Data // // links protected NodeImpl ownerNode; // typically the parent but not always! // data protected short flags; protected final static short READONLY = 0x1<<0; protected final static short SYNCDATA = 0x1<<1; protected final static short SYNCCHILDREN = 0x1<<2; protected final static short OWNED = 0x1<<3; protected final static short FIRSTCHILD = 0x1<<4; protected final static short SPECIFIED = 0x1<<5; protected final static short IGNORABLEWS = 0x1<<6; protected final static short HASSTRING = 0x1<<7; protected final static short NORMALIZED = 0x1<<8; protected final static short ID = 0x1<<9; // // Constructors // /** * No public constructor; only subclasses of Node should be * instantiated, and those normally via a Document's factory methods * <p> * Every Node knows what Document it belongs to. */ protected NodeImpl(CoreDocumentImpl ownerDocument) { // as long as we do not have any owner, ownerNode is our ownerDocument ownerNode = ownerDocument; } // <init>(CoreDocumentImpl) /** Constructor for serialization. */ public NodeImpl() {} // // Node methods // /** * A short integer indicating what type of node this is. The named * constants for this value are defined in the org.w3c.dom.Node interface. */ public abstract short getNodeType(); /** * the name of this node. */ public abstract String getNodeName(); /** * Returns the node value. * @throws DOMException(DOMSTRING_SIZE_ERR) */ public String getNodeValue() throws DOMException { return null; // overridden in some subclasses } /** * Sets the node value. * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) */ public void setNodeValue(String x) throws DOMException { // Default behavior is to do nothing, overridden in some subclasses } /** * Adds a child node to the end of the list of children for this node. * Convenience shorthand for insertBefore(newChild,null). * @see #insertBefore(Node, Node) * <P> * By default we do not accept any children, ParentNode overrides this. * @see ParentNode * * @return newChild, in its new state (relocated, or emptied in the case of * DocumentNode.) * * @throws DOMException(HIERARCHY_REQUEST_ERR) if newChild is of a * type that shouldn't be a child of this node. * * @throws DOMException(WRONG_DOCUMENT_ERR) if newChild has a * different owner document than we do. * * @throws DOMException(NO_MODIFICATION_ALLOWED_ERR) if this node is * read-only. */ public Node appendChild(Node newChild) throws DOMException { return insertBefore(newChild, null); } /** * Returns a duplicate of a given node. You can consider this a * generic "copy constructor" for nodes. The newly returned object should * be completely independent of the source object's subtree, so changes * in one after the clone has been made will not affect the other. * <P> * Note: since we never have any children deep is meaningless here, * ParentNode overrides this behavior. * @see ParentNode * * <p> * Example: Cloning a Text node will copy both the node and the text it * contains. * <p> * Example: Cloning something that has children -- Element or Attr, for * example -- will _not_ clone those children unless a "deep clone" * has been requested. A shallow clone of an Attr node will yield an * empty Attr of the same name. * <p> * NOTE: Clones will always be read/write, even if the node being cloned * is read-only, to permit applications using only the DOM API to obtain * editable copies of locked portions of the tree. */ public Node cloneNode(boolean deep) { if (needsSyncData()) { synchronizeData(); } NodeImpl newnode; try { newnode = (NodeImpl)clone(); } catch (CloneNotSupportedException e) { // if we get here we have an error in our program we may as well // be vocal about it, so that people can take appropriate action. throw new RuntimeException("**Internal Error**" + e); } // Need to break the association w/ original kids newnode.ownerNode = ownerDocument(); newnode.isOwned(false); // By default we make all clones readwrite, // this is overriden in readonly subclasses newnode.isReadOnly(false); ownerDocument().callUserDataHandlers(this, newnode, UserDataHandler.NODE_CLONED); return newnode; } // cloneNode(boolean):Node /** * Find the Document that this Node belongs to (the document in * whose context the Node was created). The Node may or may not * currently be part of that Document's actual contents. */ public Document getOwnerDocument() { // if we have an owner simply forward the request // otherwise ownerNode is our ownerDocument if (isOwned()) { return ownerNode.ownerDocument(); } else { return (Document) ownerNode; } } /** * same as above but returns internal type and this one is not overridden * by CoreDocumentImpl to return null */ CoreDocumentImpl ownerDocument() { // if we have an owner simply forward the request // otherwise ownerNode is our ownerDocument if (isOwned()) { return ownerNode.ownerDocument(); } else { return (CoreDocumentImpl) ownerNode; } } /** * NON-DOM * set the ownerDocument of this node */ void setOwnerDocument(CoreDocumentImpl doc) { if (needsSyncData()) { synchronizeData(); } // if we have an owner we rely on it to have it right // otherwise ownerNode is our ownerDocument if (!isOwned()) { ownerNode = doc; } } /** * Returns the node number */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?