KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > expression > jexl > JexlExpression


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.jelly.expression.jexl;
18
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21 import java.util.Collection JavaDoc;
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://jakarta.apache.org/commons/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 JavaDoc toString() {
54         return super.toString() + "[" + expression.getExpression() + "]";
55     }
56
57     // Expression interface
58
//-------------------------------------------------------------------------
59
public String JavaDoc getExpressionText() {
60         return "${" + expression.getExpression() + "}";
61     }
62
63     public Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc vars;
87
88     JellyJexlContext(JellyContext context) {
89         this.vars = new JellyMap( context );
90     }
91
92     public void setVars(Map JavaDoc vars) {
93         this.vars.clear();
94         this.vars.putAll( vars );
95     }
96
97     public Map JavaDoc getVars() {
98         return this.vars;
99     }
100 }
101
102
103 class JellyMap implements Map JavaDoc {
104
105     private JellyContext context;
106
107     JellyMap(JellyContext context) {
108         this.context = context;
109     }
110
111     public Object JavaDoc get(Object JavaDoc key) {
112         return context.getVariable( (String JavaDoc) key );
113     }
114
115     public void clear() {
116         // not implemented
117
}
118
119     public boolean containsKey(Object JavaDoc key) {
120         return ( get( key ) != null );
121     }
122
123     public boolean containsValue(Object JavaDoc value) {
124         return false;
125     }
126
127     public Set JavaDoc entrySet() {
128         return null;
129     }
130
131     public boolean isEmpty() {
132         return false;
133     }
134
135     public Set JavaDoc keySet() {
136         return null;
137     }
138
139     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
140         return null;
141     }
142
143     public void putAll(Map JavaDoc t) {
144         // not implemented
145
}
146
147     public Object JavaDoc remove(Object JavaDoc key) {
148         return null;
149     }
150
151     public int size() {
152         return -1;
153     }
154
155     public Collection JavaDoc values() {
156         return null;
157     }
158 }
159
Popular Tags