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.PluginType;
25  
26  /**
27   * A Configuration node.
28   */
29  public class Node {
30  
31      private final Node parent;
32      private final String name;
33      private String value;
34      private final PluginType<?> type;
35      private final Map<String, String> attributes = new HashMap<String, String>();
36      private final List<Node> children = new ArrayList<Node>();
37      private Object object;
38  
39  
40      /**
41       * Creates a new instance of <code>Node</code> and initializes it
42       * with a name and the corresponding XML element.
43       *
44       * @param parent the node's parent.
45       * @param name the node's name.
46       * @param type The Plugin Type associated with the node.
47       */
48      public Node(final Node parent, final String name, final PluginType<?> type) {
49          this.parent = parent;
50          this.name = name;
51          this.type = type;
52      }
53  
54      public Node() {
55          this.parent = null;
56          this.name = null;
57          this.type = null;
58      }
59  
60      public Node(final Node node) {
61          this.parent = node.parent;
62          this.name = node.name;
63          this.type = node.type;
64          this.attributes.putAll(node.getAttributes());
65          this.value = node.getValue();
66          for (final Node child : node.getChildren()) {
67              this.children.add(new Node(child));
68          }
69          this.object = node.object;
70      }
71  
72      public Map<String, String> getAttributes() {
73          return attributes;
74      }
75  
76      public List<Node> getChildren() {
77          return children;
78      }
79  
80      public boolean hasChildren() {
81          return children.size() > 0;
82      }
83  
84      public String getValue() {
85          return value;
86      }
87  
88      public void setValue(final String value) {
89          this.value = value;
90      }
91  
92      public Node getParent() {
93          return parent;
94      }
95  
96      public String getName() {
97          return name;
98      }
99  
100     public boolean isRoot() {
101         return parent == null;
102     }
103 
104     public void setObject(final Object obj) {
105         object = obj;
106     }
107 
108     public Object getObject() {
109         return object;
110     }
111 
112     public PluginType<?> getType() {
113         return type;
114     }
115 
116     @Override
117     public String toString() {
118         if (object == null) {
119             return "null";
120         }
121         return type.isObjectPrintable() ? object.toString() :
122             type.getPluginClass().getName() + " with name " + name;
123     }
124 }