public class FilenamePattern extends Object implements Cloneable, FileFilter, FilenameFilter
A filename pattern is a kind of regular expression that is used for matching filenames.
The matches()
method is used to match a pattern
against a filename. A static matches()
method is also provided for convenience.
Methods accept(File)
and
accept(File, String)
are also provided, which
implement the java.io.FileFilter and java.io.FilenameFilter
interfaces, respectively.
Filename Pattern Syntax
A filename pattern is composed of regular characters and special pattern matching characters, which are:
The special pattern matching characters listed above are the default settings
for a given filename pattern matcher object, but these can be changed by
calling method setSpecialChar()
.
By default, a filename pattern matcher performs case-sensitive matching
depending on the underlying operating system (e.g., matching is case-sensitive
for Unix, but case-insensitive for MS/Windows). This behavior can be changed
by calling method setIgnoreCase()
.
An empty string ("") is not a valid filename pattern.
No filename pattern matches an empty filename ("").
Examples
References
This pattern matching implementation is based on the filename pattern matching of Unix (POSIX).
This pattern matching implementation is not based on the regular expression matching capabilities provided by the Java standard library (in JRE 1.4+, see java.lang.String.matches() for details).
Future Enhancements
A future revision of this code may support a special pattern of "**", which matches zero or more directory path prefixes.
Modifier and Type | Field and Description |
---|---|
protected static char |
DIR_SEP1
System-dependent directory path separator character (1).
|
protected static char |
DIR_SEP2
System-dependent directory path separator character (2).
|
protected static boolean |
IGNORE_CASE
System-dependent filename case-sensitivity flag.
|
protected char |
m_any
Special pattern character: Match any single character.
|
protected char |
m_closure
Special pattern character: Match zero or more characters.
|
protected char |
m_dirSep
Special pattern character: Directory path separator.
|
protected char |
m_escape
Special pattern character: Escape any special meaning.
|
protected boolean |
m_hasDirSep
This pattern contains directory path separator characters.
|
protected boolean |
m_ignoreCase
Ignore case when matching filenames.
|
protected char |
m_negate
Special pattern character: Negation.
|
protected char[] |
m_pat
Filename pattern.
|
protected char |
m_setClose
Special pattern character: Character set close.
|
protected char |
m_setExcl
Special pattern character: Character set exclusion.
|
protected char |
m_setOpen
Special pattern character: Character set open.
|
protected char |
m_setThru
Special pattern character: Character set range.
|
static char |
PAT_ANY
Default pattern character: Any single character.
|
static char |
PAT_CLOSURE
Default pattern character: Zero or more characters.
|
static char |
PAT_DIR_SEP
Default pattern character: Directory path separator.
|
static char |
PAT_ESCAPE
Default pattern character: Escape any special meaning.
|
static char |
PAT_NEGATE
Default pattern character: Negation.
|
static char |
PAT_SET_CLOSE
Default pattern character: Character set close.
|
static char |
PAT_SET_EXCL
Default pattern character: Character set exclusion.
|
static char |
PAT_SET_OPEN
Default pattern character: Character set open.
|
static char |
PAT_SET_THRU
Default pattern character: Character set range.
|
Constructor and Description |
---|
FilenamePattern()
Default constructor.
|
FilenamePattern(String pat)
Constructor.
|
FilenamePattern(String pat,
boolean ignCase)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
accept(File fname)
Determine if a filename matches the filename pattern.
|
boolean |
accept(File dir,
String fname)
Determine if a filename matches the filename pattern.
|
Object |
clone()
Clone this filename pattern matcher.
|
protected static boolean |
compareCharIgnoreCase(char ch,
char lo,
char hi)
Determine if a character falls within a given character range, ignoring
case.
|
boolean |
equals(Object obj)
Compare this object to another.
|
String |
getPattern()
Retrieve the pattern for the filename pattern matcher.
|
int |
hashCode()
Calculate a hash code for this object.
|
protected boolean |
isValidPattern(String pat)
Determine if a filename pattern is well-formed.
|
static void |
main(String[] args)
Test driver for this class.
|
boolean |
matches(String fname)
Determine if a filename matches the filename pattern.
|
static boolean |
matches(String pat,
String fname)
Determine if a filename matches a filename pattern.
|
protected int |
matchSubpattern(int patOff,
String fname,
int fnameOff)
Determine if a filename substring matches a pattern substring.
|
boolean |
setIgnoreCase(boolean flag)
Establish whether or not the filename pattern matcher ignores case.
|
void |
setPattern(String pat)
Establish the pattern for the filename pattern matcher.
|
char |
setSpecialChar(int type,
char ch)
Modify a special pattern matching character for the filename pattern
matcher.
|
public static final char PAT_ESCAPE
public static final char PAT_ANY
public static final char PAT_CLOSURE
public static final char PAT_SET_OPEN
public static final char PAT_SET_CLOSE
public static final char PAT_SET_THRU
public static final char PAT_SET_EXCL
public static final char PAT_DIR_SEP
public static final char PAT_NEGATE
protected static final char DIR_SEP1
protected static final char DIR_SEP2
protected static final boolean IGNORE_CASE
protected char[] m_pat
protected boolean m_ignoreCase
protected boolean m_hasDirSep
protected char m_escape
protected char m_any
protected char m_closure
protected char m_setOpen
protected char m_setClose
protected char m_setThru
protected char m_setExcl
protected char m_dirSep
protected char m_negate
public FilenamePattern()
setPattern()
public FilenamePattern(String pat)
pat
- A filename pattern.IllegalArgumentException
- (unchecked)
Thrown if pat is malformed.NullPointerException
- (unchecked)
Thrown if pat is null.setPattern()
public FilenamePattern(String pat, boolean ignCase)
pat
- A filename pattern.ignCase
- If true, case is ignored when matching alphabetic characters in filenames
(e.g, the names "abc", "Abc", and "ABC" all
compare equal); if false, filename comparisons are case-sensitive and
alphabetic characters must match exactly.IllegalArgumentException
- (unchecked)
Thrown if pat is malformed.NullPointerException
- (unchecked)
Thrown if pat is null.setPattern()
,
setIgnoreCase()
public static boolean matches(String pat, String fname)
Note
The filename is matched against a filename pattern matcher that uses the default settings for its special pattern matching characters.
pat
- A filename pattern string.fname
- A filename to compare against pat.
Note that empty filenames ("") are not matched by any filename
pattern.IllegalArgumentException
- (unchecked)
Thrown if pat is malformed.NullPointerException
- (unchecked)
Thrown if pat or fname is null.public static void main(String[] args)
Usage
java tribble.util.FilenamePattern pattern filename result
Where pattern is a filename pattern,
filename is a filename,
and result is the expected result, which is one of:
args
- Command line arguments.protected static boolean compareCharIgnoreCase(char ch, char lo, char hi)
Note that this method might exhibit slightly different behavior than the String.compareToIgnoreCase() method for certain regional characters.
ch
- A character to compare.lo
- The lower character value in the range to compare.hi
- The upper character value in the range to compare.matchSubpattern()
public String getPattern()
setPattern()
public void setPattern(String pat)
This method is provided so that this filename pattern matcher can be reused with different patterns.
pat
- A filename pattern.
This is verified to be a well-formed pattern, using the current special
character settings of the filename pattern.IllegalArgumentException
- (unchecked)
Thrown if pat is malformed.NullPointerException
- (unchecked)
Thrown if pat is null.getPattern()
public char setSpecialChar(int type, char ch)
This method should be called prior to calling
setPattern()
.
type
- The type of the special pattern matching character to be modified, which
is one of the PAT_XXX
constants.ch
- The character to use as the special pattern matching character.
If this character is '\0' (NUL), the pattern matcher
will not recognize any character as the special pattern matching character
of type type, effectively disabling that kind of pattern
matching.IllegalArgumentException
- (unchecked)
Thrown if type is not a valid special pattern character type.public boolean setIgnoreCase(boolean flag)
flag
- If true, upper and lower case letters are considered to be identical when
matching filenames (e.g., filenames "abc", "Abc", and
"ABC" will all be considered to be the same filename).
If false, the pattern matcher is case-sensitive, and equivalent upper and
lower case letters are considered to be different.public boolean matches(String fname)
fname
- A filename to match against the filename pattern.
Note that empty filenames ("") are not matched by any filename
pattern.NullPointerException
- (unchecked)
Thrown if fname is null, or if this pattern matcher does not have
a filename pattern established.public boolean accept(File dir, String fname)
If this filename pattern matcher contains directory separator characters, it attempts to match the full pathname formed by concatenating the directory name dir with the filename fname. Otherwise it attempts to match only the filename, ignoring the directory name.
accept
in interface FilenameFilter
dir
- The pathname of the directory containing fname.
(This pattern matcher implementation ignores this argument.)fname
- A filename to match against the pattern.public boolean accept(File fname)
If this filename pattern matcher contains directory separator characters, it attempts to match the entire pathname of the given filename. Otherwise it attempts to match only the filename portion, ignoring the directory prefix.
accept
in interface FileFilter
fname
- A filename to match against the pattern.public boolean equals(Object obj)
Note
Two filename pattern matchers are considered the same (equal) if they have identical internal patterns and identical control settings.
equals
in class Object
obj
- Another object to compare this one to.
If obj is not a FilenamePattern
or one of its subtypes,
it cannot be equal to this object.public int hashCode()
Note
Two filename pattern matchers are considered the same (equal) if they have identical internal patterns and identical control settings.
public Object clone()
Note
The internal pattern is shared by both this pattern matcher and the newly
created clone object. If the internal pattern is to be modified by a
method in a subclass of this class, this method must be overridden so that
the clone gets its own copy of the internal pattern which it can then
freely modify. (Calling method setPattern()
endows a
pattern matcher with an entirely new internal pattern, of course).
protected boolean isValidPattern(String pat)
pat
- A filename pattern to verify.setSpecialChar()
protected int matchSubpattern(int patOff, String fname, int fnameOff)
Note
The pattern string (m_pat
) should be well-formed, otherwise the
pattern matching algorithm will become confused.
patOff
- The index of the first (leftmost) character within the pattern to match.fname
- A filename string to compare against the pattern.
Note that empty filenames ("") are not matched by any filename
pattern.fnameOff
- The index of the first (leftmost) character within string fname
to match.matches()
,
compareCharIgnoreCase()
Copyright © 2024. All rights reserved.