org.pf.file
Class FileWalker

java.lang.Object
  extended by org.pf.file.FileWalker

public class FileWalker
extends java.lang.Object

This class provides services to navigate through a file directory and handle files that match a name filter.

Version:
1.6
Author:
Manfred Duchrow

Field Summary
static java.lang.String PATTERN_SEPARATOR
          The character to be used to separate filename patterns (';') as String.
static char PATTERN_SEPARATOR_CHAR
          The character to be used to separate filename patterns (';').
static java.lang.String RECURSIVE_DIR_WILDCARD
          The wildcard pattern that indicates a recursive walkthrough in a single string definition (i.e. "**").
 
Constructor Summary
FileWalker(AFileProcessor processor)
          Initialize the new instance with a file processor.
FileWalker(FileHandler handler)
          Initialize the new instance with a file handler.
FileWalker(FileHandler handler, char digitWildcard)
          Initialize the new instance with a file handler and a wildcard character for digits.
 
Method Summary
 void setDigitWildcardChar(char digitWildcard)
          Sets the given character as a wildcard character to match digits ('0'-'9') only.
 long walkThrough(java.lang.String searchPattern)
          Walks through all directories specified by the given pattern and calls the file handler or file processor for each file that matches the file part in the given pattern.
 long walkThrough(java.lang.String dir, java.io.FilenameFilter filter, boolean recursive)
          Walks through the given directory and optionally through all its sub-directories as well.
 long walkThrough(java.lang.String dir, java.lang.String pattern, boolean recursive)
          This method starts in the given directory to search for all files matching the given pattern(s).
 long walkThroughDirectories(java.lang.String startDir, java.io.FilenameFilter filter, boolean recursive)
          Walks through the given start directory and optionally through all its sub-directories as well.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PATTERN_SEPARATOR_CHAR

public static final char PATTERN_SEPARATOR_CHAR
The character to be used to separate filename patterns (';').

See Also:
Constant Field Values

PATTERN_SEPARATOR

public static final java.lang.String PATTERN_SEPARATOR
The character to be used to separate filename patterns (';') as String.

See Also:
Constant Field Values

RECURSIVE_DIR_WILDCARD

public static final java.lang.String RECURSIVE_DIR_WILDCARD
The wildcard pattern that indicates a recursive walkthrough in a single string definition (i.e. "**").

See Also:
Constant Field Values
Constructor Detail

FileWalker

public FileWalker(AFileProcessor processor)
Initialize the new instance with a file processor.


FileWalker

public FileWalker(FileHandler handler)
Initialize the new instance with a file handler.


FileWalker

public FileWalker(FileHandler handler,
                  char digitWildcard)
Initialize the new instance with a file handler and a wildcard character for digits.

Parameters:
handler - The file handler that gets all found files
digitWildcard - A character that is used as wildcard for digits in filname patterns
Method Detail

walkThrough

public long walkThrough(java.lang.String dir,
                        java.lang.String pattern,
                        boolean recursive)
This method starts in the given directory to search for all files matching the given pattern(s).
There can be more than one pattern in the pattern parameter. They have to be separated by the PATTERN_SEPARATOR (';').

If recursive is true it goes down to each subdirectory and doing the same there.
For each matching file (non-directory) the defined FileHandler.handle() is called.

Parameters:
dir - The directory where to start
pattern - The file name pattern(s) for filtering out the correct files ( wildcards '*' and '?' )
recursive - If set to true, the file selection is going down to all subdirectories
Returns:
the number of found files, that have matched the given pattern.

walkThrough

public long walkThrough(java.lang.String searchPattern)
Walks through all directories specified by the given pattern and calls the file handler or file processor for each file that matches the file part in the given pattern.
The pattern may contain '**' in the path to define recursive walk through. The file part of the pattern may contain '*' and '?' and optionally the digit wildcard character for the files to be passed to the file handler or processor.

Examples:
"prog/lib\/**\/a*.jar" ==> Starts in directory prog\lib and walks recursive through all sub-directories to pass all files that match pattern "a*.jar" to the file handler.
"d:/temp/tmp???.doc" ==> Passes all files that match pattern "tmp???.doc" to the file handler.

Parameters:
searchPattern - The pattern that specifies the path and files to walk through

walkThrough

public long walkThrough(java.lang.String dir,
                        java.io.FilenameFilter filter,
                        boolean recursive)
Walks through the given directory and optionally through all its sub-directories as well. Calls the file handler for each file that matches the given filter.

Parameters:
dir - The directory to start from
filter - The filter that decides whether or not a file is passed to the FileHandler
recursive - If true all subdirectories are scanned as well

walkThroughDirectories

public long walkThroughDirectories(java.lang.String startDir,
                                   java.io.FilenameFilter filter,
                                   boolean recursive)
Walks through the given start directory and optionally through all its sub-directories as well. Calls the file handler for each directory (not files) that matches the given filter. However, it iterates over all directories, that is even those that do not match the filter.

Parameters:
startDir - The directory to start from
filter - The filter that decides whether or not a directory is passed to the FileHandler
recursive - If true all subdirectories are scanned as well

setDigitWildcardChar

public void setDigitWildcardChar(char digitWildcard)
Sets the given character as a wildcard character to match digits ('0'-'9') only.

Parameters:
digitWildcard - The placeholder character for digits