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://jakarta.apache.org/commons/configuration/team-list.html">Commons
053: * Configuration team</a>
054: * @version $Id: OverrideCombiner.java 439648 2006-09-02 20:42:10Z oheger $
055: * @since 1.3
056: */
057: public class OverrideCombiner extends NodeCombiner {
058: /**
059: * Constructs an override combination for the passed in node structures.
060: *
061: * @param node1 the first node
062: * @param node2 the second node
063: * @return the resulting combined node structure
064: */
065: public ConfigurationNode combine(ConfigurationNode node1,
066: ConfigurationNode node2) {
067: ViewNode result = createViewNode();
068: result.setName(node1.getName());
069:
070: // Process nodes from the first structure, which override the second
071: for (Iterator it = node1.getChildren().iterator(); it.hasNext();) {
072: ConfigurationNode child = (ConfigurationNode) it.next();
073: ConfigurationNode child2 = canCombine(node1, node2, child);
074: if (child2 != null) {
075: result.addChild(combine(child, child2));
076: } else {
077: result.addChild(child);
078: }
079: }
080:
081: // Process nodes from the second structure, which are not contained
082: // in the first structure
083: for (Iterator it = node2.getChildren().iterator(); it.hasNext();) {
084: ConfigurationNode child = (ConfigurationNode) it.next();
085: if (node1.getChildrenCount(child.getName()) < 1) {
086: result.addChild(child);
087: }
088: }
089:
090: // Handle attributes and value
091: addAttributes(result, node1, node2);
092: result.setValue((node1.getValue() != null) ? node1.getValue()
093: : node2.getValue());
094:
095: return result;
096: }
097:
098: /**
099: * Handles the attributes during a combination process. First all attributes
100: * of the first node will be added to the result. Then all attributes of the
101: * second node, which are not contained in the first node, will also be
102: * added.
103: *
104: * @param result the resulting node
105: * @param node1 the first node
106: * @param node2 the second node
107: */
108: protected void addAttributes(ViewNode result,
109: ConfigurationNode node1, ConfigurationNode node2) {
110: result.appendAttributes(node1);
111: for (Iterator it = node2.getAttributes().iterator(); it
112: .hasNext();) {
113: ConfigurationNode attr = (ConfigurationNode) it.next();
114: if (node1.getAttributeCount(attr.getName()) == 0) {
115: result.addAttribute(attr);
116: }
117: }
118: }
119:
120: /**
121: * Tests if a child node of the second node can be combined with the given
122: * child node of the first node. If this is the case, the corresponding node
123: * will be returned, otherwise <b>null</b>. This implementation checks
124: * whether the child node occurs only once in both hierarchies and is no
125: * known list node.
126: *
127: * @param node1 the first node
128: * @param node2 the second node
129: * @param child the child node (of the first node)
130: * @return a child of the second node, with which a combination is possible
131: */
132: protected ConfigurationNode canCombine(ConfigurationNode node1,
133: ConfigurationNode node2, ConfigurationNode child) {
134: if (node2.getChildrenCount(child.getName()) == 1
135: && node1.getChildrenCount(child.getName()) == 1
136: && !isListNode(child)) {
137: return (ConfigurationNode) node2.getChildren(
138: child.getName()).get(0);
139: } else {
140: return null;
141: }
142: }
143: }
|