Skip to content

Commit 7a9d4bb

Browse files
committed
Make getExtension() and hence removeExtension() work for both Windows and
*nix paths. Enable (and fix) tests for both. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140616 13f79535-47bb-0310-9956-ffa450edef68
1 parent ea4523f commit 7a9d4bb

2 files changed

Lines changed: 53 additions & 22 deletions

File tree

src/java/org/apache/commons/io/FilenameUtils.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,22 @@
5353
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
5454
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
5555
* @author Matthew Hawthorne
56+
* @author Martin Cooper
5657
* @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
57-
* @version $Id: FilenameUtils.java,v 1.12 2004/10/24 04:20:07 martinc Exp $
58+
* @version $Id: FilenameUtils.java,v 1.13 2004/10/24 18:34:23 martinc Exp $
5859
*/
5960
public class FilenameUtils {
6061

62+
/**
63+
* Standard separator char used when internalizing paths.
64+
*/
65+
private static final char INTERNAL_SEPARATOR_CHAR = '/';
66+
67+
/**
68+
* Standard separator string used when internalizing paths.
69+
*/
70+
private static final String INTERNAL_SEPARATOR = "/";
71+
6172
/**
6273
* Instances should NOT be constructed in standard programming.
6374
*/
@@ -140,10 +151,11 @@ public static String removeExtension(final String filename) {
140151
public static String getExtension(final String filename) {
141152
String suffix = "";
142153
String shortFilename = filename;
154+
String ifilename = internalize(filename);
143155

144-
int lastDirSeparator = filename.lastIndexOf(File.separatorChar);
156+
int lastDirSeparator = ifilename.lastIndexOf(INTERNAL_SEPARATOR_CHAR);
145157
if (lastDirSeparator > 0) {
146-
shortFilename = filename.substring(lastDirSeparator + 1);
158+
shortFilename = ifilename.substring(lastDirSeparator + 1);
147159
}
148160

149161
int index = shortFilename.lastIndexOf('.');
@@ -429,6 +441,28 @@ public static File resolveFile(File baseFile, String filename) {
429441
return file;
430442
}
431443

444+
/**
445+
* Convert all separators to the internal form. This allows manipulation
446+
* of paths without concern for which separators are used within them.
447+
* @param path The path to be internalized.
448+
* @return The internalized path.
449+
*/
450+
private static String internalize(String path) {
451+
return path.replace('\\', INTERNAL_SEPARATOR_CHAR);
452+
}
453+
454+
/**
455+
* Convert all separators to their external form. That is, ensure that all
456+
* separators are the same as File.separator.
457+
* @param path The path to be externalized.
458+
* @return The externalized path.
459+
*/
460+
private static String externalize(String path) {
461+
if (INTERNAL_SEPARATOR_CHAR != File.separatorChar) {
462+
path = path.replace(INTERNAL_SEPARATOR_CHAR, File.separatorChar);
463+
}
464+
return path;
465+
}
432466

433467
// DEPRECATED. Though no replacement exists.
434468

src/test/org/apache/commons/io/FilenameUtilsTestCase.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
*
3030
* @author Peter Donald
3131
* @author Matthew Hawthorne
32-
* @version $Id: FilenameUtilsTestCase.java,v 1.10 2004/10/24 01:27:13 martinc Exp $
32+
* @author Martin Cooper
33+
* @version $Id: FilenameUtilsTestCase.java,v 1.11 2004/10/24 18:34:23 martinc Exp $
3334
* @see FilenameUtils
3435
*/
3536
public class FilenameUtilsTestCase extends FileBasedTestCase {
@@ -220,13 +221,13 @@ public void testGetExtension() {
220221

221222
public void testGetExtensionWithPaths() {
222223
String[][] testsWithPaths =
223-
{ { File.separator + "tmp" + File.separator + "foo" + File.separator + "filename.ext", "ext" }, {
224-
//"C:\\temp\\foo\\filename.ext", "ext" }, {
225-
File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "filename.ext", "ext" }, {
226-
//"C:\\temp\\foo.bar\\filename.ext", "ext" }, {
227-
File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "README", "" }, {
228-
//"C:\\temp\\foo.bar\\README", "" }, {
229-
".." + File.separator + "filename.ext", "ext" }
224+
{ { "/tmp/foo/filename.ext", "ext" }, {
225+
"C:\\temp\\foo\\filename.ext", "ext" }, {
226+
"/tmp/foo.bar/filename.ext", "ext" }, {
227+
"C:\\temp\\foo.bar\\filename.ext", "ext" }, {
228+
"/tmp/foo.bar/README", "" }, {
229+
"C:\\temp\\foo.bar\\README", "" }, {
230+
"../filename.ext", "ext" }
230231
};
231232
for (int i = 0; i < testsWithPaths.length; i++) {
232233
assertEquals(
@@ -256,17 +257,13 @@ public void testRemoveExtension() {
256257

257258
public void testRemoveExtensionWithPaths() {
258259
String[][] testsWithPaths =
259-
{ { File.separator + "tmp" + File.separator + "foo" + File.separator + "filename.ext",
260-
File.separator + "tmp" + File.separator + "foo" + File.separator + "filename" }, {
261-
//"C:\\temp\\foo\\filename.ext", "filename" }, {
262-
File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "filename.ext",
263-
File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "filename" }, {
264-
//"C:\\temp\\foo.bar\\filename.ext", "filename" }, {
265-
File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "README",
266-
File.separator + "tmp" + File.separator + "foo.bar" + File.separator + "README" }, {
267-
//"C:\\temp\\foo.bar\\README", "README" }, {
268-
".." + File.separator + "filename.ext",
269-
".." + File.separator + "filename" }
260+
{ { "/tmp/foo/filename.ext", "/tmp/foo/filename" }, {
261+
"C:\\temp\\foo\\filename.ext", "C:\\temp\\foo\\filename" }, {
262+
"/tmp/foo.bar/filename.ext", "/tmp/foo.bar/filename" }, {
263+
"C:\\temp\\foo.bar\\filename.ext", "C:\\temp\\foo.bar\\filename" }, {
264+
"/tmp/foo.bar/README", "/tmp/foo.bar/README" }, {
265+
"C:\\temp\\foo.bar\\README", "C:\\temp\\foo.bar\\README" }, {
266+
"../filename.ext", "../filename" }
270267
};
271268

272269
for (int i = 0; i < testsWithPaths.length; i++) {

0 commit comments

Comments
 (0)