commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestConfigurationUtils.java
Date Thu, 23 Sep 2004 11:42:02 GMT
ebourg      2004/09/23 04:42:02

  Modified:    configuration/src/java/org/apache/commons/configuration
                        ConfigurationUtils.java
               configuration/src/test/org/apache/commons/configuration
                        TestConfigurationUtils.java
  Log:
  Added getFileName(URL)
  
  Revision  Changes    Path
  1.9       +31 -6     jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationUtils.java
  
  Index: ConfigurationUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/ConfigurationUtils.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ConfigurationUtils.java	23 Sep 2004 08:41:55 -0000	1.8
  +++ ConfigurationUtils.java	23 Sep 2004 11:42:00 -0000	1.9
  @@ -17,10 +17,10 @@
   package org.apache.commons.configuration;
   
   import java.io.File;
  +import java.io.IOException;
   import java.io.PrintStream;
   import java.io.PrintWriter;
   import java.io.StringWriter;
  -import java.io.IOException;
   import java.net.MalformedURLException;
   import java.net.URL;
   import java.util.Iterator;
  @@ -39,7 +39,7 @@
    */
   public final class ConfigurationUtils
   {
  -    private static Log log  = LogFactory.getLog(ConfigurationUtils.class);
  +    private static Log log = LogFactory.getLog(ConfigurationUtils.class);
   
       private ConfigurationUtils()
       {
  @@ -220,7 +220,7 @@
           {
               if (base == null)
               {
  -                url =  new URL(name);
  +                url = new URL(name);
               }
               else
               {
  @@ -241,10 +241,13 @@
               File file = new File(name);
               if (file.isAbsolute())     // already absolute?
               {
  -                try {
  +                try
  +                {
                       url = file.toURL();
                       log.debug("Configuration loaded from the absolute path " + name);
  -                } catch (MalformedURLException e) {
  +                }
  +                catch (MalformedURLException e)
  +                {
                       e.printStackTrace();
                   }
               }
  @@ -340,6 +343,28 @@
           else
           {
               return s.substring(0, s.lastIndexOf("/") + 1);
  +        }
  +    }
  +
  +    /**
  +     * Extract the file name from the specified URL.
  +     */
  +    static String getFileName(URL url)
  +    {
  +        if (url == null)
  +        {
  +            return null;
  +        }
  +
  +        String path = url.getPath();
  +
  +        if (path.endsWith("/") || StringUtils.isEmpty(path))
  +        {
  +            return null;
  +        }
  +        else
  +        {
  +            return path.substring(path.lastIndexOf("/") + 1);
           }
       }
   
  
  
  
  1.7       +11 -0     jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
  
  Index: TestConfigurationUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestConfigurationUtils.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestConfigurationUtils.java	23 Sep 2004 08:41:56 -0000	1.6
  +++ TestConfigurationUtils.java	23 Sep 2004 11:42:00 -0000	1.7
  @@ -112,4 +112,15 @@
           assertEquals("base path of " + url, "http://xyz.net", ConfigurationUtils.getBasePath(url));
       }
   
  +    public void testGetFileName() throws Exception
  +    {
  +        assertEquals("file name for a null URL", null, ConfigurationUtils.getFileName(null));
  +
  +        URL url = new URL("http://xyz.net/foo/");
  +        assertEquals("file for a directory URL " + url, null, ConfigurationUtils.getFileName(url));
  +
  +        url = new URL("http://xyz.net/foo/bar.xml");
  +        assertEquals("file name for a valid URL " + url, "bar.xml", ConfigurationUtils.getFileName(url));
  +    }
  +
   }
  
  
  

---------------------------------------------------------------------
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