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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.configuration.tree;
22
* A concrete combiner implementation that is able to construct an override
26
* An <em>override combination</em> means that nodes in the first node
27
* structure take precedence over nodes in the second, or - in other words -
28
* nodes of the second structure are only added to the resulting structure if
29
* they do not occur in the first one. This is especially suitable for dealing
30
* with the properties of configurations that are defined in an
31
* {@code override} section of a configuration definition file (hence the
35
* This combiner will iterate over the second node hierarchy and find all nodes
36
* that are not contained in the first hierarchy; these are added to the result.
37
* If a node can be found in both structures, it is checked whether a
38
* combination (in a recursive way) can be constructed for the two, which will
39
* then be added. Per default, nodes are combined, which occur only once in both
40
* structures. This test is implemented in the {@code canCombine()}
44
* As is true for the {@link UnionCombiner}, for this combiner
45
* list nodes are important. The {@code addListNode()} can be called to
46
* declare certain nodes as list nodes. This has the effect that these nodes
47
* will never be combined.
51
* href="http://commons.apache.org/configuration/team-list.html">Commons
52
* Configuration team</a>
53
* @version $Id: OverrideCombiner.java 1301991 2012-03-17 20:18:02Z sebb $
56
public class OverrideCombiner extends NodeCombiner
59
* Constructs an override combination for the passed in node structures.
61
* @param node1 the first node
62
* @param node2 the second node
63
* @return the resulting combined node structure
66
public ConfigurationNode combine(ConfigurationNode node1,
67
ConfigurationNode node2)
69
ViewNode result = createViewNode();
70
result.setName(node1.getName());
72
// Process nodes from the first structure, which override the second
73
for (ConfigurationNode child : node1.getChildren())
75
ConfigurationNode child2 = canCombine(node1, node2, child);
78
result.addChild(combine(child, child2));
82
result.addChild(child);
86
// Process nodes from the second structure, which are not contained
87
// in the first structure
88
for (ConfigurationNode child : node2.getChildren())
90
if (node1.getChildrenCount(child.getName()) < 1)
92
result.addChild(child);
96
// Handle attributes and value
97
addAttributes(result, node1, node2);
98
result.setValue((node1.getValue() != null) ? node1.getValue() : node2
105
* Handles the attributes during a combination process. First all attributes
106
* of the first node will be added to the result. Then all attributes of the
107
* second node, which are not contained in the first node, will also be
110
* @param result the resulting node
111
* @param node1 the first node
112
* @param node2 the second node
114
protected void addAttributes(ViewNode result, ConfigurationNode node1,
115
ConfigurationNode node2)
117
result.appendAttributes(node1);
118
for (ConfigurationNode attr : node2.getAttributes())
120
if (node1.getAttributeCount(attr.getName()) == 0)
122
result.addAttribute(attr);
128
* Tests if a child node of the second node can be combined with the given
129
* child node of the first node. If this is the case, the corresponding node
130
* will be returned, otherwise <b>null</b>. This implementation checks
131
* whether the child node occurs only once in both hierarchies and is no
134
* @param node1 the first node
135
* @param node2 the second node
136
* @param child the child node (of the first node)
137
* @return a child of the second node, with which a combination is possible
139
protected ConfigurationNode canCombine(ConfigurationNode node1,
140
ConfigurationNode node2, ConfigurationNode child)
142
if (node2.getChildrenCount(child.getName()) == 1
143
&& node1.getChildrenCount(child.getName()) == 1
144
&& !isListNode(child))
146
return node2.getChildren(child.getName()).get(0);