You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by jo...@apache.org on 2009/03/18 19:31:03 UTC

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

Author: joehni
Date: Wed Mar 18 18:31:02 2009
New Revision: 755692

URL: http://svn.apache.org/viewvc?rev=755692&view=rev
Log:
ConfigurationInterpolator can be nested. This allows the SubsetConfiguration to interpolate local lookups of the parent (CONFIGURATION-375) as well as interpolation of keys in the parent configuration (CONFIGURATION-376).

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SubsetConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubnodeConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubsetConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java?rev=755692&r1=755691&r2=755692&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java Wed Mar 18 18:31:02 2009
@@ -347,7 +347,7 @@
      * Returns the Converter used for the transformation of the properties.
      * By default the {@link DefaultPropertyConverter} is used.
      *
-     * @return
+     * @return the converter
      * @since 2.0
      */
     public Converter getConverter()

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SubsetConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SubsetConfiguration.java?rev=755692&r1=755691&r2=755692&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SubsetConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SubsetConfiguration.java Wed Mar 18 18:31:02 2009
@@ -17,6 +17,8 @@
 
 package org.apache.commons.configuration2;
 
+import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
+
 import java.util.Iterator;
 
 /**
@@ -206,7 +208,11 @@
         else
         {
             SubsetConfiguration config = new SubsetConfiguration(parent, "");
-            getInterpolator().registerLocalLookups(config.getInterpolator());
+            ConfigurationInterpolator interpolator = config.getInterpolator();
+            getInterpolator().registerLocalLookups(interpolator);
+            if (parent instanceof AbstractConfiguration) {
+                interpolator.setParentInterpolator(((AbstractConfiguration)parent).getInterpolator());
+            }
             return config.interpolate(base);
         }
     }

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java?rev=755692&r1=755691&r2=755692&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/interpol/ConfigurationInterpolator.java Wed Mar 18 18:31:02 2009
@@ -127,6 +127,9 @@
 
     /** Stores the default lookup object. */
     private StrLookup defaultLookup;
+    
+    /** Stores a parent interpolator objects if the interpolator is nested hierarchically. */
+    private ConfigurationInterpolator parentInterpolator;
 
     /**
      * Creates a new instance of <code>ConfigurationInterpolator</code>.
@@ -293,20 +296,30 @@
      * Returns the lookup object to be used for variables without a prefix. This
      * implementation will check whether a default lookup object was set. If
      * this is the case, it will be returned. Otherwise a <b>null</b> lookup
-     * object will be returned.
+     * object will be returned (never <code>null</code>).
      *
      * @return the lookup object to be used for variables without a prefix
      */
     protected StrLookup fetchNoPrefixLookup()
     {
-        return (getDefaultLookup() != null) ? getDefaultLookup() : StrLookup.noneLookup();
+        StrLookup lookup = null;
+        if (getDefaultLookup() != null) { 
+            lookup = getDefaultLookup();
+        }
+        if (lookup == null) { 
+            ConfigurationInterpolator parent = getParentInterpolator();
+            lookup = (parent == null) 
+                ? StrLookup.noneLookup() 
+                : parent.fetchNoPrefixLookup();
+        }
+        return lookup;
     }
 
     /**
      * Obtains the lookup object for the specified prefix. This method is called
      * by the <code>lookup()</code> method. This implementation will check
      * whether a lookup object is registered for the given prefix. If not, a
-     * <b>null</b> lookup object will be returned.
+     * <b>null</b> lookup object will be returned (never <code>null</code>).
      *
      * @param prefix the prefix
      * @return the lookup object to be used for this prefix
@@ -316,7 +329,10 @@
         StrLookup lookup = localLookups.get(prefix);
         if (lookup == null)
         {
-            lookup = StrLookup.noneLookup();
+            ConfigurationInterpolator parent = getParentInterpolator();
+            lookup = (parent == null) 
+                ? StrLookup.noneLookup() 
+                : parent.fetchLookupForPrefix(prefix);
         }
         return lookup;
     }
@@ -331,6 +347,28 @@
         interpolator.localLookups.putAll(localLookups);
     }
 
+    /**
+     * Sets the parent interpolator. This object is used if the interpolation is nested hierarchically
+     * and the current interpolation object cannot resolve a variable. 
+     * 
+     * @param parentInterpolator the parent interpolator object or <code>null</code>
+     * @since upcoming
+     */
+    public void setParentInterpolator(ConfigurationInterpolator parentInterpolator) {
+        this.parentInterpolator = parentInterpolator;
+    }
+
+    /**
+     * Requests the parent interpolator. This object is used if the interpolation is nested hierarchically
+     * and the current interpolation
+     * 
+     * @return the parent interpolator or <code>null</code>
+     * @since upcoming
+     */
+    public ConfigurationInterpolator getParentInterpolator() {
+        return this.parentInterpolator;
+    }
+
     // static initializer, sets up the map with the standard lookups
     static
     {

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubnodeConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubnodeConfiguration.java?rev=755692&r1=755691&r2=755692&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubnodeConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubnodeConfiguration.java Wed Mar 18 18:31:02 2009
@@ -25,8 +25,11 @@
 
 import junit.framework.TestCase;
 
+import org.apache.commons.configuration2.flat.BaseConfiguration;
+import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
 import org.apache.commons.configuration2.tree.ConfigurationNode;
 import org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine;
+import org.apache.commons.lang.text.StrLookup;
 
 /**
  * Test case for SubnodeConfiguration.
@@ -331,6 +334,36 @@
         }
     }
 
+    /**
+     * Tests manipulating the interpolator.
+     */
+    public void todoTestInterpolator()
+    {
+        parent.addProperty("tablespaces.tablespace.name", "default");
+        parent.addProperty("tablespaces.tablespace(-1).name", "test");
+
+        setUpSubnodeConfig();
+        InterpolationTestHelper.testGetInterpolator(config);
+    }
+
+    public void testLocalLookupsInInterpolatorAreInherited() {
+        parent.addProperty("tablespaces.tablespace.name", "default");
+        parent.addProperty("tablespaces.tablespace(-1).name", "test");
+        parent.addProperty("tables.table(0).var", "${brackets:x}");
+        
+        ConfigurationInterpolator interpolator = parent.getInterpolator();
+        interpolator.registerLookup("brackets", new StrLookup(){
+
+            @Override
+            public String lookup(String key) {
+                return "(" + key +")";
+            }
+            
+        });
+        setUpSubnodeConfig();
+        assertEquals("Local lookup was not inherited", "(x)", config.getString("var", ""));
+    }
+
 //    /**
 //     * Tests a reload operation for the parent configuration when the subnode
 //     * configuration does not support reloads. Then the new value should not be

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubsetConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubsetConfiguration.java?rev=755692&r1=755691&r2=755692&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubsetConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestSubsetConfiguration.java Wed Mar 18 18:31:02 2009
@@ -297,8 +297,7 @@
         InterpolationTestHelper.testGetInterpolator(subset);
     }
     
-    // TODO: Next step
-    public void todoTestLocalLookupsInInterpolorAreInherited() {
+    public void testLocalLookupsInInterpolatorAreInherited() {
         BaseConfiguration config = new BaseConfiguration();
         ConfigurationInterpolator interpolator = config.getInterpolator();
         interpolator.registerLookup("brackets", new StrLookup(){
@@ -312,6 +311,17 @@
         config.setProperty("prefix.var", "${brackets:x}");
         AbstractConfiguration subset = (AbstractConfiguration) config
                 .subset("prefix");
-        assertEquals("Local lookup was not inherited", "(x)", subset.getString("var", ""));
+        assertEquals("Local lookup was not inherited", "(x)", subset
+                .getString("var", ""));
+    }
+    
+    public void testInterpolationForKeysOfTheParent() {
+        BaseConfiguration config = new BaseConfiguration();
+        config.setProperty("test", "junit");
+        config.setProperty("prefix.key", "${test}");
+        AbstractConfiguration subset = (AbstractConfiguration) config
+                .subset("prefix");
+        assertEquals("Interpolation does not resolve parent keys", "junit",
+                subset.getString("key", ""));
     }
 }