|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object tribble.util.FilenamePattern
public class FilenamePattern
Filename pattern matcher.
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.
Field Summary | |
---|---|
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 Summary | |
---|---|
FilenamePattern()
Default constructor. |
|
FilenamePattern(java.lang.String pat)
Constructor. |
|
FilenamePattern(java.lang.String pat,
boolean ignCase)
Constructor. |
Method Summary | |
---|---|
boolean |
accept(java.io.File fname)
Determine if a filename matches the filename pattern. |
boolean |
accept(java.io.File dir,
java.lang.String fname)
Determine if a filename matches the filename pattern. |
java.lang.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(java.lang.Object obj)
Compare this object to another. |
java.lang.String |
getPattern()
Retrieve the pattern for the filename pattern matcher. |
int |
hashCode()
Calculate a hash code for this object. |
protected boolean |
isValidPattern(java.lang.String pat)
Determine if a filename pattern is well-formed. |
boolean |
matches(java.lang.String fname)
Determine if a filename matches the filename pattern. |
static boolean |
matches(java.lang.String pat,
java.lang.String fname)
Determine if a filename matches a filename pattern. |
protected int |
matchSubpattern(int patOff,
java.lang.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(java.lang.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. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
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
Constructor Detail |
---|
public FilenamePattern()
setPattern()
public FilenamePattern(java.lang.String pat)
pat
- A filename pattern.
java.lang.IllegalArgumentException
- (unchecked)
Thrown if pat is malformed.
java.lang.NullPointerException
- (unchecked)
Thrown if pat is null.setPattern()
public FilenamePattern(java.lang.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.
java.lang.IllegalArgumentException
- (unchecked)
Thrown if pat is malformed.
java.lang.NullPointerException
- (unchecked)
Thrown if pat is null.setPattern()
,
setIgnoreCase()
Method Detail |
---|
public static boolean matches(java.lang.String pat, java.lang.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.
java.lang.IllegalArgumentException
- (unchecked)
Thrown if pat is malformed.
java.lang.NullPointerException
- (unchecked)
Thrown if pat or fname is null.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 java.lang.String getPattern()
setPattern()
public void setPattern(java.lang.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.
java.lang.IllegalArgumentException
- (unchecked)
Thrown if pat is malformed.
java.lang.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.
java.lang.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(java.lang.String fname)
fname
- A filename to match against the filename pattern.
Note that empty filenames ("") are not matched by any filename
pattern.
java.lang.NullPointerException
- (unchecked)
Thrown if fname is null, or if this pattern matcher does not have
a filename pattern established.public boolean accept(java.io.File dir, java.lang.String fname)
If the filename pattern contains directory separator characters, this 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 java.io.FilenameFilter
dir
- The pathname of the directory containing fname.fname
- A filename to match against the pattern.
public boolean accept(java.io.File fname)
If the filename pattern contains directory separator characters, this 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 java.io.FileFilter
fname
- A filename to match against the pattern.
public boolean equals(java.lang.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 java.lang.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.
hashCode
in class java.lang.Object
public java.lang.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).
clone
in class java.lang.Object
protected boolean isValidPattern(java.lang.String pat)
pat
- A filename pattern to verify.
setSpecialChar()
protected int matchSubpattern(int patOff, java.lang.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()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |