01: /*
02: * RegExComparator.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.storage.filter;
13:
14: import java.util.regex.Matcher;
15: import java.util.regex.Pattern;
16:
17: /**
18: * Implementation of the ColumnComparator using regular expressions.
19: *
20: * @author support@sql-workbench.net
21: */
22: public class RegExComparator implements ColumnComparator {
23: public RegExComparator() {
24: }
25:
26: public boolean supportsIgnoreCase() {
27: return true;
28: }
29:
30: public String getValueExpression(Object value) {
31: return "'" + value + "'";
32: }
33:
34: public String getOperator() {
35: return "matches";
36: }
37:
38: public boolean needsValue() {
39: return true;
40: }
41:
42: public boolean comparesEquality() {
43: return false;
44: }
45:
46: public boolean supportsType(Class valueClass) {
47: return (String.class.isAssignableFrom(valueClass));
48: }
49:
50: public boolean evaluate(Object reference, Object value,
51: boolean ignoreCase) {
52: if (reference == null || value == null)
53: return false;
54:
55: Pattern p = null;
56: if (ignoreCase) {
57: p = Pattern.compile(reference.toString(),
58: Pattern.CASE_INSENSITIVE);
59: } else {
60: p = Pattern.compile(reference.toString());
61: }
62: Matcher m = p.matcher(value.toString());
63:
64: return m.find();
65: }
66:
67: public boolean equals(Object other) {
68: return (other instanceof RegExComparator);
69: }
70:
71: public boolean validateInput(Object value) {
72: if (!(value instanceof String))
73: return false;
74:
75: try {
76: Pattern.compile((String) value);
77: return true;
78: } catch (Exception e) {
79: return false;
80: }
81: }
82:
83: }
|