The WildcardExpression class of the com.ms.util package matches simple wildcard expressions, such as *.txt or report?.doc commonly recognized by users on most platforms.
public class WildcardExpression implements java.io.FilenameFilter, Cloneable, SetComparison { // Fields public static int CASE_SENSITIVE; public static final int COMPARE_FL_ALL; public static final int COMPARE_FL_REJECT; public static final int COMPARE_FL_STOP1; public static final int COMPARE_FL_STOP2; public static int ESCAPED; public static int EXACT; public static int NO_DELIMITERS; public static int NO_WILDCARDS; // Constructors public WildcardExpression(); public WildcardExpression(String expr); public WildcardExpression(String expr, int flags); // Methods public boolean accept(java.io.file dir, Sring name); public synchronized void append(WildcardExpression other); public synchronized void append(WildcardExpression[] list); public synchronized void append(String expr, int flags); public void append(String expr); protected Object clone(); public int compare(WildcardExpression other, IWildcardExpressionComparator judge, boolean requireEquality); public int compare(WildcardExpression other) public int compare(WildcardExpression other, IWildcardExpressionComparator judge); public int compareSet(Object other); public static String ComparisonResulttoString(int cmp); public synchronized void compile(String expr, int flags) public void compile(String expr) public WildcardExpression condense(); public synchronized WildcardExpression); condense( IWildcardExpressionComparator judge); public WildcardExpression copy(); public boolean equals(Object obj); public WildcardExpression[] getSubexpressions(); public WildcardExpression intersect(WildcardExpression other, IWildcardExpressionComparator judge); public WildcardExpression intersect(WildcardExpression other); public static int InvertComparisonResult(int cmp); public boolean isCaseSensitive(); public boolean isEmpty(); public boolean match(String s); public int matchex(String s); public static int matchOne(WildcardExpression[] exprs, int ofs int len, String s); public static int matchOne(WildcardExpression[] exprs, Sting s); public int resume(String s); public WildcardExpression selectSubexpressions( WildcardExpression other, int[] criteria, IWildcardExpressionComparator judge); public WildcardExpression selectSubexpressions( WildcardExpression other, int[] criteria); public void setCaseInsensitive(); public String toString(); public String toString(boolean escaped); }
The nonterminals "*" and "?" can be combined any number of times in any conceivable manner. Multiple expressions can be matched by separating the expressions with a semicolon; for example, "*.txt;*.doc" will match Strings ending with .txt or .doc. The special expression tokens can be matched by escaping them with '\'; for example, "clear\*sky" will match the string "clear*sky", but not "clearbluesky". Expressions can be case-sensitive or case-insensitive; this is selected when the expression is constructed.
This is very similar to Microsoft® MS-DOS® wildcards, but is in no way confined to file names. The expression '*' will match a path in any subdirectory, and "*.*" does not match a file with no extension. For example, unlike MS-DOS, "c:\\windows\\*" will match "c:\\windows\\system\\blue.vxd" and "*.*" will not match "notes."
WildcardExpression implements FilenameFilter, so it can readily be used with FileDialog, ClientStoreFile.list or ClientStore.listFiles.
The equals method has been overridden to special case-equality with strings. When invoked with a string, it will perform a match instead of comparing the pattern string. This allows it to be more easily used in a container, such as Vector, to find an expression matching a string. To find a matching expression in a string array, use the matchOne method.
The preferred method of matching a string against a series of expressions is to combine the expressions into a single expression using the append operator. The matchtex function will return the sub-expression number of the first matching expression, and the resume function will continue to search for matching expressions.
The use of matchex, or resume can be used on the same expression simultaneously from another thread, as shown in the following example:
WildcardExpression expr = new WildcardExpression(); expr.append("*.java"); expr.append("*.txt"); expr.append("*.doc"); expr.append("c:\\docs\\*"); // Resulting expression: "*.java;*.txt;*.doc;c:\\docs\\*" String filename = "c:\\docs\\MyText.txt"; synchronized (expr) { int exprnum; exprnum = expr.matchex(filename); // returns 1 exprnum = expr.resume(filename); // returns 3 exprnum = expr.resume(filename); // returns -1 }
Case-sensitive and case-insensitive expressions cannot be combined.
To construct expressions
To improve performance, nothing in this class is synchronized. The typical use is that an expression is compiled once by the constructor. The compile and append methods are the only operators that change the state of the expression. Callers are responsible for synchronizing these operations with callers of the other methods. Note that matchex and resume also change state of the expression but not the expression itself. These operations also might need to be synchronized, but not with the others.
WildcardExpressions are not immutable, so security-critical code should be careful to pass copies of the expression to less-trusted code using the copy method. Some of the contents of the copy will be shared with the original until one of them changes.