001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.jelly.expression.jexl;
018:
019: import java.util.Map;
020: import java.util.Set;
021: import java.util.Collection;
022:
023: import org.apache.commons.jelly.JellyContext;
024: import org.apache.commons.jelly.expression.ExpressionSupport;
025:
026: import org.apache.commons.jexl.Expression;
027: import org.apache.commons.jexl.JexlContext;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: * Represents a <a href="http://jakarta.apache.org/commons/jexl.html">Jexl</a>
034: * expression which fully supports the Expression Language in JSTL and JSP
035: * along with some extra features like object method invocation.
036: *
037: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
038: * @version $Revision: 155420 $
039: */
040:
041: public class JexlExpression extends ExpressionSupport {
042:
043: /** The Log to which logging calls will be made. */
044: private static final Log log = LogFactory
045: .getLog(JexlExpression.class);
046:
047: /** The Jexl expression object */
048: private Expression expression;
049:
050: public JexlExpression(Expression expression) {
051: this .expression = expression;
052: }
053:
054: public String toString() {
055: return super .toString() + "[" + expression.getExpression()
056: + "]";
057: }
058:
059: // Expression interface
060: //-------------------------------------------------------------------------
061: public String getExpressionText() {
062: return "${" + expression.getExpression() + "}";
063: }
064:
065: public Object evaluate(JellyContext context) {
066: try {
067: JexlContext jexlContext = new JellyJexlContext(context);
068: if (log.isDebugEnabled()) {
069: log.debug("Evaluating EL: "
070: + expression.getExpression());
071: }
072: Object value = expression.evaluate(jexlContext);
073:
074: if (log.isDebugEnabled()) {
075: log.debug("value of expression: " + value);
076: }
077:
078: return value;
079: } catch (Exception e) {
080: log.warn("Caught exception evaluating: " + expression
081: + ". Reason: " + e, e);
082: return null;
083: }
084: }
085: }
086:
087: class JellyJexlContext implements JexlContext {
088:
089: private Map vars;
090:
091: JellyJexlContext(JellyContext context) {
092: this .vars = new JellyMap(context);
093: }
094:
095: public void setVars(Map vars) {
096: this .vars.clear();
097: this .vars.putAll(vars);
098: }
099:
100: public Map getVars() {
101: return this .vars;
102: }
103: }
104:
105: class JellyMap implements Map {
106:
107: private JellyContext context;
108:
109: JellyMap(JellyContext context) {
110: this .context = context;
111: }
112:
113: public Object get(Object key) {
114: return context.getVariable((String) key);
115: }
116:
117: public void clear() {
118: // not implemented
119: }
120:
121: public boolean containsKey(Object key) {
122: return (get(key) != null);
123: }
124:
125: public boolean containsValue(Object value) {
126: return false;
127: }
128:
129: public Set entrySet() {
130: return null;
131: }
132:
133: public boolean isEmpty() {
134: return false;
135: }
136:
137: public Set keySet() {
138: return null;
139: }
140:
141: public Object put(Object key, Object value) {
142: return null;
143: }
144:
145: public void putAll(Map t) {
146: // not implemented
147: }
148:
149: public Object remove(Object key) {
150: return null;
151: }
152:
153: public int size() {
154: return -1;
155: }
156:
157: public Collection values() {
158: return null;
159: }
160: }
|