01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.expression;
13:
14: import java.util.Map;
15:
16: import net.sf.oval.exception.ExpressionEvaluationException;
17: import net.sf.oval.internal.Log;
18:
19: /**
20: * @author Sebastian Thomschke
21: */
22: public class ExpressionLanguageMVELImpl implements ExpressionLanguage {
23: private final static Log LOG = Log
24: .getLog(ExpressionLanguageMVELImpl.class);
25:
26: public Object evaluate(final String expression,
27: final Map<String, ?> values)
28: throws ExpressionEvaluationException {
29: try {
30: LOG.debug("Evaluating MVEL expression: {}", expression);
31: return org.mvel.MVEL.eval(expression, values);
32: } catch (final Exception ex) {
33: throw new ExpressionEvaluationException(
34: "Evaluating script with MVEL failed.", ex);
35: }
36: }
37:
38: public boolean evaluateAsBoolean(final String expression,
39: final Map<String, ?> values)
40: throws ExpressionEvaluationException {
41: final Object result = evaluate(expression, values);
42:
43: if (!(result instanceof Boolean))
44: throw new ExpressionEvaluationException(
45: "The script must return a boolean value.");
46: return (Boolean) result;
47: }
48: }
|