KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > expression > JexlExpression


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 package scriptella.expression;
17
18 import org.apache.commons.jexl.ExpressionFactory;
19 import org.apache.commons.jexl.JexlContext;
20 import org.apache.commons.jexl.parser.TokenMgrError;
21 import scriptella.spi.ParametersCallback;
22
23 import java.util.Collection JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27
28 /**
29  * Represents <a HREF="jakarta.apache.org/commons/jexl">JEXL</a> expression.
30  *
31  * @author Fyodor Kupolov
32  * @version 1.0
33  */

34 public class JexlExpression extends Expression {
35     private JexlContextAdapter adapter = new JexlContextAdapter();
36     private org.apache.commons.jexl.Expression expression;
37
38     protected JexlExpression(String JavaDoc expression) throws ParseException {
39         super(expression);
40
41         try {
42             this.expression = ExpressionFactory.createExpression(expression);
43         } catch (Exception JavaDoc e) {
44             throw new ParseException(e.getMessage(),e);
45         } catch (TokenMgrError e) {
46             throw new ParseException(e.getMessage(),e);
47         }
48     }
49
50     public Object JavaDoc evaluate(final ParametersCallback callback)
51             throws EvaluationException {
52         try {
53             adapter.setContext(callback);
54
55             return expression.evaluate(adapter);
56         } catch (Exception JavaDoc e) {
57             throw new EvaluationException(e);
58         } finally {
59             adapter.unsetContext();
60         }
61     }
62
63     /**
64      * Adapter for JexlContext to allow working with {@link ParametersCallback}.
65      */

66     private static class JexlContextAdapter implements JexlContext {
67         private ParametersCallbackMap map = new ParametersCallbackMap();
68
69         public void setVars(final Map JavaDoc map) {
70         }
71
72         public Map JavaDoc getVars() {
73             return map;
74         }
75
76         public void setContext(final ParametersCallback parametersCallback) {
77             map.callback = parametersCallback;
78         }
79
80         public void unsetContext() {
81             map.callback = null;
82         }
83     }
84
85     /**
86      * Represents {@link ParametersCallback} as a Map.
87      * <p>Only {@link #get(Object)} method is supported. Invocations of other methods result in
88      * {@link UnsupportedOperationException} thrown.
89      */

90     private static final class ParametersCallbackMap implements Map JavaDoc {
91         private ParametersCallback callback;
92
93         public int size() {
94             throw new UnsupportedOperationException JavaDoc();
95         }
96
97         public boolean isEmpty() {
98             throw new UnsupportedOperationException JavaDoc();
99         }
100
101         public boolean containsKey(final Object JavaDoc key) {
102             throw new UnsupportedOperationException JavaDoc();
103         }
104
105         public boolean containsValue(final Object JavaDoc value) {
106             throw new UnsupportedOperationException JavaDoc();
107         }
108
109         public Object JavaDoc get(final Object JavaDoc key) {
110             return callback.getParameter((String JavaDoc) key);
111         }
112
113         public Object JavaDoc put(final Object JavaDoc key, final Object JavaDoc o1) {
114             throw new UnsupportedOperationException JavaDoc();
115         }
116
117         public Object JavaDoc remove(final Object JavaDoc key) {
118             throw new UnsupportedOperationException JavaDoc();
119         }
120
121         public void putAll(final Map JavaDoc map) {
122             throw new UnsupportedOperationException JavaDoc();
123         }
124
125         public void clear() {
126             throw new UnsupportedOperationException JavaDoc();
127         }
128
129         public Set JavaDoc keySet() {
130             throw new UnsupportedOperationException JavaDoc();
131         }
132
133         public Collection JavaDoc values() {
134             throw new UnsupportedOperationException JavaDoc();
135         }
136
137         public Set JavaDoc<Entry<String JavaDoc, Object JavaDoc>> entrySet() {
138             throw new UnsupportedOperationException JavaDoc();
139         }
140     }
141 }
142
Popular Tags