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.commons.configuration2.tree;
018
019
020/**
021 * <p>
022 * A concrete combiner implementation that is able to construct an override
023 * combination.
024 * </p>
025 * <p>
026 * An <em>override combination</em> means that nodes in the first node
027 * structure take precedence over nodes in the second, or - in other words -
028 * nodes of the second structure are only added to the resulting structure if
029 * they do not occur in the first one. This is especially suitable for dealing
030 * with the properties of configurations that are defined in an
031 * {@code override} section of a configuration definition file (hence the
032 * name).
033 * </p>
034 * <p>
035 * This combiner will iterate over the second node hierarchy and find all nodes
036 * that are not contained in the first hierarchy; these are added to the result.
037 * If a node can be found in both structures, it is checked whether a
038 * combination (in a recursive way) can be constructed for the two, which will
039 * then be added. Per default, nodes are combined, which occur only once in both
040 * structures. This test is implemented in the {@code canCombine()}
041 * method.
042 * </p>
043 * <p>
044 * As is true for the {@link UnionCombiner}, for this combiner
045 * list nodes are important. The {@code addListNode()} can be called to
046 * declare certain nodes as list nodes. This has the effect that these nodes
047 * will never be combined.
048 * </p>
049 *
050 * @version $Id: OverrideCombiner.java 1624601 2014-09-12 18:04:36Z oheger $
051 * @since 1.3
052 */
053public class OverrideCombiner extends NodeCombiner
054{
055    /**
056     * Constructs an override combination for the passed in node structures.
057     *
058     * @param node1 the first node
059     * @param node2 the second node
060     * @return the resulting combined node structure
061     */
062    @Override
063    public ImmutableNode combine(ImmutableNode node1,
064            ImmutableNode node2)
065    {
066        ImmutableNode.Builder result = new ImmutableNode.Builder();
067        result.name(node1.getNodeName());
068
069        // Process nodes from the first structure, which override the second
070        for (ImmutableNode child : node1.getChildren())
071        {
072            ImmutableNode child2 = canCombine(node1, node2, child);
073            if (child2 != null)
074            {
075                result.addChild(combine(child, child2));
076            }
077            else
078            {
079                result.addChild(child);
080            }
081        }
082
083        // Process nodes from the second structure, which are not contained
084        // in the first structure
085        for (ImmutableNode child : node2.getChildren())
086        {
087            if (HANDLER.getChildrenCount(node1, child.getNodeName()) < 1)
088            {
089                result.addChild(child);
090            }
091        }
092
093        // Handle attributes and value
094        addAttributes(result, node1, node2);
095        result.value((node1.getValue() != null) ? node1.getValue() : node2
096                .getValue());
097
098        return result.create();
099    }
100
101    /**
102     * Handles the attributes during a combination process. First all attributes
103     * of the first node are added to the result. Then all attributes of the
104     * second node, which are not contained in the first node, are also added.
105     *
106     * @param result the resulting node
107     * @param node1 the first node
108     * @param node2 the second node
109     */
110    protected void addAttributes(ImmutableNode.Builder result,
111            ImmutableNode node1, ImmutableNode node2)
112    {
113        result.addAttributes(node1.getAttributes());
114        for (String attr : node2.getAttributes().keySet())
115        {
116            if (!node1.getAttributes().containsKey(attr))
117            {
118                result.addAttribute(attr,
119                        HANDLER.getAttributeValue(node2, attr));
120            }
121        }
122    }
123
124    /**
125     * Tests if a child node of the second node can be combined with the given
126     * child node of the first node. If this is the case, the corresponding node
127     * will be returned, otherwise <b>null</b>. This implementation checks
128     * whether the child node occurs only once in both hierarchies and is no
129     * known list node.
130     *
131     * @param node1 the first node
132     * @param node2 the second node
133     * @param child the child node (of the first node)
134     * @return a child of the second node, with which a combination is possible
135     */
136    protected ImmutableNode canCombine(ImmutableNode node1,
137            ImmutableNode node2, ImmutableNode child)
138    {
139        if (HANDLER.getChildrenCount(node2, child.getNodeName()) == 1
140                && HANDLER.getChildrenCount(node1, child.getNodeName()) == 1
141                && !isListNode(child))
142        {
143            return HANDLER.getChildren(node2, child.getNodeName()).get(0);
144        }
145        else
146        {
147            return null;
148        }
149    }
150}