Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.tools.ant
Class DirectoryScanner

    java.lang.Object
      |
      +--org.apache.tools.ant.DirectoryScanner

All Implemented Interfaces:
  FileScanner

Direct Known Subclasses:
  FTP.FTPDirectoryScanner, VAJWorkspaceScanner, ZipScanner

public class DirectoryScanner
extends java.lang.Object
implements FileScanner

Class for scanning a directory for files/directories that match a certain criteria.

These criteria consist of a set of include and exclude patterns. With these patterns, you can select which files you want to have included, and which files you want to have excluded.

The idea is simple. A given directory is recursively scanned for all files and directories. Each file/directory is matched against a set of include and exclude patterns. Only files/directories that match at least one pattern of the include pattern list, and don't match a pattern of the exclude pattern list will be placed in the list of files/directories found.

When no list of include patterns is supplied, "**" will be used, which means that everything will be matched. When no list of exclude patterns is supplied, an empty list is used, such that nothing will be excluded.

The pattern matching is done as follows: The name to be matched is split up in path segments. A path segment is the name of a directory or file, which is bounded by `File.separator` ('/' under UNIX, '\' under Windows). E.g. "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.

Then the segments of the name and the pattern will be matched against each other. When '**' is used for a path segment in the pattern, then it matches zero or more path segments of the name.

There are special case regarding the use of `File.separator`s at the beginningof the pattern and the string to match:
When a pattern starts with a `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: '*' matches zero or more characters, '?' matches one character.

Examples:

"**\*.class" matches all .class files/dirs in a directory tree.

"test\a??.java" matches all files/dirs which start with an 'a', then two more characters and then ".java", in a directory called test.

"**" matches everything in a directory tree.

"**\test\**\XYZ*" matches all files/dirs that start with "XYZ" and where there is a parent directory called test (e.g. "abc\test\def\ghi\XYZ123").

Example of usage:

       String[] includes = {"**\\*.class"};
       String[] excludes = {"modules\\*\\**"};
       ds.setIncludes(includes);
       ds.setExcludes(excludes);
       ds.setBasedir(new File("test"));
       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 .class files in all directories under a directory called "modules"

Author:
  Arnout J. Kuiper ajkuiper@wxs.nl

Field Summary
`protected java.io.File` | `basedir` The base directory which should be scanned.
`protected static java.lang.String[]` | `DEFAULTEXCLUDES` Patterns that should be excluded by default.
`protected java.util.Vector` | `dirsExcluded` The files that where found and matched at least one includes, and also matched at least one excludes.
`protected java.util.Vector` | `dirsIncluded` The directories that where found and matched at least one includes, and matched no excludes.
`protected java.util.Vector` | `dirsNotIncluded` The directories that where found and did not match any includes.
`protected java.lang.String[]` | `excludes` The patterns for the files that should be excluded.
`protected java.util.Vector` | `filesExcluded` The files that where found and matched at least one includes, and also matched at least one excludes.
`protected java.util.Vector` | `filesIncluded` The files that where found and matched at least one includes, and matched no excludes.
`protected java.util.Vector` | `filesNotIncluded` The files that where found and did not match any includes.
`protected boolean` | `haveSlowResults` Have the Vectors holding our results been built by a slow scan?
`protected java.lang.String[]` | `includes` The patterns for the files that should be included.

Constructor Summary
`DirectoryScanner()` Constructor.

Method Summary
`void` | `addDefaultExcludes()` Adds the array with default exclusions to the current exclusions set.
`protected boolean` | `couldHoldIncluded(java.lang.String name)` Tests whether a name matches the start of at least one include pattern.
`java.io.File` | `getBasedir()` Gets the basedir that is used for scanning.
`java.lang.String[]` | `getExcludedDirectories()` Get the names of the directories that matched at least one of the include patterns, an matched also at least one of the exclude patterns.
`java.lang.String[]` | `getExcludedFiles()` Get the names of the files that matched at least one of the include patterns, an matched also at least one of the exclude patterns.
`java.lang.String[]` | `getIncludedDirectories()` Get the names of the directories that matched at least one of the include patterns, an matched none of the exclude patterns.
`java.lang.String[]` | `getIncludedFiles()` Get the names of the files that matched at least one of the include patterns, an matched none of the exclude patterns.
`java.lang.String[]` | `getNotIncludedDirectories()` Get the names of the directories that matched at none of the include patterns.
`java.lang.String[]` | `getNotIncludedFiles()` Get the names of the files that matched at none of the include patterns.
`protected boolean` | `isExcluded(java.lang.String name)` Tests whether a name matches against at least one exclude pattern.
`protected boolean` | `isIncluded(java.lang.String name)` Tests whether a name matches against at least one include pattern.
`protected static boolean` | `match(java.lang.String pattern, java.lang.String str)` Matches a string against a pattern.
`protected static boolean` | `matchPath(java.lang.String pattern, java.lang.String str)` Matches a path against a pattern.
`protected static boolean` | `matchPatternStart(java.lang.String pattern, java.lang.String str)` Does the path match the start of this pattern up to the first "**".
`void` | `scan()` Scans the base directory for files that match at least one include pattern, and don't match any exclude patterns.
`protected void` | `scandir(java.io.File dir, java.lang.String vpath, boolean fast)` Scans the passed dir for files and directories.
`void` | `setBasedir(java.io.File basedir)` Sets the basedir for scanning.
`void` | `setBasedir(java.lang.String basedir)` Sets the basedir for scanning.
`void` | `setExcludes(java.lang.String[] excludes)` Sets the set of exclude patterns to use.
`void` | `setIncludes(java.lang.String[] includes)` Sets the set of include patterns to use.
`protected void` | `slowScan()` Toplevel invocation for the scan.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### DEFAULTEXCLUDES

    protected static final java.lang.String[] DEFAULTEXCLUDES

  Patterns that should be excluded by default.
  See Also:
    `addDefaultExcludes()`

### basedir

    protected java.io.File basedir

  The base directory which should be scanned.

### includes

    protected java.lang.String[] includes

  The patterns for the files that should be included.

### excludes

    protected java.lang.String[] excludes

  The patterns for the files that should be excluded.

### filesIncluded

    protected java.util.Vector filesIncluded

  The files that where found and matched at least one includes, and matched no excludes.

### filesNotIncluded

    protected java.util.Vector filesNotIncluded

  The files that where found and did not match any includes.

### filesExcluded

    protected java.util.Vector filesExcluded

  The files that where found and matched at least one includes, and also matched at least one excludes.

### dirsIncluded

    protected java.util.Vector dirsIncluded

  The directories that where found and matched at least one includes, and matched no excludes.

### dirsNotIncluded

    protected java.util.Vector dirsNotIncluded

  The directories that where found and did not match any includes.

### dirsExcluded

    protected java.util.Vector dirsExcluded

  The files that where found and matched at least one includes, and also matched at least one excludes.

### haveSlowResults

    protected boolean haveSlowResults

  Have the Vectors holding our results been built by a slow scan?

Constructor Detail

### DirectoryScanner

    public DirectoryScanner()

  Constructor.

Method Detail

### matchPatternStart

    protected static boolean matchPatternStart(java.lang.String pattern,
                                               java.lang.String str)

  Does the path match the start of this pattern up to the first "**". +

  This is not a general purpose test and should only be used if you can live with false positives.

  `pattern=**\\a` and `str=b` will yield true.

  Parameters:
    `pattern` - the (non-null) pattern to match against
    `str` - the (non-null) string (path) to match

### matchPath

    protected static boolean matchPath(java.lang.String pattern,
                                       java.lang.String str)

  Matches a path against a pattern.
  Parameters:
    `pattern` - the (non-null) pattern to match against
    `str` - the (non-null) string (path) to match
  Returns:
    `true` when the pattern matches against the string. `false` otherwise.

### match

    protected static boolean match(java.lang.String pattern,
                                   java.lang.String str)

  Matches a string against a pattern. The pattern contains two special characters: '*' which means zero or more characters, '?' which means one and only one character.
  Parameters:
    `pattern` - the (non-null) pattern to match against
    `str` - the (non-null) string that must be matched against the pattern
  Returns:
    `true` when the string matches against the pattern, `false` otherwise.

### setBasedir

    public void setBasedir(java.lang.String basedir)

  Sets the basedir for scanning. This is the directory that is scanned recursively. All '/' and '\' characters are replaced by `File.separatorChar`. So the separator used need not match `File.separatorChar`.
  Specified by:
    `setBasedir` in interface `FileScanner`
  Parameters:
    `basedir` - the (non-null) basedir for scanning

### setBasedir

    public void setBasedir(java.io.File basedir)

  Sets the basedir for scanning. This is the directory that is scanned recursively.
  Specified by:
    `setBasedir` in interface `FileScanner`
  Parameters:
    `basedir` - the basedir for scanning

### getBasedir

    public java.io.File getBasedir()

  Gets the basedir that is used for scanning. This is the directory that is scanned recursively.
  Specified by:
    `getBasedir` in interface `FileScanner`
  Returns:
    the basedir that is used for scanning

### setIncludes

    public void setIncludes(java.lang.String[] includes)

  Sets the set of include patterns to use. All '/' and '\' characters are replaced by `File.separatorChar`. So the separator used need not match `File.separatorChar`.

  When a pattern ends with a '/' or '\', "**" is appended.

  Specified by:
    `setIncludes` in interface `FileScanner`
  Parameters:
    `includes` - list of include patterns

### setExcludes

    public void setExcludes(java.lang.String[] excludes)

  Sets the set of exclude patterns to use. All '/' and '\' characters are replaced by `File.separatorChar`. So the separator used need not match `File.separatorChar`.

  When a pattern ends with a '/' or '\', "**" is appended.

  Specified by:
    `setExcludes` in interface `FileScanner`
  Parameters:
    `excludes` - list of exclude patterns

### scan

    public void scan()

  Scans the base directory for files that match at least one include pattern, and don't match any exclude patterns.
  Specified by:
    `scan` in interface `FileScanner`
  Throws:
    `java.lang.IllegalStateException` - when basedir was set incorrecly

### slowScan

    protected void slowScan()

  Toplevel invocation for the scan.

  Returns immediately if a slow scan has already been requested.

### scandir

    protected void scandir(java.io.File dir,
                           java.lang.String vpath,
                           boolean fast)

  Scans the passed dir for files and directories. Found files and directories are placed in their respective collections, based on the matching of includes and excludes. When a directory is found, it is scanned recursively.
  Parameters:
    `dir` - the directory to scan
    `vpath` - the path relative to the basedir (needed to prevent problems with an absolute path when using dir)
  See Also:
    `filesIncluded`, `filesNotIncluded`, `filesExcluded`, `dirsIncluded`, `dirsNotIncluded`, `dirsExcluded`

### isIncluded

    protected boolean isIncluded(java.lang.String name)

  Tests whether a name matches against at least one include pattern.
  Parameters:
    `name` - the name to match
  Returns:
    `true` when the name matches against at least one include pattern, `false` otherwise.

### couldHoldIncluded

    protected boolean couldHoldIncluded(java.lang.String name)

  Tests whether a name matches the start of at least one include pattern.
  Parameters:
    `name` - the name to match
  Returns:
    `true` when the name matches against at least one include pattern, `false` otherwise.

### isExcluded

    protected boolean isExcluded(java.lang.String name)

  Tests whether a name matches against at least one exclude pattern.
  Parameters:
    `name` - the name to match
  Returns:
    `true` when the name matches against at least one exclude pattern, `false` otherwise.

### getIncludedFiles

    public java.lang.String[] getIncludedFiles()

  Get the names of the files that matched at least one of the include patterns, an matched none of the exclude patterns. The names are relative to the basedir.
  Specified by:
    `getIncludedFiles` in interface `FileScanner`
  Returns:
    the names of the files

### getNotIncludedFiles

    public java.lang.String[] getNotIncludedFiles()

  Get the names of the files that matched at none of the include patterns. The names are relative to the basedir.
  Specified by:
    `getNotIncludedFiles` in interface `FileScanner`
  Returns:
    the names of the files

### getExcludedFiles

    public java.lang.String[] getExcludedFiles()

  Get the names of the files that matched at least one of the include patterns, an matched also at least one of the exclude patterns. The names are relative to the basedir.
  Specified by:
    `getExcludedFiles` in interface `FileScanner`
  Returns:
    the names of the files

### getIncludedDirectories

    public java.lang.String[] getIncludedDirectories()

  Get the names of the directories that matched at least one of the include patterns, an matched none of the exclude patterns. The names are relative to the basedir.
  Specified by:
    `getIncludedDirectories` in interface `FileScanner`
  Returns:
    the names of the directories

### getNotIncludedDirectories

    public java.lang.String[] getNotIncludedDirectories()

  Get the names of the directories that matched at none of the include patterns. The names are relative to the basedir.
  Specified by:
    `getNotIncludedDirectories` in interface `FileScanner`
  Returns:
    the names of the directories

### getExcludedDirectories

    public java.lang.String[] getExcludedDirectories()

  Get the names of the directories that matched at least one of the include patterns, an matched also at least one of the exclude patterns. The names are relative to the basedir.
  Specified by:
    `getExcludedDirectories` in interface `FileScanner`
  Returns:
    the names of the directories

### addDefaultExcludes

    public void addDefaultExcludes()

  Adds the array with default exclusions to the current exclusions set.
  Specified by:
    `addDefaultExcludes` in interface `FileScanner`

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2000 Apache Software Foundation. All Rights Reserved.