01: package org.compass.core.converter.dynamic;
02:
03: import ognl.Ognl;
04: import ognl.OgnlContext;
05: import org.compass.core.converter.ConversionException;
06: import org.compass.core.mapping.ResourcePropertyMapping;
07:
08: /**
09: * @author kimchy
10: */
11: public class OgnlDynamicConverter extends AbstractDynamicConverter {
12:
13: private Object expression;
14:
15: public void setExpression(String expression)
16: throws ConversionException {
17: try {
18: this .expression = Ognl.parseExpression(expression);
19: } catch (Exception e) {
20: throw new ConversionException(
21: "Failed to compile expression [" + expression + "]",
22: e);
23: }
24: }
25:
26: protected Object evaluate(Object o,
27: ResourcePropertyMapping resourcePropertyMapping)
28: throws ConversionException {
29: OgnlContext ctx = new OgnlContext();
30: ctx.put(DATA_CONTEXT_KEY, o);
31: try {
32: return Ognl.getValue(expression, ctx, o);
33: } catch (Exception e) {
34: throw new ConversionException("Failed to evaluate [" + o
35: + "] with expression [" + expression + "]", e);
36: }
37: }
38: }
|