public class DirectoryScanner
extends java.lang.Object
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.separator
s 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" This class must not be used from multiple Threads concurrently!
Modifier and Type | Field and Description |
---|---|
private java.io.File |
basedir
The base directory to be scanned.
|
static java.lang.String[] |
DEFAULTEXCLUDES
Patterns which should be excluded by default.
|
private java.util.List<java.lang.String> |
dirsExcluded
The directories which matched at least one include and at least one exclude.
|
private java.util.List<java.lang.String> |
dirsIncluded
The directories which matched at least one include and no excludes and were selected.
|
private java.util.List<java.lang.String> |
dirsNotIncluded
The directories which were found and did not match any includes.
|
private java.lang.String[] |
excludes
The patterns for the files to be excluded.
|
private MatchPatterns |
excludesPatterns |
private java.util.List<java.lang.String> |
filesExcluded
The files which matched at least one include and at least one exclude.
|
private java.util.List<java.lang.String> |
filesIncluded
The files which matched at least one include and no excludes and were selected.
|
private java.util.List<java.lang.String> |
filesNotIncluded
The files which did not match any includes or selectors.
|
private boolean |
followSymlinks
Whether or not symbolic links should be followed.
|
private boolean |
haveSlowResults
Whether or not our results were built by a slow scan.
|
private java.lang.String[] |
includes
The patterns for the files to be included.
|
private MatchPatterns |
includesPatterns |
private boolean |
isCaseSensitive
Whether or not the file system should be treated as a case sensitive one.
|
private ScanConductor.ScanAction |
scanAction
The last ScanAction.
|
private ScanConductor |
scanConductor
A
ScanConductor an control the scanning process. |
Constructor and Description |
---|
DirectoryScanner()
Sole constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addDefaultExcludes()
Adds default exclusions to the current exclusions set.
|
private static <T> java.util.Set<T> |
arrayAsHashSet(T[] array)
Take an array of type T and convert it into a HashSet of type T.
|
(package private) boolean |
couldHoldIncluded(java.lang.String name)
Tests whether or not a name matches the start of at least one include pattern.
|
static DirectoryScanResult |
diffFiles(java.lang.String[] oldFiles,
java.lang.String[] newFiles) |
DirectoryScanResult |
diffIncludedFiles(java.lang.String... oldFiles)
Determine the file differences between the currently included files and
a previously captured list of files.
|
private java.lang.String[] |
doNotFollowSymbolicLinks(java.io.File dir,
java.lang.String vpath,
java.lang.String[] newfiles) |
java.io.File |
getBasedir()
Returns the base directory to be scanned.
|
java.lang.String[] |
getExcludedDirectories()
Returns the names of the directories which matched at least one of the include patterns and at least one of the
exclude patterns.
|
java.lang.String[] |
getExcludedFiles()
Returns the names of the files which matched at least one of the include patterns and at least one of the exclude
patterns.
|
java.lang.String[] |
getIncludedDirectories()
Returns the names of the directories which matched at least one of the include patterns and none of the exclude
patterns.
|
java.lang.String[] |
getIncludedFiles()
Returns the names of the files which matched at least one of the include patterns and none of the exclude
patterns.
|
java.lang.String[] |
getNotIncludedDirectories()
Returns the names of the directories which matched none of the include patterns.
|
java.lang.String[] |
getNotIncludedFiles()
Returns the names of the files which matched none of the include patterns.
|
(package private) boolean |
isExcluded(java.lang.String name)
Tests whether or not a name matches against at least one exclude pattern.
|
(package private) boolean |
isIncluded(java.lang.String name)
Tests whether or not a name matches against at least one include pattern.
|
(package private) boolean |
isSymbolicLink(java.io.File parent,
java.lang.String name)
Checks whether a given file is a symbolic link.
|
void |
scan()
Scans the base directory for files which match at least one include pattern and don't match any exclude patterns.
|
(package private) void |
scandir(java.io.File dir,
java.lang.String vpath,
boolean fast)
Scans the given directory for files and directories.
|
void |
setBasedir(java.io.File basedir)
Sets the base directory to be scanned.
|
void |
setBasedir(java.lang.String basedir)
Sets the base directory to be scanned.
|
void |
setCaseSensitive(boolean isCaseSensitiveParameter)
Sets whether or not the file system should be regarded as case sensitive.
|
void |
setExcludes(java.lang.String... excludes)
Sets the list of exclude patterns to use.
|
void |
setFollowSymlinks(boolean followSymlinks)
Sets whether or not symbolic links should be followed.
|
void |
setIncludes(java.lang.String... includes)
Sets the list of include patterns to use.
|
void |
setScanConductor(ScanConductor scanConductor) |
private void |
setupDefaultFilters() |
private void |
setupMatchPatterns() |
(package private) void |
slowScan()
Top level invocation for a slow scan.
|
public static final java.lang.String[] DEFAULTEXCLUDES
addDefaultExcludes()
private java.io.File basedir
private java.lang.String[] includes
private java.lang.String[] excludes
private MatchPatterns excludesPatterns
private MatchPatterns includesPatterns
private java.util.List<java.lang.String> filesIncluded
private java.util.List<java.lang.String> filesNotIncluded
private java.util.List<java.lang.String> filesExcluded
private java.util.List<java.lang.String> dirsIncluded
private java.util.List<java.lang.String> dirsNotIncluded
private java.util.List<java.lang.String> dirsExcluded
private boolean haveSlowResults
private boolean isCaseSensitive
private boolean followSymlinks
private ScanConductor scanConductor
ScanConductor
an control the scanning process.private ScanConductor.ScanAction scanAction
public void setBasedir(java.lang.String basedir)
File.separatorChar
, so the separator used need not match
File.separatorChar
.basedir
- The base directory to scan. Must not be null
.public void setBasedir(@Nonnull java.io.File basedir)
basedir
- The base directory for scanning. Should not be null
.public java.io.File getBasedir()
public void setCaseSensitive(boolean isCaseSensitiveParameter)
isCaseSensitiveParameter
- whether or not the file system should be regarded as a case sensitive onepublic void setFollowSymlinks(boolean followSymlinks)
followSymlinks
- whether or not symbolic links should be followedpublic void setIncludes(java.lang.String... includes)
File.separatorChar
, so the separator used need not match File.separatorChar
.
When a pattern ends with a '/' or '\', "**" is appended.includes
- 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(java.lang.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
, indicating that no files should be
excluded. If a non-null
list is given, all elements must be non-null
.public void setScanConductor(ScanConductor scanConductor)
scanConductor
- scanConductor
public void scan() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the base directory was set incorrectly (i.e. if it is null
,
doesn't exist, or isn't a directory).public DirectoryScanResult diffIncludedFiles(java.lang.String... oldFiles)
scan()
if no
result of a previous scan could be found.
The result of the diff can be queried by the methods
DirectoryScanResult.getFilesAdded()
and DirectoryScanResult.getFilesRemoved()
oldFiles
- the list of previously captured files names.public static DirectoryScanResult diffFiles(@Nullable java.lang.String[] oldFiles, @Nullable java.lang.String[] newFiles)
oldFiles
- array of old files.newFiles
- array of new files.private static <T> java.util.Set<T> arrayAsHashSet(@Nullable T[] array)
null
or an empty array gets passed, an empty Set will be returned.array
- The arrayvoid slowScan()
void scandir(@Nonnull java.io.File dir, @Nonnull java.lang.String vpath, boolean fast)
dir
- The directory to scan. Must not be null
.vpath
- The path relative to the base directory (needed to prevent problems with an absolute path when using
dir). Must not be null
.fast
- Whether or not this call is part of a fast scan.filesIncluded
,
filesNotIncluded
,
filesExcluded
,
dirsIncluded
,
dirsNotIncluded
,
dirsExcluded
,
slowScan()
private java.lang.String[] doNotFollowSymbolicLinks(java.io.File dir, java.lang.String vpath, java.lang.String[] newfiles)
boolean isIncluded(java.lang.String name)
name
- The name to match. Must not be null
.true
when the name matches against at least one include pattern, or false
otherwise.boolean couldHoldIncluded(@Nonnull java.lang.String name)
name
- The name to match. Must not be null
.true
when the name matches against the start of at least one include pattern, or
false
otherwise.boolean isExcluded(@Nonnull java.lang.String name)
name
- The name to match. Must not be null
.true
when the name matches against at least one exclude pattern, or false
otherwise.public java.lang.String[] getIncludedFiles()
public java.lang.String[] getNotIncludedFiles()
slowScan()
public java.lang.String[] getExcludedFiles()
slowScan()
public java.lang.String[] getIncludedDirectories()
public java.lang.String[] getNotIncludedDirectories()
slowScan()
public java.lang.String[] getExcludedDirectories()
slowScan()
public void addDefaultExcludes()
boolean isSymbolicLink(java.io.File parent, java.lang.String name) throws java.io.IOException
It doesn't really test for symbolic links but whether the canonical and absolute paths of the file are identical - this may lead to false positives on some platforms.
parent
- the parent directory of the file to testname
- the name of the file to test.java.io.IOException
private void setupDefaultFilters()
private void setupMatchPatterns()