Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
OverrideCombiner |
|
| 4.666666666666667;4,667 |
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 | * http://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.configuration.tree; | |
18 | ||
19 | ||
20 | /** | |
21 | * <p> | |
22 | * A concrete combiner implementation that is able to construct an override | |
23 | * combination. | |
24 | * </p> | |
25 | * <p> | |
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 | |
32 | * name). | |
33 | * </p> | |
34 | * <p> | |
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()} | |
41 | * method. | |
42 | * </p> | |
43 | * <p> | |
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. | |
48 | * </p> | |
49 | * | |
50 | * @author <a | |
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 $ | |
54 | * @since 1.3 | |
55 | */ | |
56 | 72 | public class OverrideCombiner extends NodeCombiner |
57 | { | |
58 | /** | |
59 | * Constructs an override combination for the passed in node structures. | |
60 | * | |
61 | * @param node1 the first node | |
62 | * @param node2 the second node | |
63 | * @return the resulting combined node structure | |
64 | */ | |
65 | @Override | |
66 | public ConfigurationNode combine(ConfigurationNode node1, | |
67 | ConfigurationNode node2) | |
68 | { | |
69 | 263 | ViewNode result = createViewNode(); |
70 | 263 | result.setName(node1.getName()); |
71 | ||
72 | // Process nodes from the first structure, which override the second | |
73 | 263 | for (ConfigurationNode child : node1.getChildren()) |
74 | { | |
75 | 2269 | ConfigurationNode child2 = canCombine(node1, node2, child); |
76 | 2269 | if (child2 != null) |
77 | { | |
78 | 157 | result.addChild(combine(child, child2)); |
79 | } | |
80 | else | |
81 | { | |
82 | 2112 | result.addChild(child); |
83 | } | |
84 | 2269 | } |
85 | ||
86 | // Process nodes from the second structure, which are not contained | |
87 | // in the first structure | |
88 | 263 | for (ConfigurationNode child : node2.getChildren()) |
89 | { | |
90 | 1441 | if (node1.getChildrenCount(child.getName()) < 1) |
91 | { | |
92 | 1178 | result.addChild(child); |
93 | } | |
94 | } | |
95 | ||
96 | // Handle attributes and value | |
97 | 263 | addAttributes(result, node1, node2); |
98 | 263 | result.setValue((node1.getValue() != null) ? node1.getValue() : node2 |
99 | .getValue()); | |
100 | ||
101 | 263 | return result; |
102 | } | |
103 | ||
104 | /** | |
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 | |
108 | * added. | |
109 | * | |
110 | * @param result the resulting node | |
111 | * @param node1 the first node | |
112 | * @param node2 the second node | |
113 | */ | |
114 | protected void addAttributes(ViewNode result, ConfigurationNode node1, | |
115 | ConfigurationNode node2) | |
116 | { | |
117 | 263 | result.appendAttributes(node1); |
118 | 263 | for (ConfigurationNode attr : node2.getAttributes()) |
119 | { | |
120 | 27 | if (node1.getAttributeCount(attr.getName()) == 0) |
121 | { | |
122 | 20 | result.addAttribute(attr); |
123 | } | |
124 | } | |
125 | 263 | } |
126 | ||
127 | /** | |
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 | |
132 | * known list node. | |
133 | * | |
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 | |
138 | */ | |
139 | protected ConfigurationNode canCombine(ConfigurationNode node1, | |
140 | ConfigurationNode node2, ConfigurationNode child) | |
141 | { | |
142 | 2269 | if (node2.getChildrenCount(child.getName()) == 1 |
143 | && node1.getChildrenCount(child.getName()) == 1 | |
144 | && !isListNode(child)) | |
145 | { | |
146 | 157 | return node2.getChildren(child.getName()).get(0); |
147 | } | |
148 | else | |
149 | { | |
150 | 2112 | return null; |
151 | } | |
152 | } | |
153 | } |