commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r709771 - in /commons/proper/configuration/trunk/src: java/org/apache/commons/configuration/ConfigurationUtils.java test/org/apache/commons/configuration/TestConfigurationUtils.java
Date Sat, 01 Nov 2008 20:15:00 GMT
Author: oheger
Date: Sat Nov  1 13:15:00 2008
New Revision: 709771

URL: http://svn.apache.org/viewvc?rev=709771&view=rev
Log:
CONFIGURATION-336: Added a new convertToHierarchical() method to ConfigurationUtils that also
takes an expression engine as parameter.

Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=709771&r1=709770&r2=709771&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
(original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
Sat Nov  1 13:15:00 2008
@@ -33,6 +33,7 @@
 import org.apache.commons.configuration.event.ConfigurationErrorEvent;
 import org.apache.commons.configuration.event.ConfigurationErrorListener;
 import org.apache.commons.configuration.event.EventSource;
+import org.apache.commons.configuration.tree.ExpressionEngine;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.SystemUtils;
 import org.apache.commons.logging.Log;
@@ -67,7 +68,7 @@
      */
     private ConfigurationUtils()
     {
-        // to prevent instanciation...
+        // to prevent instantiation...
     }
 
     /**
@@ -182,6 +183,35 @@
     public static HierarchicalConfiguration convertToHierarchical(
             Configuration conf)
     {
+        return convertToHierarchical(conf, null);
+    }
+
+    /**
+     * Converts the passed in <code>Configuration</code> object to a
+     * hierarchical one using the specified <code>ExpressionEngine</code>. This
+     * conversion works by adding the keys found in the configuration to a newly
+     * created hierarchical configuration. When adding new keys to a
+     * hierarchical configuration the keys are interpreted by its
+     * <code>ExpressionEngine</code>. If they contain special characters (e.g.
+     * brackets) that are treated in a special way by the default expression
+     * engine, it may be necessary using a specific engine that can deal with
+     * such characters. Otherwise <b>null</b> can be passed in for the
+     * <code>ExpressionEngine</code>; then the default expression engine is
+     * used. If the passed in configuration is already hierarchical, it is
+     * directly returned. (However, the <code>ExpressionEngine</code> is set
if
+     * it is not <b>null</b>.) Otherwise all properties are copied into a new
+     * hierarchical configuration.
+     *
+     * @param conf the configuration to convert
+     * @param engine the <code>ExpressionEngine</code> for the hierarchical
+     *        configuration or <b>null</b> for the default
+     * @return the new hierarchical configuration (the result is <b>null</b>
if
+     *         and only if the passed in configuration is <b>null</b>)
+     * @since 1.6
+     */
+    public static HierarchicalConfiguration convertToHierarchical(
+            Configuration conf, ExpressionEngine engine)
+    {
         if (conf == null)
         {
             return null;
@@ -189,11 +219,22 @@
 
         if (conf instanceof HierarchicalConfiguration)
         {
-            return (HierarchicalConfiguration) conf;
+            HierarchicalConfiguration hc = (HierarchicalConfiguration) conf;
+            if (engine != null)
+            {
+                hc.setExpressionEngine(engine);
+            }
+
+            return hc;
         }
         else
         {
             HierarchicalConfiguration hc = new HierarchicalConfiguration();
+            if (engine != null)
+            {
+                hc.setExpressionEngine(engine);
+            }
+
             // Workaround for problem with copy()
             boolean delimiterParsingStatus = hc.isDelimiterParsingDisabled();
             hc.setDelimiterParsingDisabled(true);

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java?rev=709771&r1=709770&r2=709771&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
Sat Nov  1 13:15:00 2008
@@ -23,11 +23,14 @@
 import java.util.Iterator;
 import java.util.List;
 
-import com.mockobjects.dynamic.Mock;
-
 import junit.framework.TestCase;
 import junitx.framework.ListAssert;
 
+import org.apache.commons.configuration.tree.DefaultExpressionEngine;
+import org.apache.commons.configuration.tree.ExpressionEngine;
+
+import com.mockobjects.dynamic.Mock;
+
 /**
  * Tests the ConfigurationUtils class
  *
@@ -268,6 +271,53 @@
     }
 
     /**
+     * Tests converting a configuration to a hierarchical one using a specific
+     * expression engine.
+     */
+    public void testConvertToHierarchicalEngine()
+    {
+        Configuration conf = new BaseConfiguration();
+        conf.addProperty("test(a)", Boolean.TRUE);
+        conf.addProperty("test(b)", Boolean.FALSE);
+        DefaultExpressionEngine engine = new DefaultExpressionEngine();
+        engine.setIndexStart("[");
+        engine.setIndexEnd("]");
+        HierarchicalConfiguration hc = ConfigurationUtils
+                .convertToHierarchical(conf, engine);
+        assertTrue("Wrong value for test(a)", hc.getBoolean("test(a)"));
+        assertFalse("Wrong value for test(b)", hc.getBoolean("test(b)"));
+    }
+
+    /**
+     * Tests converting an already hierarchical configuration using an
+     * expression engine. The new engine should be set.
+     */
+    public void testConvertHierarchicalToHierarchicalEngine()
+    {
+        HierarchicalConfiguration hc = new HierarchicalConfiguration();
+        ExpressionEngine engine = new DefaultExpressionEngine();
+        assertSame("Created new configuration", hc, ConfigurationUtils
+                .convertToHierarchical(hc, engine));
+        assertSame("Engine was not set", engine, hc.getExpressionEngine());
+    }
+
+    /**
+     * Tests converting an already hierarchical configuration using a null
+     * expression engine. In this case the expression engine of the
+     * configuration should not be touched.
+     */
+    public void testConvertHierarchicalToHierarchicalNullEngine()
+    {
+        HierarchicalConfiguration hc = new HierarchicalConfiguration();
+        ExpressionEngine engine = new DefaultExpressionEngine();
+        hc.setExpressionEngine(engine);
+        assertSame("Created new configuration", hc, ConfigurationUtils
+                .convertToHierarchical(hc, null));
+        assertSame("Expression engine was changed", engine, hc
+                .getExpressionEngine());
+    }
+
+    /**
      * Tests cloning a configuration that supports this operation.
      */
     public void testCloneConfiguration()



Mime
View raw message