001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.config;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.logging.log4j.core.config.plugins.util.PluginType;
025
026/**
027 * A Configuration node.
028 */
029public class Node {
030
031    private final Node parent;
032    private final String name;
033    private String value;
034    private final PluginType<?> type;
035    private final Map<String, String> attributes = new HashMap<String, String>();
036    private final List<Node> children = new ArrayList<Node>();
037    private Object object;
038
039
040    /**
041     * Creates a new instance of {@code Node} and initializes it
042     * with a name and the corresponding XML element.
043     *
044     * @param parent the node's parent.
045     * @param name the node's name.
046     * @param type The Plugin Type associated with the node.
047     */
048    public Node(final Node parent, final String name, final PluginType<?> type) {
049        this.parent = parent;
050        this.name = name;
051        this.type = type;
052    }
053
054    public Node() {
055        this.parent = null;
056        this.name = null;
057        this.type = null;
058    }
059
060    public Node(final Node node) {
061        this.parent = node.parent;
062        this.name = node.name;
063        this.type = node.type;
064        this.attributes.putAll(node.getAttributes());
065        this.value = node.getValue();
066        for (final Node child : node.getChildren()) {
067            this.children.add(new Node(child));
068        }
069        this.object = node.object;
070    }
071
072    public Map<String, String> getAttributes() {
073        return attributes;
074    }
075
076    public List<Node> getChildren() {
077        return children;
078    }
079
080    public boolean hasChildren() {
081        return !children.isEmpty();
082    }
083
084    public String getValue() {
085        return value;
086    }
087
088    public void setValue(final String value) {
089        this.value = value;
090    }
091
092    public Node getParent() {
093        return parent;
094    }
095
096    public String getName() {
097        return name;
098    }
099
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}