commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r264868 - in /jakarta/commons/proper/io/trunk/src: java/org/apache/commons/io/FilenameUtils.java test/org/apache/commons/io/FilenameUtilsTestCase.java
Date Tue, 30 Aug 2005 21:23:50 GMT
Author: scolebourne
Date: Tue Aug 30 14:23:43 2005
New Revision: 264868

URL: http://svn.apache.org/viewcvs?rev=264868&view=rev
Log:
Fix to handle short prefixes

Modified:
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsTestCase.java

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java?rev=264868&r1=264867&r2=264868&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FilenameUtils.java Tue
Aug 30 14:23:43 2005
@@ -47,17 +47,20 @@
  * <p>
  * This class only supports Unix and Windows style names. Prefixes are matched as follows:
  * <pre>
- * Windows style:
+ * Windows:
  * a\b\c.txt           --> ""          --> relative
- * \a\b\c.txt          --> "\"         --> drive relative
+ * \a\b\c.txt          --> "\"         --> current drive absolute
+ * C:a\b\c.txt         --> "C:"        --> drive relative
  * C:\a\b\c.txt        --> "C:\"       --> absolute
  * \\server\a\b\c.txt  --> "\\server\" --> UNC
- * 
- * Unix style:
+ *
+ * Unix:
  * a/b/c.txt           --> ""          --> relative
  * /a/b/c.txt          --> "/"         --> absolute
- * ~/a/b/c.txt         --> "~/"        --> current user relative
- * ~user/a/b/c.txt     --> "~user/"    --> named user relative
+ * ~/a/b/c.txt         --> "~/"        --> current user absolute
+ * ~                   --> "~"         --> current user root (no path)
+ * ~user/a/b/c.txt     --> "~user/"    --> named user absolute
+ * ~user               --> "~user"     --> named user root (no path)
  * </pre>
  * Both prefix styles are matched always, irrespective of the machine that you are
  * currently running on.
@@ -354,11 +357,11 @@
      * Returns the length of the filename prefix, such as <code>C:/</code> or
<code>~/</code>.
      * <p>
      * This method will handle a file in either Unix or Windows format.
-     * The prefix includes the first slash in the full filename.
+     * The prefix includes the first slash in the full filename if present.
      * <pre>
      * Windows:
      * a\b\c.txt           --> ""          --> relative
-     * \a\b\c.txt          --> "\"         --> drive absolute
+     * \a\b\c.txt          --> "\"         --> current drive absolute
      * C:a\b\c.txt         --> "C:"        --> drive relative
      * C:\a\b\c.txt        --> "C:\"       --> absolute
      * \\server\a\b\c.txt  --> "\\server\" --> UNC
@@ -366,13 +369,15 @@
      * Unix:
      * a/b/c.txt           --> ""          --> relative
      * /a/b/c.txt          --> "/"         --> absolute
-     * ~/a/b/c.txt         --> "~/"        --> current user relative
-     * ~user/a/b/c.txt     --> "~user/"    --> named user relative
+     * ~/a/b/c.txt         --> "~/"        --> current user absolute
+     * ~                   --> "~"         --> current user root (no path)
+     * ~user/a/b/c.txt     --> "~user/"    --> named user absolute
+     * ~user               --> "~user"     --> named user root (no path)
      * </pre>
      * <p>
      * The output will be the same irrespective of the machine that the code is running on.
      * ie. both Unix and Windows prefixes are matched regardless.
-     * 
+     *
      * @param filename  the filename to find the prefix in, null returns -1
      * @return the length of the prefix, -1 if invalid or null
      */
@@ -390,7 +395,7 @@
         }
         if (len == 1) {
             if (ch0 == '~') {
-                return -1;
+                return 1;
             }
             return (isSeparator(ch0) ? 1 : 0);
         } else {
@@ -398,7 +403,7 @@
                 int posUnix = filename.indexOf(UNIX_SEPARATOR, 1);
                 int posWin = filename.indexOf(WINDOWS_SEPARATOR, 1);
                 if (posUnix == -1 && posWin == -1) {
-                    return -1;
+                    return len;
                 }
                 posUnix = (posUnix == -1 ? posWin : posUnix);
                 posWin = (posWin == -1 ? posUnix : posWin);
@@ -482,16 +487,18 @@
      * <pre>
      * Windows:
      * a\b\c.txt           --> ""          --> relative
-     * \a\b\c.txt          --> "\"         --> drive absolute
+     * \a\b\c.txt          --> "\"         --> current drive absolute
      * C:a\b\c.txt         --> "C:"        --> drive relative
      * C:\a\b\c.txt        --> "C:\"       --> absolute
      * \\server\a\b\c.txt  --> "\\server\" --> UNC
-     * 
+     *
      * Unix:
      * a/b/c.txt           --> ""          --> relative
      * /a/b/c.txt          --> "/"         --> absolute
-     * ~/a/b/c.txt         --> "~/"        --> current user relative
-     * ~user/a/b/c.txt     --> "~user/"    --> named user relative
+     * ~/a/b/c.txt         --> "~/"        --> current user absolute
+     * ~                   --> "~"         --> current user root (no path)
+     * ~user/a/b/c.txt     --> "~user/"    --> named user absolute
+     * ~user               --> "~user"     --> named user root (no path)
      * </pre>
      * <p>
      * The output will be the same irrespective of the machine that the code is running on.
@@ -538,6 +545,9 @@
         if (prefix < 0) {
             return null;
         }
+        if (prefix == filename.length()) {
+            return "";
+        }
         int index = indexOfLastSeparator(filename);
         if (index < 0) {
             return "";
@@ -572,6 +582,9 @@
         int prefix = getPrefixLength(filename); // validate the prefix
         if (prefix < 0) {
             return null;
+        }
+        if (prefix == filename.length()) {
+            return filename;
         }
         int index = indexOfLastSeparator(filename);
         if (index < 0) {

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsTestCase.java?rev=264868&r1=264867&r2=264868&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsTestCase.java
(original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FilenameUtilsTestCase.java
Tue Aug 30 14:23:43 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -90,8 +90,6 @@
         assertEquals(null, FilenameUtils.normalize("1:a"));
         assertEquals(null, FilenameUtils.normalize("\\\\\\a\\b\\c.txt"));
         assertEquals(null, FilenameUtils.normalize("\\\\a"));
-        assertEquals(null, FilenameUtils.normalize("~"));
-        assertEquals(null, FilenameUtils.normalize("~user"));
         
         assertEquals("a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("a\\b/c.txt"));
         assertEquals("" + SEP + "a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("\\a\\b/c.txt"));
@@ -152,7 +150,8 @@
         assertEquals("~" + SEP + "", FilenameUtils.normalize("~/."));
         assertEquals(null, FilenameUtils.normalize("~/../a"));
         assertEquals(null, FilenameUtils.normalize("~/.."));
-        assertEquals("~" + SEP + "", FilenameUtils.normalize("~/"));
+        assertEquals("~" + SEP, FilenameUtils.normalize("~/"));
+        assertEquals("~", FilenameUtils.normalize("~"));
         
         assertEquals("~user" + SEP + "a" + SEP + "c", FilenameUtils.normalize("~user/a/b/../c"));
         assertEquals("~user" + SEP + "c", FilenameUtils.normalize("~user/a/b/../../c"));
@@ -168,7 +167,8 @@
         assertEquals("~user" + SEP + "", FilenameUtils.normalize("~user/."));
         assertEquals(null, FilenameUtils.normalize("~user/../a"));
         assertEquals(null, FilenameUtils.normalize("~user/.."));
-        assertEquals("~user" + SEP + "", FilenameUtils.normalize("~user/"));
+        assertEquals("~user" + SEP, FilenameUtils.normalize("~user/"));
+        assertEquals("~user", FilenameUtils.normalize("~user"));
         
         assertEquals("C:" + SEP + "a" + SEP + "c", FilenameUtils.normalize("C:/a/b/../c"));
         assertEquals("C:" + SEP + "c", FilenameUtils.normalize("C:/a/b/../../c"));
@@ -186,6 +186,22 @@
         assertEquals(null, FilenameUtils.normalize("C:/.."));
         assertEquals("C:" + SEP + "", FilenameUtils.normalize("C:/"));
         
+        assertEquals("C:" + "a" + SEP + "c", FilenameUtils.normalize("C:a/b/../c"));
+        assertEquals("C:" + "c", FilenameUtils.normalize("C:a/b/../../c"));
+        assertEquals(null, FilenameUtils.normalize("C:a/b/../../../c"));
+        assertEquals("C:" + "a", FilenameUtils.normalize("C:a/b/.."));
+        assertEquals("C:" + "", FilenameUtils.normalize("C:a/b/../.."));
+        assertEquals(null, FilenameUtils.normalize("C:a/b/../../.."));
+        assertEquals("C:" + "a" + SEP + "d", FilenameUtils.normalize("C:a/b/../c/../d"));
+        assertEquals("C:" + "a" + SEP + "b" + SEP + "d", FilenameUtils.normalize("C:a/b//d"));
+        assertEquals("C:" + "a" + SEP + "b", FilenameUtils.normalize("C:a/b/././."));
+        assertEquals("C:" + "a", FilenameUtils.normalize("C:./a"));
+        assertEquals("C:" + "", FilenameUtils.normalize("C:./"));
+        assertEquals("C:" + "", FilenameUtils.normalize("C:."));
+        assertEquals(null, FilenameUtils.normalize("C:../a"));
+        assertEquals(null, FilenameUtils.normalize("C:.."));
+        assertEquals("C:" + "", FilenameUtils.normalize("C:"));
+        
         assertEquals(SEP + SEP + "server" + SEP + "a" + SEP + "c", FilenameUtils.normalize("//server/a/b/../c"));
         assertEquals(SEP + SEP + "server" + SEP + "c", FilenameUtils.normalize("//server/a/b/../../c"));
         assertEquals(null, FilenameUtils.normalize("//server/a/b/../../../c"));
@@ -280,8 +296,16 @@
         assertEquals(-1, FilenameUtils.getPrefixLength("1:a"));
         assertEquals(-1, FilenameUtils.getPrefixLength("\\\\\\a\\b\\c.txt"));
         assertEquals(-1, FilenameUtils.getPrefixLength("\\\\a"));
-        assertEquals(-1, FilenameUtils.getPrefixLength("~"));
-        assertEquals(-1, FilenameUtils.getPrefixLength("~user"));
+        
+        assertEquals(0, FilenameUtils.getPrefixLength(""));
+        assertEquals(1, FilenameUtils.getPrefixLength("\\"));
+        assertEquals(2, FilenameUtils.getPrefixLength("C:"));
+        assertEquals(3, FilenameUtils.getPrefixLength("C:\\"));
+        assertEquals(9, FilenameUtils.getPrefixLength("//server/"));
+        assertEquals(1, FilenameUtils.getPrefixLength("~"));
+        assertEquals(2, FilenameUtils.getPrefixLength("~/"));
+        assertEquals(5, FilenameUtils.getPrefixLength("~user"));
+        assertEquals(6, FilenameUtils.getPrefixLength("~user/"));
         
         assertEquals(0, FilenameUtils.getPrefixLength("a\\b\\c.txt"));
         assertEquals(1, FilenameUtils.getPrefixLength("\\a\\b\\c.txt"));
@@ -329,8 +353,16 @@
         assertEquals(null, FilenameUtils.getPrefix("1:a"));
         assertEquals(null, FilenameUtils.getPrefix("\\\\\\a\\b\\c.txt"));
         assertEquals(null, FilenameUtils.getPrefix("\\\\a"));
-        assertEquals(null, FilenameUtils.getPrefix("~"));
-        assertEquals(null, FilenameUtils.getPrefix("~user"));
+        
+        assertEquals("", FilenameUtils.getPrefix(""));
+        assertEquals("\\", FilenameUtils.getPrefix("\\"));
+        assertEquals("C:", FilenameUtils.getPrefix("C:"));
+        assertEquals("C:\\", FilenameUtils.getPrefix("C:\\"));
+        assertEquals("//server/", FilenameUtils.getPrefix("//server/"));
+        assertEquals("~", FilenameUtils.getPrefix("~"));
+        assertEquals("~/", FilenameUtils.getPrefix("~/"));
+        assertEquals("~user", FilenameUtils.getPrefix("~user"));
+        assertEquals("~user/", FilenameUtils.getPrefix("~user/"));
         
         assertEquals("", FilenameUtils.getPrefix("a\\b\\c.txt"));
         assertEquals("\\", FilenameUtils.getPrefix("\\a\\b\\c.txt"));
@@ -364,8 +396,15 @@
         assertEquals(null, FilenameUtils.getPath("1:a"));
         assertEquals(null, FilenameUtils.getPath("///a/b/c.txt"));
         assertEquals(null, FilenameUtils.getPath("//a"));
-        assertEquals(null, FilenameUtils.getPath("~"));
-        assertEquals(null, FilenameUtils.getPath("~user"));
+        
+        assertEquals("", FilenameUtils.getPath(""));
+        assertEquals("", FilenameUtils.getPath("C:"));
+        assertEquals("", FilenameUtils.getPath("C:/"));
+        assertEquals("", FilenameUtils.getPath("//server/"));
+        assertEquals("", FilenameUtils.getPath("~"));
+        assertEquals("", FilenameUtils.getPath("~/"));
+        assertEquals("", FilenameUtils.getPath("~user"));
+        assertEquals("", FilenameUtils.getPath("~user/"));
         
         assertEquals("a/b", FilenameUtils.getPath("a/b/c.txt"));
         assertEquals("a/b", FilenameUtils.getPath("/a/b/c.txt"));
@@ -389,8 +428,15 @@
         assertEquals(null, FilenameUtils.getFullPath("1:a"));
         assertEquals(null, FilenameUtils.getFullPath("///a/b/c.txt"));
         assertEquals(null, FilenameUtils.getFullPath("//a"));
-        assertEquals(null, FilenameUtils.getFullPath("~"));
-        assertEquals(null, FilenameUtils.getFullPath("~user"));
+        
+        assertEquals("", FilenameUtils.getFullPath(""));
+        assertEquals("C:", FilenameUtils.getFullPath("C:"));
+        assertEquals("C:/", FilenameUtils.getFullPath("C:/"));
+        assertEquals("//server/", FilenameUtils.getFullPath("//server/"));
+        assertEquals("~", FilenameUtils.getFullPath("~"));
+        assertEquals("~/", FilenameUtils.getFullPath("~/"));
+        assertEquals("~user", FilenameUtils.getFullPath("~user"));
+        assertEquals("~user/", FilenameUtils.getFullPath("~user/"));
         
         assertEquals("a/b", FilenameUtils.getFullPath("a/b/c.txt"));
         assertEquals("/a/b", FilenameUtils.getFullPath("/a/b/c.txt"));



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