Class Node
java.lang.Object
groovy.util.Node
- All Implemented Interfaces:
Serializable
Represents an arbitrary tree node which can be used for structured metadata or any arbitrary XML-like tree.
A node can have a name, a value and an optional Map of attributes.
Typically the name is a String and a value is either a String or a List of other Nodes,
though the types are extensible to provide a flexible structure, e.g. you could use a
QName as the name which includes a namespace URI and a local name. Or a JMX ObjectName etc.
So this class can represent metadata like
{foo a=1 b="abc"} or nested
metadata like {foo a=1 b="123" { bar x=12 text="hello" }}- Author:
- James Strachan, Paul King
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new Node namednameand if a parent is supplied, adds the newly created node as a child of the parent.Creates a new Node namednamewith valuevalueand if a parent is supplied, adds the newly created node as a child of the parent.Creates a new Node namednamewith attributes specified in theattributesMap.Creates a new Node namednamewith valuevalueand with attributes specified in theattributesMap. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAppends a child to the current node.appendNode(Object name) Creates a new node as a child of the current node.appendNode(Object name, Object value) Creates a new node as a child of the current node.appendNode(Object name, Map attributes) Creates a new node as a child of the current node.appendNode(Object name, Map attributes, Object value) Creates a new node as a child of the current node.Provides lookup of attributes by key.Returns aMapof the attributes of the node or an emptyMapif the node does not have any attributes.Provides a collection of all the nodes in the tree using a breadth-first traversal.children()Returns aListof the nodes children.Provides a collection of all the nodes in the tree using a depth first traversal.Provides lookup of elements by non-namespaced nameProvides lookup of elements by QName.iterator()Returns anIteratorof the children of the node.name()Returns anObjectrepresenting the name of the node.parent()Returns the parent of the node.voidAdds sibling nodes (defined using builder-style notation via a Closure) after the current node.voidprint(PrintWriter out) Writes the node to the specifiedPrintWriter.booleanRemoves a child of the current node.Replaces the current node with nodes defined using builder-style notation via a Closure.protected static voidsetMetaClass(MetaClass metaClass, Class nodeClass) Extension point for subclasses to override the metaclass.protected voidAdds or replaces the parent of the node.voidAdds or replaces the value of the node.text()Returns the textual representation of the current node and all its child nodes.toString()value()Returns anObjectrepresenting the value of the node.
-
Constructor Details
-
Node
-
Node
Creates a new Node namednamewith valuevalueand if a parent is supplied, adds the newly created node as a child of the parent.- Parameters:
parent- the parent node or null if no parentname- the name of the nodevalue- the Node value, e.g. some text but in general any Object
-
Node
Creates a new Node namednamewith attributes specified in theattributesMap. If a parent is supplied, the newly created node is added as a child of the parent.- Parameters:
parent- the parent node or null if no parentname- the name of the nodeattributes- a Map of name-value pairs
-
Node
Creates a new Node namednamewith valuevalueand with attributes specified in theattributesMap. If a parent is supplied, the newly created node is added as a child of the parent.- Parameters:
parent- the parent node or null if no parentname- the name of the nodeattributes- a Map of name-value pairsvalue- the Node value, e.g. some text but in general any Object
-
-
Method Details
-
append
Appends a child to the current node.- Parameters:
child- the child to append- Returns:
true
-
remove
Removes a child of the current node.- Parameters:
child- the child to remove- Returns:
trueif the param was a child of the current node
-
appendNode
-
appendNode
-
appendNode
-
appendNode
-
replaceNode
-
plus
Adds sibling nodes (defined using builder-style notation via a Closure) after the current node.- Parameters:
c- A Closure defining the new sibling nodes to add using builder-style notation.
-
setMetaClass
Extension point for subclasses to override the metaclass. The default one supports the property and @ attribute notations.- Parameters:
metaClass- the original metaclassnodeClass- the class whose metaclass we wish to override (this class or a subclass)
-
text
Returns the textual representation of the current node and all its child nodes.- Returns:
- the text value of the node including child text
-
iterator
Returns anIteratorof the children of the node.- Returns:
- the iterator of the nodes children
-
children
-
attributes
Returns aMapof the attributes of the node or an emptyMapif the node does not have any attributes.- Returns:
- the attributes of the node
-
attribute
-
name
Returns anObjectrepresenting the name of the node.- Returns:
- the name or
nullif name is empty
-
value
Returns anObjectrepresenting the value of the node.- Returns:
- the value or
nullif value is empty
-
setValue
Adds or replaces the value of the node.- Parameters:
value- the new value of the node
-
parent
-
setParent
Adds or replaces the parent of the node.- Parameters:
parent- the new parent of the node
-
get
-
getAt
-
depthFirst
Provides a collection of all the nodes in the tree using a depth first traversal.- Returns:
- the list of (depth-first) ordered nodes
-
breadthFirst
Provides a collection of all the nodes in the tree using a breadth-first traversal.- Returns:
- the list of (breadth-first) ordered nodes
-
toString
-
print
Writes the node to the specifiedPrintWriter.- Parameters:
out- the writer receiving the output
-