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 * <p>
021 * A concrete combiner implementation that is able to construct an override combination.
022 * </p>
023 * <p>
024 * An <em>override combination</em> means that nodes in the first node structure take precedence over nodes in the
025 * second, or - in other words - nodes of the second structure are only added to the resulting structure if they do not
026 * occur in the first one. This is especially suitable for dealing with the properties of configurations that are
027 * defined in an {@code override} section of a configuration definition file (hence the name).
028 * </p>
029 * <p>
030 * This combiner will iterate over the second node hierarchy and find all nodes that are not contained in the first
031 * hierarchy; these are added to the result. If a node can be found in both structures, it is checked whether a
032 * combination (in a recursive way) can be constructed for the two, which will then be added. Per default, nodes are
033 * combined, which occur only once in both structures. This test is implemented in the {@code canCombine()} method.
034 * </p>
035 * <p>
036 * As is true for the {@link UnionCombiner}, for this combiner list nodes are important. The {@code addListNode()} can
037 * be called to declare certain nodes as list nodes. This has the effect that these nodes will never be combined.
038 * </p>
039 *
040 * @since 1.3
041 */
042public class OverrideCombiner extends NodeCombiner {
043    /**
044     * Constructs an override combination for the passed in node structures.
045     *
046     * @param node1 the first node
047     * @param node2 the second node
048     * @return the resulting combined node structure
049     */
050    @Override
051    public ImmutableNode combine(final ImmutableNode node1, final ImmutableNode node2) {
052        final ImmutableNode.Builder result = new ImmutableNode.Builder();
053        result.name(node1.getNodeName());
054
055        // Process nodes from the first structure, which override the second
056        node1.forEach(child -> {
057            final ImmutableNode child2 = canCombine(node1, node2, child);
058            result.addChild(child2 != null ? combine(child, child2) : child);
059        });
060
061        // Process nodes from the second structure, which are not contained
062        // in the first structure
063        node2.stream().filter(child -> HANDLER.getChildrenCount(node1, child.getNodeName()) < 1).forEach(result::addChild);
064
065        // Handle attributes and value
066        addAttributes(result, node1, node2);
067        result.value(node1.getValue() != null ? node1.getValue() : node2.getValue());
068
069        return result.create();
070    }
071
072    /**
073     * Handles the attributes during a combination process. First all attributes of the first node are added to the result.
074     * Then all attributes of the second node, which are not contained in the first node, are also added.
075     *
076     * @param result the resulting node
077     * @param node1 the first node
078     * @param node2 the second node
079     */
080    protected void addAttributes(final ImmutableNode.Builder result, final ImmutableNode node1, final ImmutableNode node2) {
081        result.addAttributes(node1.getAttributes());
082        node2.getAttributes().keySet().forEach(attr -> {
083            if (!node1.getAttributes().containsKey(attr)) {
084                result.addAttribute(attr, HANDLER.getAttributeValue(node2, attr));
085            }
086        });
087    }
088
089    /**
090     * Tests if a child node of the second node can be combined with the given child node of the first node. If this is the
091     * case, the corresponding node will be returned, otherwise <b>null</b>. This implementation checks whether the child
092     * node occurs only once in both hierarchies and is no known list node.
093     *
094     * @param node1 the first node
095     * @param node2 the second node
096     * @param child the child node (of the first node)
097     * @return a child of the second node, with which a combination is possible
098     */
099    protected ImmutableNode canCombine(final ImmutableNode node1, final ImmutableNode node2, final ImmutableNode child) {
100        if (HANDLER.getChildrenCount(node2, child.getNodeName()) == 1 && HANDLER.getChildrenCount(node1, child.getNodeName()) == 1 && !isListNode(child)) {
101            return HANDLER.getChildren(node2, child.getNodeName()).get(0);
102        }
103        return null;
104    }
105}