com.xmlmind.util
Class GlobFilter

java.lang.Object
  extended by com.xmlmind.util.GlobFilter
All Implemented Interfaces:
java.io.FilenameFilter

public final class GlobFilter
extends java.lang.Object
implements java.io.FilenameFilter

An implementation of java.io.FilenameFilter which accepts file names matching a given glob pattern (as used by Unix shells).

This class is not thread-safe.

Caveat: on Windows, '\' (backslash) is always considered to be the path separator (e.g. C:\foo\bar*\*.xml) and thus, cannot be used to escape special characters in the pattern passed to the constructor and to the convenience functions.


Field Summary
 GlobMatcher globMatcher
          The underlying matcher.
 java.lang.String globPattern
          The string representation of the glob pattern.
 
Constructor Summary
GlobFilter(java.lang.String globPattern)
          Constructs filter using specified glob pattern.
 
Method Summary
 boolean accept(java.io.File dir, java.lang.String name)
           
static GlobFilter create(java.lang.String globPattern)
          Convenience method: creates a filter for specified pattern, but unlike the constructor, this method does not raise an exception if this fails.
static java.io.File[] glob(java.io.File patternsFile)
          Utility used by the glob(String) convenience method: List all the files matching specified glob pattern.
static java.io.File[] glob(java.lang.String patterns)
          Convenience method: List all the files matching specified glob pattern.
static java.io.File resolve(java.lang.String path, java.io.File baseDir, boolean substituteTilde)
          Utility used by the glob(String) convenience method: Resolve specified path against specified base directory.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

globPattern

public final java.lang.String globPattern
The string representation of the glob pattern.


globMatcher

public final GlobMatcher globMatcher
The underlying matcher.

Constructor Detail

GlobFilter

public GlobFilter(java.lang.String globPattern)
           throws java.util.regex.PatternSyntaxException
Constructs filter using specified glob pattern.

Parameters:
globPattern - the glob pattern
Throws:
java.util.regex.PatternSyntaxException - if specified glob pattern cannot be compiled into a java.util.regex.Pattern
Method Detail

accept

public boolean accept(java.io.File dir,
                      java.lang.String name)
Specified by:
accept in interface java.io.FilenameFilter

create

public static GlobFilter create(java.lang.String globPattern)
Convenience method: creates a filter for specified pattern, but unlike the constructor, this method does not raise an exception if this fails. Instead, this method returns null.


glob

public static java.io.File[] glob(java.lang.String patterns)
Convenience method: List all the files matching specified glob pattern.

Parameters:
patterns - a path, relative or absolute, possibly containing one or more glob patterns. Example: "~/src/_?/[A-Z]*.java". If this path is not absolute, it is resolved against the home directory of the user if it starts with '~' and against the current working directory otherwise.
Returns:
a possibly empty array of absolute, canonical files

resolve

public static java.io.File resolve(java.lang.String path,
                                   java.io.File baseDir,
                                   boolean substituteTilde)
Utility used by the glob(String) convenience method: Resolve specified path against specified base directory.

Parameters:
path - path to be made absolute
baseDir - an absolute directory. Resolve path against this base directory. May be null which means: use the current working directory.
substituteTilde - if true and path starts with '~', resolve path against the home directory of the user
Returns:
an absolute, canonical, file

glob

public static java.io.File[] glob(java.io.File patternsFile)
Utility used by the glob(String) convenience method: List all the files matching specified glob pattern.

Parameters:
patternsFile - an absolute, canonical file, whose path possibly contains one or more glob patterns. Example: "/home/john/src/_?/[A-Z]*.java".
Returns:
a possibly empty array of absolute, canonical files.