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 */
017 package org.apache.commons.configuration.tree;
018
019 import java.util.Iterator;
020
021 /**
022 * <p>
023 * A concrete combiner implementation that is able to construct an override
024 * combination.
025 * </p>
026 * <p>
027 * An <em>override combination</em> means that nodes in the first node
028 * structure take precedence over nodes in the second, or - in other words -
029 * nodes of the second structure are only added to the resulting structure if
030 * they do not occure in the first one. This is especially suitable for dealing
031 * with the properties of configurations that are defined in an
032 * <code>override</code> section of a configuration definition file (hence the
033 * name).
034 * </p>
035 * <p>
036 * This combiner will iterate over the second node hierarchy and find all nodes
037 * that are not contained in the first hierarchy; these are added to the result.
038 * If a node can be found in both structures, it is checked whether a
039 * combination (in a recursive way) can be constructed for the two, which will
040 * then be added. Per default, nodes are combined, which occur only once in both
041 * structures. This test is implemented in the <code>canCombine()</code>
042 * method.
043 * </p>
044 * <p>
045 * As is true for the <code>{@link UnionCombiner}</code>, for this combiner
046 * list nodes are important. The <code>addListNode()</code> can be called to
047 * declare certain nodes as list nodes. This has the effect that these nodes
048 * will never be combined.
049 * </p>
050 *
051 * @author <a
052 * href="http://commons.apache.org/configuration/team-list.html">Commons
053 * Configuration team</a>
054 * @version $Id: OverrideCombiner.java 561230 2007-07-31 04:17:09Z rahul $
055 * @since 1.3
056 */
057 public class OverrideCombiner extends NodeCombiner
058 {
059 /**
060 * Constructs an override combination for the passed in node structures.
061 *
062 * @param node1 the first node
063 * @param node2 the second node
064 * @return the resulting combined node structure
065 */
066 public ConfigurationNode combine(ConfigurationNode node1,
067 ConfigurationNode node2)
068 {
069 ViewNode result = createViewNode();
070 result.setName(node1.getName());
071
072 // Process nodes from the first structure, which override the second
073 for (Iterator it = node1.getChildren().iterator(); it.hasNext();)
074 {
075 ConfigurationNode child = (ConfigurationNode) it.next();
076 ConfigurationNode child2 = canCombine(node1, node2, child);
077 if (child2 != null)
078 {
079 result.addChild(combine(child, child2));
080 }
081 else
082 {
083 result.addChild(child);
084 }
085 }
086
087 // Process nodes from the second structure, which are not contained
088 // in the first structure
089 for (Iterator it = node2.getChildren().iterator(); it.hasNext();)
090 {
091 ConfigurationNode child = (ConfigurationNode) it.next();
092 if (node1.getChildrenCount(child.getName()) < 1)
093 {
094 result.addChild(child);
095 }
096 }
097
098 // Handle attributes and value
099 addAttributes(result, node1, node2);
100 result.setValue((node1.getValue() != null) ? node1.getValue() : node2
101 .getValue());
102
103 return result;
104 }
105
106 /**
107 * Handles the attributes during a combination process. First all attributes
108 * of the first node will be added to the result. Then all attributes of the
109 * second node, which are not contained in the first node, will also be
110 * added.
111 *
112 * @param result the resulting node
113 * @param node1 the first node
114 * @param node2 the second node
115 */
116 protected void addAttributes(ViewNode result, ConfigurationNode node1,
117 ConfigurationNode node2)
118 {
119 result.appendAttributes(node1);
120 for (Iterator it = node2.getAttributes().iterator(); it.hasNext();)
121 {
122 ConfigurationNode attr = (ConfigurationNode) it.next();
123 if (node1.getAttributeCount(attr.getName()) == 0)
124 {
125 result.addAttribute(attr);
126 }
127 }
128 }
129
130 /**
131 * Tests if a child node of the second node can be combined with the given
132 * child node of the first node. If this is the case, the corresponding node
133 * will be returned, otherwise <b>null</b>. This implementation checks
134 * whether the child node occurs only once in both hierarchies and is no
135 * known list node.
136 *
137 * @param node1 the first node
138 * @param node2 the second node
139 * @param child the child node (of the first node)
140 * @return a child of the second node, with which a combination is possible
141 */
142 protected ConfigurationNode canCombine(ConfigurationNode node1,
143 ConfigurationNode node2, ConfigurationNode child)
144 {
145 if (node2.getChildrenCount(child.getName()) == 1
146 && node1.getChildrenCount(child.getName()) == 1
147 && !isListNode(child))
148 {
149 return (ConfigurationNode) node2.getChildren(child.getName())
150 .get(0);
151 }
152 else
153 {
154 return null;
155 }
156 }
157 }