01: /* MVELXelExpression.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Sun Sep 2 22:08:01 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.mvel;
20:
21: import java.io.Serializable;
22:
23: import org.mvel.MVEL;
24:
25: import org.zkoss.lang.Classes;
26: import org.zkoss.xel.Expression;
27: import org.zkoss.xel.VariableResolver;
28: import org.zkoss.xel.XelContext;
29:
30: /**
31: * A XEL expression that is implemented based on MVEL's expression.
32: *
33: * @author tomyeh
34: * @since 3.0.0
35: */
36: /*package*/class MVELXelExpression implements Expression, Serializable {
37: /** A list of fragments, either a string or a parsed expression
38: */
39: private final Object[] _frags;
40: private final Class _expected;
41:
42: /*package*/MVELXelExpression(Object[] frags, Class expectedType) {
43: _frags = frags;
44: _expected = expectedType;
45: }
46:
47: //Expression//
48: public Object evaluate(XelContext ctx) {
49: final XelMVELResolver resolver = new XelMVELResolver(ctx
50: .getVariableResolver());
51:
52: if (_frags.length == 1) { //optimize this most common case
53: return Classes.coerce(_expected,
54: _frags[0] instanceof String ? _frags[0] : MVEL
55: .executeExpression(_frags[0], resolver));
56: }
57:
58: final StringBuffer sb = new StringBuffer(256);
59: for (int j = 0; j < _frags.length; ++j) {
60: if (_frags[j] instanceof String) {
61: sb.append(_frags[j]);
62: } else {
63: Object val = MVEL
64: .executeExpression(_frags[j], resolver);
65: if (val != null)
66: sb.append(val);
67: }
68: }
69: return Classes.coerce(_expected, sb.toString());
70: }
71: }
|