You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2008/03/26 19:12:32 UTC

svn commit: r641451 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/combined/ test/java/org/apache/commons/configuration2/combined/

Author: oheger
Date: Wed Mar 26 11:12:25 2008
New Revision: 641451

URL: http://svn.apache.org/viewvc?rev=641451&view=rev
Log:
New OverrideCombiner implementation based on node handlers

Added:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/OverrideCombiner.java
      - copied, changed from r636104, commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/tree/OverrideCombiner.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestOverrideCombiner.java
      - copied, changed from r636104, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/TestOverrideCombiner.java
Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java

Copied: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/OverrideCombiner.java (from r636104, commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/tree/OverrideCombiner.java)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/OverrideCombiner.java?p2=commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/OverrideCombiner.java&p1=commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/tree/OverrideCombiner.java&r1=636104&r2=641451&rev=641451&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/tree/OverrideCombiner.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/combined/OverrideCombiner.java Wed Mar 26 11:12:25 2008
@@ -15,9 +15,9 @@
  * limitations under the License.
  */
 
-package org.apache.commons.configuration2.tree;
+package org.apache.commons.configuration2.combined;
 
-import java.util.Iterator;
+import org.apache.commons.configuration2.expr.NodeHandler;
 
 /**
  * <p>
@@ -28,7 +28,7 @@
  * An <em>override combination</em> means that nodes in the first node
  * structure take precedence over nodes in the second, or - in other words -
  * nodes of the second structure are only added to the resulting structure if
- * they do not occure in the first one. This is especially suitable for dealing
+ * they do not occur in the first one. This is especially suitable for dealing
  * with the properties of configurations that are defined in an
  * <code>override</code> section of a configuration definition file (hence the
  * name).
@@ -44,16 +44,15 @@
  * </p>
  * <p>
  * As is true for the <code>{@link UnionCombiner}</code>, for this combiner
- * list nodes are important. The <code>addListNode()</code> can be called to
- * declare certain nodes as list nodes. This has the effect that these nodes
- * will never be combined.
+ * list nodes are important. The <code>addListNode()</code> method can be
+ * called to declare certain nodes as list nodes. This has the effect that these
+ * nodes will never be combined.
  * </p>
  *
- * @author <a
- * href="http://commons.apache.org/configuration/team-list.html">Commons
- * Configuration team</a>
+ * @author <a href="http://commons.apache.org/configuration/team-list.html">Commons
+ *         Configuration team</a>
  * @version $Id$
- * @since 1.3
+ * @since 2.0
  */
 public class OverrideCombiner extends NodeCombiner
 {
@@ -61,42 +60,48 @@
      * Constructs an override combination for the passed in node structures.
      *
      * @param node1 the first node
+     * @param handler1 the node handler for the first source node
      * @param node2 the second node
+     * @param handler2 the node handler for the second source node
      * @return the resulting combined node structure
      */
-    public ConfigurationNode combine(ConfigurationNode node1, ConfigurationNode node2)
+    public <T, U> CombinedNode combine(T node1, NodeHandler<T> handler1,
+            U node2, NodeHandler<U> handler2)
     {
-        ViewNode result = createViewNode();
-        result.setName(node1.getName());
+        CombinedNode result = createViewNode();
+        result.setName(handler1.nodeName(node1));
 
         // Process nodes from the first structure, which override the second
-        for (Iterator<ConfigurationNode> it = node1.getChildren().iterator(); it.hasNext();)
+        for (T child : handler1.getChildren(node1))
         {
-            ConfigurationNode child = it.next();
-            ConfigurationNode child2 = canCombine(node1, node2, child);
+            String childName = handler1.nodeName(child);
+            U child2 = canCombine(node1, handler1, node2, handler2, child);
             if (child2 != null)
             {
-                result.addChild(combine(child, child2));
+                result.addChild(childName, combine(child, handler1, child2,
+                        handler2));
             }
             else
             {
-                result.addChild(child);
+                result.addChild(childName, child);
             }
         }
 
         // Process nodes from the second structure, which are not contained
         // in the first structure
-        for (ConfigurationNode child : node2.getChildren())
+        for (U child : handler2.getChildren(node2))
         {
-            if (node1.getChildrenCount(child.getName()) < 1)
+            String childName = handler2.nodeName(child);
+            if (handler1.getChildrenCount(node1, childName) < 1)
             {
-                result.addChild(child);
+                result.addChild(childName, child);
             }
         }
 
         // Handle attributes and value
-        addAttributes(result, node1, node2);
-        result.setValue((node1.getValue() != null) ? node1.getValue() : node2.getValue());
+        addAttributes(result, node1, handler1, node2, handler2);
+        result.setValue((handler1.getValue(node1) != null) ? handler1
+                .getValue(node1) : handler2.getValue(node2));
 
         return result;
     }
@@ -109,16 +114,20 @@
      *
      * @param result the resulting node
      * @param node1 the first node
+     * @param handler1 the node handler for the first node
      * @param node2 the second node
+     * @param handler2 the node handler for the second node
      */
-    protected void addAttributes(ViewNode result, ConfigurationNode node1, ConfigurationNode node2)
+    protected <T, U> void addAttributes(CombinedNode result, T node1,
+            NodeHandler<T> handler1, U node2, NodeHandler<U> handler2)
     {
-        result.appendAttributes(node1);
-        for (ConfigurationNode attr : node2.getAttributes())
+        appendAttributes(result, node1, handler1);
+        for (String attr : handler2.getAttributes(node2))
         {
-            if (node1.getAttributeCount(attr.getName()) == 0)
+            if (handler1.getAttributeValue(node1, attr) == null)
             {
-                result.addAttribute(attr);
+                result.addAttributeValue(attr, handler2.getAttributeValue(
+                        node2, attr));
             }
         }
     }
@@ -131,17 +140,21 @@
      * known list node.
      *
      * @param node1 the first node
+     * @param handler1 the node handler for the first node
      * @param node2 the second node
+     * @param handler2 the node handler for the second node
      * @param child the child node (of the first node)
      * @return a child of the second node, with which a combination is possible
      */
-    protected ConfigurationNode canCombine(ConfigurationNode node1, ConfigurationNode node2, ConfigurationNode child)
+    protected <T, U> U canCombine(T node1, NodeHandler<T> handler1, U node2,
+            NodeHandler<U> handler2, T child)
     {
-        if (node2.getChildrenCount(child.getName()) == 1
-                && node1.getChildrenCount(child.getName()) == 1
-                && !isListNode(child))
+        String childName = handler1.nodeName(child);
+        if (handler2.getChildrenCount(node2, childName) == 1
+                && handler1.getChildrenCount(node1, childName) == 1
+                && !isListNode(child, handler1))
         {
-            return node2.getChildren(child.getName()).get(0);
+            return handler2.getChildren(node2, childName).get(0);
         }
         else
         {

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java?rev=641451&r1=641450&r2=641451&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/AbstractCombinerTest.java Wed Mar 26 11:12:25 2008
@@ -107,6 +107,7 @@
         CombinedNode cn = (CombinedNode) node;
         ConfigurationNode result = new DefaultConfigurationNode(cn.getName(),
                 cn.getValue());
+        result.setReference(node);
 
         for (String n : cn.getAttributes())
         {

Copied: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestOverrideCombiner.java (from r636104, commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/TestOverrideCombiner.java)
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestOverrideCombiner.java?p2=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestOverrideCombiner.java&p1=commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/TestOverrideCombiner.java&r1=636104&r2=641451&rev=641451&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/tree/TestOverrideCombiner.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/combined/TestOverrideCombiner.java Wed Mar 26 11:12:25 2008
@@ -14,16 +14,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.commons.configuration2.tree;
-
-import java.util.List;
+package org.apache.commons.configuration2.combined;
 
 import org.apache.commons.configuration2.ConfigurationException;
-import org.apache.commons.configuration2.HierarchicalConfiguration;
+import org.apache.commons.configuration2.InMemoryConfiguration;
+import org.apache.commons.configuration2.SubConfiguration;
+import org.apache.commons.configuration2.expr.NodeList;
 import org.apache.commons.configuration2.tree.ConfigurationNode;
-import org.apache.commons.configuration2.tree.NodeCombiner;
-import org.apache.commons.configuration2.tree.OverrideCombiner;
-import org.apache.commons.configuration2.tree.ViewNode;
 
 /**
  * Test class for OverrideCombiner.
@@ -37,6 +34,7 @@
      *
      * @return the combiner
      */
+    @Override
     protected NodeCombiner createCombiner()
     {
         return new OverrideCombiner();
@@ -47,7 +45,7 @@
      */
     public void testSimpleValues() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        InMemoryConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong number of bgcolors", 0, config
                 .getMaxIndex("gui.bgcolor"));
         assertEquals("Wrong bgcolor", "green", config.getString("gui.bgcolor"));
@@ -62,7 +60,7 @@
      */
     public void testAttributes() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        InMemoryConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong value of min attribute", 1, config
                 .getInt("gui.level[@min]"));
         assertEquals("Wrong value of default attribute", 2, config
@@ -78,7 +76,7 @@
      */
     public void testOverrideValues() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        InMemoryConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong user", "Admin", config
                 .getString("base.services.security.login.user"));
         assertEquals("Wrong user type", "default", config
@@ -95,7 +93,7 @@
      */
     public void testListFromFirstStructure() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        InMemoryConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong number of services", 0, config
                 .getMaxIndex("net.service.url"));
         assertEquals("Wrong service", "http://service1.org", config
@@ -110,7 +108,7 @@
      */
     public void testListFromSecondStructure() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        InMemoryConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong number of servers", 3, config
                 .getMaxIndex("net.server.url"));
         assertEquals("Wrong server", "http://testsvr.com", config
@@ -122,12 +120,13 @@
      * not declared as a list node the structures will be combined. But this
      * won't make any difference because the values in the first table override
      * the values in the second table. Only the node for the table element will
-     * be a ViewNode.
+     * be a CombinedNode.
      */
     public void testCombinedTableNoList() throws ConfigurationException
     {
         ConfigurationNode tabNode = checkTable(createCombinedConfiguration());
-        assertTrue("Node is not a view node", tabNode instanceof ViewNode);
+        assertTrue("Node is not a combined node",
+                tabNode.getReference() instanceof CombinedNode);
     }
 
     /**
@@ -140,7 +139,8 @@
     {
         combiner.addListNode("table");
         ConfigurationNode tabNode = checkTable(createCombinedConfiguration());
-        assertFalse("Node is a view node", tabNode instanceof ViewNode);
+        assertFalse("Node is a combined node",
+                tabNode.getReference() instanceof CombinedNode);
     }
 
     /**
@@ -149,11 +149,11 @@
      * @param config the config
      * @return the node for the table element
      */
-    private ConfigurationNode checkTable(HierarchicalConfiguration config)
+    private ConfigurationNode checkTable(InMemoryConfiguration config)
     {
         assertEquals("Wrong number of tables", 0, config
                 .getMaxIndex("database.tables.table"));
-        HierarchicalConfiguration c = config
+        SubConfiguration<ConfigurationNode> c = config
                 .configurationAt("database.tables.table");
         assertEquals("Wrong table name", "documents", c.getString("name"));
         assertEquals("Wrong number of fields", 2, c
@@ -161,9 +161,10 @@
         assertEquals("Wrong field", "docname", c
                 .getString("fields.field(1).name"));
 
-        List nds = config.getExpressionEngine().query(config.getRootNode(),
-                "database.tables.table");
-        assertFalse("No node found", nds.isEmpty());
-        return (ConfigurationNode) nds.get(0);
+        NodeList<ConfigurationNode> nds = config.getExpressionEngine().query(
+                config.getRootNode(), "database.tables.table",
+                config.getNodeHandler());
+        assertTrue("No node found", nds.size() > 0);
+        return nds.getNode(0);
     }
 }