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 2007/09/22 17:12:17 UTC

svn commit: r578455 - in /commons/proper/configuration/trunk/src: java/org/apache/commons/configuration/ test/org/apache/commons/configuration/

Author: oheger
Date: Sat Sep 22 08:12:16 2007
New Revision: 578455

URL: http://svn.apache.org/viewvc?rev=578455&view=rev
Log:
CONFIGURATION-290: DefaultConfigurationBuilder now passes registered entity IDs to the XML configurations it loads

Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java?rev=578455&r1=578454&r2=578455&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java Sat Sep 22 08:12:16 2007
@@ -96,7 +96,7 @@
  * <p>
  * Each configuration declaration consists of a tag whose name is associated
  * with a <code>ConfigurationProvider</code>. This can be one of the
- * pre-defined tags like <code>properties</code>, or <code>xml</code>, or
+ * predefined tags like <code>properties</code>, or <code>xml</code>, or
  * a custom tag, for which a configuration provider was registered. Attributes
  * and sub elements with specific initialization parameters can be added. There
  * are some reserved attributes with a special meaning that can be used in every
@@ -287,8 +287,7 @@
             EXT_XML);
 
     /** Constant for the provider for XML files. */
-    private static final ConfigurationProvider XML_PROVIDER = new FileConfigurationProvider(
-            XMLConfiguration.class);
+    private static final ConfigurationProvider XML_PROVIDER = new XMLConfigurationProvider();
 
     /** Constant for the provider for JNDI sources. */
     private static final ConfigurationProvider JNDI_PROVIDER = new ConfigurationProvider(
@@ -1228,6 +1227,46 @@
     }
 
     /**
+     * A specialized configuration provider for XML configurations. This
+     * implementation acts like a <code>FileConfigurationProvider</code>, but
+     * it will copy all entity IDs that have been registered for the
+     * configuration builder to the new XML configuration before it is loaded.
+     */
+    static class XMLConfigurationProvider extends FileConfigurationProvider
+    {
+        /**
+         * Creates a new instance of <code>XMLConfigurationProvider</code>.
+         */
+        public XMLConfigurationProvider()
+        {
+            super(XMLConfiguration.class);
+        }
+
+        /**
+         * Returns a new empty configuration instance. This implementation
+         * performs some additional initialization specific to XML
+         * configurations.
+         *
+         * @param decl the configuration declaration
+         * @return the new configuration
+         * @throws Exception if an error occurs
+         */
+        public AbstractConfiguration getEmptyConfiguration(
+                ConfigurationDeclaration decl) throws Exception
+        {
+            XMLConfiguration config = (XMLConfiguration) super
+                    .getEmptyConfiguration(decl);
+
+            // copy the registered entities
+            DefaultConfigurationBuilder builder = decl
+                    .getConfigurationBuilder();
+            config.getRegisteredEntities().putAll(
+                    builder.getRegisteredEntities());
+            return config;
+        }
+    }
+
+    /**
      * A specialized configuration provider for file based configurations that
      * can handle configuration sources whose concrete type depends on the
      * extension of the file to be loaded. One example is the
@@ -1291,7 +1330,7 @@
          * file extension matches
          * @param defaultClassName the name of the class to be created when the
          * file extension does not match
-         * @param extension the file extension to be checked agains
+         * @param extension the file extension to be checked against
          * @since 1.4
          */
         public FileExtensionConfigurationProvider(String matchingClassName,

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java?rev=578455&r1=578454&r2=578455&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java Sat Sep 22 08:12:16 2007
@@ -885,7 +885,7 @@
         {
             throw new IllegalArgumentException("Public ID must not be null!");
         }
-        registeredEntities.put(publicId, entityURL);
+        getRegisteredEntities().put(publicId, entityURL);
     }
 
     /**
@@ -906,7 +906,7 @@
         URL entityURL = null;
         if (publicId != null)
         {
-            entityURL = (URL) registeredEntities.get(publicId);
+            entityURL = (URL) getRegisteredEntities().get(publicId);
         }
 
         if (entityURL != null)
@@ -932,6 +932,17 @@
             // default processing behavior
             return null;
         }
+    }
+
+    /**
+     * Returns a map with the entity IDs that have been registered using the
+     * <code>registerEntityId()</code> method.
+     *
+     * @return a map with the registered entity IDs
+     */
+    Map getRegisteredEntities()
+    {
+        return registeredEntities;
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java?rev=578455&r1=578454&r2=578455&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java (original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestDefaultConfigurationBuilder.java Sat Sep 22 08:12:16 2007
@@ -17,6 +17,7 @@
 package org.apache.commons.configuration;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Collection;
 import java.util.Set;
 
@@ -701,5 +702,45 @@
         assertEquals("Wrong number of configurations", 1, cc
                 .getNumberOfConfigurations());
         checkProperties(cc);
+    }
+
+    /**
+     * Tests whether XML settings can be inherited.
+     */
+    public void testLoadXMLWithSettings() throws ConfigurationException,
+            IOException
+    {
+        File confDir = new File("conf");
+        File targetDir = new File("target");
+        File testXMLSource = new File(confDir, "testDtd.xml");
+        File testXMLValidationSource = new File(confDir,
+                "testValidateInvalid.xml");
+        File testSavedXML = new File(targetDir, "testSave.xml");
+        File testSavedFactory = new File(targetDir, "testSaveFactory.xml");
+        File dtdFile = new File(confDir, "properties.dtd");
+        final String publicId = "http://commons.apache.org/test.dtd";
+
+        XMLConfiguration config = new XMLConfiguration(testXMLSource);
+        config.setPublicID(publicId);
+        config.save(testSavedXML);
+        factory.addProperty("xml[@fileName]", testSavedXML.getAbsolutePath());
+        factory.addProperty("xml(0)[@validating]", "true");
+        factory.addProperty("xml(-1)[@fileName]", testXMLValidationSource
+                .getAbsolutePath());
+        factory.addProperty("xml(1)[@config-optional]", "true");
+        factory.addProperty("xml(1)[@validating]", "true");
+        factory.save(testSavedFactory);
+
+        factory = new DefaultConfigurationBuilder();
+        factory.setFile(testSavedFactory);
+        factory.registerEntityId(publicId, dtdFile.toURL());
+        factory.clearErrorListeners();
+        Configuration c = factory.getConfiguration();
+        assertEquals("Wrong property value", "value1", c.getString("entry(0)"));
+        assertFalse("Invalid XML source was loaded", c
+                .containsKey("table.name"));
+
+        testSavedXML.delete();
+        testSavedFactory.delete();
     }
 }