01: /* ApacheELFactory.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Fri Aug 31 17:00:40 2007, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.zkmax.xel.el;
20:
21: import org.zkoss.xel.Expression;
22: import org.zkoss.xel.ExpressionFactory;
23: import org.zkoss.xel.XelContext;
24: import org.zkoss.xel.XelException;
25:
26: import javax.servlet.jsp.el.ExpressionEvaluator;
27: import javax.servlet.jsp.el.ELException;
28:
29: /**
30: * An implemetation that is based on Apache commons-el:
31: * org.apache.commons.el.ExpressionEvaluatorImpl.
32: *
33: * <p>The org.zkoss.xel.el.ELFactory class is recommended since the
34: * implementation it encapsulates has the better performance.
35: *
36: * @author tomyeh
37: * @since 3.0.0
38: */
39: public class ApacheELFactory implements ExpressionFactory {
40: private final ExpressionEvaluator _eval;
41:
42: public ApacheELFactory() {
43: _eval = newExpressionEvaluator();
44: }
45:
46: //ExpressionFactory//
47: public boolean isSupported(int feature) {
48: return feature == FEATURE_FUNCTION;
49: }
50:
51: public Expression parseExpression(XelContext xelc,
52: String expression, Class expectedType) throws XelException {
53: try {
54: return new ELXelExpression(_eval.parseExpression(
55: expression, expectedType,
56: xelc != null ? new XelELMapper(xelc
57: .getFunctionMapper()) : null));
58: } catch (ELException ex) {
59: throw new XelException("Failed to parse " + expression, ex);
60: }
61: }
62:
63: public Object evaluate(XelContext xelc, String expression,
64: Class expectedType) throws XelException {
65: try {
66: return _eval.evaluate(expression, expectedType,
67: xelc != null ? new XelELResolver(xelc
68: .getVariableResolver()) : null,
69: xelc != null ? new XelELMapper(xelc
70: .getFunctionMapper()) : null);
71: } catch (ELException ex) {
72: throw new XelException("Failed to evaluate " + expression,
73: ex);
74: }
75: }
76:
77: /** Returns the EL expression factory.
78: * <p>Default: Use org.apache.commons.el.ExpressionEvaluatorImpl.
79: * <p>You might override it to use a different implementation.
80: */
81: protected ExpressionEvaluator newExpressionEvaluator() {
82: return new org.apache.commons.el.ExpressionEvaluatorImpl();
83: }
84: }
|