KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > el > ExpressionEvaluatorImpl


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.script.el;
19
20 import javax.servlet.jsp.el.VariableResolver JavaDoc;
21
22 import org.apache.beehive.netui.script.Expression;
23 import org.apache.beehive.netui.script.ExpressionEvaluator;
24 import org.apache.beehive.netui.script.ExpressionEvaluationException;
25 import org.apache.beehive.netui.script.ExpressionUpdateException;
26 import org.apache.beehive.netui.script.IllegalExpressionException;
27 import org.apache.beehive.netui.script.el.util.ParseUtils;
28 import org.apache.beehive.netui.util.logging.Logger;
29
30 /**
31  *
32  */

33 public class ExpressionEvaluatorImpl
34     implements ExpressionEvaluator {
35
36     private static final Logger LOGGER = Logger.getInstance(ExpressionEvaluatorImpl.class);
37     private static final boolean DEBUG_ENABLED = LOGGER.isDebugEnabled();
38
39     public static class NetUIELEngineFactory
40         extends org.apache.beehive.netui.script.ExpressionEngineFactory {
41
42         public ExpressionEvaluator getInstance() {
43             return new ExpressionEvaluatorImpl();
44         }
45     }
46
47     public Object JavaDoc evaluateStrict(String JavaDoc expression, VariableResolver JavaDoc variableResolver)
48         throws ExpressionEvaluationException {
49         NetUIReadVariableResolver vr = null;
50         try {
51             /* todo: is there compelling enough reson to keep this code here? */
52             vr = new NetUIReadVariableResolver(variableResolver);
53
54             return ParseUtils.evaluate(expression, vr);
55         } catch(Exception JavaDoc e) {
56             if(DEBUG_ENABLED)
57                 LOGGER.debug("Expression evaluation failed in NetUIEL. Cause: " + e, e);
58
59             Exception JavaDoc act = e;
60
61             String JavaDoc contextStr = ParseUtils.getContextString(vr.getAvailableVariables());
62             String JavaDoc msg = "Caught exception when evaluating expression \"" + expression + "\" with available binding contexts " +
63                 contextStr + ". Root cause: " + ParseUtils.getRootCause(act).toString();
64
65             if(LOGGER.isErrorEnabled())
66                 LOGGER.error(msg, act);
67
68             throw new ExpressionEvaluationException(msg, expression, act);
69         }
70     }
71
72     public void update(String JavaDoc expression, Object JavaDoc value, VariableResolver JavaDoc variableResolver, boolean requestParameter)
73         throws ExpressionUpdateException {
74         assert variableResolver instanceof NetUIVariableResolver;
75
76         NetUIVariableResolver vr = (NetUIVariableResolver)variableResolver;
77
78         try {
79             if(DEBUG_ENABLED)
80                 LOGGER.debug("Update expression \"" + expression + "\"");
81
82             ParseUtils.update(expression, value, vr);
83         } catch(Exception JavaDoc e) {
84             if(DEBUG_ENABLED)
85                 LOGGER.debug("Expression update failed in NetUIEL. Cause: " + e, e);
86
87             String JavaDoc contextStr = ParseUtils.getContextString(vr.getAvailableVariables());
88             String JavaDoc msg = "Exception when attempting to update the expression \"" + expression + "\" with available binding contexts " +
89                 contextStr + ". Root cause: " + ParseUtils.getRootCause(e).toString();
90
91             if(LOGGER.isErrorEnabled())
92                 LOGGER.error(msg, e);
93
94             ExpressionUpdateException eee = new ExpressionUpdateException(msg, expression, e);
95             eee.setLocalizedMessage(msg);
96
97             throw eee;
98         }
99     }
100
101     /* todo: fix the lookup index to be Object */
102     public String JavaDoc changeContext(String JavaDoc expression, String JavaDoc oldContext, String JavaDoc newContext, int lookupIndex)
103         throws ExpressionEvaluationException {
104         try {
105             ParsedExpression pe = ParseUtils.parse(expression);
106             return pe.changeContext(oldContext, newContext, new Integer JavaDoc(lookupIndex));
107         } catch(Exception JavaDoc e) {
108             String JavaDoc msg = "Error when trying to replace old context '" + oldContext + "' with new context '" +
109                 newContext + "' and index '" + lookupIndex + "': " + ParseUtils.getRootCause(e).toString();
110
111             if(LOGGER.isErrorEnabled()) LOGGER.error(msg, e);
112
113             throw new ExpressionEvaluationException(msg, e);
114         }
115     }
116
117     public String JavaDoc qualify(String JavaDoc contextName, String JavaDoc expression)
118         throws ExpressionEvaluationException {
119         try {
120             ParsedExpression pe = ParseUtils.parse(expression);
121             return pe.qualify(contextName);
122         } catch(Exception JavaDoc e) {
123             String JavaDoc msg = "Error when trying to create an expression in namespace \"" + contextName + "\" with fragment \"" +
124                 expression + "\". Root cause: " + ParseUtils.getRootCause(e).toString();
125
126             throw new ExpressionEvaluationException(msg, e);
127         }
128     }
129
130     public boolean isExpression(String JavaDoc expression) {
131         try {
132             ParsedExpression pe = ParseUtils.parse(expression);
133             return pe.isExpression();
134         } catch(Exception JavaDoc e) {
135             if(LOGGER.isErrorEnabled())
136                 LOGGER.error("Exception parsing expression \"" + expression + "\". Cause: " +
137                     ParseUtils.getRootCause(e).toString(), e);
138
139             if(e instanceof IllegalExpressionException)
140                 throw (IllegalExpressionException)e;
141             else if(e instanceof ExpressionParseException)
142                 throw new IllegalExpressionException(e);
143             else
144                 return false;
145         }
146     }
147
148     public boolean containsExpression(String JavaDoc expression) {
149         if(expression == null) return false;
150
151         try {
152             ParsedExpression pe = ParseUtils.parse(expression);
153             assert pe != null;
154             return pe.containsExpression();
155         } catch(Exception JavaDoc e) {
156             if(LOGGER.isErrorEnabled())
157                 LOGGER.error("Exception checking for expressions in \"" + expression + "\"", e);
158
159             return false;
160         }
161     }
162
163     public Expression parseExpression(String JavaDoc expression) {
164         if(isExpression(expression)) {
165             ParsedExpression pe = ParseUtils.parse(expression);
166             assert pe != null;
167             return pe.getAtomicExpressionTerm();
168         } else
169             throw new IllegalExpressionException("The expression \"" + expression + "\" can not be parsed as it is not an atomic expression.");
170     }
171 }
172
Popular Tags