01: package org.compass.core.converter.dynamic;
02:
03: import javax.servlet.jsp.el.ELException;
04: import javax.servlet.jsp.el.Expression;
05: import javax.servlet.jsp.el.ExpressionEvaluator;
06: import javax.servlet.jsp.el.VariableResolver;
07:
08: import org.apache.commons.el.ExpressionEvaluatorImpl;
09: import org.compass.core.converter.ConversionException;
10: import org.compass.core.mapping.ResourcePropertyMapping;
11:
12: /**
13: * @author kimchy
14: */
15: public class JakartaElDynamicConverter extends AbstractDynamicConverter {
16:
17: private ExpressionEvaluator expressionEvaluator;
18:
19: private Expression expression;
20:
21: public DynamicConverter copy() {
22: JakartaElDynamicConverter converter = (JakartaElDynamicConverter) super
23: .copy();
24: converter.expressionEvaluator = expressionEvaluator;
25: return converter;
26: }
27:
28: public void setExpression(String expression)
29: throws ConversionException {
30: if (expressionEvaluator == null) {
31: expressionEvaluator = new ExpressionEvaluatorImpl();
32: }
33: Class type = String.class;
34: if (getType() != null) {
35: type = getType();
36: }
37: try {
38: this .expression = expressionEvaluator.parseExpression(
39: expression, type, null);
40: } catch (ELException e) {
41: throw new ConversionException(
42: "Failed to compile expression [" + expression + "]",
43: e);
44: }
45: }
46:
47: protected Object evaluate(final Object o,
48: ResourcePropertyMapping resourcePropertyMapping)
49: throws ConversionException {
50: try {
51: VariableResolver variableResolver = new VariableResolver() {
52: public Object resolveVariable(String variable)
53: throws ELException {
54: return o;
55: }
56: };
57: return expression.evaluate(variableResolver);
58: } catch (Exception e) {
59: throw new ConversionException("Failed to evaluate [" + o
60: + "] with expression [" + expression + "]", e);
61: }
62: }
63: }
|