scolebourne 2004/10/30 16:23:54
Modified: io/src/test/org/apache/commons/io FilenameUtilsTestCase.java
io/src/java/org/apache/commons/io FilenameUtils.java
Log:
Update removeExtension method and remove unused code from FilenameUtils
Revision Changes Path
1.17 +18 -39
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.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- FilenameUtilsTestCase.java 30 Oct 2004 23:12:18 -0000 1.16
+++ FilenameUtilsTestCase.java 30 Oct 2004 23:23:53 -0000 1.17
@@ -171,43 +171,6 @@
return sb.toString();
}
- public void testRemoveExtension() {
- String[][] tests = {
- { "filename.ext", "filename" },
- { "first.second.third.ext", "first.second.third" },
- { "README", "README" },
- { "domain.dot.com", "domain.dot" },
- { "image.jpeg", "image" },
- { "a.b/c", "a.b/c" },
- { "a.b/c.txt", "a.b/c" },
- { "a/b/c", "a/b/c" },
- };
-
- for (int i = 0; i < tests.length; i++) {
- assertEquals(tests[i][1], FilenameUtils.removeExtension(tests[i][0]));
- //assertEquals(tests[i][1], FilenameUtils.basename(tests[i][0]));
- }
- }
-
- public void testRemoveExtensionWithPaths() {
- String[][] testsWithPaths =
- { { "/tmp/foo/filename.ext", "/tmp/foo/filename" }, {
- "C:\\temp\\foo\\filename.ext", "C:\\temp\\foo\\filename" }, {
- "/tmp/foo.bar/filename.ext", "/tmp/foo.bar/filename" }, {
- "C:\\temp\\foo.bar\\filename.ext", "C:\\temp\\foo.bar\\filename" },
{
- "/tmp/foo.bar/README", "/tmp/foo.bar/README" }, {
- "C:\\temp\\foo.bar\\README", "C:\\temp\\foo.bar\\README" }, {
- "../filename.ext", "../filename" }
- };
-
- for (int i = 0; i < testsWithPaths.length; i++) {
- assertEquals(
- testsWithPaths[i][1],
- FilenameUtils.removeExtension(testsWithPaths[i][0]));
- //assertEquals(testsWithPaths[i][1],
FilenameUtils.basename(testsWithPaths[i][0]));
- }
- }
-
//-----------------------------------------------------------------------
public void testSeparatorsToUnix() {
assertEquals(null, FilenameUtils.separatorsToUnix(null));
@@ -274,7 +237,7 @@
assertEquals("a\\b", FilenameUtils.getPath("a\\b\\c"));
}
- public void testRemovePath() {
+ public void testGetName() {
assertEquals(null, FilenameUtils.getName(null));
assertEquals("noseperator.inthispath",
FilenameUtils.getName("noseperator.inthispath"));
assertEquals("c.txt", FilenameUtils.getName("a/b/c.txt"));
@@ -297,6 +260,22 @@
assertEquals("", FilenameUtils.getExtension("a\\b\\c"));
assertEquals("", FilenameUtils.getExtension("C:\\temp\\foo.bar\\README"));
assertEquals("ext", FilenameUtils.getExtension("../filename.ext"));
+ }
+
+ public void testRemoveExtension() {
+ assertEquals(null, FilenameUtils.removeExtension(null));
+ assertEquals("file", FilenameUtils.removeExtension("file.ext"));
+ assertEquals("README", FilenameUtils.removeExtension("README"));
+ assertEquals("domain.dot", FilenameUtils.removeExtension("domain.dot.com"));
+ assertEquals("image", FilenameUtils.removeExtension("image.jpeg"));
+ assertEquals("a.b/c", FilenameUtils.removeExtension("a.b/c"));
+ assertEquals("a.b/c", FilenameUtils.removeExtension("a.b/c.txt"));
+ assertEquals("a/b/c", FilenameUtils.removeExtension("a/b/c"));
+ assertEquals("a.b\\c", FilenameUtils.removeExtension("a.b\\c"));
+ assertEquals("a.b\\c", FilenameUtils.removeExtension("a.b\\c.txt"));
+ assertEquals("a\\b\\c", FilenameUtils.removeExtension("a\\b\\c"));
+ assertEquals("C:\\temp\\foo.bar\\README",
FilenameUtils.removeExtension("C:\\temp\\foo.bar\\README"));
+ assertEquals("../filename",
FilenameUtils.removeExtension("../filename.ext"));
}
}
1.23 +33 -64
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.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- FilenameUtils.java 30 Oct 2004 23:12:18 -0000 1.22
+++ FilenameUtils.java 30 Oct 2004 23:23:54 -0000 1.23
@@ -69,11 +69,6 @@
public class FilenameUtils {
/**
- * Standard separator char used when internalizing paths.
- */
- private static final char INTERNAL_SEPARATOR_CHAR = '/';
-
- /**
* The extension separator character.
*/
private static final char EXTENSION_SEPARATOR = '.';
@@ -94,40 +89,11 @@
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
- private static final String INTERNAL_SEPARATOR = "/";
-
- /**
* Instances should NOT be constructed in standard programming.
*/
public FilenameUtils() { }
- /**
- * Remove extension from filename.
- * ie
- * <pre>
- * foo.txt --> foo
- * a\b\c.jpg --> a\b\c
- * a\b\c --> a\b\c
- * a.b\c --> a.b\c
- * </pre>
- *
- * @param filename the filename
- * @return the filename minus extension
- */
- public static String removeExtension(String filename) {
- String ext = getExtension(filename);
- int index = ext.length();
- if (index > 0) {
- // include the . in the count
- index++;
- }
- index = filename.length() - index;
- return filename.substring(0, index);
- }
-
+ //-----------------------------------------------------------------------
/**
* Normalize a path.
* Eliminates "/../" and "/./" in a string. Returns <code>null</code> if
@@ -309,31 +275,6 @@
return file;
}
- /**
- * Convert all separators to the internal form. This allows manipulation
- * of paths without concern for which separators are used within them.
- * @param path The path to be internalized.
- * @return The internalized path.
- */
- // KILL: Inline into the one place this is used?
- private static String internalize(String path) {
- return path.replace('\\', INTERNAL_SEPARATOR_CHAR);
- }
-
- /**
- * Convert all separators to their external form. That is, ensure that all
- * separators are the same as File.separator.
- * @param path The path to be externalized.
- * @return The externalized path.
- */
- // KILL: Nothing uses this. It exists as symmetry of internalize and to promote
its use
- private static String externalize(String path) {
- if (INTERNAL_SEPARATOR_CHAR != File.separatorChar) {
- path = path.replace(INTERNAL_SEPARATOR_CHAR, File.separatorChar);
- }
- return path;
- }
-
//-----------------------------------------------------------------------
/**
* Converts all separators to the Unix separator of forward slash.
@@ -433,7 +374,7 @@
* </pre>
*
* @param filename the filename to query, null returns null
- * @return the filename minus path
+ * @return the path of the file, or an empty string if none exists
*/
public static String getPath(String filename) {
if (filename == null) {
@@ -461,7 +402,7 @@
* </pre>
*
* @param filename the filename to query, null returns null
- * @return the filename minus path
+ * @return the name of the file without the path, or an empty string if none
exists
*/
public static String getName(String filename) {
if (filename == null) {
@@ -484,7 +425,7 @@
* </pre>
*
* @param filename the filename to retrieve the extension of.
- * @return the extension of filename or an empty string if none exists.
+ * @return the extension of the file or an empty string if none exists.
*/
public static String getExtension(String filename) {
if (filename == null) {
@@ -495,6 +436,34 @@
return "";
} else {
return filename.substring(index + 1);
+ }
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Removes the extension from a filename.
+ * <p>
+ * This method returns the textual part of the filename before the last dot.
+ * There must be no directory separator after the dot.
+ * <pre>
+ * foo.txt --> foo
+ * a\b\c.jpg --> a\b\c
+ * a\b\c --> a\b\c
+ * a.b\c --> a.b\c
+ * </pre>
+ *
+ * @param filename the filename to query, null returns null
+ * @return the filename minus the extension
+ */
+ public static String removeExtension(String filename) {
+ if (filename == null) {
+ return null;
+ }
+ int index = indexOfExtension(filename);
+ if (index == -1) {
+ return filename;
+ } else {
+ return filename.substring(0, index);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]