01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.compass.core.converter.dynamic;
18:
19: import org.apache.commons.jexl.Expression;
20: import org.apache.commons.jexl.ExpressionFactory;
21: import org.apache.commons.jexl.JexlContext;
22: import org.apache.commons.jexl.JexlHelper;
23: import org.compass.core.converter.ConversionException;
24: import org.compass.core.mapping.ResourcePropertyMapping;
25:
26: /**
27: * @author kimchy
28: */
29: public class JexlDynamicConverter extends AbstractDynamicConverter {
30:
31: private Expression expression;
32:
33: public void setExpression(String expression)
34: throws ConversionException {
35: try {
36: this .expression = ExpressionFactory
37: .createExpression(expression);
38: } catch (Exception e) {
39: throw new ConversionException(
40: "Failed to compile expression [" + expression + "]",
41: e);
42: }
43: }
44:
45: protected Object evaluate(Object o,
46: ResourcePropertyMapping resourcePropertyMapping)
47: throws ConversionException {
48: JexlContext jc = JexlHelper.createContext();
49: jc.getVars().put(DATA_CONTEXT_KEY, o);
50: try {
51: return expression.evaluate(jc);
52: } catch (Exception e) {
53: throw new ConversionException("Failed to evaluate [" + o
54: + "] with expression [" + expression + "]", e);
55: }
56: }
57: }
|