public class FileSpecList extends FileList
The user-defined file specification is a shell matching pattern which can accept a modified regular expression where '*' matches any number of any character, '?' matches any single character and '[ ]' is a single character match based on the set of defined alternatives. As an example, to match all files, "*" can be used. Or to match all files that end in '.c' or '.h' one could use "*.[cChH]". Any '.' in the pattern will be escaped such that it matches a '.' directly instead of being a single character wildcard match as in standard regular expressions.
The caller may optionally ask for the list to be created recursively (all files that match the specification in the start/current directory and all sub-directories).
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
fileSpec
The regular expression for filtering the file list.
|
private java.lang.String |
origFileSpec
Original unmodified file specification.
|
private boolean |
recursion
Specifies if matching files in subdirectories will be included.
|
private java.io.File |
startDir
The relative or absolute path to the starting directory.
|
Constructor and Description |
---|
FileSpecList(java.io.File startDir,
java.lang.String fileSpec,
boolean recursion)
Base constructor to handle most likely cases.
|
FileSpecList(java.io.File startDir,
java.lang.String fileSpec,
boolean recursion,
boolean caseSens)
Base constructor to handle most likely cases.
|
Modifier and Type | Method and Description |
---|---|
private java.lang.String |
convertToRegEx(java.lang.String pattern)
Converts a shell-type matching "pattern" to a traditional regular
expression.
|
java.lang.String |
getFileSpec()
Get the original unmodified file specification used for the match.
|
java.lang.String |
getFileSpecAsRegEx()
Get the file specification (modified to be a regular expression) used
for the match.
|
java.io.File |
getStartDir()
Get the starting directory for the match.
|
boolean |
isRecursive()
Indicates whether the match is applied recursively into subdirectories.
|
protected void |
listImpl(java.util.ArrayList results)
Core implementation of the listing algorithm which uses the
File class to make a complete list of the directory
contents, then this list is used to build a filtered list of just
those files that match the user-defined regular expression. |
protected void |
listImpl(java.io.File dir,
java.util.ArrayList results)
Core implementation of the listing algorithm which uses the
File class to make a complete list of the directory
contents, then this list is used to build a filtered list of just
those files that match the user-defined regular expression. |
static void |
main(java.lang.String[] args)
Provides a command line interface for an end user to drive and/or test
the this class.
|
isCaseSensitive, list, listFilenames, setCaseSensitive, size
private java.io.File startDir
private java.lang.String fileSpec
private java.lang.String origFileSpec
private boolean recursion
public FileSpecList(java.io.File startDir, java.lang.String fileSpec, boolean recursion, boolean caseSens)
null
or a non-directory is passed for the starting directory, the current
directory will be used. In addition, if the directory doesn't exist,
the current directory will be used.
If the file specification is null
or an empty string, the
file specification will match all filenames.
startDir
- The starting directory for the file listing or the current
directory if null
or if it does not exist.fileSpec
- A shell pattern used to filter the list of files.
Defaults to "*" if null
recursion
- Specifies if matching filenames in all subdirectories should
be included.caseSens
- Sets the case-sensitivity of the sorting algorithm.public FileSpecList(java.io.File startDir, java.lang.String fileSpec, boolean recursion)
null
or a non-directory is passed for the starting directory, the current
directory will be used. In addition, if the directory doesn't exist,
the current directory will be used.
If the file specification is null
or an empty string, the
file specification will match all filenames.
startDir
- The starting directory for the file listing or the current
directory if null
or if it does not exist.fileSpec
- A shell pattern used to filter the list of files.
Defaults to "*" if null
recursion
- Specifies if matching filenames in all subdirectories should
be included.public java.io.File getStartDir()
public java.lang.String getFileSpecAsRegEx()
public java.lang.String getFileSpec()
public boolean isRecursive()
true
if match is recursive, else
false
.protected void listImpl(java.util.ArrayList results)
File
class to make a complete list of the directory
contents, then this list is used to build a filtered list of just
those files that match the user-defined regular expression. Note
that if recursion was previously requested (on construction) by the
caller, this method will recursively call itself for every directory
it encounters in the list. Only files will be returned in the results
list and all results are added to the results list that is passed as
a parameter.protected void listImpl(java.io.File dir, java.util.ArrayList results)
File
class to make a complete list of the directory
contents, then this list is used to build a filtered list of just
those files that match the user-defined regular expression. Note
that if recursion was previously requested (on construction) by the
caller, this method will recursively call itself for every directory
it encounters in the list. Only files will be returned in the results
list and all results are added to the results list that is passed as
a parameter.dir
- The directory in which to list all files.results
- The list to which all matching files must be appended.private java.lang.String convertToRegEx(java.lang.String pattern)
The following conversions are done:
Shell Pattern Regular Expression ------------- ------------------ . \. ? . * .*
This seems trivial, except that the backslash '\' character must be handled properly since it needs to be doubled up for Java to recognise one in a Java string and if two are needed in the regular expression then one needs to double them up, twice.
pattern
- Shell matching pattern.public static void main(java.lang.String[] args)
stdout
.
Syntax:
java FileSpecList <directory> "file_spec_pattern" [recursion]Where:
args
- List of command line arguments.