commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/io/src/java/org/apache/commons/io FilenameUtils.java
Date Sat, 30 Oct 2004 22:16:23 GMT
scolebourne    2004/10/30 15:16:23

  Modified:    io/src/test/org/apache/commons/io FilenameUtilsTestCase.java
               io/src/java/org/apache/commons/io FilenameUtils.java
  Log:
  Add separatorsToXxx methods to FilenameUtils
  
  Revision  Changes    Path
  1.13      +38 -1     jakarta-commons/io/src/test/org/apache/commons/io/FilenameUtilsTestCase.java
  
  Index: FilenameUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/FilenameUtilsTestCase.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- FilenameUtilsTestCase.java	24 Oct 2004 18:44:51 -0000	1.12
  +++ FilenameUtilsTestCase.java	30 Oct 2004 22:16:23 -0000	1.13
  @@ -274,4 +274,41 @@
           }
       }
   
  +    //-----------------------------------------------------------------------
  +    public void testSeparatorsToUnix() {
  +        assertEquals(null, FilenameUtils.separatorsToUnix(null));
  +        assertEquals("/a/b/c", FilenameUtils.separatorsToUnix("/a/b/c"));
  +        assertEquals("/a/b/c.txt", FilenameUtils.separatorsToUnix("/a/b/c.txt"));
  +        assertEquals("/a/b/c", FilenameUtils.separatorsToUnix("/a/b\\c"));
  +        assertEquals("/a/b/c", FilenameUtils.separatorsToUnix("\\a\\b\\c"));
  +        assertEquals("D:/a/b/c", FilenameUtils.separatorsToUnix("D:\\a\\b\\c"));
  +    }
  +
  +    public void testSeparatorsToWindows() {
  +        assertEquals(null, FilenameUtils.separatorsToWindows(null));
  +        assertEquals("\\a\\b\\c", FilenameUtils.separatorsToWindows("\\a\\b\\c"));
  +        assertEquals("\\a\\b\\c.txt", FilenameUtils.separatorsToWindows("\\a\\b\\c.txt"));
  +        assertEquals("\\a\\b\\c", FilenameUtils.separatorsToWindows("\\a\\b/c"));
  +        assertEquals("\\a\\b\\c", FilenameUtils.separatorsToWindows("/a/b/c"));
  +        assertEquals("D:\\a\\b\\c", FilenameUtils.separatorsToWindows("D:/a/b/c"));
  +    }
  +
  +    public void testSeparatorsToSystem() {
  +        if (File.separatorChar == '/') {
  +            assertEquals(null, FilenameUtils.separatorsToSystem(null));
  +            assertEquals("/a/b/c", FilenameUtils.separatorsToSystem("/a/b/c"));
  +            assertEquals("/a/b/c.txt", FilenameUtils.separatorsToSystem("/a/b/c.txt"));
  +            assertEquals("/a/b/c", FilenameUtils.separatorsToSystem("/a/b\\c"));
  +            assertEquals("/a/b/c", FilenameUtils.separatorsToSystem("\\a\\b\\c"));
  +            assertEquals("D:/a/b/c", FilenameUtils.separatorsToSystem("D:\\a\\b\\c"));
  +        } else {
  +            assertEquals(null, FilenameUtils.separatorsToSystem(null));
  +            assertEquals("\\a\\b\\c", FilenameUtils.separatorsToSystem("\\a\\b\\c"));
  +            assertEquals("\\a\\b\\c.txt", FilenameUtils.separatorsToSystem("\\a\\b\\c.txt"));
  +            assertEquals("\\a\\b\\c", FilenameUtils.separatorsToSystem("\\a\\b/c"));
  +            assertEquals("\\a\\b\\c", FilenameUtils.separatorsToSystem("/a/b/c"));
  +            assertEquals("D:\\a\\b\\c", FilenameUtils.separatorsToSystem("D:/a/b/c"));
  +        }
  +    }
  +
   }
  
  
  
  1.19      +65 -1     jakarta-commons/io/src/java/org/apache/commons/io/FilenameUtils.java
  
  Index: FilenameUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/FilenameUtils.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- FilenameUtils.java	29 Oct 2004 21:38:49 -0000	1.18
  +++ FilenameUtils.java	30 Oct 2004 22:16:23 -0000	1.19
  @@ -55,6 +55,7 @@
    * @author Matthew Hawthorne
    * @author Martin Cooper
    * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
  + * @author Stephen Colebourne
    * @version $Id$
    * @since Commons IO 1.1
    */
  @@ -66,6 +67,21 @@
       private static final char INTERNAL_SEPARATOR_CHAR = '/';
   
       /**
  +     * The Unix separator character.
  +     */
  +    private static final char UNIX_SEPARATOR = '/';
  +
  +    /**
  +     * The Windows separator character.
  +     */
  +    private static final char WINDOWS_SEPARATOR = '\\';
  +
  +    /**
  +     * The system separator character.
  +     */
  +    private static final char SYSTEM_SEPARATOR = File.separatorChar;
  +
  +    /**
        * Standard separator string used when internalizing paths.
        */
       // KILL? It's here to match symmetry of File.separator and to promote its use
  @@ -472,6 +488,54 @@
           if (INTERNAL_SEPARATOR_CHAR != File.separatorChar) {
               path = path.replace(INTERNAL_SEPARATOR_CHAR, File.separatorChar);
           }
  +        return path;
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Convert all separators to the Unix separator of forward slash.
  +     * 
  +     * @param path  the path to be changed, null ignored
  +     * @return the updated path
  +     */
  +    public static String separatorsToUnix(String path) {
  +        if (path == null || path.indexOf(WINDOWS_SEPARATOR) == -1) {
  +            return path;
  +        }
  +        return path.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR);
  +    }
  +
  +    /**
  +     * Convert all separators to the Windows separator of backslash.
  +     * 
  +     * @param path  the path to be changed, null ignored
  +     * @return the updated path
  +     */
  +    public static String separatorsToWindows(String path) {
  +        if (path == null || path.indexOf(UNIX_SEPARATOR) == -1) {
  +            return path;
  +        }
  +        return path.replace(UNIX_SEPARATOR, WINDOWS_SEPARATOR);
  +    }
  +
  +    /**
  +     * Convert all separators to the system separator.
  +     * 
  +     * @param path  the path to be changed, null ignored
  +     * @return the updated path
  +     */
  +    public static String separatorsToSystem(String path) {
  +        if (path == null) {
  +            return null;
  +        }
  +        if (SYSTEM_SEPARATOR == UNIX_SEPARATOR) {
  +            return separatorsToUnix(path);
  +        }
  +        if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) {
  +            return separatorsToWindows(path);
  +        }
  +        path = path.replace(UNIX_SEPARATOR, SYSTEM_SEPARATOR);
  +        path = path.replace(WINDOWS_SEPARATOR, SYSTEM_SEPARATOR);
           return path;
       }
   
  
  
  

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