View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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   * A Configuration node.
28   */
29  public class Node {
30  
31      /**
32       * Main plugin category for plugins which are represented as a configuration node. Such plugins tend to be
33       * available as XML elements in a configuration file.
34       *
35       * @since 2.1
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<String, String>();
44      private final List<Node> children = new ArrayList<Node>();
45      private Object object;
46  
47  
48      /**
49       * Creates a new instance of {@code Node} and initializes it
50       * with a name and the corresponding XML element.
51       *
52       * @param parent the node's parent.
53       * @param name the node's name.
54       * @param type The Plugin Type associated with the node.
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      * Returns this node's object cast to the given class.
123      *
124      * @param clazz the class to cast this node's object to.
125      * @param <T>   the type to cast to.
126      * @return this node's object.
127      * @since 2.1
128      */
129     public <T> T getObject(final Class<T> clazz) {
130         return clazz.cast(object);
131     }
132 
133     /**
134      * Determines if this node's object is an instance of the given class.
135      *
136      * @param clazz the class to check.
137      * @return {@code true} if this node's object is an instance of the given class.
138      * @since 2.1
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 }