Wildcard FilenameFilter
by Roedy Green ©1996-2008 Canadian Mind Products
This essay is about a suggested
student project in
Java programming. This essay gives a rough overview of how it might work. It
does not describe an actual complete program. I have
no source, object,
specifications, file layouts or anything else useful to implementing this
project. Everything I have to say to help you with this project is written below.
I am
not prepared to help you implement it; I have too many other
projects of my own.
I do contract work for a living, which could include writing a program such as
this. However, I don’t do people’s homework
for them. That just robs them of an education.
You have my full permission to implement this project any way you please.
The java.io package does not directly support wildcard
filters. You have to implement the FilenameFilter
interface and write an accept method that examines each filename and decides
whether it should be included.
Writing even a simple wildcard filter is a bit of hassle. Have a look at this
one that selects all files of the form ?gloss*.html :
Your job is to create a tool for creating FilenameFilter
objects specifying only a list of positive and negative wildcards. For example
you might use the only a list of positive and negative wildcards. For example
you might use the tool like this:
FilenameFilter f = new WildCardFilter( "*.txt,*.doc,*.c*,-*.cnt" );
String[] filenames = new File ( "MyDir" ).list( f );
You process the wildcards in order using an algorithm something like this:
Ideas for speeding up the code:
- Create a case sensitive and case insensitive version. For the case insensitive
version it is probably best to convert all Strings to upper case before you
start.
- Consider a coarse prefilter to quickly determine some of the definite
yeses and definite nos. For example, you could index into a boolean array using
the first character of the extension, to get a definite yes, definite no or
definite maybe. Also, you could use such a rough filter to determine which fine
filters to bother checking.
- Consider treating the filename and extension separately since most wildcards are
just lists of extensions.
- Consider implementing much the way a parser would, as a finite state machine
that changes state depending on what the next letter is.
- If you get a hit on a positive wildcard, you can bypass further positive
wildcard processing up to the next negative wildcard.
- Consider implementing with a regex matcher.
That would allow you to have quite elaborate wildcards. You leave the
optimisation up to the regex package. Now that Java has a built-in regex package
this is by far the easiest way to implement the filters. Your job then becomes
documenting how to use the regex package to do all the usual filtering tasks, or
alternatively to convert your simple wildcards into true regex patterns.