public class DirectoryScanner extends Object implements FileScanner, ResourceFactory
File.separator ('/' under UNIX, '\' under Windows).
For example, "abc/def/ghi/xyz.java" is split up in the segments "abc",
"def","ghi" and "xyz.java".
The same is done for the pattern against which should be matched.
The segments of the name and the pattern are then matched against each
other. When '**' is used for a path segment in the pattern, it matches
zero or more path segments of the name.
There is a special case regarding the use of File.separators
at the beginning of the pattern and the string to match:File.separator, the string
to match must also start with a File.separator.
When a pattern does not start with a File.separator, the
string to match may not start with a File.separator.
When one of these rules is not obeyed, the string will not
match.
When a name path segment is matched against a pattern path segment, the
following special characters can be used:
String[] includes = {"*\*\*.class"};
String[] excludes = {"modules\\\*\**"};
ds.setIncludes(includes);
ds.setExcludes(excludes);
ds.setBasedir(new File("test"));
ds.setCaseSensitive(true);
ds.scan();
System.out.println("FILES:");
String[] files = ds.getIncludedFiles();
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
This will scan a directory called test for .class files, but excludes all
files in all proper subdirectories of a directory called "modules"| Modifier and Type | Field and Description |
|---|---|
protected static String[] |
DEFAULTEXCLUDES
Deprecated.
use the
getDefaultExcludes method instead. |
| Constructor and Description |
|---|
DirectoryScanner()
Sole constructor.
|
| Modifier and Type | Method and Description |
|---|---|
static boolean |
addDefaultExclude(String s)
Add a pattern to the default excludes unless it is already a
default exclude.
|
void |
addDefaultExcludes()
Add default exclusions to the current exclusions set.
|
void |
addExcludes(String[] excludes)
Add to the list of exclude patterns to use.
|
File |
getBasedir()
Return the base directory to be scanned.
|
String[] |
getDeselectedDirectories()
Return the names of the directories which were selected out and
therefore not ultimately included.
|
String[] |
getDeselectedFiles()
Return the names of the files which were selected out and
therefore not ultimately included.
|
String[] |
getExcludedDirectories()
Return the names of the directories which matched at least one of the
include patterns and at least one of the exclude patterns.
|
String[] |
getExcludedFiles()
Return the names of the files which matched at least one of the
include patterns and at least one of the exclude patterns.
|
String[] |
getIncludedDirectories()
Return the names of the directories which matched at least one of the
include patterns and none of the exclude patterns.
|
int |
getIncludedDirsCount()
Return the count of included directories.
|
String[] |
getIncludedFiles()
Return the names of the files which matched at least one of the
include patterns and none of the exclude patterns.
|
int |
getIncludedFilesCount()
Return the count of included files.
|
String[] |
getNotIncludedDirectories()
Return the names of the directories which matched none of the include
patterns.
|
String[] |
getNotIncludedFiles()
Return the names of the files which matched none of the include
patterns.
|
Resource |
getResource(String name)
Get the named resource.
|
boolean |
isEverythingIncluded()
Return whether or not the scanner has included all the files or
directories it has come across so far.
|
boolean |
isFollowSymlinks()
Get whether or not a DirectoryScanner follows symbolic links.
|
static boolean |
match(String pattern,
String str)
Test whether or not a string matches against a pattern.
|
protected static boolean |
match(String pattern,
String str,
boolean isCaseSensitive)
Test whether or not a string matches against a pattern.
|
protected static boolean |
matchPath(String pattern,
String str)
Test whether or not a given path matches a given pattern.
|
protected static boolean |
matchPatternStart(String pattern,
String str)
Test whether or not a given path matches the start of a given
pattern up to the first "**".
|
static boolean |
removeDefaultExclude(String s)
Remove a string if it is a default exclude.
|
void |
scan()
Scan the base directory for files which match at least one include
pattern and don't match any exclude patterns.
|
void |
setBasedir(File basedir)
Set the base directory to be scanned.
|
void |
setBasedir(String basedir)
Set the base directory to be scanned.
|
void |
setCaseSensitive(boolean isCaseSensitive)
Set whether or not include and exclude patterns are matched
in a case sensitive way.
|
void |
setExcludes(String[] excludes)
Set the list of exclude patterns to use.
|
void |
setFollowSymlinks(boolean followSymlinks)
Set whether or not symbolic links should be followed.
|
void |
setIncludes(String[] includes)
Set the list of include patterns to use.
|
protected static final String[] DEFAULTEXCLUDES
getDefaultExcludes method instead.Note that you can now add patterns to the list of default excludes. Added patterns will not become part of this array that has only been kept around for backwards compatibility reasons.
protected static boolean matchPatternStart(String pattern, String str)
pattern=**\a
and str=b will yield true.pattern - The pattern to match against. Must not be
null.str - The path to match, as a String. Must not be
null.protected static boolean matchPath(String pattern, String str)
pattern - The pattern to match against. Must not be
null.str - The path to match, as a String. Must not be
null.true if the pattern matches against the string,
or false otherwise.public static boolean match(String pattern, String str)
pattern - The pattern to match against.
Must not be null.str - The string which must be matched against the pattern.
Must not be null.true if the string matches against the pattern,
or false otherwise.protected static boolean match(String pattern, String str, boolean isCaseSensitive)
pattern - The pattern to match against.
Must not be null.str - The string which must be matched against the pattern.
Must not be null.isCaseSensitive - Whether or not matching should be performed
case sensitively.true if the string matches against the pattern,
or false otherwise.public static boolean addDefaultExclude(String s)
s - A string to add as an exclude pattern.true if the string was added;
false if it already existed.public static boolean removeDefaultExclude(String s)
s - The string to attempt to remove.true if s was a default
exclude (and thus was removed);
false if s was not
in the default excludes list to begin with.public void setBasedir(String basedir)
File.separatorChar, so the separator used need not match
File.separatorChar.setBasedir in interface FileScannerbasedir - The base directory to scan.
Must not be null.public void setBasedir(File basedir)
setBasedir in interface FileScannerbasedir - The base directory for scanning.
Should not be null.public File getBasedir()
getBasedir in interface FileScannerpublic void setCaseSensitive(boolean isCaseSensitive)
setCaseSensitive in interface FileScannerisCaseSensitive - whether or not the file system should be
regarded as a case sensitive one.public boolean isFollowSymlinks()
public void setFollowSymlinks(boolean followSymlinks)
followSymlinks - whether or not symbolic links should be followed.public void setIncludes(String[] includes)
File.separatorChar, so the separator used
need not match File.separatorChar.
When a pattern ends with a '/' or '\', "**" is appended.setIncludes in interface FileScannerincludes - A list of include patterns.
May be null, indicating that all files
should be included. If a non-null
list is given, all elements must be
non-null.public void setExcludes(String[] excludes)
File.separatorChar, so the separator used
need not match File.separatorChar.
When a pattern ends with a '/' or '\', "**" is appended.setExcludes in interface FileScannerexcludes - A list of exclude patterns.
May be null, indicating that no files
should be excluded. If a non-null list is
given, all elements must be non-null.public void addExcludes(String[] excludes)
File.separatorChar, so
the separator used need not match File.separatorChar.
When a pattern ends with a '/' or '\', "**" is appended.excludes - A list of exclude patterns.
May be null, in which case the
exclude patterns don't get changed at all.public boolean isEverythingIncluded()
true if all files and directories which have
been found so far have been included.public void scan()
throws Exception
scan in interface FileScannerException - if the base directory was set
incorrectly (i.e. if it is null, doesn't exist,
or isn't a directory).public String[] getIncludedFiles() throws Exception
getIncludedFiles in interface FileScannerExceptionpublic int getIncludedFilesCount()
throws Exception
int.Exceptionpublic String[] getNotIncludedFiles() throws Exception
getNotIncludedFiles in interface FileScannerExceptionslowScan()public String[] getExcludedFiles() throws Exception
getExcludedFiles in interface FileScannerExceptionslowScan()public String[] getDeselectedFiles() throws Exception
Return the names of the files which were selected out and therefore not ultimately included.
The names are relative to the base directory. This involves performing a slow scan if one has not already been completed.
ExceptionslowScan()public String[] getIncludedDirectories() throws Exception
getIncludedDirectories in interface FileScannerExceptionpublic int getIncludedDirsCount()
throws Exception
int.Exceptionpublic String[] getNotIncludedDirectories() throws Exception
getNotIncludedDirectories in interface FileScannerExceptionslowScan()public String[] getExcludedDirectories() throws Exception
getExcludedDirectories in interface FileScannerExceptionslowScan()public String[] getDeselectedDirectories() throws Exception
Return the names of the directories which were selected out and therefore not ultimately included.
The names are relative to the base directory. This involves performing a slow scan if one has not already been completed.
ExceptionslowScan()public void addDefaultExcludes()
addDefaultExcludes in interface FileScannerpublic Resource getResource(String name) throws Exception
getResource in interface ResourceFactoryname - path name of the file relative to the dir attribute.ExceptionCopyright © 2001–2025. All rights reserved.