Top Description Fields Constructors Methods
groovy.util

public Class Node

extends Object
implements Serializable, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, java.io.Serializable
Imports
groovy.lang.Closure, .DelegatingMetaClass, .GroovySystem, .MetaClass, .Tuple2, groovy.namespace.QName, org.codehaus.groovy.runtime.DefaultGroovyMethods, .InvokerHelper, .StringGroovyMethods, org.codehaus.groovy.util.ListHashMap, java.io.PrintWriter, .Serializable, java.math.BigDecimal, .BigInteger, java.util.ArrayList, .Collection, .HashMap, .Iterator, .List, .Map, .Stack

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" }}

Field Summary

Modifier and TypeField and Description
private final Map<K, V>
private final Object
private Node
private static final long
private Object

Constructor Summary

AccessConstructor and Description
public
Node(Node
the parent node or null if no parent
parent
,
Object
the name of the node
name
)

Creates a new Node named name and if a parent is supplied, adds the newly created node as a child of the parent.

public
Node(Node
the parent node or null if no parent
parent
,
Object
the name of the node
name
,
Object
the Node value, e.g. some text but in general any Object
value
)

Creates a new Node named name with value value and if a parent is supplied, adds the newly created node as a child of the parent.

public
Node(Node
the parent node or null if no parent
parent
,
Object
the name of the node
name
,
Map<K, V>
a Map of name-value pairs
attributes
)

Creates a new Node named name with attributes specified in the attributes Map.

public
Node(Node
the parent node or null if no parent
parent
,
Object
the name of the node
name
,
Map<K, V>
a Map of name-value pairs
attributes
,
Object
the Node value, e.g. some text but in general any Object
value
)

Creates a new Node named name with value value and with attributes specified in the attributes Map.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true
append
(Node
the child to append
child
)

Appends a child to the current node.

public Node

Returns:

the newly created Node
appendNode
(Object
the name of the new node
name
,
Map<K, V>
the attributes of the new node
attributes
)

Creates a new node as a child of the current node.

public Node

Returns:

the newly created Node
appendNode
(Object
the name of the new node
name
)

Creates a new node as a child of the current node.

public Node

Returns:

the newly created Node
appendNode
(Object
the name of the new node
name
,
Object
the value of the new node
value
)

Creates a new node as a child of the current node.

public Node

Returns:

the newly created Node
appendNode
(Object
the name of the new node
name
,
Map<K, V>
the attributes of the new node
attributes
,
Object
the value of the new node
value
)

Creates a new node as a child of the current node.

private void
public Object

Returns:

the attribute matching the key or null if no match exists
attribute
(Object
the key of interest
key
)

Provides lookup of attributes by key.

public Map<K, V>

Returns:

the attributes of the node
attributes
()

Returns a Map of the attributes of the node or an empty Map if the node does not have any attributes.

public List<E>

Returns:

the list of (breadth-first) ordered nodes
breadthFirst
()

Provides a collection of all the nodes in the tree using a breadth-first preorder traversal.

public List<E>

Returns:

the list of (breadth-first) ordered nodes
breadthFirst
(boolean
if false, a postorder breadth-first traversal will be performed
preorder
)

Provides a collection of all the nodes in the tree using a breadth-first traversal.

public void
breadthFirst(Closure<V>
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).
c
)

Calls the provided closure for all the nodes in the tree using a breadth-first preorder traversal.

public void
breadthFirst(Map<String, Object>
map containing options
options
,
Closure<V>
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).
c
)

Calls the provided closure for all the nodes in the tree using a breadth-first traversal.

private List<E>
breadthFirstRest(boolean preorder)

private void
breadthFirstRest(boolean preorder, int level, Closure<V> c)

private static List<Node>
private static <T> T
callClosureForNode(Closure<T> closure, Object node, int level)

public List<E>

Returns:

the nodes children
children
()

Returns a List of the nodes children.

public Object

Returns:

the clone
clone
()

Overrides 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.
public List<E>

Returns:

the list of (depth-first) ordered nodes
depthFirst
()

Provides a collection of all the nodes in the tree using a depth-first preorder traversal.

public List<E>

Returns:

the list of (depth-first) ordered nodes
depthFirst
(boolean
if false, a postorder depth-first traversal will be performed
preorder
)

Provides a collection of all the nodes in the tree using a depth-first traversal.

public void
depthFirst(Closure<V>
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).
c
)

Provides a collection of all the nodes in the tree using a depth-first preorder traversal.

public void
depthFirst(Map<String, Object>
map containing options
options
,
Closure<V>
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).
c
)

Provides a collection of all the nodes in the tree using a depth-first traversal.

private List<E>
depthFirstRest(boolean preorder)

private void
depthFirstRest(boolean preorder, int level, Closure<V> c)

public Object

Returns:

the nodes which match key
get
(String
the name (or shortcut key) of the node(s) of interest
key
)

Provides lookup of elements by non-namespaced name

public NodeList

Returns:

the nodes matching name
getAt
(QName
the QName of interest
name
)

Provides lookup of elements by QName.

public NodeList

Returns:

the nodes matching name
getAt
(QName
the QName of interest
name
)

Deprecated use getAt(groovy.namespace.QName) instead
Provides lookup of elements by QName.
private NodeList

Returns:

the nodes matching name
getByName
(String
the name of interest
name
)

Provides lookup of elements by name.

private List<E>
private static List<E>
private List<E>
public Iterator<E>

Returns:

the iterator of the nodes children
iterator
()

Returns an Iterator of the children of the node.

public List<String>

Returns:

the list of String values from this node
localText
()

Returns the list of any direct String nodes of this node.

public Object

Returns:

the name or null if name is empty
name
()

Returns an Object representing the name of the node.

public Node

Returns:

the parent or null for the root node
parent
()

Returns the parent of the node.

public void
plus(Closure<V>
A Closure defining the new sibling nodes to add using builder-style notation.
c
)

Adds sibling nodes (defined using builder-style notation via a Closure) after the current node.

public boolean

Returns:

true if the param was a child of the current node
remove
(Node
the child to remove
child
)

Removes a child of the current node.

public Node

Returns:

the original now replaced node
replaceNode
(Closure<V>
A Closure defining the new nodes using builder-style notation.
c
)

Replaces the current node with nodes defined using builder-style notation via a Closure.

public Node

Returns:

the original now replaced node
replaceNode
(Node
the new Node
n
)

Replaces the current node with the supplied node.

protected static void
setMetaClass(final MetaClass
the original metaclass
metaClass
,
Class<T>
the class whose metaclass we wish to override (this class or a subclass)
nodeClass
)

Extension point for subclasses to override the metaclass.

protected void
setParent(Node
the new parent of the node
parent
)

Adds or replaces the parent of the node.

public void
setValue(Object
the new value of the node
value
)

Adds or replaces the value of the node.

public String

Returns:

the text value of the node including child text
text
()

Returns the textual representation of the current node and all its child nodes.

private boolean
public BigDecimal

Returns:

the GPathResult, converted to a BigDecimal
toBigDecimal
()

Converts the text of this GPathResult to a BigDecimal object.

public BigInteger

Returns:

the GPathResult, converted to a BigInteger
toBigInteger
()

Converts the text of this GPathResult to a BigInteger object.

public Double

Returns:

the GPathResult, converted to a Double
toDouble
()

Converts the text of this GPathResult to a Double object.

public Float

Returns:

the GPathResult, converted to a Float
toFloat
()

Converts the text of this GPathResult to a Float object.

public Integer

Returns:

the GPathResult, converted to a Integer
toInteger
()

Converts the text of this GPathResult to a Integer object.

public Long

Returns:

the GPathResult, converted to a Long
toLong
()

Converts the text of this GPathResult to a Long object.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public Object

Returns:

the value or null if value is empty
value
()

Returns an Object representing the value of the node.

Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

attributesback to summary
private final Map<K, V> attributes
nameback to summary
private final Object name
parentback to summary
private Node parent
serialVersionUIDback to summary
private static final long serialVersionUID
valueback to summary
private Object value

Constructor Detail

Nodeback to summary
public Node(Node parent, Object name)

Creates a new Node named name and if a parent is supplied, adds the newly created node as a child of the parent.

Parameters
parent:Node

the parent node or null if no parent

name:Object

the name of the node

Nodeback to summary
public Node(Node parent, Object name, Object value)

Creates a new Node named name with value value and if a parent is supplied, adds the newly created node as a child of the parent.

Parameters
parent:Node

the parent node or null if no parent

name:Object

the name of the node

value:Object

the Node value, e.g. some text but in general any Object

Nodeback to summary
public Node(Node parent, Object name, Map<K, V> attributes)

Creates a new Node named name with attributes specified in the attributes Map. If a parent is supplied, the newly created node is added as a child of the parent.

Parameters
parent:Node

the parent node or null if no parent

name:Object

the name of the node

attributes:Map<K, V>

a Map of name-value pairs

Nodeback to summary
public Node(Node parent, Object name, Map<K, V> attributes, Object value)

Creates a new Node named name with value value and with attributes specified in the attributes Map. If a parent is supplied, the newly created node is added as a child of the parent.

Parameters
parent:Node

the parent node or null if no parent

name:Object

the name of the node

attributes:Map<K, V>

a Map of name-value pairs

value:Object

the Node value, e.g. some text but in general any Object

Method Detail

appendback to summary
public boolean append(Node child)

Appends a child to the current node.

Parameters
child:Node

the child to append

Returns:boolean

true

appendNodeback to summary
public Node appendNode(Object name, Map<K, V> attributes)

Creates a new node as a child of the current node.

Parameters
name:Object

the name of the new node

attributes:Map<K, V>

the attributes of the new node

Returns:Node

the newly created Node

appendNodeback to summary
public Node appendNode(Object name)

Creates a new node as a child of the current node.

Parameters
name:Object

the name of the new node

Returns:Node

the newly created Node

appendNodeback to summary
public Node appendNode(Object name, Object value)

Creates a new node as a child of the current node.

Parameters
name:Object

the name of the new node

value:Object

the value of the new node

Returns:Node

the newly created Node

appendNodeback to summary
public Node appendNode(Object name, Map<K, V> attributes, Object value)

Creates a new node as a child of the current node.

Parameters
name:Object

the name of the new node

attributes:Map<K, V>

the attributes of the new node

value:Object

the value of the new node

Returns:Node

the newly created Node

appendNodesback to summary
private void appendNodes(Closure<V> c)
attributeback to summary
public Object attribute(Object key)

Provides lookup of attributes by key.

Parameters
key:Object

the key of interest

Returns:Object

the attribute matching the key or null if no match exists

attributesback to summary
public Map<K, V> attributes()

Returns a Map of the attributes of the node or an empty Map if the node does not have any attributes.

Returns:Map<K, V>

the attributes of the node

breadthFirstback to summary
public List<E> breadthFirst()

Provides a collection of all the nodes in the tree using a breadth-first preorder traversal.

Returns:List<E>

the list of (breadth-first) ordered nodes

breadthFirstback to summary
public List<E> breadthFirst(boolean preorder)

Provides a collection of all the nodes in the tree using a breadth-first traversal.

Parameters
preorder:boolean

if false, a postorder breadth-first traversal will be performed

Returns:List<E>

the list of (breadth-first) ordered nodes

Since
2.5.0
breadthFirstback to summary
public void breadthFirst(Closure<V> c)

Calls the provided closure for all the nodes in the tree using a breadth-first preorder traversal.

Parameters
c:Closure<V>

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
breadthFirstback to summary
public void breadthFirst(Map<String, Object> options, Closure<V> c)

Calls the provided closure for all the nodes in the tree using a breadth-first traversal. A boolean 'preorder' options is supported.

Parameters
options:Map<String, Object>

map containing options

c:Closure<V>

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
breadthFirstRestback to summary
private List<E> breadthFirstRest(boolean preorder)
breadthFirstRestback to summary
private void breadthFirstRest(boolean preorder, int level, Closure<V> c)
buildChildrenFromClosureback to summary
private static List<Node> buildChildrenFromClosure(Closure<V> c)
callClosureForNodeback to summary
private static <T> T callClosureForNode(Closure<T> closure, Object node, int level)
childrenback to summary
public List<E> children()

Returns a List of the nodes children.

Returns:List<E>

the nodes children

cloneback to summary
public Object clone()

Overrides 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.

Returns:Object

the clone

Annotations
@Override
depthFirstback to summary
public List<E> depthFirst()

Provides a collection of all the nodes in the tree using a depth-first preorder traversal.

Returns:List<E>

the list of (depth-first) ordered nodes

depthFirstback to summary
public List<E> depthFirst(boolean preorder)

Provides a collection of all the nodes in the tree using a depth-first traversal.

Parameters
preorder:boolean

if false, a postorder depth-first traversal will be performed

Returns:List<E>

the list of (depth-first) ordered nodes

Since
2.5.0
depthFirstback to summary
public void depthFirst(Closure<V> c)

Provides a collection of all the nodes in the tree using a depth-first preorder traversal.

Parameters
c:Closure<V>

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
depthFirstback to summary
public void depthFirst(Map<String, Object> options, Closure<V> c)

Provides a collection of all the nodes in the tree using a depth-first traversal. A boolean 'preorder' options is supported.

Parameters
options:Map<String, Object>

map containing options

c:Closure<V>

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
depthFirstRestback to summary
private List<E> depthFirstRest(boolean preorder)
depthFirstRestback to summary
private void depthFirstRest(boolean preorder, int level, Closure<V> c)
getback to summary
public Object get(String key)

Provides lookup of elements by non-namespaced name

Parameters
key:String

the name (or shortcut key) of the node(s) of interest

Returns:Object

the nodes which match key

getAtback to summary
public NodeList getAt(QName name)

Provides lookup of elements by QName.

Parameters
name:QName

the QName of interest

Returns:NodeList

the nodes matching name

getAtback to summary
public NodeList getAt(QName name)

Deprecated

use getAt(groovy.namespace.QName) instead

Provides lookup of elements by QName.

Parameters
name:QName

the QName of interest

Returns:NodeList

the nodes matching name

Annotations
@Deprecated
getByNameback to summary
private NodeList getByName(String name)

Provides lookup of elements by name.

Parameters
name:String

the name of interest

Returns:NodeList

the nodes matching name

getDirectChildrenback to summary
private List<E> getDirectChildren()
getParentListback to summary
private static List<E> getParentList(Node parent)
getTailback to summary
private List<E> getTail()
iteratorback to summary
public Iterator<E> iterator()

Returns an Iterator of the children of the node.

Returns:Iterator<E>

the iterator of the nodes children

localTextback to summary
public List<String> localText()

Returns the list of any direct String nodes of this node.

Returns:List<String>

the list of String values from this node

Since
2.3.0
nameback to summary
public Object name()

Returns an Object representing the name of the node.

Returns:Object

the name or null if name is empty

parentback to summary
public Node parent()

Returns the parent of the node.

Returns:Node

the parent or null for the root node

plusback to summary
public void plus(Closure<V> c)

Adds sibling nodes (defined using builder-style notation via a Closure) after the current node.

Parameters
c:Closure<V>

A Closure defining the new sibling nodes to add using builder-style notation.

removeback to summary
public boolean remove(Node child)

Removes a child of the current node.

Parameters
child:Node

the child to remove

Returns:boolean

true if the param was a child of the current node

replaceNodeback to summary
public Node replaceNode(Closure<V> c)

Replaces the current node with nodes defined using builder-style notation via a Closure.

Parameters
c:Closure<V>

A Closure defining the new nodes using builder-style notation.

Returns:Node

the original now replaced node

replaceNodeback to summary
public Node replaceNode(Node n)

Replaces the current node with the supplied node.

Parameters
n:Node

the new Node

Returns:Node

the original now replaced node

setMetaClassback to summary
protected static void setMetaClass(final MetaClass metaClass, Class<T> nodeClass)

Extension point for subclasses to override the metaclass. The default one supports the property and @ attribute notations.

Parameters
metaClass:MetaClass

the original metaclass

nodeClass:Class<T>

the class whose metaclass we wish to override (this class or a subclass)

setParentback to summary
protected void setParent(Node parent)

Adds or replaces the parent of the node.

Parameters
parent:Node

the new parent of the node

setValueback to summary
public void setValue(Object value)

Adds or replaces the value of the node.

Parameters
value:Object

the new value of the node

textback to summary
public String text()

Returns the textual representation of the current node and all its child nodes.

Returns:String

the text value of the node including child text

textIsEmptyOrNullback to summary
private boolean textIsEmptyOrNull()
toBigDecimalback to summary
public BigDecimal toBigDecimal()

Converts the text of this GPathResult to a BigDecimal object.

Returns:BigDecimal

the GPathResult, converted to a BigDecimal

toBigIntegerback to summary
public BigInteger toBigInteger()

Converts the text of this GPathResult to a BigInteger object.

Returns:BigInteger

the GPathResult, converted to a BigInteger

toDoubleback to summary
public Double toDouble()

Converts the text of this GPathResult to a Double object.

Returns:Double

the GPathResult, converted to a Double

toFloatback to summary
public Float toFloat()

Converts the text of this GPathResult to a Float object.

Returns:Float

the GPathResult, converted to a Float

toIntegerback to summary
public Integer toInteger()

Converts the text of this GPathResult to a Integer object.

Returns:Integer

the GPathResult, converted to a Integer

toLongback to summary
public Long toLong()

Converts the text of this GPathResult to a Long object.

Returns:Long

the GPathResult, converted to a Long

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

valueback to summary
public Object value()

Returns an Object representing the value of the node.

Returns:Object

the value or null if value is empty