scolebourne 2004/10/30 15:41:57 Modified: io/src/test/org/apache/commons/io FilenameUtilsTestCase.java io/src/java/org/apache/commons/io FilenameUtils.java Log: Add indexOfXxx methods to FilenameUtils Revision Changes Path 1.14 +20 -7 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.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- FilenameUtilsTestCase.java 30 Oct 2004 22:16:23 -0000 1.13 +++ FilenameUtilsTestCase.java 30 Oct 2004 22:41:56 -0000 1.14 @@ -114,12 +114,6 @@ assertEquals("C:\\a" + File.separator + "d", FilenameUtils.catPath("C:\\a\\b\\c", "../d")); } - public void testIndexOfLastPathSeparator() { - assertEquals(-1, FilenameUtils.indexOfLastPathSeparator("noseperator.inthispath")); - assertEquals(3, FilenameUtils.indexOfLastPathSeparator("a/b/c")); - assertEquals(3, FilenameUtils.indexOfLastPathSeparator("a\\b\\c")); - } - // resolveFile public void testResolveFileDotDot() throws Exception { @@ -309,6 +303,25 @@ assertEquals("\\a\\b\\c", FilenameUtils.separatorsToSystem("/a/b/c")); assertEquals("D:\\a\\b\\c", FilenameUtils.separatorsToSystem("D:/a/b/c")); } + } + + //----------------------------------------------------------------------- + public void testIndexOfLastSeparator() { + assertEquals(-1, FilenameUtils.indexOfLastSeparator(null)); + assertEquals(-1, FilenameUtils.indexOfLastSeparator("noseperator.inthispath")); + assertEquals(3, FilenameUtils.indexOfLastSeparator("a/b/c")); + assertEquals(3, FilenameUtils.indexOfLastSeparator("a\\b\\c")); + } + + public void testIndexOfExtension() { + assertEquals(-1, FilenameUtils.indexOfExtension(null)); + assertEquals(-1, FilenameUtils.indexOfExtension("file")); + assertEquals(4, FilenameUtils.indexOfExtension("file.txt")); + assertEquals(13, FilenameUtils.indexOfExtension("a.txt/b.txt/c.txt")); + assertEquals(-1, FilenameUtils.indexOfExtension("a/b/c")); + assertEquals(-1, FilenameUtils.indexOfExtension("a\\b\\c")); + assertEquals(-1, FilenameUtils.indexOfExtension("a/b.notextension/c")); + assertEquals(-1, FilenameUtils.indexOfExtension("a\\b.notextension\\c")); } } 1.20 +63 -31 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.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- FilenameUtils.java 30 Oct 2004 22:16:23 -0000 1.19 +++ FilenameUtils.java 30 Oct 2004 22:41:57 -0000 1.20 @@ -19,9 +19,16 @@ import java.io.IOException; /** - * Common [EMAIL PROTECTED] java.io.File} manipulation routines through - * use of a filename/path. - * + * Utility class that provides methods to manipulate filenames and filepaths. + * <p> + * This class defines three basic components within a filename (example C:\dev\file.txt): + * <ul> + * <li>the path - C:\dev + * <li>the name - file.txt + * <li>the extension - txt + * </ul> + * The class only supports Unix and Windows style names. + * * <h3>Path-related methods</h3> * * <p>Methods exist to retrieve the components of a typical file path. For @@ -67,6 +74,11 @@ private static final char INTERNAL_SEPARATOR_CHAR = '/'; /** + * The extension separator character. + */ + private static final char EXTENSION_SEPARATOR = '.'; + + /** * The Unix separator character. */ private static final char UNIX_SEPARATOR = '/'; @@ -356,14 +368,14 @@ // TODO UNIX/Windows only. Is this a problem? public static String catPath(String lookupPath, String path) { // Cut off the last slash and everything beyond - int index = indexOfLastPathSeparator(lookupPath); + int index = indexOfLastSeparator(lookupPath); String lookup = lookupPath.substring(0, index); String pth = path; // Deal with .. by chopping dirs off the lookup path while (pth.startsWith("../") || pth.startsWith("..\\")) { if (lookup.length() > 0) { - index = indexOfLastPathSeparator(lookup); + index = indexOfLastSeparator(lookup); lookup = lookup.substring(0, index); } else { // More ..'s than dirs, return null @@ -378,22 +390,6 @@ } /** - * Return the index of the last 'path separator' character. The 'path - * separator' character is '/' for UNIX systems and '\' for Microsoft - * Windows systems. - * - * @param path The path to find the last path separator in - * @return The index of the last 'path separator' character, or -1 if there - * is no such character. - */ - // KILL: Inline into above method - public static int indexOfLastPathSeparator(String path) { - int lastUnixPos = path.lastIndexOf('/'); - int lastWindowsPos = path.lastIndexOf('\\'); - return Math.max(lastUnixPos, lastWindowsPos); - } - - /** * Resolve a file <code>filename</code> to it's canonical form. If * <code>filename</code> is relative (doesn't start with <code>/</code>), * it will be resolved relative to <code>baseFile</code>, otherwise it is @@ -493,7 +489,7 @@ //----------------------------------------------------------------------- /** - * Convert all separators to the Unix separator of forward slash. + * Converts all separators to the Unix separator of forward slash. * * @param path the path to be changed, null ignored * @return the updated path @@ -506,7 +502,7 @@ } /** - * Convert all separators to the Windows separator of backslash. + * Converts all separators to the Windows separator of backslash. * * @param path the path to be changed, null ignored * @return the updated path @@ -519,7 +515,7 @@ } /** - * Convert all separators to the system separator. + * Converts all separators to the system separator. * * @param path the path to be changed, null ignored * @return the updated path @@ -528,15 +524,51 @@ if (path == null) { return null; } - if (SYSTEM_SEPARATOR == UNIX_SEPARATOR) { - return separatorsToUnix(path); - } if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) { return separatorsToWindows(path); + } else { + return separatorsToUnix(path); } - path = path.replace(UNIX_SEPARATOR, SYSTEM_SEPARATOR); - path = path.replace(WINDOWS_SEPARATOR, SYSTEM_SEPARATOR); - return path; + } + + //----------------------------------------------------------------------- + /** + * Returns the index of the last directory separator character. + * <p> + * This method will handle a file in either Unix or Windows format. + * The position of the last forward or backslash is returned. + * + * @param path the path to find the last path separator in + * @return the index of the last separator character, or -1 if there + * is no such character. + */ + public static int indexOfLastSeparator(String path) { + if (path == null) { + return -1; + } + int lastUnixPos = path.lastIndexOf(UNIX_SEPARATOR); + int lastWindowsPos = path.lastIndexOf(WINDOWS_SEPARATOR); + return Math.max(lastUnixPos, lastWindowsPos); + } + + /** + * Returns the index of the last extension separator character, which is a dot. + * <p> + * This method also checks that there is no directory separator after the last dot. + * To do this it uses [EMAIL PROTECTED] #indexOfLastSeparator(String)} which will + * handle a file in either Unix or Windows format. + * + * @param path the path to find the last path separator in + * @return the index of the last separator character, or -1 if there + * is no such character. + */ + public static int indexOfExtension(String path) { + if (path == null) { + return -1; + } + int extensionPos = path.lastIndexOf(EXTENSION_SEPARATOR); + int lastSeparator = indexOfLastSeparator(path); + return (lastSeparator > extensionPos ? -1 : extensionPos); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]