1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.configuration2.tree;
18
19 /**
20 * <p>
21 * A concrete combiner implementation that is able to construct an override combination.
22 * </p>
23 * <p>
24 * An <em>override combination</em> means that nodes in the first node structure take precedence over nodes in the
25 * second, or - in other words - nodes of the second structure are only added to the resulting structure if they do not
26 * occur in the first one. This is especially suitable for dealing with the properties of configurations that are
27 * defined in an {@code override} section of a configuration definition file (hence the name).
28 * </p>
29 * <p>
30 * This combiner will iterate over the second node hierarchy and find all nodes that are not contained in the first
31 * hierarchy; these are added to the result. If a node can be found in both structures, it is checked whether a
32 * combination (in a recursive way) can be constructed for the two, which will then be added. Per default, nodes are
33 * combined, which occur only once in both structures. This test is implemented in the {@code canCombine()} method.
34 * </p>
35 * <p>
36 * As is true for the {@link UnionCombiner}, for this combiner list nodes are important. The {@code addListNode()} can
37 * be called to declare certain nodes as list nodes. This has the effect that these nodes will never be combined.
38 * </p>
39 *
40 * @since 1.3
41 */
42 public class OverrideCombiner extends NodeCombiner {
43
44 /**
45 * Constructs a new instance.
46 */
47 public OverrideCombiner() {
48 // empty
49 }
50
51 /**
52 * Handles the attributes during a combination process. First all attributes of the first node are added to the result.
53 * Then all attributes of the second node, which are not contained in the first node, are also added.
54 *
55 * @param result the resulting node
56 * @param node1 the first node
57 * @param node2 the second node
58 */
59 protected void addAttributes(final ImmutableNode.Builder result, final ImmutableNode node1, final ImmutableNode node2) {
60 result.addAttributes(node1.getAttributes());
61 node2.getAttributes().keySet().forEach(attr -> {
62 if (!node1.getAttributes().containsKey(attr)) {
63 result.addAttribute(attr, HANDLER.getAttributeValue(node2, attr));
64 }
65 });
66 }
67
68 /**
69 * 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
70 * case, the corresponding node will be returned, otherwise <strong>null</strong>. This implementation checks whether the child
71 * node occurs only once in both hierarchies and is no known list node.
72 *
73 * @param node1 the first node
74 * @param node2 the second node
75 * @param child the child node (of the first node)
76 * @return a child of the second node, with which a combination is possible
77 */
78 protected ImmutableNode canCombine(final ImmutableNode node1, final ImmutableNode node2, final ImmutableNode child) {
79 if (HANDLER.getChildrenCount(node2, child.getNodeName()) == 1 && HANDLER.getChildrenCount(node1, child.getNodeName()) == 1 && !isListNode(child)) {
80 return HANDLER.getChildren(node2, child.getNodeName()).get(0);
81 }
82 return null;
83 }
84
85 /**
86 * Constructs an override combination for the passed in node structures.
87 *
88 * @param node1 the first node
89 * @param node2 the second node
90 * @return the resulting combined node structure
91 */
92 @Override
93 public ImmutableNode combine(final ImmutableNode node1, final ImmutableNode node2) {
94 final ImmutableNode.Builder result = new ImmutableNode.Builder();
95 result.name(node1.getNodeName());
96
97 // Process nodes from the first structure, which override the second
98 node1.forEach(child -> {
99 final ImmutableNode child2 = canCombine(node1, node2, child);
100 result.addChild(child2 != null ? combine(child, child2) : child);
101 });
102
103 // Process nodes from the second structure, which are not contained
104 // in the first structure
105 node2.stream().filter(child -> HANDLER.getChildrenCount(node1, child.getNodeName()) < 1).forEach(result::addChild);
106
107 // Handle attributes and value
108 addAttributes(result, node1, node2);
109 result.value(node1.getValue() != null ? node1.getValue() : node2.getValue());
110
111 return result.create();
112 }
113 }