01: // Modified or written by Object Mentor, Inc. for inclusion with FitNesse.
02: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
03: // Released under the terms of the GNU General Public License version 2 or later.
04: package fit;
05:
06: import java.util.regex.*;
07: import fit.exception.FitMatcherException;
08:
09: class FitMatcher {
10: private String expression;
11: private Object parameter;
12:
13: public FitMatcher(String expression, Object parameter) {
14: this .expression = expression;
15: this .parameter = parameter;
16: }
17:
18: public boolean matches() throws Exception {
19: Pattern p = Pattern
20: .compile("\\s*_?\\s*(<|>|<=|>=)\\s*([-+]?[\\d]*\\.?[\\d]+)");
21: Matcher m = p.matcher(expression);
22: if (m.matches()) {
23: String op = m.group(1);
24: String operandString = m.group(2);
25: double operand = Double.parseDouble(operandString);
26: double n = ((Number) parameter).doubleValue();
27: if (op.equals("<"))
28: return (n < operand);
29: if (op.equals(">"))
30: return (n > operand);
31: if (op.equals("<="))
32: return (n <= operand);
33: if (op.equals(">="))
34: return (n >= operand);
35: return false;
36: }
37:
38: p = Pattern
39: .compile("\\s*([-+]?[\\d]*\\.?[\\d]+)\\s*(<|>|<=|>=)\\s*_\\s*(<|>|<=|>=)\\s*([-+]?[\\d]*\\.?[\\d]+)");
40: m = p.matcher(expression);
41: if (m.matches()) {
42: double a = Double.parseDouble(m.group(1));
43: String aop = m.group(2);
44: String bop = m.group(3);
45: double b = Double.parseDouble(m.group(4));
46: double n = ((Number) parameter).doubleValue();
47:
48: boolean an = false;
49: if (aop.equals("<"))
50: an = a < n;
51: if (aop.equals("<="))
52: an = a <= n;
53: if (aop.equals(">"))
54: an = a > n;
55: if (aop.equals(">="))
56: an = a >= n;
57:
58: boolean nb = false;
59: if (bop.equals("<"))
60: nb = n < b;
61: if (bop.equals("<="))
62: nb = n <= b;
63: if (bop.equals(">"))
64: nb = n > b;
65: if (bop.equals(">="))
66: nb = n >= b;
67:
68: return an && nb;
69: }
70: throw new FitMatcherException("Invalid FitMatcher Expression");
71: }
72:
73: public String message() {
74: String message = null;
75: String parmString = "<b>" + parameter.toString() + "</b>";
76: if (expression.indexOf("_") == -1)
77: message = parmString + expression;
78: else
79: message = expression.replaceFirst("_", parmString);
80: return message;
81: }
82: }
|