Class ParentNode
- java.lang.Object
-
- nu.xom.Node
-
- nu.xom.ParentNode
-
public abstract class ParentNode extends Node
The generic superclass for nodes that have children. Not counting subclasses, there are exactly two such public classes in XOM:
DocumentElement
This class provides methods to add and remove child nodes.
- Version:
- 1.3.0
- Author:
- Elliotte Rusty Harold
-
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidappendChild(Node child)Appends a node to the children of this node.NodegetChild(int position)Returns the child of this node at the specified position.intgetChildCount()Returns the number of child nodes this node contains.intindexOf(Node child)Returns the position of a node within the children of this node.voidinsertChild(Node child, int position)Inserts a child node at the specified position.NoderemoveChild(int position)Removes the child of this node at the specified position.NoderemoveChild(Node child)Removes the specified child of this node.voidreplaceChild(Node oldChild, Node newChild)Replaces an existing child with a new child node.abstract voidsetBaseURI(java.lang.String URI)Sets the URI against which relative URIs in this node will be resolved.
-
-
-
Method Detail
-
getChildCount
public int getChildCount()
Returns the number of child nodes this node contains. This is always greater than or equal to 0.
- Specified by:
getChildCountin classNode- Returns:
- the number of children of this node
-
insertChild
public void insertChild(Node child, int position)
Inserts a child node at the specified position. The child node previously at that position (if any) and all subsequent child nodes are moved up by one. That is, when inserting a node at 2, the old node at 2 is moved to 3, the old child at 3 is moved to 4, and so forth. Inserting at position 0 makes the child the first child of this node. Inserting at the position
getChildCount()makes the child the last child of the node.All the other methods that add a node to the tree ultimately invoke this method.
- Parameters:
position- where to insert the childchild- the node to insert- Throws:
IllegalAddException- if this node cannot have a child of the argument's typeMultipleParentException- ifchildalready has a parentjava.lang.NullPointerException- ifchildis nulljava.lang.IndexOutOfBoundsException- if the position is negative or greater than the number of children of this node
-
appendChild
public void appendChild(Node child)
Appends a node to the children of this node.
- Parameters:
child- node to append to this node- Throws:
IllegalAddException- if this node cannot have children of this typeMultipleParentException- if child already has a parentjava.lang.NullPointerException- ifchildis null
-
getChild
public Node getChild(int position)
Returns the child of this node at the specified position. Indexes begin at 0 and count up to one less than the number of children in this node.
-
indexOf
public int indexOf(Node child)
Returns the position of a node within the children of this node. This is a number between 0 and one less than the number of children of this node. It returns -1 if
childdoes not have this node as a parent.This method does a linear search through the node's children. On average, it executes in O(N) where N is the number of children of the node.
- Parameters:
child- the node whose position is desired- Returns:
- the position of the argument node among the children of this node
-
removeChild
public Node removeChild(int position)
Removes the child of this node at the specified position. Indexes begin at 0 and count up to one less than the number of children in this node.
- Parameters:
position- index of the node to remove- Returns:
- the node which was removed
- Throws:
java.lang.IndexOutOfBoundsException- if the index is negative or greater than or equal to the number of children of this node
-
removeChild
public Node removeChild(Node child)
Removes the specified child of this node.
- Parameters:
child- child node to remove- Returns:
- the node which was removed
- Throws:
NoSuchChildException- ifchildis not in fact a child of this node
-
replaceChild
public void replaceChild(Node oldChild, Node newChild)
Replaces an existing child with a new child node. If
oldChildis not a child of this node, then aNoSuchChildExceptionis thrown.- Parameters:
oldChild- the node removed from the treenewChild- the node inserted into the tree- Throws:
MultipleParentException- ifnewChildalready has a parentNoSuchChildException- ifoldChildis not a child of this nodejava.lang.NullPointerException- if either argument is nullIllegalAddException- if this node cannot have children of the type ofnewChild
-
setBaseURI
public abstract void setBaseURI(java.lang.String URI)
Sets the URI against which relative URIs in this node will be resolved. Generally, it's only necessary to set this property if it's different from a node's parent's base URI, as it may be in a document assembled from multiple entities or by XInclude.
Relative URIs are not allowed here. Base URIs must be absolute. However, the base URI may be set to null or the empty string to indicate that the node has no explicit base URI. In this case, it inherits the base URI of its parent node, if any.
URIs with fragment identifiers are also not allowed. The value passed to this method must be a pure URI, not a URI reference.
You can also add an
xml:baseattribute to an element in the same way you'd add any other namespaced attribute to an element. If an element's base URI conflicts with itsxml:baseattribute, then the value found in thexml:baseattribute is used.If the base URI is null or the empty string and there is no
xml:baseattribute, then the base URI is determined by the nearest ancestor node which does have a base URI. Moving such a node from one location to another can change its base URI.- Parameters:
URI- the new base URI for this node- Throws:
MalformedURIException- ifURIis not a legal RFC 3986 absolute URI
-
-