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
| Constructor: |
public Node(Node parent,
Object name) {
this(parent, name, new NodeList());
}
|
public Node(Node parent,
Object name,
Object value) {
this(parent, name, new HashMap(), value);
}
|
public Node(Node parent,
Object name,
Map attributes) {
this(parent, name, attributes, new NodeList());
}
|
public Node(Node parent,
Object name,
Map attributes,
Object value) {
this.parent = parent;
this.name = name;
this.attributes = attributes;
this.value = value;
if (parent != null) {
getParentList(parent).add(this);
}
}
|
| Method from groovy.util.Node Detail: |
public boolean append(Node child) {
child.parent = this;
return getParentList(this).add(child);
}
|
public Node appendNode(Object name) {
return new Node(this, name);
}
|
public Node appendNode(Object name,
Map attributes) {
return new Node(this, name, attributes);
}
|
public Node appendNode(Object name,
Object value) {
return new Node(this, name, value);
}
|
public Node appendNode(Object name,
Map attributes,
Object value) {
return new Node(this, name, attributes, value);
}
|
public Object attribute(Object key) {
return (attributes != null) ? attributes.get(key) : null;
}
|
public Map attributes() {
return attributes;
}
|
public List breadthFirst() {
List answer = new NodeList();
answer.add(this);
answer.addAll(breadthFirstRest());
return answer;
}
Provide a collection of all the nodes in the tree
using a breadth-first traversal. |
public List children() {
if (value == null) {
return new NodeList();
}
if (value instanceof List) {
return (List) value;
}
// we're probably just a String
List result = new NodeList();
result.add(value);
return result;
}
|
public List depthFirst() {
List answer = new NodeList();
answer.add(this);
answer.addAll(depthFirstRest());
return answer;
}
Provide a collection of all the nodes in the tree
using a depth first traversal. |
public Object get(String key) {
if (key != null && key.charAt(0) == '@") {
String attributeName = key.substring(1);
return attributes().get(attributeName);
}
if ("..".equals(key)) {
return parent();
}
if ("*".equals(key)) {
return children();
}
if ("**".equals(key)) {
return depthFirst();
}
return getByName(key);
}
Provides lookup of elements by non-namespaced name |
public NodeList getAt(QName name) {
NodeList answer = new NodeList();
for (Iterator iter = children().iterator(); iter.hasNext();) {
Object child = iter.next();
if (child instanceof Node) {
Node childNode = (Node) child;
Object childNodeName = childNode.name();
if (name.matches(childNodeName)) {
answer.add(childNode);
}
}
}
return answer;
}
Provides lookup of elements by QName. |
public Iterator iterator() {
return children().iterator();
}
|
public Object name() {
return name;
}
|
public Node parent() {
return parent;
}
|
public void print(PrintWriter out) {
new NodePrinter(out).print(this);
}
|
public boolean remove(Node child) {
child.parent = null;
return getParentList(this).remove(child);
}
|
protected static void setMetaClass(MetaClass metaClass,
Class nodeClass) {
final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) {
/* (non-Javadoc)
* @see groovy.lang.DelegatingMetaClass#getAttribute(java.lang.Object, java.lang.String)
*/
public Object getAttribute(final Object object, final String attribute) {
Node n = (Node) object;
return n.get("@" + attribute);
}
/* (non-Javadoc)
* @see groovy.lang.MetaClass#setAttribute(java.lang.Object, java.lang.String, java.lang.Object)
*/
public void setAttribute(final Object object, final String attribute, final Object newValue) {
Node n = (Node) object;
n.attributes().put(attribute, newValue);
}
/* (non-Javadoc)
* @see groovy.lang.MetaClass#getProperty(java.lang.Object, java.lang.String)
*/
public Object getProperty(Object object, String property) {
if (object instanceof Node) {
Node n = (Node) object;
return n.get(property);
}
return super.getProperty(object, property);
}
/* (non-Javadoc)
* @see groovy.lang.MetaClass#setProperty(java.lang.Object, java.lang.String, java.lang.Object)
*/
public void setProperty(Object object, String property, Object newValue) {
if (property.startsWith("@")) {
String attribute = property.substring(1);
Node n = (Node) object;
n.attributes().put(attribute, newValue);
return;
}
delegate.setProperty(object, property, newValue);
}
};
GroovySystem.getMetaClassRegistry().setMetaClass(nodeClass, newMetaClass);
}
|
public void setValue(Object value) {
this.value = value;
}
|
public String text() {
if (value instanceof String) {
return (String) value;
} else if (value instanceof Collection) {
Collection coll = (Collection) value;
String previousText = null;
StringBuffer buffer = null;
for (Iterator iter = coll.iterator(); iter.hasNext();) {
Object child = iter.next();
if (child instanceof String) {
String childText = (String) child;
if (previousText == null) {
previousText = childText;
} else {
if (buffer == null) {
buffer = new StringBuffer();
buffer.append(previousText);
}
buffer.append(childText);
}
}
}
if (buffer != null) {
return buffer.toString();
} else {
if (previousText != null) {
return previousText;
}
}
}
return "";
}
|
public String toString() {
return name + "[attributes=" + attributes + "; value=" + value + "]";
}
|
public Object value() {
return value;
}
|