1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.scripting.xmltags;
17
18 import java.lang.reflect.Array;
19 import java.math.BigDecimal;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.ibatis.builder.BuilderException;
25
26
27
28
29 public class ExpressionEvaluator {
30
31 public boolean evaluateBoolean(String expression, Object parameterObject) {
32 Object value = OgnlCache.getValue(expression, parameterObject);
33 if (value instanceof Boolean) {
34 return (Boolean) value;
35 }
36 if (value instanceof Number) {
37 return new BigDecimal(String.valueOf(value)).compareTo(BigDecimal.ZERO) != 0;
38 }
39 return value != null;
40 }
41
42
43
44
45 @Deprecated
46 public Iterable<?> evaluateIterable(String expression, Object parameterObject) {
47 return evaluateIterable(expression, parameterObject, false);
48 }
49
50
51
52
53 public Iterable<?> evaluateIterable(String expression, Object parameterObject, boolean nullable) {
54 Object value = OgnlCache.getValue(expression, parameterObject);
55 if (value == null) {
56 if (nullable) {
57 return null;
58 }
59 throw new BuilderException("The expression '" + expression + "' evaluated to a null value.");
60 }
61 if (value instanceof Iterable) {
62 return (Iterable<?>) value;
63 }
64 if (value.getClass().isArray()) {
65
66
67
68 int size = Array.getLength(value);
69 List<Object> answer = new ArrayList<>();
70 for (int i = 0; i < size; i++) {
71 Object o = Array.get(value, i);
72 answer.add(o);
73 }
74 return answer;
75 }
76 if (value instanceof Map) {
77 return ((Map) value).entrySet();
78 }
79 throw new BuilderException(
80 "Error evaluating expression '" + expression + "'. Return value (" + value + ") was not iterable.");
81 }
82
83 }