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 * @since 1.3
051 */
052public class OverrideCombiner extends NodeCombiner
053{
054    /**
055     * Constructs an override combination for the passed in node structures.
056     *
057     * @param node1 the first node
058     * @param node2 the second node
059     * @return the resulting combined node structure
060     */
061    @Override
062    public ImmutableNode combine(final ImmutableNode node1,
063            final ImmutableNode node2)
064    {
065        final ImmutableNode.Builder result = new ImmutableNode.Builder();
066        result.name(node1.getNodeName());
067
068        // Process nodes from the first structure, which override the second
069        for (final ImmutableNode child : node1.getChildren())
070        {
071            final ImmutableNode child2 = canCombine(node1, node2, child);
072            if (child2 != null)
073            {
074                result.addChild(combine(child, child2));
075            }
076            else
077            {
078                result.addChild(child);
079            }
080        }
081
082        // Process nodes from the second structure, which are not contained
083        // in the first structure
084        for (final ImmutableNode child : node2.getChildren())
085        {
086            if (HANDLER.getChildrenCount(node1, child.getNodeName()) < 1)
087            {
088                result.addChild(child);
089            }
090        }
091
092        // Handle attributes and value
093        addAttributes(result, node1, node2);
094        result.value(node1.getValue() != null ? node1.getValue() : node2
095                .getValue());
096
097        return result.create();
098    }
099
100    /**
101     * Handles the attributes during a combination process. First all attributes
102     * of the first node are added to the result. Then all attributes of the
103     * second node, which are not contained in the first node, are also added.
104     *
105     * @param result the resulting node
106     * @param node1 the first node
107     * @param node2 the second node
108     */
109    protected void addAttributes(final ImmutableNode.Builder result,
110            final ImmutableNode node1, final ImmutableNode node2)
111    {
112        result.addAttributes(node1.getAttributes());
113        for (final String attr : node2.getAttributes().keySet())
114        {
115            if (!node1.getAttributes().containsKey(attr))
116            {
117                result.addAttribute(attr,
118                        HANDLER.getAttributeValue(node2, attr));
119            }
120        }
121    }
122
123    /**
124     * Tests if a child node of the second node can be combined with the given
125     * child node of the first node. If this is the case, the corresponding node
126     * will be returned, otherwise <b>null</b>. This implementation checks
127     * whether the child node occurs only once in both hierarchies and is no
128     * known list node.
129     *
130     * @param node1 the first node
131     * @param node2 the second node
132     * @param child the child node (of the first node)
133     * @return a child of the second node, with which a combination is possible
134     */
135    protected ImmutableNode canCombine(final ImmutableNode node1,
136            final ImmutableNode node2, final ImmutableNode child)
137    {
138        if (HANDLER.getChildrenCount(node2, child.getNodeName()) == 1
139                && HANDLER.getChildrenCount(node1, child.getNodeName()) == 1
140                && !isListNode(child))
141        {
142            return HANDLER.getChildren(node2, child.getNodeName()).get(0);
143        }
144        return null;
145    }
146}