org.pf.file
Class ExtendedFileFilter

java.lang.Object
  extended by org.pf.file.ExtendedFileFilter
All Implemented Interfaces:
java.io.FilenameFilter, IObjectFilter

public class ExtendedFileFilter
extends java.lang.Object
implements java.io.FilenameFilter, IObjectFilter

This filter implements the standard pattern matching on UNIX and Windows platforms. It supports the wildcards '*' and '?' on file names.
It allows to set more than one pattern. Apart from that it allows control over inclusion/exclusion of directories independently from name patterns.

Version:
1.3
Author:
Manfred Duchrow

Field Summary
static java.lang.String PATTERN_SEPARATOR
          The character to be used to separate filename patterns (';') as String.
 
Constructor Summary
ExtendedFileFilter()
          Initialize the new instance with default values.
ExtendedFileFilter(boolean restrictive)
          Initialize the new instance with the option of a more restrictive handling of wildcard matching.
ExtendedFileFilter(char wildcardForDigits)
          Initialize the new instance with a wildcard character for digits.
ExtendedFileFilter(char wildcardForDigits, boolean restrictive)
          Initialize the new instance with a wildcard character for digits in filenames and the option of a more restrictive handling of wildcard matching.
ExtendedFileFilter(java.lang.String patternList)
          Initialize the new instance with patterns.
 
Method Summary
 boolean accept(java.io.File dir, java.lang.String name)
          Tests if a specified file should be included in a file list.
 void addPattern(java.lang.String pattern)
          Adds a pattern.
 void addPattern(java.lang.String pattern, boolean ignoreCase)
          Adds a pattern.
 void addPattern(java.lang.String pattern, boolean ignoreCase, char digitWildcardChar)
          Adds a pattern.
 void addPattern(java.lang.String pattern, char digitWildcardChar)
          Adds a pattern.
 void addPatterns(java.lang.String patternList)
          Adds one or more patterns separated by semi-colon (';').
 void alwaysExcludeDirectories()
          Sets the filter to never accept directories.
 void alwaysIncludeDirectories()
          Sets the filter to always accept directories, even if they don't match a given pattern.
 void checkNameOfDirectories()
          Sets the filter to only accept directories that match a defined pattern.
 boolean matches(java.lang.Object object)
          Returns if this filter matches the given object.
 boolean mustExcludeDirectories()
          Returns true if the filter never accepts directories.
 boolean mustIncludeDirectories()
          Returns true if the filter always accepts directories, even if they don't match a given pattern.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

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
Constructor Detail

ExtendedFileFilter

public ExtendedFileFilter()
Initialize the new instance with default values.
The default for "restrictive" is false! See other constructor for explanation.


ExtendedFileFilter

public ExtendedFileFilter(java.lang.String patternList)
Initialize the new instance with patterns.

Parameters:
patternList - A list of patterns (may be just one)
See Also:
addPatterns(String)

ExtendedFileFilter

public ExtendedFileFilter(char wildcardForDigits)
Initialize the new instance with a wildcard character for digits.
The default for "restrictive" is false! See other constructor for explanation.

Parameters:
wildcardForDigits - The character that stands for any digit (0-9) in a filename

ExtendedFileFilter

public ExtendedFileFilter(boolean restrictive)
Initialize the new instance with the option of a more restrictive handling of wildcard matching. If the argument restrictive is true, empty strings won't match the '*' wildcard character.

Example: restrictive=true => "text.doc" does NOT match the pattern "text*.doc"
restrictive=false => "text.doc" does match the pattern "text*.doc"

Parameters:
restrictive - To restrict the match of '*' to at least one character

ExtendedFileFilter

public ExtendedFileFilter(char wildcardForDigits,
                          boolean restrictive)
Initialize the new instance with a wildcard character for digits in filenames and the option of a more restrictive handling of wildcard matching. If the argument restrictive is true, empty strings won't match the '*' wildcard character.

See other constructor for examples.

Parameters:
wildcardForDigits - The character that stands for any digit (0-9) in a filename
restrictive - To restrict the match of '*' to at least one character
Method Detail

addPattern

public void addPattern(java.lang.String pattern)
Adds a pattern. All filenames match this pattern are acceptable.
Case sensitivity is switched on !

Parameters:
pattern - The pattern string containing optional wildcards ( '*', '?' )

addPattern

public void addPattern(java.lang.String pattern,
                       char digitWildcardChar)
Adds a pattern. All filenames match this pattern are acceptable.
Case sensitivity is switched on ! The second parameter specifies a character that will be recognized in the pattern as a placeholder for a single digit character.

A patterb "XX-####.log" with a digitWildcard set to '#' wil match to "XX-2000.log" and "XX-7376.log" but not to "XX-C363.log" and "XX-dddd.log".

Parameters:
pattern - The pattern string containing optional wildcards ( '*', '?' )
digitWildcardChar - The character that will be treated as wildcard for digits ('0'-'9')

addPattern

public void addPattern(java.lang.String pattern,
                       boolean ignoreCase)
Adds a pattern. All filenames match this pattern are acceptable.

Parameters:
pattern - The pattern string containing optional wildcards ( '*', '?' )
ignoreCase - If true, all character comparisons are ignoring uppercase/lowercase

addPattern

public void addPattern(java.lang.String pattern,
                       boolean ignoreCase,
                       char digitWildcardChar)
Adds a pattern. All filenames that match this pattern are acceptable. Additionally to the standard wildcards '*' and '?' a wildcard for single digit characters ('0' - '9') can be specified here.

Parameters:
pattern - The pattern string containing optional wildcards ( '*', '?' )
ignoreCase - If true, all character comparisons are ignoring uppercase/lowercase
digitWildcardChar - The character that will be treated as wildcard for digits ('0'-'9')

addPatterns

public void addPatterns(java.lang.String patternList)
Adds one or more patterns separated by semi-colon (';').

Parameters:
patternList - The list of filename patterns

checkNameOfDirectories

public void checkNameOfDirectories()
Sets the filter to only accept directories that match a defined pattern.


alwaysIncludeDirectories

public void alwaysIncludeDirectories()
Sets the filter to always accept directories, even if they don't match a given pattern.


alwaysExcludeDirectories

public void alwaysExcludeDirectories()
Sets the filter to never accept directories.


accept

public boolean accept(java.io.File dir,
                      java.lang.String name)
Tests if a specified file should be included in a file list.

Specified by:
accept in interface java.io.FilenameFilter
Parameters:
dir - the directory in which the file was found.
name - the name of the file.
Returns:
true if and only if the name should be included in the file list, false otherwise.

matches

public boolean matches(java.lang.Object object)
Returns if this filter matches the given object. Currently the supported object types are java.lang.String (i.e. a filename), java.io.File, org.pf.file.FileLocator (not for remote files), org.pf.file.FileInfo.

Specified by:
matches in interface IObjectFilter
Parameters:
object - Any object
Returns:
true if the object matches this filter

mustIncludeDirectories

public boolean mustIncludeDirectories()
Returns true if the filter always accepts directories, even if they don't match a given pattern.


mustExcludeDirectories

public boolean mustExcludeDirectories()
Returns true if the filter never accepts directories.