Author: rgoers
Date: Sun Apr 26 07:46:18 2009
New Revision: 768677

URL: http://svn.apache.org/viewvc?rev=768677&view=rev
Log:
Allow CatalogResolver to interpolate catalog entries.

Added:
    commons/proper/configuration/trunk/conf/catalog2.xml
      - copied, changed from r763836, 
commons/proper/configuration/trunk/conf/catalog.xml
    commons/proper/configuration/trunk/conf/testValidation2.xml
      - copied, changed from r768095, 
commons/proper/configuration/trunk/conf/testValidation.xml
Modified:
    
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/DefaultConfigurationBuilder.java
    
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/VFSFileSystem.java
    
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/resolver/CatalogResolver.java
    
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestVFSConfigurationBuilder.java

Copied: commons/proper/configuration/trunk/conf/catalog2.xml (from r763836, 
commons/proper/configuration/trunk/conf/catalog.xml)
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/conf/catalog2.xml?p2=commons/proper/configuration/trunk/conf/catalog2.xml&p1=commons/proper/configuration/trunk/conf/catalog.xml&r1=763836&r2=768677&rev=768677&view=diff
==============================================================================
--- commons/proper/configuration/trunk/conf/catalog.xml (original)
+++ commons/proper/configuration/trunk/conf/catalog2.xml Sun Apr 26 07:46:18 
2009
@@ -5,5 +5,6 @@
           uri="resolver.dtd"/>
   <rewriteSystem systemIdStartString="http://java.sun.com/dtd/";
                  rewritePrefix="./"/>
-  <rewriteSystem systemIdStartString="http://commons.apache.org/"; 
rewritePrefix="./"/>
+  <rewriteSystem systemIdStartString="http://commons.apache.org/";
+                 rewritePrefix="file://${sys:user.dir}/conf/"/>
 </catalog>
\ No newline at end of file

Copied: commons/proper/configuration/trunk/conf/testValidation2.xml (from 
r768095, commons/proper/configuration/trunk/conf/testValidation.xml)
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/conf/testValidation2.xml?p2=commons/proper/configuration/trunk/conf/testValidation2.xml&p1=commons/proper/configuration/trunk/conf/testValidation.xml&r1=768095&r2=768677&rev=768677&view=diff
==============================================================================
--- commons/proper/configuration/trunk/conf/testValidation.xml (original)
+++ commons/proper/configuration/trunk/conf/testValidation2.xml Sun Apr 26 
07:46:18 2009
@@ -6,11 +6,11 @@
       <nodeCombiner 
config-class="org.apache.commons.configuration.tree.OverrideCombiner"/>
       <expressionEngine 
config-class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine"/>
     </result>
-    <entity-resolver catalogFiles="catalog.xml"/>    
+    <entity-resolver catalogFiles="file://${sys:user.dir}/conf/catalog2.xml"/> 
   
   </header>
   <system/>
   <properties fileName="test.properties.xml" throwExceptionOnMissing="true"
     config-name="properties">
   </properties>
-  <xml fileName="sample.xml" config-name="xml" schemaValidation="true"/>
+  <xml fileName="file://${sys:user.dir}/conf/sample.xml" config-name="xml" 
schemaValidation="true"/>
 </configuration>
\ No newline at end of file

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=768677&r1=768676&r2=768677&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
 Sun Apr 26 07:46:18 2009
@@ -749,6 +749,7 @@
             EntityResolver resolver = (EntityResolver) 
BeanHelper.createBean(decl, CatalogResolver.class);
             BeanHelper.setProperty(resolver, "fileSystem", getFileSystem());
             BeanHelper.setProperty(resolver, "baseDir", getBasePath());
+            BeanHelper.setProperty(resolver, "substitutor", getSubstitutor());
             setEntityResolver(resolver);
         }
     }

Modified: 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/VFSFileSystem.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/VFSFileSystem.java?rev=768677&r1=768676&r2=768677&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/VFSFileSystem.java
 (original)
+++ 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/VFSFileSystem.java
 Sun Apr 26 07:46:18 2009
@@ -285,8 +285,10 @@
 
     public URL locateFromURL(String basePath, String fileName)
     {
-        if ((basePath != null && UriParser.extractScheme(basePath) == null)
-            || (basePath == null && UriParser.extractScheme(fileName) == null))
+        String fileScheme = UriParser.extractScheme(fileName);
+
+        // Use DefaultFileSystem if basePath and fileName don't have a scheme.
+        if ((basePath == null || UriParser.extractScheme(basePath) == null) && 
fileScheme == null)
         {
             return super.locateFromURL(basePath, fileName);
         }
@@ -295,7 +297,8 @@
             FileSystemManager fsManager = VFS.getManager();
 
             FileObject file;
-            if (basePath != null)
+            // Only use the base path if the file name doesn't have a scheme.
+            if (basePath != null && fileScheme == null)
             {
                 FileObject base = fsManager.resolveFile(basePath);
                 if (base.getType() == FileType.FILE)

Modified: 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/resolver/CatalogResolver.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/resolver/CatalogResolver.java?rev=768677&r1=768676&r2=768677&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/resolver/CatalogResolver.java
 (original)
+++ 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/resolver/CatalogResolver.java
 Sun Apr 26 07:46:18 2009
@@ -26,6 +26,7 @@
 import org.apache.commons.configuration.FileSystem;
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.ConfigurationUtils;
+import org.apache.commons.lang.text.StrSubstitutor;
 
 import java.io.InputStream;
 import java.io.IOException;
@@ -122,6 +123,15 @@
     }
 
     /**
+     * Set the StrSubstitutor.
+     * @param substitutor The StrSubstitutor.
+     */
+    public void setSubstitutor(StrSubstitutor substitutor)
+    {
+        manager.setSubstitutor(substitutor);
+    }
+
+    /**
      * Enables debug logging of xml-commons Catalog processing.
      * @param debug True if debugging should be enabled, false otherwise.
      */
@@ -242,6 +252,9 @@
         /** The base directory */
         private String baseDir = System.getProperty("user.dir");
 
+        /** The String Substitutor */
+        private StrSubstitutor substitutor;
+
         /**
          * Set the FileSystem
          * @param fileSystem The FileSystem in use.
@@ -280,6 +293,16 @@
         {
             return this.baseDir;
         }
+
+        public void setSubstitutor(StrSubstitutor substitutor)
+        {
+            this.substitutor = substitutor;
+        }
+
+        public StrSubstitutor getStrSubstitutor()
+        {
+            return this.substitutor;
+        }
     }
 
     /**
@@ -425,5 +448,18 @@
                 parsePendingCatalogs();
             }
         }
+
+        /**
+         * Perform character normalization on a URI reference.
+         *
+         * @param uriref The URI reference
+         * @return The normalized URI reference.
+         */
+        protected String normalizeURI(String uriref)
+        {
+            StrSubstitutor substitutor = ((CatalogManager) 
catalogManager).getStrSubstitutor();
+            String resolved = substitutor != null ? 
substitutor.replace(uriref) : uriref;
+            return super.normalizeURI(resolved);
+        }
     }
 }

Modified: 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestVFSConfigurationBuilder.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestVFSConfigurationBuilder.java?rev=768677&r1=768676&r2=768677&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestVFSConfigurationBuilder.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestVFSConfigurationBuilder.java
 Sun Apr 26 07:46:18 2009
@@ -79,6 +79,9 @@
     private static final File VALIDATION_FILE = new File(
             "conf/testValidation.xml");
 
+    private static final File VALIDATION2_FILE = new File(
+            "conf/testValidation2.xml");
+
     private static final File MULTI_TENENT_FILE = new File(
             "conf/testMultiTenentConfigurationBuilder.xml");
 
@@ -855,6 +858,15 @@
         assertEquals("Incorrect value retrieved","value1",value);
     }
 
+    public void testValidation2() throws Exception
+    {
+        factory.setFile(VALIDATION2_FILE);
+        factory.getConfiguration(true);
+        String value = System.getProperty("key1");
+        assertNotNull("The test key was not located", value);
+        assertEquals("Incorrect value retrieved","value1",value);
+    }
+
     public void testMultiTenentConfiguration() throws Exception
     {
         factory.setFile(MULTI_TENENT_FILE);


Reply via email to