org.apache.xerces.dom

## Class ParentNode

- java.lang.Object
- - org.apache.xerces.dom.NodeImpl
  - - org.apache.xerces.dom.ChildNode
    - - org.apache.xerces.dom.ParentNode

- All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, org.w3c.dom.events.EventTarget, org.w3c.dom.Node, org.w3c.dom.NodeList
  Direct Known Subclasses:
    CoreDocumentImpl, DocumentFragmentImpl, DocumentTypeImpl, ElementDefinitionImpl, ElementImpl, EntityImpl, EntityReferenceImpl

      public abstract class ParentNode
      extends ChildNode

  ParentNode inherits from ChildNode and adds the capability of having child nodes. Not every node in the DOM can have children, so only nodes that can should inherit from this class and pay the price for it.

  ParentNode, just like 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. On the other hand, subclasses may need to override this, in case of conflicting names. This is the case for the classes HTMLSelectElementImpl and HTMLFormElementImpl of the HTML DOM.

  While we have a direct reference to the first child, the last child is stored as the previous sibling of the first child. First child nodes are marked as being so, and getNextSibling hides this fact.

  Note: Not all parent nodes actually need to also be a child. At some point we used to have ParentNode inheriting from NodeImpl and another class called ChildAndParentNode that inherited from ChildNode. But due to the lack of multiple inheritance a lot of code had to be duplicated which led to a maintenance nightmare. At the same time only a few nodes (Document, DocumentFragment, Entity, and Attribute) cannot be a child so the gain in memory wasn't really worth it. The only type for which this would be the case is Attribute, but we deal with there in another special way, so this is not applicable.

  This class doesn't directly support mutation events, however, it notifies the document when mutations are performed so that the document class do so.

  WARNING: Some of the code here is partially duplicated in AttrImpl, be careful to keep these two classes in sync!

  Version:
    $Id: ParentNode.java 724081 2008-12-07 05:46:26Z mrglavas $
  Author:
    Arnaud Le Hors, IBM, Joe Kesselman, IBM, Andy Clark, IBM
  See Also:
    Serialized Form

  # INTERNAL:

    Usage of this class is not supported. It may be altered or removed at any time.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected ChildNode` | `firstChild` First child.
    `protected org.apache.xerces.dom.NodeListCache` | `fNodeListCache` NodeList cache
    `protected CoreDocumentImpl` | `ownerDocument` Owner document.

    - ### Fields inherited from class org.apache.xerces.dom.ChildNode

      `nextSibling, previousSibling`

    - ### Fields inherited from class org.apache.xerces.dom.NodeImpl

      `DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_IS_CONTAINED, DOCUMENT_POSITION_PRECEDING, ELEMENT_DEFINITION_NODE, FIRSTCHILD, flags, HASSTRING, ID, IGNORABLEWS, NORMALIZED, OWNED, ownerNode, READONLY, SPECIFIED, SYNCCHILDREN, SYNCDATA, TREE_POSITION_ANCESTOR, TREE_POSITION_DESCENDANT, TREE_POSITION_DISCONNECTED, TREE_POSITION_EQUIVALENT, TREE_POSITION_FOLLOWING, TREE_POSITION_PRECEDING, TREE_POSITION_SAME_NODE`

    - ### Fields inherited from interface org.w3c.dom.Node

      `ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `` | `ParentNode()` Constructor for serialization.
    `protected` | `ParentNode(CoreDocumentImpl ownerDocument)` No public constructor; only subclasses of ParentNode should be instantiated, and those normally via a Document's factory methods

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `org.w3c.dom.Node` | `cloneNode(boolean deep)` Returns a duplicate of a given node.
    `org.w3c.dom.NodeList` | `getChildNodes()` Obtain a NodeList enumerating all children of this node.
    `protected org.w3c.dom.NodeList` | `getChildNodesUnoptimized()` Create a NodeList to access children that is use by subclass elements that have methods named getLength() or item(int).
    `org.w3c.dom.Node` | `getFirstChild()` The first child of this Node, or null if none.
    `org.w3c.dom.Node` | `getLastChild()` The last child of this Node, or null if none.
    `int` | `getLength()` NodeList method: Count the immediate children of this node
    `org.w3c.dom.Document` | `getOwnerDocument()` Find the Document that this Node belongs to (the document in whose context the Node was created).
    `java.lang.String` | `getTextContent()` This attribute returns the text content of this node and its descendants.
    `boolean` | `hasChildNodes()` Test whether this node has any children.
    `org.w3c.dom.Node` | `insertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild)` Move one or more node(s) to our list of children.
    `boolean` | `isEqualNode(org.w3c.dom.Node arg)` DOM Level 3 WD- Experimental.
    `org.w3c.dom.Node` | `item(int index)` NodeList method: Return the Nth immediate child of this node, or null if the index is out of bounds.
    `void` | `normalize()` Override default behavior to call normalize() on this Node's children.
    `org.w3c.dom.Node` | `removeChild(org.w3c.dom.Node oldChild)` Remove a child from this Node.
    `org.w3c.dom.Node` | `replaceChild(org.w3c.dom.Node newChild, org.w3c.dom.Node oldChild)` Make newChild occupy the location that oldChild used to have.
    `protected void` | `setOwnerDocument(CoreDocumentImpl doc)` NON-DOM set the ownerDocument of this node and its children
    `void` | `setReadOnly(boolean readOnly, boolean deep)` Override default behavior so that if deep is true, children are also toggled.
    `void` | `setTextContent(java.lang.String textContent)` This attribute returns the text content of this node and its descendants.
    `protected void` | `synchronizeChildren()` Override this method in subclass to hook in efficient internal data structure.

    - ### Methods inherited from class org.apache.xerces.dom.ChildNode

      `getNextSibling, getParentNode, getPreviousSibling`

    - ### Methods inherited from class org.apache.xerces.dom.NodeImpl

      `addEventListener, appendChild, changed, changes, compareDocumentPosition, compareTreePosition, dispatchEvent, getAttributes, getBaseURI, getContainer, getFeature, getLocalName, getNamespaceURI, getNodeName, getNodeNumber, getNodeType, getNodeValue, getPrefix, getReadOnly, getUserData, getUserData, getUserDataRecord, hasAttributes, isDefaultNamespace, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, needsSyncChildren, removeEventListener, setNodeValue, setPrefix, setUserData, setUserData, synchronizeData, toString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### ownerDocument

          protected CoreDocumentImpl ownerDocument

      Owner document.

    - #### firstChild

          protected ChildNode firstChild

      First child.

    - #### fNodeListCache

          protected transient org.apache.xerces.dom.NodeListCache fNodeListCache

      NodeList cache

  - ### Constructor Detail

    - #### ParentNode

          protected ParentNode(CoreDocumentImpl ownerDocument)

      No public constructor; only subclasses of ParentNode should be instantiated, and those normally via a Document's factory methods

    - #### ParentNode

          public ParentNode()

      Constructor for serialization.

  - ### Method Detail

    - #### cloneNode

          public org.w3c.dom.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 `org.w3c.dom.Node`
      Overrides:
        `cloneNode` in class `ChildNode`
      Parameters:
        `deep` - If `true`, recursively clone the subtree under the specified node; if `false`, clone only the node itself (and its attributes, if it is an `Element`).
      Returns:
        The duplicate node.
      See Also:
        `

        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.

        `

    - #### getOwnerDocument

          public org.w3c.dom.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 `org.w3c.dom.Node`
      Overrides:
        `getOwnerDocument` in class `NodeImpl`

    - #### setOwnerDocument

          protected void setOwnerDocument(CoreDocumentImpl doc)

      NON-DOM set the ownerDocument of this node and its children
      Overrides:
        `setOwnerDocument` in class `NodeImpl`

    - #### hasChildNodes

          public boolean hasChildNodes()

      Test whether this node has any children. Convenience shorthand for (Node.getFirstChild()!=null)
      Specified by:
        `hasChildNodes` in interface `org.w3c.dom.Node`
      Overrides:
        `hasChildNodes` in class `NodeImpl`
      Returns:
        Returns `true` if this node has any children, `false` otherwise.
      See Also:
        `ParentNode`

    - #### getChildNodes

          public org.w3c.dom.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 `org.w3c.dom.Node`
      Overrides:
        `getChildNodes` in class `NodeImpl`

    - #### getFirstChild

          public org.w3c.dom.Node getFirstChild()

      The first child of this Node, or null if none.
      Specified by:
        `getFirstChild` in interface `org.w3c.dom.Node`
      Overrides:
        `getFirstChild` in class `NodeImpl`
      See Also:
        `ParentNode`

    - #### getLastChild

          public org.w3c.dom.Node getLastChild()

      The last child of this Node, or null if none.
      Specified by:
        `getLastChild` in interface `org.w3c.dom.Node`
      Overrides:
        `getLastChild` in class `NodeImpl`
      See Also:
        `ParentNode`

    - #### insertBefore

          public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild,
                                               org.w3c.dom.Node refChild)
                                        throws org.w3c.dom.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 `org.w3c.dom.Node`
      Overrides:
        `insertBefore` in class `NodeImpl`
      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().
      Returns:
        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, 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.
        `org.w3c.dom.DOMException` - HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the `newChild` node, or if the node to insert is one of this node's ancestors or this node itself, or if this node is of type `Document` and the DOM application attempts to insert a second `DocumentType` or `Element` node.
        WRONG_DOCUMENT_ERR: Raised if `newChild` was created from a different document than the one that created this node.
        NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the parent of the node being inserted is readonly.
        NOT_FOUND_ERR: Raised if `refChild` is not a child of this node.
        NOT_SUPPORTED_ERR: if this node is of type `Document`, this exception might be raised if the DOM implementation doesn't support the insertion of a `DocumentType` or `Element` node.
      See Also:
        `ParentNode`

    - #### removeChild

          public org.w3c.dom.Node removeChild(org.w3c.dom.Node oldChild)
                                       throws org.w3c.dom.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 `org.w3c.dom.Node`
      Overrides:
        `removeChild` in class `NodeImpl`
      Parameters:
        `oldChild` - The node being removed.
      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.
        `org.w3c.dom.DOMException` - NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
        NOT_FOUND_ERR: Raised if `oldChild` is not a child of this node.
        NOT_SUPPORTED_ERR: if this node is of type `Document`, this exception might be raised if the DOM implementation doesn't support the removal of the `DocumentType` child or the `Element` child.
      See Also:
        `ParentNode`

    - #### replaceChild

          public org.w3c.dom.Node replaceChild(org.w3c.dom.Node newChild,
                                               org.w3c.dom.Node oldChild)
                                        throws org.w3c.dom.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 `org.w3c.dom.Node`
      Overrides:
        `replaceChild` in class `NodeImpl`
      Parameters:
        `newChild` - The new node to put in the child list.
        `oldChild` - The node being replaced in the list.
      Returns:
        oldChild, in its new state (removed).
      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.
        `org.w3c.dom.DOMException` - HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the `newChild` node, or if the node to put in is one of this node's ancestors or this node itself, or if this node is of type `Document` and the result of the replacement operation would add a second `DocumentType` or `Element` on the `Document` node.
        WRONG_DOCUMENT_ERR: Raised if `newChild` was created from a different document than the one that created this node.
        NO_MODIFICATION_ALLOWED_ERR: Raised if this node or the parent of the new node is readonly.
        NOT_FOUND_ERR: Raised if `oldChild` is not a child of this node.
        NOT_SUPPORTED_ERR: if this node is of type `Document`, this exception might be raised if the DOM implementation doesn't support the replacement of the `DocumentType` child or `Element` child.
      See Also:
        `ParentNode`

    - #### getTextContent

          public java.lang.String getTextContent()
                                          throws org.w3c.dom.DOMException

      Description copied from class: `NodeImpl`
      This attribute returns the text content of this node and its descendants. When it is defined to be null, setting it has no effect. When set, any possible children this node may have are removed and replaced by a single `Text` node containing the string this attribute is set to. On getting, no serialization is performed, the returned string does not contain any markup. No whitespace normalization is performed, the returned string does not contain the element content whitespaces . Similarly, on setting, no parsing is performed either, the input string is taken as pure textual content.
      The string returned is made of the text content of this node depending on its type, as defined below:

      Node type | Content
      --- | ---
      /** This attribute returns the text content of this node and its descendants. When it is defined to be null, setting it has no effect. When set, any possible children this node may have are removed and replaced by a single `Text` node containing the string this attribute is set to. On getting, no serialization is performed, the returned string does not contain any markup. No whitespace normalization is performed, the returned string does not contain the element content whitespaces . Similarly, on setting, no parsing is performed either, the input string is taken as pure textual content.
      The string returned is made of the text content of this node depending on its type, as defined below:

      Node type | Content
      --- | ---
      ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, DOCUMENT_FRAGMENT_NODE | concatenation of the `textContent` attribute value of every child node, excluding COMMENT_NODE and PROCESSING_INSTRUCTION_NODE nodes
      ATTRIBUTE_NODE, TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE | `nodeValue`
      DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE | null

      Specified by:
        `getTextContent` in interface `org.w3c.dom.Node`
      Overrides:
        `getTextContent` in class `NodeImpl`
      Throws:
        `org.w3c.dom.DOMException` - NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.

    - #### setTextContent

          public void setTextContent(java.lang.String textContent)
                              throws org.w3c.dom.DOMException

      Description copied from class: `NodeImpl`
      This attribute returns the text content of this node and its descendants. When it is defined to be null, setting it has no effect. When set, any possible children this node may have are removed and replaced by a single `Text` node containing the string this attribute is set to. On getting, no serialization is performed, the returned string does not contain any markup. No whitespace normalization is performed, the returned string does not contain the element content whitespaces . Similarly, on setting, no parsing is performed either, the input string is taken as pure textual content.
      The string returned is made of the text content of this node depending on its type, as defined below:

      Node type | Content
      --- | ---
      ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, DOCUMENT_FRAGMENT_NODE | concatenation of the `textContent` attribute value of every child node, excluding COMMENT_NODE and PROCESSING_INSTRUCTION_NODE nodes
      ATTRIBUTE_NODE, TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE | `nodeValue`
      DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE | null

      Specified by:
        `setTextContent` in interface `org.w3c.dom.Node`
      Overrides:
        `setTextContent` in class `NodeImpl`
      Throws:
        `org.w3c.dom.DOMException` - NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.

    - #### getLength

          public int getLength()

      NodeList method: Count the immediate children of this node
      Specified by:
        `getLength` in interface `org.w3c.dom.NodeList`
      Overrides:
        `getLength` in class `NodeImpl`
      Returns:
        int
      See Also:
        `ParentNode`

    - #### item

          public org.w3c.dom.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 `org.w3c.dom.NodeList`
      Overrides:
        `item` in class `NodeImpl`
      Parameters:
        `index` - int
      Returns:
        org.w3c.dom.Node
      See Also:
        `ParentNode`

    - #### getChildNodesUnoptimized

          protected final org.w3c.dom.NodeList getChildNodesUnoptimized()

      Create a NodeList to access children that is use by subclass elements that have methods named getLength() or item(int). ChildAndParentNode optimizes getChildNodes() by implementing NodeList itself. However if a subclass Element implements methods with the same name as the NodeList methods, they will override the actually methods in this class.

      To use this method, the subclass should implement getChildNodes() and have it call this method. The resulting NodeList instance maybe shared and cached in a transient field, but the cached value must be cleared if the node is cloned.

    - #### normalize

          public void normalize()

      Override default behavior to call normalize() on this Node's children. It is up to implementors or Node to override normalize() to take action.
      Specified by:
        `normalize` in interface `org.w3c.dom.Node`
      Overrides:
        `normalize` in class `NodeImpl`

    - #### isEqualNode

          public boolean isEqualNode(org.w3c.dom.Node arg)

      DOM Level 3 WD- Experimental. Override inherited behavior from NodeImpl to support deep equal.
      Specified by:
        `isEqualNode` in interface `org.w3c.dom.Node`
      Overrides:
        `isEqualNode` in class `NodeImpl`
      Parameters:
        `arg` - The node to compare equality with.
      Returns:
        If the nodes, and possibly subtrees are equal, `true` otherwise `false`.

    - #### setReadOnly

          public void setReadOnly(boolean readOnly,
                                  boolean deep)

      Override default behavior so that if deep is true, children are also toggled.
      Overrides:
        `setReadOnly` in class `NodeImpl`
      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.
      See Also:
        `

        Note: this will not change the state of an EntityReference or its children, which are always read-only.

        `

    - #### synchronizeChildren

          protected void synchronizeChildren()

      Override this method in subclass to hook in efficient internal data structure.

Copyright © 1999-2010 The Apache Software Foundation. All Rights Reserved.