com.ccg.util
Class FilenameFilterWild

java.lang.Object
  extended by com.ccg.util.FilenameFilterWild
All Implemented Interfaces:
FilenameFilter

public class FilenameFilterWild
extends Object
implements FilenameFilter

Class to get a list of file names using wild cards. This class is used to allow one to get a listing of filenames matching (or not matching) a criteria similar to the standard wild cards used by Unix, Windows, and OS/2 type systems when listing files. For example, one could use this class to get a listing of all the "*.java" files in a particular directory. This class can be passed as an argument to the java.io.File.list function, or one can use the static list member declared in this class to automatically generate a listing of file names.

                                // Get list of java & html files
 String[] files = FilenameFilterWild.list("ccg\\util\\*.java;*.html");

                                // get list by constructing matches
 FilenameFilterWild ffw = new FilenameFilterWild();
                                // Matches Fullname.java, FilenameWild.java,...
 ffw.addWildString("F?l*name*.java");
 ffw.addWildString("*.c;*.h"); // Matches C source files
 java.io.File dir = new java.io.File(".");
 String[] files = dir.list(ffw);

 ffw.setShouldMatch(false);     // now get files which don't match
 String[] rest = dir.list(ffw);
 

Version:
$Revision: 1.1.1.1 $
Author:
Paul Blankenbaker
See Also:
FilenameFilter, File.list(java.io.FilenameFilter), list(java.lang.String)

Constructor Summary
FilenameFilterWild()
          Constructor for the FilenameFilterWild class.
 
Method Summary
 boolean accept(File f, String n)
          Determine if a file should be included based on matching rules.
 void addWildString(String ws)
          Add more matching rules.
static String[] list(String matching)
          Get a list of files matching a specified criteria (handy).
 void setShouldMatch(boolean f)
          Toggle whether to get files which match or which do NOT match.
 String toString()
          Get current matching rules in String form.
static boolean wildCompare(String wildString, String name)
          Determine if a string matches a "wild" string (handy).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FilenameFilterWild

public FilenameFilterWild()
Constructor for the FilenameFilterWild class. The following things are done:

Method Detail

accept

public boolean accept(File f,
                      String n)
Determine if a file should be included based on matching rules. This routine is typically invoked when this object is passed as a filter to the File.list() member. It checks the name of the file to see if it matches any of the strings in its list.

Specified by:
accept in interface FilenameFilter
Parameters:
f - File to be checked
n - Name of the file to be checked (this is compared against the wild strings)
Returns:
Returns true if the name should be accepted
See Also:
list(java.lang.String), File.list(java.io.FilenameFilter)

addWildString

public void addWildString(String ws)
Add more matching rules. This routine is used to add one or more wild strings to the object which it will then use when it attempts to match file names. A wild string can contain normal characters and special characters. There are two types of special characters '*' and '?' all other characters are considered normal. The rules for matching filenames is similar to what most Unix/DOS users are familiar with using at a command prompt.

Match Any ('*')
This character can be used to indicate that it will match against 0 or more normal characters
Match One ('?')
This character can be used to indicate that it will match the next character

For example: "*.cc" would match against files like: "Fred.cc" and "Mary.cc", but not against files like: "Frank.cc.old" or "Mary.hh". The wild strings can be complex, for example: "Fr?n*k*" would match "Franky" and "Fran.kc", but not "Fred.k".

Parameters:
ws - Wild string(s) to add - semicolons must be used to separate the strings if more than one is included (typically something like: "*.c" or "*.h;*.c")
See Also:
accept(java.io.File, java.lang.String), wildCompare(java.lang.String, java.lang.String)

list

public static String[] list(String matching)
Get a list of files matching a specified criteria (handy). This is a fairly handy static function that anyone can access. It allows you to get a list of files in a directory matching a string of wildcards. You pass a string in the form of "[DIR]MATCH[;MATCH]" with the following characteristics:

DIR
This field is optional and is used to specify which directory is to be looked at - if omitted, the current directory is used.
MATCH
You can specify one or more strings to match file names against (if you specify more than one you must speparate it by a semicolon ';'). These matching strings can contain any normal character or the wild card characters as described in the addWildString() member.

For example: list("/opt/include/*.hh;*.h") would return a list of all C++ and C header files in the "/opt/include" directory. list("F*name*.java") would return a list of all files matching "F*name*.java" in the current directory.

Parameters:
matching - This is the string used to specify what files you are looking for. It may contain optional path information at the start (if omitted, the current directory will be searched), and one or more wild strings to match filenames against. The following are all valid examples: "*.java", "/o/opt/java/ccg/util/FilenameFilterWild.java;*.class;F*.htm*", and "c:\winnt\*.exe;*.com". You can pass null, but you'll get null back.
Returns:
List of file names which met the criteria specified. If directory information was present in the original 'matching' argument, then the path information will precede each filename in the list. NOTE: this routine will return 'null' if there were no matching files found.
See Also:
accept(java.io.File, java.lang.String), Example Program

setShouldMatch

public final void setShouldMatch(boolean f)
Toggle whether to get files which match or which do NOT match. This routine is used to specify whether the filter object should return a list of files which match the criteria specified, or a list of files which do not match the criteria specified.

Parameters:
f - Set to true if you want this object to be used to return a list of files which match the criteria specified (this is the default behaviour). Set to false if you want this object to be used to return a list of files which do not match the criteria specified.
See Also:
accept(java.io.File, java.lang.String)

toString

public String toString()
Get current matching rules in String form. This routine converts the FilenameFilterWild object to a string representation.

Overrides:
toString in class Object
Returns:
A list of wild matches separated by semicolons. For example, it might return something like: "*.cc;*.c"

wildCompare

public static boolean wildCompare(String wildString,
                                  String name)
Determine if a string matches a "wild" string (handy). This is a static routine which can be used to determine if a wild string (a string which may contain 0 or more of the wildcard characters '*' and/or '?') matches a regular string. It was originally intended for the purpose of matching filenames, however it can be used for any type of string. The rules for matching are similar to what most Unix/DOS users are familiar with using at a command prompt.

Match Any ('*')
This character can be used to indicate that it will match against 0 or more normal characters
Match One ('?')
This character can be used to indicate that it will match the next character

For example: "*.cc" would match against strings like: "Fred.cc" and "Mary.cc", but not against strings like: "Frank.cc.old" or "Mary.hh". The wild strings can be complex, for example: "Fr?n*k*" would match "Franky" and "Fran.kc", but not "Fred.k".

Parameters:
wildString - String which can contain the wildcard characters ('*' and '?')
name - String to see if it matches against the wildString
Returns:
true if they match, false if not
See Also:
accept(java.io.File, java.lang.String)


Copyright 1998-1998-2006 null. All Rights Reserved.