001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.script.el;
020:
021: import javax.servlet.jsp.el.VariableResolver;
022:
023: import org.apache.beehive.netui.script.Expression;
024: import org.apache.beehive.netui.script.ExpressionEvaluator;
025: import org.apache.beehive.netui.script.ExpressionEvaluationException;
026: import org.apache.beehive.netui.script.ExpressionUpdateException;
027: import org.apache.beehive.netui.script.IllegalExpressionException;
028: import org.apache.beehive.netui.script.el.util.ParseUtils;
029: import org.apache.beehive.netui.util.logging.Logger;
030:
031: /**
032: *
033: */
034: public class ExpressionEvaluatorImpl implements ExpressionEvaluator {
035:
036: private static final Logger LOGGER = Logger
037: .getInstance(ExpressionEvaluatorImpl.class);
038: private static final boolean TRACE_ENABLED = LOGGER
039: .isTraceEnabled();
040:
041: /**
042: * Factory that creates an instance of an {@link ExpressionEvaluator} for this expression language.
043: */
044: public static class NetUIELEngineFactory extends
045: org.apache.beehive.netui.script.ExpressionEngineFactory {
046:
047: public ExpressionEvaluator getInstance() {
048: return new ExpressionEvaluatorImpl();
049: }
050: }
051:
052: public Object evaluateStrict(String expression,
053: VariableResolver variableResolver)
054: throws ExpressionEvaluationException {
055:
056: assert variableResolver instanceof NetUIReadVariableResolver;
057:
058: NetUIReadVariableResolver netuiVariableResolver = (NetUIReadVariableResolver) variableResolver;
059: try {
060: return ParseUtils.evaluate(expression,
061: netuiVariableResolver);
062: } catch (Exception e) {
063: String contextStr = ParseUtils
064: .getContextString(netuiVariableResolver
065: .getAvailableVariables());
066: String msg = "Caught exception when evaluating expression \""
067: + expression
068: + "\" with available binding contexts "
069: + contextStr
070: + ". Root cause: "
071: + ParseUtils.getRootCause(e).toString();
072: LOGGER.error(msg, e);
073: throw new ExpressionEvaluationException(msg, expression, e);
074: }
075: }
076:
077: public void update(String expression, Object value,
078: VariableResolver variableResolver, boolean requestParameter)
079: throws ExpressionUpdateException {
080:
081: assert variableResolver instanceof NetUIVariableResolver;
082: NetUIVariableResolver vr = (NetUIVariableResolver) variableResolver;
083:
084: try {
085: if (TRACE_ENABLED)
086: LOGGER
087: .trace("Update expression \"" + expression
088: + "\"");
089:
090: ParseUtils.update(expression, value, vr);
091: } catch (Exception e) {
092: String contextStr = ParseUtils.getContextString(vr
093: .getAvailableVariables());
094: String msg = "Exception when attempting to update the expression \""
095: + expression
096: + "\" with available binding contexts "
097: + contextStr
098: + ". Root cause: "
099: + ParseUtils.getRootCause(e).toString();
100:
101: LOGGER.error(msg, e);
102: ExpressionUpdateException eee = new ExpressionUpdateException(
103: msg, expression, e);
104: eee.setLocalizedMessage(msg);
105: throw eee;
106: }
107: }
108:
109: public String changeContext(String expression, String oldContext,
110: String newContext, int lookupIndex)
111: throws ExpressionEvaluationException {
112:
113: try {
114: ParsedExpression pe = ParseUtils.parse(expression);
115: return pe.changeContext(oldContext, newContext,
116: new Integer(lookupIndex));
117: } catch (Exception e) {
118: String msg = "Error when trying to replace old context '"
119: + oldContext + "' with new context '" + newContext
120: + "' and index '" + lookupIndex + "': "
121: + ParseUtils.getRootCause(e).toString();
122:
123: LOGGER.error(msg, e);
124: throw new ExpressionEvaluationException(msg, e);
125: }
126: }
127:
128: public String qualify(String contextName, String expression)
129: throws ExpressionEvaluationException {
130: try {
131: ParsedExpression pe = ParseUtils.parse(expression);
132: return pe.qualify(contextName);
133: } catch (Exception e) {
134: String msg = "Error when trying to create an expression in namespace \""
135: + contextName
136: + "\" with fragment \""
137: + expression
138: + "\". Root cause: "
139: + ParseUtils.getRootCause(e).toString();
140:
141: throw new ExpressionEvaluationException(msg, e);
142: }
143: }
144:
145: public boolean isExpression(String expression) {
146: try {
147: ParsedExpression pe = ParseUtils.parse(expression);
148: return pe.isExpression();
149: } catch (Exception e) {
150: LOGGER.error("Exception parsing expression \"" + expression
151: + "\". Cause: "
152: + ParseUtils.getRootCause(e).toString(), e);
153:
154: if (e instanceof IllegalExpressionException)
155: throw (IllegalExpressionException) e;
156: else if (e instanceof ExpressionParseException)
157: throw new IllegalExpressionException(e);
158: else
159: return false;
160: }
161: }
162:
163: public boolean containsExpression(String expression) {
164: if (expression == null)
165: return false;
166:
167: try {
168: ParsedExpression pe = ParseUtils.parse(expression);
169: assert pe != null;
170: return pe.containsExpression();
171: } catch (Exception e) {
172: LOGGER.error("Exception checking for expressions in \""
173: + expression + "\"", e);
174: return false;
175: }
176: }
177:
178: public Expression parseExpression(String expression) {
179: if (isExpression(expression)) {
180: ParsedExpression pe = ParseUtils.parse(expression);
181: assert pe != null;
182: return pe.getAtomicExpressionTerm();
183: } else
184: throw new IllegalExpressionException(
185: "The expression \""
186: + expression
187: + "\" can not be parsed as it is not an atomic expression.");
188: }
189: }
|