1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.beanfilter;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22
23
24
25
26
27
28 public class Filter {
29
30
31
32 private static Log log = LogFactory.getLog(Filter.class);
33
34
35
36
37 private String expr;
38
39
40
41
42 private Class<?> c;
43
44
45
46
47 private EvalNode root;
48
49
50
51
52
53
54
55
56 public Filter(String expr, Class<?> c) throws EvalException {
57 try {
58 this.expr = (expr == null) ? "" : expr.trim();
59 this.root = (this.expr.length() == 0) ? null : Parser.parse(this.expr, c);
60 this.c = c;
61 } catch (EvalException ke) {
62 log.error("Error in filter: [Expression: " + expr + "] [Class: " + c + "] -> "
63 + ke.getMessage());
64 throw ke;
65 }
66 }
67
68
69
70
71
72
73
74
75 public synchronized boolean eval(Object src) throws EvalException {
76 if (root == null) {
77 return true;
78 } else {
79 if ((src == null) || (src.getClass().equals(c))) {
80 return root.eval(src);
81 }
82
83
84 throw new EvalException("FTR011", src.getClass(), c);
85 }
86 }
87
88
89
90
91
92
93 public String getExpr() {
94 return this.expr;
95 }
96
97
98
99
100
101
102
103 public void setExpr(String expr) throws EvalException {
104 expr = (expr == null) ? "" : expr.trim();
105 if (!expr.equals(this.expr)) {
106
107 this.root = Parser.parse(expr, c);
108 this.expr = expr;
109 }
110 }
111
112
113
114
115 @Override public String toString() {
116 StringBuffer sb = new StringBuffer();
117 sb.append("Filter { ");
118 sb.append(expr);
119 sb.append(" }");
120 return sb.toString();
121 }
122
123
124
125
126
127 public String getFormattedExpr() {
128 if (root == null) {
129 return null;
130 } else {
131 return root.toString();
132 }
133 }
134 }