1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.config;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.logging.log4j.core.config.plugins.util.PluginType;
25
26
27
28
29 public class Node {
30
31
32
33
34
35
36
37 public static final String CATEGORY = "Core";
38
39 private final Node parent;
40 private final String name;
41 private String value;
42 private final PluginType<?> type;
43 private final Map<String, String> attributes = new HashMap<>();
44 private final List<Node> children = new ArrayList<>();
45 private Object object;
46
47
48
49
50
51
52
53
54
55
56 public Node(final Node parent, final String name, final PluginType<?> type) {
57 this.parent = parent;
58 this.name = name;
59 this.type = type;
60 }
61
62 public Node() {
63 this.parent = null;
64 this.name = null;
65 this.type = null;
66 }
67
68 public Node(final Node node) {
69 this.parent = node.parent;
70 this.name = node.name;
71 this.type = node.type;
72 this.attributes.putAll(node.getAttributes());
73 this.value = node.getValue();
74 for (final Node child : node.getChildren()) {
75 this.children.add(new Node(child));
76 }
77 this.object = node.object;
78 }
79
80 public Map<String, String> getAttributes() {
81 return attributes;
82 }
83
84 public List<Node> getChildren() {
85 return children;
86 }
87
88 public boolean hasChildren() {
89 return !children.isEmpty();
90 }
91
92 public String getValue() {
93 return value;
94 }
95
96 public void setValue(final String value) {
97 this.value = value;
98 }
99
100 public Node getParent() {
101 return parent;
102 }
103
104 public String getName() {
105 return name;
106 }
107
108 public boolean isRoot() {
109 return parent == null;
110 }
111
112 public void setObject(final Object obj) {
113 object = obj;
114 }
115
116 @SuppressWarnings("unchecked")
117 public <T> T getObject() {
118 return (T) object;
119 }
120
121
122
123
124
125
126
127
128
129 public <T> T getObject(final Class<T> clazz) {
130 return clazz.cast(object);
131 }
132
133
134
135
136
137
138
139
140 public boolean isInstanceOf(final Class<?> clazz) {
141 return clazz.isInstance(object);
142 }
143
144 public PluginType<?> getType() {
145 return type;
146 }
147
148 @Override
149 public String toString() {
150 if (object == null) {
151 return "null";
152 }
153 return type.isObjectPrintable() ? object.toString() :
154 type.getPluginClass().getName() + " with name " + name;
155 }
156 }