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