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 * https://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 that performs a merge from two passed in node 029 * 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 * Checks whether the attributes of the passed in node are compatible. 047 * 048 * @param attrs1 the attributes of the first node 049 * @param node the 2nd node 050 * @return a flag whether these nodes can be combined regarding their attributes 051 */ 052 private static boolean matchAttributes(final Map<String, Object> attrs1, final ImmutableNode node) { 053 final Map<String, Object> attrs2 = node.getAttributes(); 054 for (final Map.Entry<String, Object> e : attrs1.entrySet()) { 055 if (attrs2.containsKey(e.getKey()) && !Objects.equals(e.getValue(), attrs2.get(e.getKey()))) { 056 return false; 057 } 058 } 059 return true; 060 } 061 062 /** 063 * Constructs a new instance. 064 */ 065 public MergeCombiner() { 066 // empty 067 } 068 069 /** 070 * Handles the attributes during a combination process. First all attributes of the first node will be added to the 071 * result. Then all attributes of the second node, which are not contained in the first node, will also be added. 072 * 073 * @param result the builder for the resulting node 074 * @param node1 the first node 075 * @param node2 the second node 076 */ 077 protected void addAttributes(final ImmutableNode.Builder result, final ImmutableNode node1, final ImmutableNode node2) { 078 final Map<String, Object> attributes = new HashMap<>(node1.getAttributes()); 079 node2.getAttributes().forEach(attributes::putIfAbsent); 080 result.addAttributes(attributes); 081 } 082 083 /** 084 * Tests if the first node can be combined with the second node. A node can only be combined if its attributes are all 085 * present in the second node and they all have the same value. 086 * 087 * @param node2 the second node 088 * @param child the child node (of the first node) 089 * @param children2 the children of the 2nd node 090 * @return a child of the second node, with which a combination is possible 091 */ 092 protected ImmutableNode canCombine(final ImmutableNode node2, final ImmutableNode child, final List<ImmutableNode> children2) { 093 final Map<String, Object> attrs1 = child.getAttributes(); 094 final List<ImmutableNode> nodes = new ArrayList<>(); 095 096 final List<ImmutableNode> children = HANDLER.getChildren(node2, child.getNodeName()); 097 children.forEach(node -> { 098 if (matchAttributes(attrs1, node)) { 099 nodes.add(node); 100 } 101 }); 102 103 if (nodes.size() == 1) { 104 return nodes.get(0); 105 } 106 if (nodes.size() > 1 && !isListNode(child)) { 107 nodes.forEach(children2::remove); 108 } 109 110 return null; 111 } 112 113 /** 114 * Combines the given nodes to a new union node. 115 * 116 * @param node1 the first source node 117 * @param node2 the second source node 118 * @return the union node 119 */ 120 121 @Override 122 public ImmutableNode combine(final ImmutableNode node1, final ImmutableNode node2) { 123 final ImmutableNode.Builder result = new ImmutableNode.Builder(); 124 result.name(node1.getNodeName()); 125 result.value(node1.getValue()); 126 addAttributes(result, node1, node2); 127 128 // Check if nodes can be combined 129 final List<ImmutableNode> children2 = new LinkedList<>(node2.getChildren()); 130 node1.forEach(child1 -> { 131 final ImmutableNode child2 = canCombine(node2, child1, children2); 132 if (child2 != null) { 133 result.addChild(combine(child1, child2)); 134 children2.remove(child2); 135 } else { 136 result.addChild(child1); 137 } 138 }); 139 140 // Add remaining children of node 2 141 children2.forEach(result::addChild); 142 return result.create(); 143 } 144}