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).
The caller may specify zero or more blacklist filters to be applied to each
file found by the primary file specification. Filter specifications support
the asterisk (*
) for zero or more wildcard characters, and the
question mark (?
) for exactly one wildcard character.
Important: note that blacklist filter specifications must
EXACTLY match (except wildcard characters) the names of file paths reported
by the operating system, relative to the starting directory. This includes
any leading characters (e.g., ./
on Linux), or lack thereof. If not,
the filters will not exclude files as expected.
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
fileSpec
The regular expression for filtering the file list.
|
private java.util.regex.Pattern[] |
filterPatterns
Regular expression patterns to specify file names to be excluded from the final 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.
|
FileSpecList(java.io.File startDir,
java.lang.String fileSpec,
boolean recursion,
boolean caseSens,
java.util.List<java.lang.String> filters)
Base constructor to handle most likely cases.
|
Modifier and Type | Method and Description |
---|---|
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.io.File dir,
java.util.List<java.io.File> results)
Core implementation of the listing algorithm which uses the
File class to make a
complete list of the directory contents. |
protected void |
listImpl(java.util.List<java.io.File> 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, listFilenames, listWithSuffix, setCaseSensitive, size, sortFiles, withSuffix
private final java.io.File startDir
private final java.lang.String fileSpec
private final java.lang.String origFileSpec
private final boolean recursion
private final java.util.regex.Pattern[] filterPatterns
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 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, boolean caseSens, java.util.List<java.lang.String> filters)
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.
Filter specifications may overlap; the first match excludes a file from the list. If no filters are specified, this class behaves the same as its parent.
startDir
- The starting directory for the file listing. Uses the current directory if
null
or if the given directory does not exist or actually specifies a
non-directory.fileSpec
- A shell pattern used to filter the list of files. Defaults to "*" if
null
. This filter is inclusive (i.e., only files which pass the filter
can be included in the file list).recursion
- Specifies if matching filenames in all subdirectories should be included.caseSens
- Sets the case-sensitivity of the sorting algorithm.filters
- Zero or more filter specifications of file name patterns to exclude from the
final file list. May be null
or empty, in which case no additional
filtering is performed beyond the primary fileSpec
inclusive filter.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.List<java.io.File> 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.List<java.io.File> 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. If recursion was
requested (on construction) by the caller, this method will recursively call itself for
every directory it encounters in the list. If blacklist filters have been specified at
construction, they are applied after a file name passes the primary file specification
test. 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.public static void main(java.lang.String[] args)
stdout
.
Syntax:
java FileSpecList <directory> "file_spec_pattern" [recursion] [ "filter_spec_pattern" [ ... ] ]Where:
args
- List of command line arguments.