01: package org.uispec4j.finder;
02:
03: import java.util.regex.Matcher;
04: import java.util.regex.Pattern;
05:
06: /**
07: * String matching policy used withing the component searching mechanism.
08: * Three ready-to-use strategies are provided (identity/substring/regexp),
09: * but you can provide your own by implementing this class.
10: */
11: public abstract class StringMatcher {
12: public abstract boolean matches(String toCompare);
13:
14: public static StringMatcher identity(final String reference) {
15: return new StringMatcher() {
16: public boolean matches(String toCompare) {
17: if (reference == null) {
18: return toCompare == null;
19: }
20: return reference.equalsIgnoreCase(toCompare);
21: }
22: };
23: }
24:
25: public static StringMatcher substring(final String reference) {
26: final String uppercasedReference = reference.toUpperCase();
27: return new StringMatcher() {
28: public boolean matches(String toCompare) {
29: if ((toCompare == null)) {
30: return false;
31: }
32: String realToCompare = toCompare.toUpperCase();
33: return (realToCompare.indexOf(uppercasedReference) >= 0);
34: }
35: };
36: }
37:
38: public static StringMatcher regexp(String reference) {
39: final Pattern pattern = Pattern.compile(reference);
40: return new StringMatcher() {
41: public boolean matches(String toCompare) {
42: String realToCompare = (toCompare == null) ? ""
43: : toCompare;
44: Matcher matcher = pattern.matcher(realToCompare);
45: return matcher.matches();
46: }
47: };
48: }
49: }
|