Loosely modeled on java.util.regex.Pattern, this class provides a simple mechanism for expanding UNIX-style pathname patterns into a list of pathnames for filesystem objects. Depending on the flags supplied when a pattern is compiled, the following pattern constructs are available:
- A star ("*") matches zero or more characters.
- A question mark ("?") matches exactly one character.
- A matching pair of square brackets ("[]") denote a character class. The character class "[abz]" matches one of "a", "b" or "z". Ranges are allowed, so that "[0-9A-F]" matches a hexadecimal digit. If the first character of a character class is "!" or "^", the character class is negated; i.e. "[^a-zA-Z]" matches any character that is not an ASCII letter.
- A single quote ("'") causes characters up to the next "'" to be treated as literal characters.
- A backslash ("\") causes the next character (even a single quote) to be treated as a literal character; i.e. any special meaning.
Patterns are first split into file components on "/" boundaries, then the sub-patterns are used to match names in a given directory. Neither quoting or escaping affect "/" interpretation, and a "/" in a character class causes it to be treated as literal characters.
The pattern expander treats "dot" files (i.e. files starting with ".") as hidden. A hidden file is only matched when the pattern has an explicit "." as the first character of a component. Thus the pattern "*" does not match "." or "..", but the pattern ".*" does.
This class also exposes a static method for compiling patterns in the UNIX shell-style syntax to Java {@link Pattern} objects. The resultingobjects allow you to use the shell-style syntax for matching arbitrary strings. The pathname-specific matching behaviors of PathnamePattern such as implicit anchoring, and the handling of '/' in character classes are supported via flags.
TODO:
- Provide a method that returns a "lazy" pathname iterator for cases where we don't want to build a (potentially huge) in-memory list of pathnames.
- Support expansions of ~ and {..,..} patterns. (Note that the latter are not part of the POSIX specification.)
- Add a parameter (or parameters) to allow the caller to limit the size of the result list.
@author crawley@jnode