XML for Java 2.0.15

com.ibm.xml.dom
Class NodeImpl

java.lang.Object
  |
  +--com.ibm.xml.dom.NodeImpl
Direct Known Subclasses:
AttrImpl, CharacterDataImpl, DocumentFragmentImpl, DocumentImpl, DocumentTypeImpl, ElementDefinitionImpl, ElementImpl, EntityImpl, EntityReferenceImpl, NotationImpl, ProcessingInstructionImpl

public abstract class NodeImpl
extends java.lang.Object
implements Node, NodeList, java.lang.Cloneable, java.io.Serializable

Node 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.

Node itself implements 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.

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.

Note that the default behavior is that children are forbidden. To permit them, the subclass's constructor sets isLeafNode false.

LEVEL 1 RESTRICTION: 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.

Since:
PR-DOM-Level-1-19980818.
Version:
66 1.3 src/com/ibm/xml/dom/NodeImpl.java, parser, xml4j2
See Also:
Serialized Form

Field Summary
static short ELEMENT_DEFINITION_NODE
          Element definition node type.
protected  NodeImpl firstChild
          First child.
protected static int[] kidOK
          Table for quick check of child insertion.
protected  NodeImpl lastChild
          Last child.
protected  java.lang.String name
          Node name.
protected  NodeImpl nextSibling
          Next sibling.
protected  DocumentImpl ownerDocument
          Owner document.
protected  NodeImpl parentNode
          Parent node.
protected  NodeImpl previousSibling
          Previous sibling.
protected  boolean readOnly
          Read-only property.
protected  boolean syncChildren
          Synchronization of child nodes needed.
protected  boolean syncData
          Synchronization of data needed.
protected  java.lang.Object userData
          NON-DOM FEATURE; see setUserData/getUserData.
protected  java.lang.String value
          Node value.
 
Constructor Summary
  NodeImpl()
          Constructor for serialization.
protected NodeImpl(DocumentImpl ownerDocument, java.lang.String name, java.lang.String value)
          No public constructor; only subclasses of Node should be instantiated, and those normally via a Document's factory methods Every Node knows what Document it belongs to.
 
Method Summary
 Node appendChild(Node newChild)
          Adds a child node to the end of the list of children for this node.
protected  void changed()
          Denotes that this node has changed.
 Node cloneNode(boolean deep)
          Returns a duplicate of a given node.
 NamedNodeMap getAttributes()
          Return the collection of attributes associated with this node, or null if none.
 NodeList getChildNodes()
          Obtain a NodeList enumerating all children of this node.
 Node getFirstChild()
          The first child of this Node, or null if none.
 Node getLastChild()
          The first child of this Node, or null if none.
 int getLength()
          NodeList method: Count the immediate children of this node
 Node getNextSibling()
          The next child of this node's parent, or null if none
 java.lang.String getNodeName()
          For many nodes, nodeName is derived from their nodeType mnemonic (lowercasified and preceeded by a '#' character).
abstract  short getNodeType()
          A short integer indicating what type of node this is.
 java.lang.String getNodeValue()
          nodeValue is a string representation of the data contained in this node.
 Document getOwnerDocument()
          Find the Document that this Node belongs to (the document in whose context the Node was created).
 Node getParentNode()
          Obtain the DOM-tree parent of this node, or null if it is not currently active in the DOM tree (perhaps because it has just been created or removed).
 Node getPreviousSibling()
          The previous child of this node's parent, or null if none
 boolean getReadOnly()
          NON-DOM: Returns true if this node is read-only.
 java.lang.Object getUserData()
          Returns the user data associated to this node.
 boolean hasChildNodes()
          Test whether this node has any children.
 Node insertBefore(Node newChild, Node refChild)
          Move one or more node(s) to our list of children.
protected static boolean isKidOK(Node parent, Node child)
          Uses the kidOK lookup table to check whether the proposed tree structure is legal.
 Node item(int index)
          NodeList method: Return the Nth immediate child of this node, or null if the index is out of bounds.
 Node removeChild(Node oldChild)
          Remove a child from this Node.
 Node replaceChild(Node newChild, Node oldChild)
          Make newChild occupy the location that oldChild used to have.
 void setNodeValue(java.lang.String value)
          Any node which can have a nodeValue (@see getNodeValue) will also accept requests to set it to a string.
 void setReadOnly(boolean readOnly, boolean deep)
          NON-DOM: PR-DOM-Level-1-19980818 mentions readonly nodes in conjunction with Entities, but provides no API to support this.
 void setUserData(java.lang.Object data)
          NON-DOM: As an alternative to subclassing the DOM, this implementation has been extended with the ability to attach an object to each node.
protected  void synchronizeChildren()
          Override this method in subclass to hook in efficient internal data structure.
protected  void synchronizeData()
          Override this method in subclass to hook in efficient internal data structure.
 java.lang.String toString()
          NON-DOM method for debugging convenience.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ELEMENT_DEFINITION_NODE

public static final short ELEMENT_DEFINITION_NODE
Element definition node type.

ownerDocument

protected DocumentImpl ownerDocument
Owner document.

parentNode

protected NodeImpl parentNode
Parent node.

previousSibling

protected NodeImpl previousSibling
Previous sibling.

nextSibling

protected NodeImpl nextSibling
Next sibling.

name

protected java.lang.String name
Node name.

value

protected java.lang.String value
Node value.

readOnly

protected boolean readOnly
Read-only property.

userData

protected java.lang.Object userData
NON-DOM FEATURE; see setUserData/getUserData.

firstChild

protected NodeImpl firstChild
First child.

lastChild

protected NodeImpl lastChild
Last child.

syncChildren

protected transient boolean syncChildren
Synchronization of child nodes needed.

syncData

protected transient boolean syncData
Synchronization of data needed.

kidOK

protected static int[] kidOK
Table for quick check of child insertion.
Constructor Detail

NodeImpl

protected NodeImpl(DocumentImpl ownerDocument,
                   java.lang.String name,
                   java.lang.String value)
No public constructor; only subclasses of Node should be instantiated, and those normally via a Document's factory methods

Every Node knows what Document it belongs to.


NodeImpl

public NodeImpl()
Constructor for serialization.
Method Detail

getNodeType

public abstract short getNodeType()
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.
Specified by:
getNodeType in interface Node

getNodeName

public java.lang.String getNodeName()
For many nodes, nodeName is derived from their nodeType mnemonic (lowercasified and preceeded by a '#' character). Others return the "name of the data" -- tag name, attribute name, and so on.
Specified by:
getNodeName in interface Node

setNodeValue

public void setNodeValue(java.lang.String value)
Any node which can have a nodeValue (@see getNodeValue) will also accept requests to set it to a string. The exact response to this varies from node to node -- Attribute, for example, stores its values in its children and has to replace them with a new Text holding the replacement value.

For most types of Node, value is null and attempting to set it will throw DOMException(NO_MODIFICATION_ALLOWED_ERR). This will also be thrown if the node is read-only.

Specified by:
setNodeValue in interface Node

getNodeValue

public java.lang.String getNodeValue()
nodeValue is a string representation of the data contained in this node. Each subclass of Node defines what information is considered its value, or returns null if the query is not appropriate. Entity references should be expanded before being returned.

For most types of Node, value is null and may not be changed.

For Text, CDATASection, ProcessingInstruction, and Comment value is a string representing the contents of the node.

For Attribute, value is the string returned by the attribute's getValue method. This will be gathered from the Attribute's children.

Specified by:
getNodeValue in interface Node
Throws:
DOMException(DOMSTRING_SIZE_ERR) - when it would return more characters than fit in a DOMString variable on the implementation platform. (Will never arise in this implementation)

appendChild

public Node appendChild(Node newChild)
                 throws DOMException
Adds a child node to the end of the list of children for this node. Convenience shorthand for insertBefore(newChild,null).
Specified by:
appendChild in interface Node
Throws:
DOMException(HIERARCHY_REQUEST_ERR) - if newChild is of a type that shouldn't be a child of this node.
DOMException(WRONG_DOCUMENT_ERR) - if newChild has a different owner document than we do.
DOMException(NO_MODIFICATION_ALLOWED_ERR) - if this node is read-only.
See Also:
insertBefore()

cloneNode

public Node cloneNode(boolean deep)
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.

Example: Cloning a Text node will copy both the node and the text it contains.

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.

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.

Specified by:
cloneNode in interface Node

getOwnerDocument

public Document getOwnerDocument()
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.
Specified by:
getOwnerDocument in interface Node

getParentNode

public Node getParentNode()
Obtain the DOM-tree parent of this node, or null if it is not currently active in the DOM tree (perhaps because it has just been created or removed). Note that Document, DocumentFragment, and Attribute will never have parents.
Specified by:
getParentNode in interface Node

getNextSibling

public Node getNextSibling()
The next child of this node's parent, or null if none
Specified by:
getNextSibling in interface Node

getPreviousSibling

public Node getPreviousSibling()
The previous child of this node's parent, or null if none
Specified by:
getPreviousSibling in interface Node

getAttributes

public NamedNodeMap getAttributes()
Return the collection of attributes associated with this node, or null if none. At this writing, Element is the only type of node which will ever have attributes.
Specified by:
getAttributes in interface Node
See Also:
com.ibm.domimpl.Element

hasChildNodes

public boolean hasChildNodes()
Test whether this node has any children. Convenience shorthand for (Node.getFirstChild()!=null)
Specified by:
hasChildNodes in interface Node

getChildNodes

public NodeList getChildNodes()
Obtain a NodeList enumerating all children of this node. If there are none, an (initially) empty NodeList is returned.

NodeLists are "live"; as children are added/removed the NodeList will immediately reflect those changes. Also, the NodeList refers to the actual nodes, so changes to those nodes made via the DOM tree will be reflected in the NodeList and vice versa.

In this implementation, Nodes implement the NodeList interface and provide their own getChildNodes() support. Other DOMs may solve this differently.

Specified by:
getChildNodes in interface Node

getFirstChild

public Node getFirstChild()
The first child of this Node, or null if none.
Specified by:
getFirstChild in interface Node

getLastChild

public Node getLastChild()
The first child of this Node, or null if none.
Specified by:
getLastChild in interface Node

insertBefore

public Node insertBefore(Node newChild,
                         Node refChild)
                  throws DOMException
Move one or more node(s) to our list of children. Note that this implicitly removes them from their previous parent.
Specified by:
insertBefore in interface Node
Parameters:
newChild - The Node to be moved to our subtree. As a convenience feature, inserting a DocumentNode will instead insert all its children.
refChild - Current child which newChild should be placed immediately before. If refChild is null, the insertion occurs after all existing Nodes, like appendChild().
Throws:
DOMException(HIERARCHY_REQUEST_ERR) - if newChild is of a type that shouldn't be a child of this node, or if newChild is an ancestor of this node.
DOMException(WRONG_DOCUMENT_ERR) - if newChild has a different owner document than we do.
DOMException(NOT_FOUND_ERR) - if refChild is not a child of this node.
DOMException(NO_MODIFICATION_ALLOWED_ERR) - if this node is read-only.

removeChild

public Node removeChild(Node oldChild)
                 throws DOMException
Remove a child from this Node. The removed child's subtree remains intact so it may be re-inserted elsewhere.
Specified by:
removeChild in interface Node
Returns:
oldChild, in its new state (removed).
Throws:
DOMException(NOT_FOUND_ERR) - if oldChild is not a child of this node.
DOMException(NO_MODIFICATION_ALLOWED_ERR) - if this node is read-only.

replaceChild

public Node replaceChild(Node newChild,
                         Node oldChild)
                  throws DOMException
Make newChild occupy the location that oldChild used to have. Note that newChild will first be removed from its previous parent, if any. Equivalent to inserting newChild before oldChild, then removing oldChild.
Specified by:
replaceChild in interface Node
Throws:
DOMException(HIERARCHY_REQUEST_ERR) - if newChild is of a type that shouldn't be a child of this node, or if newChild is one of our ancestors.
DOMException(WRONG_DOCUMENT_ERR) - if newChild has a different owner document than we do.
DOMException(NOT_FOUND_ERR) - if oldChild is not a child of this node.
DOMException(NO_MODIFICATION_ALLOWED_ERR) - if this node is read-only.

getLength

public int getLength()
NodeList method: Count the immediate children of this node
Specified by:
getLength in interface NodeList
Returns:
int

item

public Node item(int index)
NodeList method: Return the Nth immediate child of this node, or null if the index is out of bounds.
Specified by:
item in interface NodeList
Parameters:
Index - int
Returns:
org.w3c.dom.Node

setReadOnly

public void setReadOnly(boolean readOnly,
                        boolean deep)
NON-DOM: PR-DOM-Level-1-19980818 mentions readonly nodes in conjunction with Entities, but provides no API to support this.

Most DOM users should not touch this method. Its anticpated use is during construction of EntityRefernces, where it will be used to lock the contents replicated from Entity so they can't be casually altered. It _could_ be published as a DOM extension, if desired.

Parameters:
readOnly - True or false as desired.
deep - If true, children are also toggled. Note that this will not change the state of an EntityReference or its children, which are always read-only.

getReadOnly

public boolean getReadOnly()
NON-DOM: Returns true if this node is read-only. This is a shallow check.

setUserData

public void setUserData(java.lang.Object data)
NON-DOM: As an alternative to subclassing the DOM, this implementation has been extended with the ability to attach an object to each node. (If you need multiple objects, you can attach a collection such as a vector or hashtable, then attach your application information to that.)

getUserData

public java.lang.Object getUserData()
Returns the user data associated to this node.

synchronizeChildren

protected void synchronizeChildren()
Override this method in subclass to hook in efficient internal data structure.

synchronizeData

protected void synchronizeData()
Override this method in subclass to hook in efficient internal data structure.

changed

protected void changed()
Denotes that this node has changed.

isKidOK

protected static boolean isKidOK(Node parent,
                                 Node child)
Uses the kidOK lookup table to check whether the proposed tree structure is legal.

toString

public java.lang.String toString()
NON-DOM method for debugging convenience.
Overrides:
toString in class java.lang.Object

XML for Java 2.0.15