    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
    019import java.util.ArrayList;
    020import java.util.HashMap;
    021import java.util.LinkedList;
    022import java.util.List;
    023import java.util.Map;
    024import java.util.Objects;
    025
    026/**
    027 * <p>
    028 * A specialized implementation of the {@code NodeCombiner} interface
    029 * that performs a merge from two passed in node hierarchies.
    030 * </p>
    031 * <p>
    032 * This combiner performs the merge using a few rules:
    033 * </p>
    034 * <ol>
    035 * <li>Nodes can be merged when attributes that appear in both have the same value.</li>
    036 * <li>Only a single node in the second file is considered a match to the node in the first file.</li>
    037 * <li>Attributes in nodes that match are merged.
    038 * <li>Nodes in both files that do not match are added to the result.</li>
    039 * </ol>
    040 *
    041 * @since 1.7
    042 */
    043public class MergeCombiner extends NodeCombiner
    044{
    045    /**
    046     * Combines the given nodes to a new union node.
    047     *
    048     * @param node1 the first source node
    049     * @param node2 the second source node
    050     * @return the union node
    051     */
    052
    053    @Override
    054    public ImmutableNode combine(final ImmutableNode node1, final ImmutableNode node2)
    055    {
    056        final ImmutableNode.Builder result = new ImmutableNode.Builder();
    057        result.name(node1.getNodeName());
    058        result.value(node1.getValue());
    059        addAttributes(result, node1, node2);
    060
    061        // Check if nodes can be combined
    062        final List<ImmutableNode> children2 = new LinkedList<>(node2.getChildren());
    063        for (final ImmutableNode child1 : node1.getChildren())
    064        {
    065            final ImmutableNode child2 = canCombine(node2, child1, children2);
    066            if (child2 != null)
    067            {
    068                result.addChild(combine(child1, child2));
    069                children2.remove(child2);
    070            }
    071            else
    072            {
    073                result.addChild(child1);
    074            }
    075        }
    076
    077        // Add remaining children of node 2
    078        for (final ImmutableNode c : children2)
    079        {
    080            result.addChild(c);
    081        }
    082        return result.create();
    083    }
    084
    085    /**
    086     * Handles the attributes during a combination process. First all attributes
    087     * of the first node will be added to the result. Then all attributes of the
    088     * second node, which are not contained in the first node, will also be
    089     * added.
    090     *
    091     * @param result the builder for the resulting node
    092     * @param node1 the first node
    093     * @param node2 the second node
    094     */
    095    protected void addAttributes(final ImmutableNode.Builder result, final ImmutableNode node1,
    096            final ImmutableNode node2)
    097    {
    098        final Map<String, Object> attributes = new HashMap<>();
    099        attributes.putAll(node1.getAttributes());
    100        for (final Map.Entry<String, Object> e : node2.getAttributes().entrySet())
    101        {
    102            if (!attributes.containsKey(e.getKey()))
    103            {
    104                attributes.put(e.getKey(), e.getValue());
    105            }
    106        }
    107        result.addAttributes(attributes);
    108    }
    109
    110    /**
    111     * Tests if the first node can be combined with the second node. A node can
    112     * only be combined if its attributes are all present in the second node and
    113     * they all have the same value.
    114     *
    115     * @param node2 the second node
    116     * @param child the child node (of the first node)
    117     * @param children2 the children of the 2nd node
    118     * @return a child of the second node, with which a combination is possible
    119     */
    120    protected ImmutableNode canCombine(final ImmutableNode node2,
    121            final ImmutableNode child, final List<ImmutableNode> children2)
    122    {
    123        final Map<String, Object> attrs1 = child.getAttributes();
    124        final List<ImmutableNode> nodes = new ArrayList<>();
    125
    126        final List<ImmutableNode> children =
    127                HANDLER.getChildren(node2, child.getNodeName());
    128        for (final ImmutableNode node : children)
    129        {
    130            if (matchAttributes(attrs1, node))
    131            {
    132                nodes.add(node);
    133            }
    134        }
    135
    136        if (nodes.size() == 1)
    137        {
    138            return nodes.get(0);
    139        }
    140        if (nodes.size() > 1 && !isListNode(child))
    141        {
    142            for (final ImmutableNode node : nodes)
    143            {
    144                children2.remove(node);
    145            }
    146        }
    147
    148        return null;
    149    }
    150
    151    /**
    152     * Checks whether the attributes of the passed in node are compatible.
    153     *
    154     * @param attrs1 the attributes of the first node
    155     * @param node the 2nd node
    156     * @return a flag whether these nodes can be combined regarding their
    157     *         attributes
    158     */
    159    private static boolean matchAttributes(final Map<String, Object> attrs1,
    160            final ImmutableNode node)
    161    {
    162        final Map<String, Object> attrs2 = node.getAttributes();
    163        for (final Map.Entry<String, Object> e : attrs1.entrySet())
    164        {
    165            if (attrs2.containsKey(e.getKey())
    166                    && !Objects
    167                            .equals(e.getValue(), attrs2.get(e.getKey())))
    168            {
    169                return false;
    170            }
    171        }
    172        return true;
    173    }
    174}