Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 88455 invoked from network); 23 Sep 2004 11:42:09 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 23 Sep 2004 11:42:09 -0000 Received: (qmail 84613 invoked by uid 500); 23 Sep 2004 11:42:05 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 84556 invoked by uid 500); 23 Sep 2004 11:42:04 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 84542 invoked by uid 500); 23 Sep 2004 11:42:04 -0000 Received: (qmail 84537 invoked by uid 99); 23 Sep 2004 11:42:04 -0000 X-ASF-Spam-Status: No, hits=-10.0 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.28) with SMTP; Thu, 23 Sep 2004 04:42:03 -0700 Received: (qmail 88394 invoked by uid 1865); 23 Sep 2004 11:42:02 -0000 Date: 23 Sep 2004 11:42:02 -0000 Message-ID: <20040923114202.88393.qmail@minotaur.apache.org> From: ebourg@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestConfigurationUtils.java X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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