Class ParentNode
- java.lang.Object
-
- org.htmlunit.cyberneko.xerces.dom.NodeImpl
-
- org.htmlunit.cyberneko.xerces.dom.ChildNode
-
- org.htmlunit.cyberneko.xerces.dom.ParentNode
-
- All Implemented Interfaces:
java.lang.Cloneable
,org.w3c.dom.events.EventTarget
,org.w3c.dom.Node
,org.w3c.dom.NodeList
- Direct Known Subclasses:
CoreDocumentImpl
,DocumentFragmentImpl
,DocumentTypeImpl
,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!
-
-
Field Summary
Fields Modifier and Type Field Description protected ChildNode
firstChild
First child.protected NodeListCache
fNodeListCache
NodeList cacheprotected CoreDocumentImpl
ownerDocument
Owner document.-
Fields inherited from class org.htmlunit.cyberneko.xerces.dom.ChildNode
nextSibling_, previousSibling_
-
Fields inherited from class org.htmlunit.cyberneko.xerces.dom.NodeImpl
DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_IS_CONTAINED, DOCUMENT_POSITION_PRECEDING, FIRSTCHILD, HASSTRING, ID, NORMALIZED, OWNED, ownerNode_, READONLY, SPECIFIED, SYNCCHILDREN
-
-
Constructor Summary
Constructors Modifier Constructor Description 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 Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
checkNormalizationAfterInsert(ChildNode insertedChild)
Checks the normalized state of this node after inserting a child.(package private) void
checkNormalizationAfterRemove(ChildNode previousSibling)
Checks the normalized of this node after removing a child.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 first child of this Node, or null if none.int
getLength()
NodeList method: Count the immediate children of this nodeorg.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.(package private) void
getTextContent(java.lang.StringBuilder builder)
boolean
hasChildNodes()
Test whether this node has any children.(package private) static boolean
hasTextContent(org.w3c.dom.Node child)
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.(package private) org.w3c.dom.Node
internalInsertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild, boolean replace)
(package private) org.w3c.dom.Node
internalRemoveChild(org.w3c.dom.Node oldChild, boolean replace)
boolean
isEqualNode(org.w3c.dom.Node arg)
Tests whether two nodes are equal.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.(package private) ChildNode
lastChild()
(package private) int
nodeListGetLength()
Count the immediate children of this node.(package private) org.w3c.dom.Node
nodeListItem(int index)
(package private) CoreDocumentImpl
ownerDocument()
same as above but returns internal type and this one is not overridden by CoreDocumentImpl to return nullorg.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 childrenvoid
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.htmlunit.cyberneko.xerces.dom.ChildNode
getNextSibling, getParentNode, getPreviousSibling, parentNode, previousSibling
-
Methods inherited from class org.htmlunit.cyberneko.xerces.dom.NodeImpl
addEventListener, appendChild, changed, changes, compareDocumentPosition, dispatchEvent, getAttributes, getBaseURI, getElementAncestor, getFeature, getLocalName, getNamespaceURI, getNodeName, getNodeNumber, getNodeType, getNodeValue, getPrefix, getUserData, hasAttributes, hasStringValue, hasStringValue, isDefaultNamespace, isFirstChild, isFirstChild, isIdAttribute, isIdAttribute, isNormalized, isNormalized, isOwned, isOwned, isSameNode, isSpecified, isSpecified, isSupported, lookupNamespacePrefix, lookupNamespaceURI, lookupPrefix, needsSyncChildren, needsSyncChildren, normalize, removeEventListener, setNodeValue, setPrefix, setUserData, toString
-
-
-
-
Field Detail
-
ownerDocument
protected CoreDocumentImpl ownerDocument
Owner document.
-
firstChild
protected ChildNode firstChild
First child.
-
fNodeListCache
protected 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- Parameters:
ownerDocument
- the owner document
-
-
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.Note: since we never have any children deep is meaningless here, ParentNode overrides this behavior. 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.
Note: since we never have any children deep is meaningless here, ParentNode overrides this behavior. 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 interfaceorg.w3c.dom.Node
- Overrides:
cloneNode
in classChildNode
- 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. 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 interfaceorg.w3c.dom.Node
- Overrides:
getOwnerDocument
in classNodeImpl
-
ownerDocument
CoreDocumentImpl ownerDocument()
same as above but returns internal type and this one is not overridden by CoreDocumentImpl to return null Same as above but returns internal type and this one is not overridden by CoreDocumentImpl to return null- Overrides:
ownerDocument
in classNodeImpl
-
setOwnerDocument
protected void setOwnerDocument(CoreDocumentImpl doc)
NON-DOM set the ownerDocument of this node and its children- Overrides:
setOwnerDocument
in classNodeImpl
-
hasChildNodes
public boolean hasChildNodes()
Test whether this node has any children. Convenience shorthand for (Node.getFirstChild()!=null)By default we do not have any children, ParentNode overrides this. Test whether this node has any children. Convenience shorthand for (Node.getFirstChild()!=null)
- Specified by:
hasChildNodes
in interfaceorg.w3c.dom.Node
- Overrides:
hasChildNodes
in classNodeImpl
- 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. 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 interfaceorg.w3c.dom.Node
- Overrides:
getChildNodes
in classNodeImpl
-
getFirstChild
public org.w3c.dom.Node getFirstChild()
The first child of this Node, or null if none.By default we do not have any children, ParentNode overrides this.
- Specified by:
getFirstChild
in interfaceorg.w3c.dom.Node
- Overrides:
getFirstChild
in classNodeImpl
- See Also:
ParentNode
-
getLastChild
public org.w3c.dom.Node getLastChild()
The first child of this Node, or null if none.By default we do not have any children, ParentNode overrides this.
- Specified by:
getLastChild
in interfaceorg.w3c.dom.Node
- Overrides:
getLastChild
in classNodeImpl
- See Also:
ParentNode
-
lastChild
final ChildNode lastChild()
-
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.By default we do not accept any children, ParentNode overrides this. 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 interfaceorg.w3c.dom.Node
- Overrides:
insertBefore
in classNodeImpl
- 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:
org.w3c.dom.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.org.w3c.dom.DOMException
- WRONG_DOCUMENT_ERR if newChild has a different owner document than we do.org.w3c.dom.DOMException
- NOT_FOUND_ERR if refChild is not a child of this node.org.w3c.dom.DOMException
- NO_MODIFICATION_ALLOWED_ERR if this node is read-only.- See Also:
ParentNode
-
internalInsertBefore
org.w3c.dom.Node internalInsertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild, boolean replace) throws org.w3c.dom.DOMException
- Throws:
org.w3c.dom.DOMException
-
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.By default we do not have any children, ParentNode overrides this. Remove a child from this Node. The removed child's subtree remains intact so it may be re-inserted elsewhere.
- Specified by:
removeChild
in interfaceorg.w3c.dom.Node
- Overrides:
removeChild
in classNodeImpl
- Returns:
- oldChild, in its new state (removed).
- Throws:
org.w3c.dom.DOMException
- NOT_FOUND_ERR if oldChild is not a child of this node.org.w3c.dom.DOMException
- NO_MODIFICATION_ALLOWED_ERR if this node is read-only.- See Also:
ParentNode
-
internalRemoveChild
org.w3c.dom.Node internalRemoveChild(org.w3c.dom.Node oldChild, boolean replace) throws org.w3c.dom.DOMException
- Throws:
org.w3c.dom.DOMException
-
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.By default we do not have any children, ParentNode overrides this. 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 interfaceorg.w3c.dom.Node
- Overrides:
replaceChild
in classNodeImpl
- Returns:
- oldChild, in its new state (removed).
- Throws:
org.w3c.dom.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.org.w3c.dom.DOMException
- WRONG_DOCUMENT_ERR if newChild has a different owner document than we do.org.w3c.dom.DOMException
- NOT_FOUND_ERR if oldChild is not a child of this node.org.w3c.dom.DOMException
- NO_MODIFICATION_ALLOWED_ERR if this node is read-only.- 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 singleText
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 nodesATTRIBUTE_NODE, TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE nodeValue
DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE null - Specified by:
getTextContent
in interfaceorg.w3c.dom.Node
- Overrides:
getTextContent
in classNodeImpl
- Throws:
org.w3c.dom.DOMException
- DOMSTRING_SIZE_ERR: Raised when it would return more characters than fit in aDOMString
variable on the implementation platform.
-
getTextContent
void getTextContent(java.lang.StringBuilder builder) throws org.w3c.dom.DOMException
- Overrides:
getTextContent
in classNodeImpl
- Throws:
org.w3c.dom.DOMException
-
hasTextContent
static final boolean hasTextContent(org.w3c.dom.Node child)
-
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 singleText
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 nodesATTRIBUTE_NODE, TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE nodeValue
DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE null - Specified by:
setTextContent
in interfaceorg.w3c.dom.Node
- Overrides:
setTextContent
in classNodeImpl
- Throws:
org.w3c.dom.DOMException
- DOMSTRING_SIZE_ERR: Raised when it would return more characters than fit in aDOMString
variable on the implementation platform.
-
nodeListGetLength
int nodeListGetLength()
Count the immediate children of this node. Use to implement NodeList.getLength().- Returns:
- the length
-
getLength
public int getLength()
NodeList method: Count the immediate children of this nodeBy default we do not have any children, ParentNode overrides this. NodeList method: Count the immediate children of this node
-
nodeListItem
org.w3c.dom.Node nodeListItem(int index)
- Parameters:
index
- the index- Returns:
- the Nth immediate child of this node, or null if the index is out of bounds. Use to implement NodeList.item().
-
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.By default we do not have any children, ParentNode overrides this. NodeList method: Return the Nth immediate child of this node, or null if the index is out of bounds.
-
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.- Returns:
- a node list
-
isEqualNode
public boolean isEqualNode(org.w3c.dom.Node arg)
Tests whether two nodes are equal.
This method tests for equality of nodes, not sameness (i.e., whether the two nodes are references to the same object) which can be tested withNode.isSameNode
. All nodes that are the same will also be equal, though the reverse may not be true.
Two nodes are equal if and only if the following conditions are satisfied: The two nodes are of the same type.The following string attributes are equal:nodeName
,localName
,namespaceURI
,prefix
,nodeValue
,baseURI
. This is: they are bothnull
, or they have the same length and are character for character identical. Theattributes
NamedNodeMaps
are equal. This is: they are bothnull
, or they have the same length and for each node that exists in one map there is a node that exists in the other map and is equal, although not necessarily at the same index.ThechildNodes
NodeLists
are equal. This is: they are bothnull
, or they have the same length and contain equal nodes at the same index. This is true forAttr
nodes as for any other type of node. Note that normalization can affect equality; to avoid this, nodes should be normalized before being compared.
For twoDocumentType
nodes to be equal, the following conditions must also be satisfied: The following string attributes are equal:publicId
,systemId
,internalSubset
.Theentities
NamedNodeMaps
are equal.Thenotations
NamedNodeMaps
are equal.
On the other hand, the following do not affect equality: theownerDocument
attribute, thespecified
attribute forAttr
nodes, theisWhitespaceInElementContent
attribute forText
nodes, as well as any user data or event listeners registered on the nodes. DOM Level 3 WD- Experimental. Override inherited behavior from NodeImpl to support deep equal.- Specified by:
isEqualNode
in interfaceorg.w3c.dom.Node
- Overrides:
isEqualNode
in classNodeImpl
- Parameters:
arg
- The node to compare equality with.- Returns:
- If the nodes, and possibly subtrees are equal,
true
otherwisefalse
.
-
synchronizeChildren
protected void synchronizeChildren()
Override this method in subclass to hook in efficient internal data structure.
-
checkNormalizationAfterInsert
void checkNormalizationAfterInsert(ChildNode insertedChild)
Checks the normalized state of this node after inserting a child. If the inserted child causes this node to be unnormalized, then this node is flagged accordingly. The conditions for changing the normalized state are:- The inserted child is a text node and one of its adjacent siblings is also a text node.
- The inserted child is is itself unnormalized.
- Parameters:
insertedChild
- the child node that was inserted into this node- Throws:
java.lang.NullPointerException
- if the inserted child isnull
-
checkNormalizationAfterRemove
void checkNormalizationAfterRemove(ChildNode previousSibling)
Checks the normalized of this node after removing a child. If the removed child causes this node to be unnormalized, then this node is flagged accordingly. The conditions for changing the normalized state are:- The removed child had two adjacent siblings that were text nodes.
- Parameters:
previousSibling
- the previous sibling of the removed child, ornull
-
-