Skip to content

Commit 799a5e8

Browse files
author
Stephen Colebourne
committed
Refactor getXxx methods in FilenameUtils
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140635 13f79535-47bb-0310-9956-ffa450edef68
1 parent e8cb32f commit 799a5e8

2 files changed

Lines changed: 127 additions & 179 deletions

File tree

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

Lines changed: 91 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
* @author Martin Cooper
6464
* @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
6565
* @author Stephen Colebourne
66-
* @version $Id: FilenameUtils.java,v 1.21 2004/10/30 22:43:21 scolebourne Exp $
66+
* @version $Id: FilenameUtils.java,v 1.22 2004/10/30 23:12:18 scolebourne Exp $
6767
* @since Commons IO 1.1
6868
*/
6969
public class FilenameUtils {
@@ -128,121 +128,6 @@ public static String removeExtension(String filename) {
128128
return filename.substring(0, index);
129129
}
130130

131-
/**
132-
* Gets the extension of a filename.
133-
* <p>
134-
* eg
135-
* <pre>
136-
* foo.txt --> "txt"
137-
* a/b/c.jpg --> "jpg"
138-
* a/b/c --> ""
139-
* a.b/c.txt --> "txt"
140-
* a.b/c --> ""
141-
* </pre>
142-
*
143-
* @param filename the filename to retrieve the extension of.
144-
* @return the extension of filename or an empty string if none exists.
145-
*/
146-
public static String getExtension(String filename) {
147-
String suffix = "";
148-
String shortFilename = filename;
149-
String ifilename = internalize(filename);
150-
151-
int lastDirSeparator = ifilename.lastIndexOf(INTERNAL_SEPARATOR_CHAR);
152-
if (lastDirSeparator > 0) {
153-
shortFilename = ifilename.substring(lastDirSeparator + 1);
154-
}
155-
156-
int index = shortFilename.lastIndexOf('.');
157-
158-
if (index > 0 && index < shortFilename.length() - 1) {
159-
suffix = shortFilename.substring(index + 1);
160-
}
161-
162-
return suffix;
163-
}
164-
165-
/**
166-
* Remove path from filename. Equivalent to the unix command
167-
* <code>basename</code>.
168-
* ie.
169-
* <pre>
170-
* a/b/c.txt --> c.txt
171-
* a.txt --> a.txt
172-
* </pre>
173-
*
174-
* @param filepath the filepath
175-
* @return the filename minus path
176-
*/
177-
// KILL? Just use StringUtils?
178-
public static String removePath(String filepath) {
179-
return removePath(filepath, File.separatorChar);
180-
}
181-
182-
/**
183-
* Remove path from filename.
184-
* ie.
185-
* <pre>
186-
* a/b/c.txt --> c.txt
187-
* a.txt --> a.txt
188-
* </pre>
189-
*
190-
* @param filepath the filepath
191-
* @param fileSeparatorChar the file separator character to use
192-
* @return the filename minus path
193-
*/
194-
// KILL: Why allow the char to be specified?
195-
public static String removePath( String filepath, char fileSeparatorChar) {
196-
int index = filepath.lastIndexOf(fileSeparatorChar);
197-
198-
if (-1 == index) {
199-
return filepath;
200-
} else {
201-
return filepath.substring(index + 1);
202-
}
203-
}
204-
205-
/**
206-
* Get path from filename. Roughly equivalent to the unix command
207-
* <code>dirname</code>.
208-
* ie.
209-
* <pre>
210-
* a/b/c.txt --> a/b
211-
* a.txt --> ""
212-
* </pre>
213-
*
214-
* @param filepath the filepath
215-
* @return the filename minus path
216-
*/
217-
// KILL? Just use StringUtils?
218-
public static String getPath(String filepath) {
219-
return getPath(filepath, File.separatorChar);
220-
}
221-
222-
/**
223-
* Get path from filename.
224-
* ie.
225-
* <pre>
226-
* a/b/c.txt --> a/b
227-
* a.txt --> ""
228-
* </pre>
229-
*
230-
* @param filepath the filepath
231-
* @param fileSeparatorChar the file separator character to use
232-
* @return the filename minus path
233-
*/
234-
// KILL: Why allow the char to be specified?
235-
public static String getPath( String filepath, char fileSeparatorChar) {
236-
int index = filepath.lastIndexOf(fileSeparatorChar);
237-
if (-1 == index) {
238-
return "";
239-
} else {
240-
return filepath.substring(0, index);
241-
}
242-
}
243-
244-
245-
246131
/**
247132
* Normalize a path.
248133
* Eliminates "/../" and "/./" in a string. Returns <code>null</code> if
@@ -500,16 +385,16 @@ public static String separatorsToSystem(String path) {
500385
* This method will handle a file in either Unix or Windows format.
501386
* The position of the last forward or backslash is returned.
502387
*
503-
* @param path the path to find the last path separator in
388+
* @param filename the filename to find the last path separator in, null returns -1
504389
* @return the index of the last separator character, or -1 if there
505390
* is no such character.
506391
*/
507-
public static int indexOfLastSeparator(String path) {
508-
if (path == null) {
392+
public static int indexOfLastSeparator(String filename) {
393+
if (filename == null) {
509394
return -1;
510395
}
511-
int lastUnixPos = path.lastIndexOf(UNIX_SEPARATOR);
512-
int lastWindowsPos = path.lastIndexOf(WINDOWS_SEPARATOR);
396+
int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
397+
int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
513398
return Math.max(lastUnixPos, lastWindowsPos);
514399
}
515400

@@ -520,17 +405,97 @@ public static int indexOfLastSeparator(String path) {
520405
* To do this it uses {@link #indexOfLastSeparator(String)} which will
521406
* handle a file in either Unix or Windows format.
522407
*
523-
* @param path the path to find the last path separator in
408+
* @param filename the filename to find the last path separator in, null returns -1
524409
* @return the index of the last separator character, or -1 if there
525410
* is no such character.
526411
*/
527-
public static int indexOfExtension(String path) {
528-
if (path == null) {
412+
public static int indexOfExtension(String filename) {
413+
if (filename == null) {
529414
return -1;
530415
}
531-
int extensionPos = path.lastIndexOf(EXTENSION_SEPARATOR);
532-
int lastSeparator = indexOfLastSeparator(path);
416+
int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
417+
int lastSeparator = indexOfLastSeparator(filename);
533418
return (lastSeparator > extensionPos ? -1 : extensionPos);
534419
}
535420

421+
//-----------------------------------------------------------------------
422+
/**
423+
* Gets the path from a full filename.
424+
* <p>
425+
* This method will handle a file in either Unix or Windows format.
426+
* The text before the last forward or backslash is returned.
427+
* This method is roughly equivalent to the unix command <code>dirname</code>.
428+
* <pre>
429+
* a/b/c.txt --> a/b
430+
* a.txt --> ""
431+
* a/b/c --> a/b
432+
* a/b/c/ --> a/b/c
433+
* </pre>
434+
*
435+
* @param filename the filename to query, null returns null
436+
* @return the filename minus path
437+
*/
438+
public static String getPath(String filename) {
439+
if (filename == null) {
440+
return null;
441+
}
442+
int index = indexOfLastSeparator(filename);
443+
if (index == -1) {
444+
return "";
445+
} else {
446+
return filename.substring(0, index);
447+
}
448+
}
449+
450+
/**
451+
* Gets the name minus the path from a full filename.
452+
* <p>
453+
* This method will handle a file in either Unix or Windows format.
454+
* The text after the last forward or backslash is returned.
455+
* This method is roughly equivalent to the unix command <code>basename</code>.
456+
* <pre>
457+
* a/b/c.txt --> c.txt
458+
* a.txt --> a.txt
459+
* a/b/c --> c
460+
* a/b/c/ --> ""
461+
* </pre>
462+
*
463+
* @param filename the filename to query, null returns null
464+
* @return the filename minus path
465+
*/
466+
public static String getName(String filename) {
467+
if (filename == null) {
468+
return null;
469+
}
470+
int index = indexOfLastSeparator(filename);
471+
return filename.substring(index + 1);
472+
}
473+
474+
/**
475+
* Gets the extension of a filename.
476+
* <p>
477+
* This method returns the textual part of the filename after the last dot.
478+
* There must be no directory separator after the dot.
479+
* <pre>
480+
* foo.txt --> "txt"
481+
* a/b/c.jpg --> "jpg"
482+
* a/b.txt/c --> ""
483+
* a/b/c --> ""
484+
* </pre>
485+
*
486+
* @param filename the filename to retrieve the extension of.
487+
* @return the extension of filename or an empty string if none exists.
488+
*/
489+
public static String getExtension(String filename) {
490+
if (filename == null) {
491+
return null;
492+
}
493+
int index = indexOfExtension(filename);
494+
if (index == -1) {
495+
return "";
496+
} else {
497+
return filename.substring(index + 1);
498+
}
499+
}
500+
536501
}

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

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @author Peter Donald
3131
* @author Matthew Hawthorne
3232
* @author Martin Cooper
33-
* @version $Id: FilenameUtilsTestCase.java,v 1.15 2004/10/30 22:43:21 scolebourne Exp $
33+
* @version $Id: FilenameUtilsTestCase.java,v 1.16 2004/10/30 23:12:18 scolebourne Exp $
3434
* @see FilenameUtils
3535
*/
3636
public class FilenameUtilsTestCase extends FileBasedTestCase {
@@ -75,24 +75,6 @@ protected void tearDown() throws Exception {
7575
FileUtils.deleteDirectory(getTestDirectory());
7676
}
7777

78-
// removePath
79-
80-
public void testRemovePath() {
81-
String fileName =
82-
FilenameUtils.removePath(
83-
new File(getTestDirectory(), getName()).getAbsolutePath());
84-
assertEquals(getName(), fileName);
85-
}
86-
87-
// getPath
88-
89-
public void testGetPath() {
90-
String fileName =
91-
FilenameUtils.getPath(
92-
new File(getTestDirectory(), getName()).getAbsolutePath());
93-
assertEquals(getTestDirectory().getAbsolutePath(), fileName);
94-
}
95-
9678
// catPath
9779

9880
public void testCatPath() {
@@ -189,40 +171,6 @@ private String replaceAll(
189171
return sb.toString();
190172
}
191173

192-
public void testGetExtension() {
193-
String[][] tests = {
194-
{ "filename.ext", "ext" },
195-
{ "README", "" },
196-
{ "domain.dot.com", "com" },
197-
{ "image.jpeg", "jpeg" },
198-
{ "a.b/c", "" },
199-
{ "a.b/c.txt", "txt" },
200-
{ "a/b/c", "" },
201-
};
202-
for (int i = 0; i < tests.length; i++) {
203-
assertEquals(tests[i][1], FilenameUtils.getExtension(tests[i][0]));
204-
//assertEquals(tests[i][1], FilenameUtils.extension(tests[i][0]));
205-
}
206-
}
207-
208-
public void testGetExtensionWithPaths() {
209-
String[][] testsWithPaths =
210-
{ { "/tmp/foo/filename.ext", "ext" }, {
211-
"C:\\temp\\foo\\filename.ext", "ext" }, {
212-
"/tmp/foo.bar/filename.ext", "ext" }, {
213-
"C:\\temp\\foo.bar\\filename.ext", "ext" }, {
214-
"/tmp/foo.bar/README", "" }, {
215-
"C:\\temp\\foo.bar\\README", "" }, {
216-
"../filename.ext", "ext" }
217-
};
218-
for (int i = 0; i < testsWithPaths.length; i++) {
219-
assertEquals(
220-
testsWithPaths[i][1],
221-
FilenameUtils.getExtension(testsWithPaths[i][0]));
222-
//assertEquals(testsWithPaths[i][1], FilenameUtils.extension(testsWithPaths[i][0]));
223-
}
224-
}
225-
226174
public void testRemoveExtension() {
227175
String[][] tests = {
228176
{ "filename.ext", "filename" },
@@ -316,4 +264,39 @@ public void testIndexOfExtension() {
316264
assertEquals(-1, FilenameUtils.indexOfExtension("a\\b.notextension\\c"));
317265
}
318266

267+
//-----------------------------------------------------------------------
268+
public void testGetPath() {
269+
assertEquals(null, FilenameUtils.getPath(null));
270+
assertEquals("", FilenameUtils.getPath("noseperator.inthispath"));
271+
assertEquals("a/b", FilenameUtils.getPath("a/b/c.txt"));
272+
assertEquals("a/b", FilenameUtils.getPath("a/b/c"));
273+
assertEquals("a/b/c", FilenameUtils.getPath("a/b/c/"));
274+
assertEquals("a\\b", FilenameUtils.getPath("a\\b\\c"));
275+
}
276+
277+
public void testRemovePath() {
278+
assertEquals(null, FilenameUtils.getName(null));
279+
assertEquals("noseperator.inthispath", FilenameUtils.getName("noseperator.inthispath"));
280+
assertEquals("c.txt", FilenameUtils.getName("a/b/c.txt"));
281+
assertEquals("c", FilenameUtils.getName("a/b/c"));
282+
assertEquals("", FilenameUtils.getName("a/b/c/"));
283+
assertEquals("c", FilenameUtils.getName("a\\b\\c"));
284+
}
285+
286+
public void testGetExtension() {
287+
assertEquals(null, FilenameUtils.getExtension(null));
288+
assertEquals("ext", FilenameUtils.getExtension("file.ext"));
289+
assertEquals("", FilenameUtils.getExtension("README"));
290+
assertEquals("com", FilenameUtils.getExtension("domain.dot.com"));
291+
assertEquals("jpeg", FilenameUtils.getExtension("image.jpeg"));
292+
assertEquals("", FilenameUtils.getExtension("a.b/c"));
293+
assertEquals("txt", FilenameUtils.getExtension("a.b/c.txt"));
294+
assertEquals("", FilenameUtils.getExtension("a/b/c"));
295+
assertEquals("", FilenameUtils.getExtension("a.b\\c"));
296+
assertEquals("txt", FilenameUtils.getExtension("a.b\\c.txt"));
297+
assertEquals("", FilenameUtils.getExtension("a\\b\\c"));
298+
assertEquals("", FilenameUtils.getExtension("C:\\temp\\foo.bar\\README"));
299+
assertEquals("ext", FilenameUtils.getExtension("../filename.ext"));
300+
}
301+
319302
}

0 commit comments

Comments
 (0)