commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r225536 - in /jakarta/commons/proper/configuration/trunk: conf/ src/java/org/apache/commons/configuration/ src/test/org/apache/commons/configuration/ xdocs/
Date Wed, 27 Jul 2005 15:36:47 GMT
Author: ebourg
Date: Wed Jul 27 08:36:40 2005
New Revision: 225536

URL: http://svn.apache.org/viewcvs?rev=225536&view=rev
Log:
ConfigurationFactory now generates a XMLPropertyConfiguration on <properties> elements
with a filename ending with .xml

Modified:
    jakarta/commons/proper/configuration/trunk/conf/testDigesterConfiguration.xml
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationFactory.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationFactory.java
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml

Modified: jakarta/commons/proper/configuration/trunk/conf/testDigesterConfiguration.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/conf/testDigesterConfiguration.xml?rev=225536&r1=225535&r2=225536&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/conf/testDigesterConfiguration.xml (original)
+++ jakarta/commons/proper/configuration/trunk/conf/testDigesterConfiguration.xml Wed Jul
27 08:36:40 2005
@@ -2,5 +2,6 @@
 
 <configuration>
   <properties fileName="conf/test.properties"/>
+  <properties fileName="conf/test.properties.xml"/>
   <xml fileName="conf/test.xml"/>
 </configuration>

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationFactory.java?rev=225536&r1=225535&r2=225536&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationFactory.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationFactory.java
Wed Jul 27 08:36:40 2005
@@ -280,7 +280,7 @@
         setupDigesterInstance(
             digester,
             matchString + "properties",
-            new FileConfigurationFactory(PropertiesConfiguration.class),
+            new PropertiesConfigurationFactory(),
             null,
             additional);
 
@@ -335,16 +335,16 @@
         {
             setupUnionRules(digester, matchString);
         }
+
         digester.addFactoryCreate(matchString, factory);
         digester.addSetProperties(matchString);
+
         if (method != null)
         {
             digester.addCallMethod(matchString, method);
         }
-        digester.addSetNext(
-            matchString,
-            "addConfiguration",
-            Configuration.class.getName());
+
+        digester.addSetNext(matchString, "addConfiguration", Configuration.class.getName());
     }
 
     /**
@@ -492,7 +492,7 @@
          */
         public Object createObject(Attributes attributes) throws Exception
         {
-            FileConfiguration conf = (FileConfiguration) super.createObject(attributes);
+            FileConfiguration conf = createConfiguration(attributes);
             conf.setBasePath(getBasePath());
             conf.setFileName(attributes.getValue(ATTR_FILENAME));
             try
@@ -500,9 +500,9 @@
                 log.info("Trying to load configuration " + conf.getFileName());
                 conf.load();
             }
-            catch(ConfigurationException cex)
+            catch (ConfigurationException cex)
             {
-                if(attributes.getValue(ATTR_OPTIONAL) != null
+                if (attributes.getValue(ATTR_OPTIONAL) != null
                         && PropertyConverter.toBoolean(attributes.getValue(ATTR_OPTIONAL)).booleanValue())
                 {
                     log.warn("Could not load optional configuration " + conf.getFileName());
@@ -513,6 +513,39 @@
                 }
             }
             return conf;
+        }
+
+        protected FileConfiguration createConfiguration(Attributes attributes) throws Exception
+        {
+            return (FileConfiguration) super.createObject(attributes);
+        }
+    }
+
+    /**
+     * A factory that returns an XMLPropertiesConfiguration for .xml files
+     * and a PropertiesConfiguration for the others.
+     *
+     * @since 1.2
+     */
+    public class PropertiesConfigurationFactory extends FileConfigurationFactory
+    {
+        public PropertiesConfigurationFactory()
+        {
+            super(null);
+        }
+
+        protected FileConfiguration createConfiguration(Attributes attributes) throws Exception
+        {
+            String filename = attributes.getValue(ATTR_FILENAME);
+
+            if (filename != null && filename.toLowerCase().trim().endsWith(".xml"))
+            {
+                return new XMLPropertiesConfiguration();
+            }
+            else
+            {
+                return new PropertiesConfiguration();
+            }
         }
     }
 

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationFactory.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationFactory.java?rev=225536&r1=225535&r2=225536&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationFactory.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationFactory.java
Wed Jul 27 08:36:40 2005
@@ -78,45 +78,51 @@
 
     public void testLoadingConfiguration() throws Exception
     {
-        factory.setConfigurationFileName(
-                testDigesterFile.toString());
+        factory.setConfigurationFileName(testDigesterFile.toString());
+
+        compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
+
+        assertEquals("Number of configurations", 4, compositeConfiguration.getNumberOfConfigurations());
+        assertEquals(PropertiesConfiguration.class, compositeConfiguration.getConfiguration(0).getClass());
+        assertEquals(XMLPropertiesConfiguration.class, compositeConfiguration.getConfiguration(1).getClass());
+        assertEquals(XMLConfiguration.class, compositeConfiguration.getConfiguration(2).getClass());
+
+        // check the first configuration
+        PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration.getConfiguration(0);
+        assertNotNull("Make sure we have a fileName: " + pc.getFileName(), pc.getFileName());
+
+        // check some properties
+        assertTrue("Make sure we have loaded our key", compositeConfiguration.getBoolean("test.boolean"));
+        assertEquals("I'm complex!", compositeConfiguration.getProperty("element2.subelement.subsubelement"));
+        assertEquals("property in the XMLPropertiesConfiguration", "value1", compositeConfiguration.getProperty("key1"));
+    }
+
+    public void testLoadingConfigurationWithRulesXML() throws Exception
+    {
+        factory.setConfigurationFileName(testDigesterFile.toString());
+        factory.setDigesterRules(digesterRules.toURL());
+
+        compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
 
-        compositeConfiguration =
-                (CompositeConfiguration) factory.getConfiguration();
+        assertEquals("Number of configurations", 4, compositeConfiguration.getNumberOfConfigurations());
+        assertEquals(PropertiesConfiguration.class, compositeConfiguration.getConfiguration(0).getClass());
+        //assertEquals(XMLPropertiesConfiguration.class, compositeConfiguration.getConfiguration(1).getClass());
// doesn't work
+        assertEquals(XMLConfiguration.class, compositeConfiguration.getConfiguration(2).getClass());
+
+        // check the first configuration
+        PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration.getConfiguration(0);
+        assertNotNull("Make sure we have a fileName: " + pc.getFileName(), pc.getFileName());
+
+        // check some properties
+        assertTrue("Make sure we have loaded our key", pc.getBoolean("test.boolean"));
+        assertTrue("Make sure we have loaded our key", compositeConfiguration.getBoolean("test.boolean"));
 
-        assertEquals(
-                "Verify how many configs",
-                3,
-                compositeConfiguration.getNumberOfConfigurations());
-        assertEquals(
-                PropertiesConfiguration.class,
-                compositeConfiguration.getConfiguration(0).getClass());
-        PropertiesConfiguration pc =
-                (PropertiesConfiguration) compositeConfiguration.getConfiguration(
-                        0);
-
-        assertNotNull(
-                "Make sure we have a fileName:" + pc.getFileName(),
-                pc.getFileName());
-
-        assertTrue(
-                "Make sure we have loades our key",
-                compositeConfiguration.getBoolean("test.boolean"));
-        assertEquals(
-                "I'm complex!",
-                compositeConfiguration.getProperty(
-                        "element2.subelement.subsubelement"));
-
-        configuration = compositeConfiguration;
-        assertEquals(
-                "I'm complex!",
-                configuration.getProperty("element2.subelement.subsubelement"));
+        assertEquals("I'm complex!", compositeConfiguration.getProperty("element2.subelement.subsubelement"));
     }
 
     public void testLoadingConfigurationReverseOrder() throws Exception
     {
-        factory.setConfigurationFileName(
-                testDigesterFileReverseOrder.toString());
+        factory.setConfigurationFileName(testDigesterFileReverseOrder.toString());
 
         configuration = factory.getConfiguration();
 
@@ -128,47 +134,6 @@
         assertEquals("1", configuration.getProperty("test.short"));
     }
 
-    public void testLoadingConfigurationWithRulesXML() throws Exception
-    {
-        factory.setConfigurationFileName(testDigesterFile.toString());
-        factory.setDigesterRules(digesterRules.toURL());
-
-        compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
-
-        assertEquals(
-                "Verify how many configs",
-                3,
-                compositeConfiguration.getNumberOfConfigurations());
-
-        assertEquals(
-                PropertiesConfiguration.class,
-                compositeConfiguration.getConfiguration(0).getClass());
-
-        PropertiesConfiguration pc =
-                (PropertiesConfiguration) compositeConfiguration.getConfiguration(
-                        0);
-        assertNotNull(
-                "Make sure we have a fileName:" + pc.getFileName(),
-                pc.getFileName());
-        assertTrue(
-                "Make sure we have loaded our key",
-                pc.getBoolean("test.boolean"));
-
-        assertTrue(
-                "Make sure we have loaded our key",
-                compositeConfiguration.getBoolean("test.boolean"));
-
-        assertEquals(
-                "I'm complex!",
-                compositeConfiguration.getProperty(
-                        "element2.subelement.subsubelement"));
-
-        configuration = compositeConfiguration;
-        assertEquals(
-                "I'm complex!",
-                configuration.getProperty("element2.subelement.subsubelement"));
-    }
-
     public void testLoadingConfigurationNamespaceAware() throws Exception
     {
         factory.setConfigurationFileName(testDigesterFileNamespaceAware.toString());
@@ -318,82 +283,44 @@
     private void checkUnionConfig() throws Exception
     {
         compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
-        assertEquals(
-                "Verify how many configs",
-                3,
-                compositeConfiguration.getNumberOfConfigurations());
+        assertEquals("Verify how many configs", 3, compositeConfiguration.getNumberOfConfigurations());
 
         // Test if union was constructed correctly
         Object prop = compositeConfiguration.getProperty("tables.table.name");
         assertTrue(prop instanceof Collection);
         assertEquals(3, ((Collection) prop).size());
-        assertEquals(
-                "users",
-                compositeConfiguration.getProperty("tables.table(0).name"));
-        assertEquals(
-                "documents",
-                compositeConfiguration.getProperty("tables.table(1).name"));
-        assertEquals(
-                "tasks",
-                compositeConfiguration.getProperty("tables.table(2).name"));
-
-        prop =
-                compositeConfiguration.getProperty(
-                        "tables.table.fields.field.name");
+        assertEquals("users", compositeConfiguration.getProperty("tables.table(0).name"));
+        assertEquals("documents", compositeConfiguration.getProperty("tables.table(1).name"));
+        assertEquals("tasks", compositeConfiguration.getProperty("tables.table(2).name"));
+
+        prop = compositeConfiguration.getProperty("tables.table.fields.field.name");
         assertTrue(prop instanceof Collection);
         assertEquals(17, ((Collection) prop).size());
 
-        assertEquals(
-                "smtp.mydomain.org",
-                compositeConfiguration.getString("mail.host.smtp"));
-        assertEquals(
-                "pop3.mydomain.org",
-                compositeConfiguration.getString("mail.host.pop"));
+        assertEquals("smtp.mydomain.org", compositeConfiguration.getString("mail.host.smtp"));
+        assertEquals("pop3.mydomain.org", compositeConfiguration.getString("mail.host.pop"));
 
         // This was overriden
-        assertEquals(
-                "masterOfPost",
-                compositeConfiguration.getString("mail.account.user"));
-        assertEquals(
-                "topsecret",
-                compositeConfiguration.getString("mail.account.psswd"));
+        assertEquals("masterOfPost", compositeConfiguration.getString("mail.account.user"));
+        assertEquals("topsecret", compositeConfiguration.getString("mail.account.psswd"));
 
         // This was overriden, too, but not in additional section
-        assertEquals(
-                "enhanced factory",
-                compositeConfiguration.getString("test.configuration"));
+        assertEquals("enhanced factory", compositeConfiguration.getString("test.configuration"));
     }
 
     private void checkCompositeConfiguration() throws Exception
     {
         compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
 
-        assertEquals(
-                "Verify how many configs",
-                2,
-                compositeConfiguration.getNumberOfConfigurations());
-
-        assertEquals(
-                PropertiesConfiguration.class,
-                compositeConfiguration.getConfiguration(0).getClass());
-
-        PropertiesConfiguration pc =
-                (PropertiesConfiguration) compositeConfiguration.getConfiguration(
-                        0);
-        assertNotNull(
-                "Make sure we have a fileName:" + pc.getFileName(),
-                pc.getFileName());
-        assertTrue(
-                "Make sure we have loaded our key",
-                pc.getBoolean("test.boolean"));
-
-        assertTrue(
-                "Make sure we have loaded our key",
-                compositeConfiguration.getBoolean("test.boolean"));
+        assertEquals("Verify how many configs", 2, compositeConfiguration.getNumberOfConfigurations());
+        assertEquals(PropertiesConfiguration.class, compositeConfiguration.getConfiguration(0).getClass());
 
+        PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration.getConfiguration(0);
+        assertNotNull("Make sure we have a fileName:" + pc.getFileName(), pc.getFileName());
+        assertTrue("Make sure we have loaded our key", pc.getBoolean("test.boolean"));
+        assertTrue("Make sure we have loaded our key", compositeConfiguration.getBoolean("test.boolean"));
 
-        Object property = compositeConfiguration.getProperty(
-                "element2.subelement.subsubelement");
+        Object property = compositeConfiguration.getProperty("element2.subelement.subsubelement");
         assertNull("Should have returned a null", property);
     }
 }

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=225536&r1=225535&r2=225536&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Wed Jul 27 08:36:40 2005
@@ -23,6 +23,11 @@
   <body>
 
     <release version="1.2-dev" date="in SVN">
+      <action dev="ebourg" type="add">
+        Added support for XMLPropertiesConfiguration in ConfigurationFactory.
+        A &lt;properties> element will generate a XMLPropertiesConfiguration
+        if the filename ends with ".xml".
+      </action>
       <action dev="ebourg" type="add" issue="29192">
         PropertiesConfiguration now supports escaped key/value separators in the keys
         (i.e foo\:key = bar).



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message