This patch is against commons-collections 2.1.  It enhances
ExtendedProperties in 2 ways.  The first is that it tries to process
includes as URLs first, then falls back to the preexisting file
processing.  We use this to refer to classpath resources instead of
files, which is how you normally need to work in the J2EE world.

The second is that any properties that you explicitly set in your
properties file overrides duplicate properties in included files.  This
behaves more like XSLT, and is generally more useful than the previous
behavior.
Index: src/java/org/apache/commons/collections/ExtendedProperties.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/collections/ExtendedProperties.java,v
retrieving revision 1.7
diff -u -r1.7 ExtendedProperties.java
--- src/java/org/apache/commons/collections/ExtendedProperties.java	12 Jun 2002 03:59:15 -0000	1.7
+++ src/java/org/apache/commons/collections/ExtendedProperties.java	21 Oct 2003 19:38:05 -0000
@@ -1,5 +1,5 @@
 /*
- * $Header: /home/cvs/patches/commons-collections/2.1/includeEnhancements.txt,v 1.1 2003/10/21 19:39:30 tmccune Exp $
+ * $Header: /home/cvs/patches/commons-collections/2.1/includeEnhancements.txt,v 1.1 2003/10/21 19:39:30 tmccune Exp $
  * $Revision: 1.1 $
  * $Date: 2003/10/21 19:39:30 $
  *
@@ -61,17 +61,19 @@
 
 package org.apache.commons.collections;
 
-import java.io.IOException;
+import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.IOException;
 import java.io.LineNumberReader;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
-
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.Hashtable;
@@ -478,6 +480,7 @@
     public synchronized void load(InputStream input, String enc)
         throws IOException
     {
+        ExtendedProperties includedProps = new ExtendedProperties();
         PropertiesReader reader = null;
         if (enc != null)
         {
@@ -520,39 +523,45 @@
                     if (getInclude() != null && 
                         key.equalsIgnoreCase(getInclude()))
                     {
-                        /*
-                         * Recursively load properties files.
-                         */
-                        File file = null;
-                        
-                        if (value.startsWith(fileSeparator))
-                        {
+                        ExtendedProperties nextInclude = new ExtendedProperties();
+                        try {
+                            nextInclude.load(new BufferedInputStream(new URL(value).openStream()));
+                        } catch (MalformedURLException e) {
                             /*
-                             * We have an absolute path so we'll
-                             * use this.
+                             * Recursively load properties files.
                              */
-                            file = new File(value);
-                        }
-                        else
-                        {   
-                            /* 
-                             * We have a relative path, and we have
-                             * two possible forms here. If we have the
-                             * "./" form then just strip that off first
-                             * before continuing.
-                             */
-                            if (value.startsWith("." + fileSeparator))
+                            File file = null;
+                            
+                            if (value.startsWith(fileSeparator))
                             {
-                                value = value.substring(2);
+                                /*
+                                 * We have an absolute path so we'll
+                                 * use this.
+                                 */
+                                file = new File(value);
+                            }
+                            else
+                            {   
+                                /* 
+                                 * We have a relative path, and we have
+                                 * two possible forms here. If we have the
+                                 * "./" form then just strip that off first
+                                 * before continuing.
+                                 */
+                                if (value.startsWith("." + fileSeparator))
+                                {
+                                    value = value.substring(2);
+                                }
+                                
+                                file = new File(basePath + value);
                             }
                             
-                            file = new File(basePath + value);
-                        }
-                        
-                        if (file != null && file.exists() && file.canRead())
-                        {
-                            load ( new FileInputStream(file));
+                            if (file != null && file.exists() && file.canRead())
+                            {
+                                nextInclude.load ( new FileInputStream(file));
+                            }
                         }
+                        includedProps.include(nextInclude);
                     }
                     else
                     {
@@ -567,6 +576,22 @@
              * Should happen only when EOF is reached.
              */
             return;
+        } finally {
+          include(includedProps);
+        }
+    }
+    
+    /**
+     * Include another properties set in this one.
+     * Properties in this set override properties in the included set.
+     */
+    private void include(ExtendedProperties props) {
+        Iterator iter = props.getKeys();
+        while (iter.hasNext()) {
+           String nextKey = (String) iter.next();
+           if (!containsKey(nextKey)) {
+               addProperty(nextKey, props.getProperty(nextKey));
+           }
         }
     }
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to