Skip to content

Commit 4694d5a

Browse files
committed
merged WildcardUtils into FilenameUtils; thought the test case is still a separate file
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140639 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4a61d8c commit 4694d5a

7 files changed

Lines changed: 134 additions & 162 deletions

File tree

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

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.File;
1919
import java.io.IOException;
2020
import java.util.Collection;
21+
import java.util.ArrayList;
2122

2223
/**
2324
* Utility class that provides methods to manipulate filenames and filepaths.
@@ -64,7 +65,7 @@
6465
* @author Martin Cooper
6566
* @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
6667
* @author Stephen Colebourne
67-
* @version $Id: FilenameUtils.java,v 1.24 2004/10/30 23:59:17 scolebourne Exp $
68+
* @version $Id: FilenameUtils.java,v 1.25 2004/10/31 04:17:34 bayard Exp $
6869
* @since Commons IO 1.1
6970
*/
7071
public class FilenameUtils {
@@ -552,4 +553,100 @@ public static boolean isExtension(String filename, Collection extensions) {
552553
return isExtension(filename, array);
553554
}
554555

556+
/**
557+
* See if a particular piece of text, often a filename,
558+
* matches to a specified wildcard, as seen on DOS/UNIX command lines.
559+
*/
560+
public static boolean wildcardMatch(String text, String wildcard) {
561+
String[] wcs = splitOnTokens(wildcard);
562+
563+
int textIdx = 0;
564+
int wcsIdx = 0;
565+
boolean anyChars = false;
566+
567+
// loop whilst tokens and text left to process
568+
while (wcsIdx < wcs.length && textIdx < text.length()) {
569+
570+
// ? so move to next text char
571+
if (wcs[wcsIdx].equals("?")) {
572+
textIdx++;
573+
} else
574+
if (!wcs[wcsIdx].equals("*")) {
575+
// matching text token
576+
if (anyChars) {
577+
// any chars then try to locate text token
578+
textIdx = text.indexOf(wcs[wcsIdx], textIdx);
579+
580+
if (textIdx == -1) {
581+
// token not found
582+
return false;
583+
}
584+
} else {
585+
// matching from current position
586+
if (!text.startsWith(wcs[wcsIdx], textIdx)) {
587+
// couldnt match token
588+
return false;
589+
}
590+
}
591+
592+
// matched text token, move text index to end of matched token
593+
textIdx += wcs[wcsIdx].length();
594+
}
595+
596+
// set any chars status
597+
anyChars = wcs[wcsIdx].equals("*");
598+
599+
wcsIdx++;
600+
}
601+
602+
// didnt match all wildcards
603+
if (wcsIdx < wcs.length) {
604+
// ok if one remaining and wildcard or empty
605+
if (wcsIdx + 1 != wcs.length || !(wcs[wcsIdx].equals("*") || wcs[wcsIdx].equals("")) ) {
606+
return false;
607+
}
608+
}
609+
610+
// ran out of text chars
611+
if (textIdx > text.length()) {
612+
return false;
613+
}
614+
615+
// didnt match all text chars, only ok if any chars set
616+
if (textIdx < text.length() && !anyChars) {
617+
return false;
618+
}
619+
620+
return true;
621+
}
622+
623+
// used by wildcardMatch
624+
// package level so a unit test may run on this
625+
static String[] splitOnTokens(String text) {
626+
char[] array = text.toCharArray();
627+
if(text.indexOf("?") == -1 && text.indexOf("*") == -1) {
628+
return new String[] { text };
629+
}
630+
631+
ArrayList list = new ArrayList();
632+
StringBuffer buffer = new StringBuffer();
633+
for(int i=0; i<array.length; i++) {
634+
if(array[i] == '?' || array[i] == '*') {
635+
if(buffer.length() != 0) {
636+
list.add(buffer.toString());
637+
buffer.setLength(0);
638+
}
639+
list.add(new String( new char[] { array[i] } ));
640+
} else {
641+
buffer.append(array[i]);
642+
}
643+
}
644+
if(buffer.length() != 0) {
645+
list.add(buffer.toString());
646+
}
647+
648+
return (String[]) list.toArray(new String[0]);
649+
}
650+
651+
555652
}

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

Lines changed: 0 additions & 125 deletions
This file was deleted.

src/java/org/apache/commons/io/filefilter/WildcardFilter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import java.io.File;
44
import java.util.List;
5-
import org.apache.commons.io.WildcardUtils;
5+
import org.apache.commons.io.FilenameUtils;
66

77
/**
88
* Filters files using supplied wildcard(s).
99
* <p/>
10-
* See org.apache.commons.io.find.WildcardUtils for wildcard matching rules
10+
* See org.apache.commons.io.find.FilenameUtils.wildcardMatch() for wildcard matching rules
1111
* <p/>
1212
*
1313
* <p/>
@@ -22,7 +22,7 @@
2222
* </pre>
2323
*
2424
* @author Jason Anderson
25-
* @version $Revision: 1.4 $ $Date: 2004/10/25 23:28:29 $
25+
* @version $Revision: 1.5 $ $Date: 2004/10/31 04:17:34 $
2626
* @since Commons IO 1.1
2727
*/
2828
public class WildcardFilter extends AbstractFileFilter {
@@ -86,7 +86,7 @@ public boolean accept(File dir, String name) {
8686
}
8787

8888
for (int i = 0; i < wildcards.length; i++) {
89-
if (WildcardUtils.match(name, wildcards[i])) {
89+
if (FilenameUtils.wildcardMatch(name, wildcards[i])) {
9090
return true;
9191
}
9292
}
@@ -106,7 +106,7 @@ public boolean accept(File file) {
106106
}
107107

108108
for (int i = 0; i < wildcards.length; i++) {
109-
if (WildcardUtils.match(file.getName(), wildcards[i])) {
109+
if (FilenameUtils.wildcardMatch(file.getName(), wildcards[i])) {
110110
return true;
111111
}
112112
}

src/java/org/apache/commons/io/find/FileFinder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public File[] find(File directory, Map options) {
7777

7878
boolean depthFirst = toBoolean(options.get(Finder.DEPTH));
7979

80-
// TODO: to implement
8180
int maxDepth = toInt(options.get(Finder.MAXDEPTH));
8281
int minDepth = toInt(options.get(Finder.MINDEPTH));
8382
boolean ignoreHiddenDirs = toBoolean(options.get(Finder.IGNORE_HIDDEN_DIRS));

src/java/org/apache/commons/io/find/FindingFilter.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.regex.Pattern;
2626
import java.util.regex.Matcher;
2727

28-
import org.apache.commons.io.WildcardUtils;
28+
import org.apache.commons.io.FilenameUtils;
2929

3030
/**
3131
* This is where most of the find functionality occurs. Nearly every option
@@ -219,6 +219,7 @@ public boolean accept(File file) {
219219
/**
220220
* @todo needs to handle +5 for > 5 and -5 for < 5.
221221
* @todo Also needs to handle k, m, g, as suffixes.
222+
* @todo Switch from 512 byte block size to bytes
222223
*/
223224
class SizeFilter implements FileFilter {
224225
private Object option;
@@ -251,9 +252,9 @@ public NameFilter(Object option, Object argument, boolean invert, boolean ignore
251252
}
252253
public boolean accept(File file) {
253254
if(this.ignoreCase) {
254-
return FindingFilter.invert( this.invert, WildcardUtils.match(file.getName().toLowerCase(), this.argument.toString().toLowerCase()) );
255+
return FindingFilter.invert( this.invert, FilenameUtils.wildcardMatch(file.getName().toLowerCase(), this.argument.toString().toLowerCase()) );
255256
} else {
256-
return FindingFilter.invert( this.invert, WildcardUtils.match(file.getName(), this.argument.toString()) );
257+
return FindingFilter.invert( this.invert, FilenameUtils.wildcardMatch(file.getName(), this.argument.toString()) );
257258
}
258259
}
259260
}
@@ -271,9 +272,9 @@ public PathFilter(Object option, Object argument, boolean invert, boolean ignore
271272
}
272273
public boolean accept(File file) {
273274
if(this.ignoreCase) {
274-
return FindingFilter.invert( this.invert, WildcardUtils.match(file.getPath().toLowerCase(), this.argument.toString().toLowerCase()) );
275+
return FindingFilter.invert( this.invert, FilenameUtils.wildcardMatch(file.getPath().toLowerCase(), this.argument.toString().toLowerCase()) );
275276
} else {
276-
return FindingFilter.invert( this.invert, WildcardUtils.match(file.getPath(), this.argument.toString()) );
277+
return FindingFilter.invert( this.invert, FilenameUtils.wildcardMatch(file.getPath(), this.argument.toString()) );
277278
}
278279
}
279280
}

0 commit comments

Comments
 (0)