Author: oheger
Date: Sat Sep 28 19:30:08 2013
New Revision: 1527226

URL: http://svn.apache.org/r1527226
Log:
Added a constant for the default file location strategy to FileLocatorUtils.

This strategy will be used if no specific strategy is provided in a
FileLocator. The default location strategy is a combined strategy which
simulates the algortihm used in Commons Configuration 1.x.

Modified:
    
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
    
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java

Modified: 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java?rev=1527226&r1=1527225&r2=1527226&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
 (original)
+++ 
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileLocatorUtils.java
 Sat Sep 28 19:30:08 2013
@@ -20,6 +20,7 @@ import java.io.File;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URL;
+import java.util.Arrays;
 
 import org.apache.commons.configuration.ConfigurationUtils;
 import org.apache.commons.lang3.ObjectUtils;
@@ -52,6 +53,39 @@ public final class FileLocatorUtils
     public static final FileSystem DEFAULT_FILE_SYSTEM =
             new DefaultFileSystem();
 
+    /**
+     * Constant for the default {@code FileLocationStrategy}. This strategy is
+     * used by the {@code locate()} method if the passed in {@code FileLocator}
+     * does not define its own location strategy. The default location strategy
+     * is roughly equivalent to the search algorithm used in version 1.x of
+     * <em>Commons Configuration</em> (there it was hard-coded though). It
+     * behaves in the following way when passed a {@code FileLocator}:
+     * <ul>
+     * <li>If the {@code FileLocator} has a defined URL, this URL is used as 
the
+     * file's URL (without any further checks).</li>
+     * <li>Otherwise, base path and file name stored in the {@code FileLocator}
+     * are passed to the current {@code FileSystem}'s {@code locateFromURL()}
+     * method. If this results in a URL, it is returned.</li>
+     * <li>Otherwise, if the locator's file name is an absolute path to an
+     * existing file, the URL of this file is returned.</li>
+     * <li>Otherwise, the concatenation of base path and file name is
+     * constructed. If this path points to an existing file, its URL is
+     * returned.</li>
+     * <li>Otherwise, a sub directory of the current user's home directory as
+     * defined by the base path is searched for the referenced file. If the 
file
+     * can be found there, its URL is returned.</li>
+     * <li>Otherwise, the base path is ignored, and the file name is searched 
in
+     * the current user's home directory. If the file can be found there, its
+     * URL is returned.</li>
+     * <li>Otherwise, a resource with the name of the locator's file name is
+     * searched in the classpath. If it can be found, its URL is returned.</li>
+     * <li>Otherwise, the strategy gives up and returns <b>null</b> indicating
+     * that the file cannot be resolved.</li>
+     * </ul>
+     */
+    public static final FileLocationStrategy DEFAULT_LOCATION_STRATEGY =
+            initDefaultLocationStrategy();
+
     /** Constant for the file URL protocol */
     private static final String FILE_SCHEME = "file:";
 
@@ -629,4 +663,26 @@ public final class FileLocatorUtils
         return fileLocator(src).sourceURL(url).fileName(getFileName(url))
                 .basePath(getBasePath(url)).create();
     }
+
+    /**
+     * Creates the default location strategy. This method creates a combined
+     * location strategy as described in the comment of the
+     * {@link #DEFAULT_LOCATION_STRATEGY} member field.
+     *
+     * @return the default {@code FileLocationStrategy}
+     */
+    private static FileLocationStrategy initDefaultLocationStrategy()
+    {
+        FileLocationStrategy[] subStrategies =
+                new FileLocationStrategy[] {
+                        new ProvidedURLLocationStrategy(),
+                        new FileSystemLocationStrategy(),
+                        new AbsoluteNameLocationStrategy(),
+                        new BasePathLocationStrategy(),
+                        new HomeDirectoryLocationStrategy(true),
+                        new HomeDirectoryLocationStrategy(false),
+                        new ClasspathLocationStrategy()
+                };
+        return new CombinedLocationStrategy(Arrays.asList(subStrategies));
+    }
 }

Modified: 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java?rev=1527226&r1=1527225&r2=1527226&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileLocatorUtils.java
 Sat Sep 28 19:30:08 2013
@@ -28,6 +28,7 @@ import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.util.Iterator;
 
 import org.apache.commons.configuration.ConfigurationAssert;
 import org.apache.commons.configuration.ConfigurationException;
@@ -402,4 +403,36 @@ public class TestFileLocatorUtils
         handler.setURL(FileLocatorUtils.convertFileToURL(file));
         checkTestConfiguration(handler);
     }
+
+    /**
+     * Tests the definition of the default location strategy.
+     */
+    @Test
+    public void testDefaultFileLocationStrategy()
+    {
+        CombinedLocationStrategy strategy =
+                (CombinedLocationStrategy) 
FileLocatorUtils.DEFAULT_LOCATION_STRATEGY;
+        Iterator<FileLocationStrategy> it =
+                strategy.getSubStrategies().iterator();
+        assertTrue("Wrong strategy (1)",
+                it.next() instanceof ProvidedURLLocationStrategy);
+        assertTrue("Wrong strategy (2)",
+                it.next() instanceof FileSystemLocationStrategy);
+        assertTrue("Wrong strategy (3)",
+                it.next() instanceof AbsoluteNameLocationStrategy);
+        assertTrue("Wrong strategy (4)",
+                it.next() instanceof BasePathLocationStrategy);
+        FileLocationStrategy sub = it.next();
+        assertTrue("Wrong strategy (5)",
+                sub instanceof HomeDirectoryLocationStrategy);
+        assertTrue("Base path ignored",
+                ((HomeDirectoryLocationStrategy) sub).isEvaluateBasePath());
+        sub = it.next();
+        assertTrue("Wrong strategy (6)",
+                sub instanceof HomeDirectoryLocationStrategy);
+        assertFalse("Base path not ignored",
+                ((HomeDirectoryLocationStrategy) sub).isEvaluateBasePath());
+        assertTrue("Wrong strategy (7)",
+                it.next() instanceof ClasspathLocationStrategy);
+    }
 }


Reply via email to