1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springmodules.validation.util.cel.ognl;
18
19 import ognl.Ognl;
20 import ognl.OgnlException;
21 import org.springframework.util.Assert;
22 import org.springmodules.validation.util.condition.AbstractCondition;
23 import org.springmodules.validation.util.condition.Condition;
24 import org.springmodules.validation.util.lang.NestedIllegalArgumentException;
25
26
27
28
29
30
31 public class OgnlCondition extends AbstractCondition {
32
33 private String expression;
34
35 private Object compiledExpression;
36
37
38
39
40
41
42 public OgnlCondition(String expression) {
43 Assert.notNull(expression, "OGNL expression cannot be null");
44 this.expression = expression;
45 try {
46 compiledExpression = Ognl.parseExpression(expression);
47 } catch (OgnlException oe) {
48 throw new NestedIllegalArgumentException("Could not parse OGNL boolean expression '" + expression + "'", oe);
49 }
50 }
51
52
53
54
55 public boolean doCheck(Object object) {
56 try {
57 return ((Boolean) Ognl.getValue(compiledExpression, object, Boolean.class)).booleanValue();
58 } catch (OgnlException e) {
59 throw new IllegalArgumentException("Could not evaluate expression '" + expression +
60 "' on given object '" + String.valueOf(object) + "'");
61 }
62 }
63
64 }