01: /* OGNLXelExpression.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.ognl;
20:
21: import java.util.Map;
22: import java.io.Serializable;
23:
24: import ognl.Ognl;
25: import ognl.OgnlException;
26:
27: import org.zkoss.lang.Classes;
28: import org.zkoss.xel.Expression;
29: import org.zkoss.xel.VariableResolver;
30: import org.zkoss.xel.XelContext;
31: import org.zkoss.xel.XelException;
32:
33: /**
34: * A XEL expression that is implemented based on OGNL's expression.
35: *
36: * @author tomyeh
37: * @since 3.0.0
38: */
39: /*package*/class OGNLXelExpression implements Expression, Serializable {
40: /** A list of fragments, either a string or a parsed expression
41: */
42: private final Object[] _frags;
43: private final Class _expected;
44:
45: /*package*/OGNLXelExpression(Object[] frags, Class expectedType) {
46: _frags = frags;
47: _expected = expectedType;
48: }
49:
50: //Expression//
51: public Object evaluate(XelContext ctx) throws XelException {
52: final Map ognlctx = OGNLFactory.getContext(ctx);
53: final Object root = OGNLFactory.getRoot(ctx);
54:
55: try {
56: if (_frags.length == 1) { //optimize this most common case
57: return Classes.coerce(_expected,
58: _frags[0] instanceof String ? _frags[0] : Ognl
59: .getValue(_frags[0], ognlctx, root,
60: null));
61: }
62:
63: final StringBuffer sb = new StringBuffer(256);
64: for (int j = 0; j < _frags.length; ++j) {
65: if (_frags[j] instanceof String) {
66: sb.append(_frags[j]);
67: } else {
68: Object val = Ognl.getValue(_frags[j], ognlctx,
69: root, null);
70: if (val != null)
71: sb.append(val);
72: }
73: }
74: return Classes.coerce(_expected, sb.toString());
75: } catch (OgnlException ex) {
76: throw new XelException(ex);
77: }
78: }
79: }
|