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 pr
| 54 | * metadata like <code>{foo a=1 b="123" { bar x=12 text="hello" }}</code> |
| 55 | */ |
| 56 | public class Node implements Serializable, Cloneable { |
| 57 | |
| 58 | static { |
| 59 | // wrap the standard MetaClass with the delegate |
| 60 | setMetaClass(GroovySystem.getMetaClassRegistry().getMetaClass(Node.class), Node.class); |
| 61 | } |
| 62 | |
| 63 | @Serial private static final long serialVersionUID = 4121134753270542643L; |
| 64 | |
| 65 | private Node parent; |
| 66 | |
| 67 | private final Object name; |
| 68 | |
| 69 | private final Map attributes; |
| 70 | |
| 71 | private Object value; |
| 72 | |
| 73 | /** |
| 74 | * Creates a new Node with the same name, no parent, shallow cloned attributes |
| 75 | * and if the value is a NodeList, a (deep) clone of those nodes. |
| 76 | * |
| 77 | * @return the clone |
| 78 | */ |
| 79 | @Override |
| 80 | public Object clone() { |
| 81 | Object newValue = value; |
| 82 | if (value instanceof NodeList nodes) { |
| 83 | newValue = nodes.clone(); |
| 84 | } |
| 85 | return new Node(null, name, new HashMap(attributes), newValue); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Creates a new Node named <code>name</code> and if a parent is supplied, adds |
| 90 | * the newly created node as a child of the parent. |
| 91 | * |
| 92 | * @param parent the parent node or null if no parent |
| 93 | * @param name the name of the node |
| 94 | */ |
| 95 | public Node(Node parent, Object name) { |
| 96 | this(parent, name, new NodeList()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Creates a new Node named <code>name</code> with value <code>value</code> and |
| 101 | * if a parent is supplied, adds the newly created node as a child of the parent. |
| 102 | * |
| 103 | * @param parent the parent node or null if no parent |
| 104 | * @param name the name of the node |
| 105 | * @param value the Node value, e.g. some text but in general any Object |
| 106 | */ |
| 107 | public Node(Node parent, Object name, Object value) { |
| 108 | this(parent, name, new HashMap(), value); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Creates a new Node named <code>name</code> with |
| 113 | * attributes specified in the <code>attributes</code> Map. If a parent is supplied, |
nothing calls this directly
no test coverage detected
searching dependent graphs…