1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jelly.expression.jexl;
18
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.Collection;
22
23 import org.apache.commons.jelly.JellyContext;
24 import org.apache.commons.jelly.expression.ExpressionSupport;
25
26 import org.apache.commons.jexl.Expression;
27 import org.apache.commons.jexl.JexlContext;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /***
33 * Represents a <a href="http://commons.apache.org/jexl.html">Jexl</a>
34 * expression which fully supports the Expression Language in JSTL and JSP
35 * along with some extra features like object method invocation.
36 *
37 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
38 * @version $Revision: 155420 $
39 */
40
41 public class JexlExpression extends ExpressionSupport {
42
43 /*** The Log to which logging calls will be made. */
44 private static final Log log = LogFactory.getLog(JexlExpression.class);
45
46 /*** The Jexl expression object */
47 private Expression expression;
48
49 public JexlExpression(Expression expression) {
50 this.expression = expression;
51 }
52
53 public String toString() {
54 return super.toString() + "[" + expression.getExpression() + "]";
55 }
56
57
58
59 public String getExpressionText() {
60 return "${" + expression.getExpression() + "}";
61 }
62
63 public Object evaluate(JellyContext context) {
64 try {
65 JexlContext jexlContext = new JellyJexlContext( context );
66 if (log.isDebugEnabled()) {
67 log.debug("Evaluating EL: " + expression.getExpression());
68 }
69 Object value = expression.evaluate(jexlContext);
70
71 if (log.isDebugEnabled()) {
72 log.debug("value of expression: " + value);
73 }
74
75 return value;
76 }
77 catch (Exception e) {
78 log.warn("Caught exception evaluating: " + expression + ". Reason: " + e, e);
79 return null;
80 }
81 }
82 }
83
84 class JellyJexlContext implements JexlContext {
85
86 private Map vars;
87
88 JellyJexlContext(JellyContext context) {
89 this.vars = new JellyMap( context );
90 }
91
92 public void setVars(Map vars) {
93 this.vars.clear();
94 this.vars.putAll( vars );
95 }
96
97 public Map getVars() {
98 return this.vars;
99 }
100 }
101
102
103 class JellyMap implements Map {
104
105 private JellyContext context;
106
107 JellyMap(JellyContext context) {
108 this.context = context;
109 }
110
111 public Object get(Object key) {
112 return context.getVariable( (String) key );
113 }
114
115 public void clear() {
116
117 }
118
119 public boolean containsKey(Object key) {
120 return ( get( key ) != null );
121 }
122
123 public boolean containsValue(Object value) {
124 return false;
125 }
126
127 public Set entrySet() {
128 return null;
129 }
130
131 public boolean isEmpty() {
132 return false;
133 }
134
135 public Set keySet() {
136 return null;
137 }
138
139 public Object put(Object key, Object value) {
140 return null;
141 }
142
143 public void putAll(Map t) {
144
145 }
146
147 public Object remove(Object key) {
148 return null;
149 }
150
151 public int size() {
152 return -1;
153 }
154
155 public Collection values() {
156 return null;
157 }
158 }