Package groovy.util
Class Node
java.lang.Object
groovy.util.Node
- All Implemented Interfaces:
- java.io.Serializable,- java.lang.Cloneable
public class Node
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable
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" }}- See Also:
- Serialized Form
- 
Constructor SummaryConstructors Constructor Description Node(Node parent, java.lang.Object name)Creates a new Node namednameand if a parent is supplied, adds the newly created node as a child of the parent.Node(Node parent, java.lang.Object name, java.lang.Object value)Creates a new Node namednamewith valuevalueand if a parent is supplied, adds the newly created node as a child of the parent.Node(Node parent, java.lang.Object name, java.util.Map attributes)Creates a new Node namednamewith attributes specified in theattributesMap.Node(Node parent, java.lang.Object name, java.util.Map attributes, java.lang.Object value)Creates a new Node namednamewith valuevalueand with attributes specified in theattributesMap.
- 
Method SummaryModifier and Type Method Description booleanappend(Node child)Appends a child to the current node.NodeappendNode(java.lang.Object name)Creates a new node as a child of the current node.NodeappendNode(java.lang.Object name, java.lang.Object value)Creates a new node as a child of the current node.NodeappendNode(java.lang.Object name, java.util.Map attributes)Creates a new node as a child of the current node.NodeappendNode(java.lang.Object name, java.util.Map attributes, java.lang.Object value)Creates a new node as a child of the current node.java.lang.Objectattribute(java.lang.Object key)Provides lookup of attributes by key.java.util.Mapattributes()Returns aMapof the attributes of the node or an emptyMapif the node does not have any attributes.java.util.ListbreadthFirst()Provides a collection of all the nodes in the tree using a breadth-first preorder traversal.java.util.ListbreadthFirst(boolean preorder)Provides a collection of all the nodes in the tree using a breadth-first traversal.voidbreadthFirst(Closure c)Calls the provided closure for all the nodes in the tree using a breadth-first preorder traversal.voidbreadthFirst(java.util.Map<java.lang.String,java.lang.Object> options, Closure c)Calls the provided closure for all the nodes in the tree using a breadth-first traversal.java.util.Listchildren()Returns aListof the nodes children.java.lang.Objectclone()Creates a new Node with the same name, no parent, shallow cloned attributes and if the value is a NodeList, a (deep) clone of those nodes.java.util.ListdepthFirst()Provides a collection of all the nodes in the tree using a depth-first preorder traversal.java.util.ListdepthFirst(boolean preorder)Provides a collection of all the nodes in the tree using a depth-first traversal.voiddepthFirst(Closure c)Provides a collection of all the nodes in the tree using a depth-first preorder traversal.voiddepthFirst(java.util.Map<java.lang.String,java.lang.Object> options, Closure c)Provides a collection of all the nodes in the tree using a depth-first traversal.java.lang.Objectget(java.lang.String key)Provides lookup of elements by non-namespaced nameNodeListgetAt(QName name)Provides lookup of elements by QName.NodeListgetAt(QName name)Deprecated.java.util.Iteratoriterator()Returns anIteratorof the children of the node.java.util.List<java.lang.String>localText()Returns the list of any direct String nodes of this node.java.lang.Objectname()Returns anObjectrepresenting the name of the node.Nodeparent()Returns the parent of the node.voidplus(Closure c)Adds sibling nodes (defined using builder-style notation via a Closure) after the current node.voidprint(java.io.PrintWriter out)Writes the node to the specifiedPrintWriter.booleanremove(Node child)Removes a child of the current node.NodereplaceNode(Closure c)Replaces the current node with nodes defined using builder-style notation via a Closure.NodereplaceNode(Node n)Replaces the current node with the supplied node.protected static voidsetMetaClass(MetaClass metaClass, java.lang.Class nodeClass)Extension point for subclasses to override the metaclass.protected voidsetParent(Node parent)Adds or replaces the parent of the node.voidsetValue(java.lang.Object value)Adds or replaces the value of the node.java.lang.Stringtext()Returns the textual representation of the current node and all its child nodes.java.math.BigDecimaltoBigDecimal()Converts the text of this GPathResult to a BigDecimal object.java.math.BigIntegertoBigInteger()Converts the text of this GPathResult to a BigInteger object.java.lang.DoubletoDouble()Converts the text of this GPathResult to a Double object.java.lang.FloattoFloat()Converts the text of this GPathResult to a Float object.java.lang.IntegertoInteger()Converts the text of this GPathResult to a Integer object.java.lang.LongtoLong()Converts the text of this GPathResult to a Long object.java.lang.StringtoString()java.lang.Objectvalue()Returns anObjectrepresenting the value of the node.Methods inherited from class java.lang.Objectequals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
- 
Constructor Details- 
NodeCreates a new Node namednameand 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 parent
- name- the name of the node
 
- 
NodeCreates 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 parent
- name- the name of the node
- value- the Node value, e.g. some text but in general any Object
 
- 
NodeCreates 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 parent
- name- the name of the node
- attributes- a Map of name-value pairs
 
- 
NodeCreates 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 parent
- name- the name of the node
- attributes- a Map of name-value pairs
- value- the Node value, e.g. some text but in general any Object
 
 
- 
- 
Method Details- 
clonepublic java.lang.Object clone()Creates a new Node with the same name, no parent, shallow cloned attributes and if the value is a NodeList, a (deep) clone of those nodes.- Overrides:
- clonein class- java.lang.Object
- Returns:
- the clone
 
- 
appendAppends a child to the current node.- Parameters:
- child- the child to append
- Returns:
- true
 
- 
removeRemoves a child of the current node.- Parameters:
- child- the child to remove
- Returns:
- trueif the param was a child of the current node
 
- 
appendNodeCreates a new node as a child of the current node.- Parameters:
- name- the name of the new node
- attributes- the attributes of the new node
- Returns:
- the newly created Node
 
- 
appendNodeCreates a new node as a child of the current node.- Parameters:
- name- the name of the new node
- Returns:
- the newly created Node
 
- 
appendNodeCreates a new node as a child of the current node.- Parameters:
- name- the name of the new node
- value- the value of the new node
- Returns:
- the newly created Node
 
- 
appendNodeCreates a new node as a child of the current node.- Parameters:
- name- the name of the new node
- attributes- the attributes of the new node
- value- the value of the new node
- Returns:
- the newly created Node
 
- 
replaceNodeReplaces the current node with nodes defined using builder-style notation via a Closure.- Parameters:
- c- A Closure defining the new nodes using builder-style notation.
- Returns:
- the original now replaced node
 
- 
replaceNodeReplaces the current node with the supplied node.- Parameters:
- n- the new Node
- Returns:
- the original now replaced node
 
- 
plusAdds 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.
 
- 
setMetaClassExtension point for subclasses to override the metaclass. The default one supports the property and @ attribute notations.- Parameters:
- metaClass- the original metaclass
- nodeClass- the class whose metaclass we wish to override (this class or a subclass)
 
- 
textpublic java.lang.String text()Returns the textual representation of the current node and all its child nodes.- Returns:
- the text value of the node including child text
 
- 
iteratorpublic java.util.Iterator iterator()Returns anIteratorof the children of the node.- Returns:
- the iterator of the nodes children
 
- 
childrenpublic java.util.List children()Returns aListof the nodes children.- Returns:
- the nodes children
 
- 
attributespublic java.util.Map attributes()Returns aMapof the attributes of the node or an emptyMapif the node does not have any attributes.- Returns:
- the attributes of the node
 
- 
attributepublic java.lang.Object attribute(java.lang.Object key)Provides lookup of attributes by key.- Parameters:
- key- the key of interest
- Returns:
- the attribute matching the key or nullif no match exists
 
- 
namepublic java.lang.Object name()Returns anObjectrepresenting the name of the node.- Returns:
- the name or nullif name is empty
 
- 
valuepublic java.lang.Object value()Returns anObjectrepresenting the value of the node.- Returns:
- the value or nullif value is empty
 
- 
setValuepublic void setValue(java.lang.Object value)Adds or replaces the value of the node.- Parameters:
- value- the new value of the node
 
- 
parentReturns the parent of the node.- Returns:
- the parent or nullfor the root node
 
- 
setParentAdds or replaces the parent of the node.- Parameters:
- parent- the new parent of the node
 
- 
getpublic java.lang.Object get(java.lang.String key)Provides lookup of elements by non-namespaced name- Parameters:
- key- the name (or shortcut key) of the node(s) of interest
- Returns:
- the nodes which match key
 
- 
getAtProvides lookup of elements by QName.- Parameters:
- name- the QName of interest
- Returns:
- the nodes matching name
 
- 
getAtDeprecated.usegetAt(groovy.namespace.QName)insteadProvides lookup of elements by QName.- Parameters:
- name- the QName of interest
- Returns:
- the nodes matching name
 
- 
depthFirstpublic java.util.List depthFirst()Provides a collection of all the nodes in the tree using a depth-first preorder traversal.- Returns:
- the list of (depth-first) ordered nodes
 
- 
depthFirstpublic java.util.List depthFirst(boolean preorder)Provides a collection of all the nodes in the tree using a depth-first traversal.- Parameters:
- preorder- if false, a postorder depth-first traversal will be performed
- Returns:
- the list of (depth-first) ordered nodes
- Since:
- 2.5.0
 
- 
depthFirstProvides a collection of all the nodes in the tree using a depth-first preorder traversal.- Parameters:
- c- the closure to run for each node (a one or two parameter can be used; if one parameter is given the closure will be passed the node, for a two param closure the second parameter will be the level).
- Since:
- 2.5.0
 
- 
depthFirstProvides a collection of all the nodes in the tree using a depth-first traversal. A boolean 'preorder' options is supported.- Parameters:
- options- map containing options
- c- the closure to run for each node (a one or two parameter can be used; if one parameter is given the closure will be passed the node, for a two param closure the second parameter will be the level).
- Since:
- 2.5.0
 
- 
breadthFirstpublic java.util.List breadthFirst()Provides a collection of all the nodes in the tree using a breadth-first preorder traversal.- Returns:
- the list of (breadth-first) ordered nodes
 
- 
breadthFirstpublic java.util.List breadthFirst(boolean preorder)Provides a collection of all the nodes in the tree using a breadth-first traversal.- Parameters:
- preorder- if false, a postorder breadth-first traversal will be performed
- Returns:
- the list of (breadth-first) ordered nodes
- Since:
- 2.5.0
 
- 
breadthFirstCalls the provided closure for all the nodes in the tree using a breadth-first preorder traversal.- Parameters:
- c- the closure to run for each node (a one or two parameter can be used; if one parameter is given the closure will be passed the node, for a two param closure the second parameter will be the level).
- Since:
- 2.5.0
 
- 
breadthFirstCalls the provided closure for all the nodes in the tree using a breadth-first traversal. A boolean 'preorder' options is supported.- Parameters:
- options- map containing options
- c- the closure to run for each node (a one or two parameter can be used; if one parameter is given the closure will be passed the node, for a two param closure the second parameter will be the level).
- Since:
- 2.5.0
 
- 
localTextpublic java.util.List<java.lang.String> localText()Returns the list of any direct String nodes of this node.- Returns:
- the list of String values from this node
- Since:
- 2.3.0
 
- 
toStringpublic java.lang.String toString()- Overrides:
- toStringin class- java.lang.Object
 
- 
printpublic void print(java.io.PrintWriter out)Writes the node to the specifiedPrintWriter.- Parameters:
- out- the writer receiving the output
 
- 
toIntegerpublic java.lang.Integer toInteger()Converts the text of this GPathResult to a Integer object.- Returns:
- the GPathResult, converted to a Integer
 
- 
toLongpublic java.lang.Long toLong()Converts the text of this GPathResult to a Long object.- Returns:
- the GPathResult, converted to a Long
 
- 
toFloatpublic java.lang.Float toFloat()Converts the text of this GPathResult to a Float object.- Returns:
- the GPathResult, converted to a Float
 
- 
toDoublepublic java.lang.Double toDouble()Converts the text of this GPathResult to a Double object.- Returns:
- the GPathResult, converted to a Double
 
- 
toBigDecimalpublic java.math.BigDecimal toBigDecimal()Converts the text of this GPathResult to a BigDecimal object.- Returns:
- the GPathResult, converted to a BigDecimal
 
- 
toBigIntegerpublic java.math.BigInteger toBigInteger()Converts the text of this GPathResult to a BigInteger object.- Returns:
- the GPathResult, converted to a BigInteger
 
 
- 
getAt(groovy.namespace.QName)instead