001: /* Expressions.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Aug 30 11:09:33 2007, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.xel;
020:
021: import java.util.Collection;
022: import java.util.Collections;
023:
024: import org.zkoss.lang.Classes;
025: import org.zkoss.util.logging.Log;
026:
027: /**
028: * Utilities to use XEL.
029: *
030: * @author tomyeh
031: * @since 3.0.0
032: */
033: public class Expressions {
034: private static final Log log = Log.lookup(Expressions.class);
035:
036: /** An empty function mapper, i.e., it has no function defined at all.
037: * It is serializable.
038: */
039: public static final FunctionMapper EMPTY_MAPPER = new EmptyMapper();
040: /** An empty variable resolver, i.e., it has no variable defined at all.
041: * It is serializable.
042: */
043: public static final VariableResolver EMPTY_RESOLVER = new EmptyResolver();
044:
045: /** An dummy expression that does nothing.
046: * It is usually used as a flag to denote exceptional cases.
047: * It it serializable.
048: */
049: public static final Expression DUMMY_EXPRESSION = new DummyExpr();
050:
051: /** The implemetation of {@link ExpressionFactory}. */
052: private static Class _expfcls;
053:
054: /** Instantiates an instance of {@link ExpressionFactory}.
055: *
056: * <p>The default class is {@link org.zkoss.xel.el.ELFactory}.
057: * To override it, you can specify the class by calling
058: * {@link #setExpressionFactoryClass}.
059: *
060: * <p>For the ZK user, you can override it with zk.xml
061: * or org.zkoss.zk.ui.util.Configuration.
062: *
063: * @exception XelException if the specified class failed to load,
064: * or instantiate.
065: */
066: public static final ExpressionFactory newExpressionFactory()
067: throws XelException {
068: return newExpressionFactory(_expfcls);
069: }
070:
071: /** Instantiates an instance of {@link ExpressionFactory}.
072: *
073: * <p>The default class is {@link org.zkoss.xel.el.ELFactory}.
074: * To override it, you can specify the class by calling
075: * {@link #setExpressionFactoryClass}.
076: *
077: * <p>For the ZK user, you can override it with zk.xml
078: * or org.zkoss.zk.ui.util.Configuration.
079: *
080: * @param expfcls the class that implements {@link ExpressionFactory},
081: * or null to use the default.
082: * @exception XelException if the specified class failed to load,
083: * or instantiate.
084: */
085: public static final ExpressionFactory newExpressionFactory(
086: Class expfcls) {
087: if (expfcls != null) {
088: try {
089: return (ExpressionFactory) expfcls.newInstance();
090: } catch (Throwable ex) {
091: throw XelException.Aide.wrap(ex,
092: "Unable to instantiate " + expfcls);
093: }
094: }
095: return newDefautFactory();
096: }
097:
098: private static final ExpressionFactory newDefautFactory() {
099: return new org.zkoss.xel.el.ELFactory();
100: }
101:
102: /** Evaluates an expression.
103: *
104: * @param ctx the context information to evaluate the expression
105: * It can be null, in which case no functions are supported for this
106: * invocation.
107: * @param expression the expression to be evaluated.
108: * Note: the expression is enclosed
109: * with ${ and }, regardingless what implemetnation is used.
110: * @param expectedType the expected type of the result of the evaluation
111: */
112: public static final Object evaluate(XelContext ctx,
113: String expression, Class expectedType) throws XelException {
114: return newExpressionFactory().evaluate(ctx, expression,
115: expectedType);
116: }
117:
118: /** Sets the implementation of the expression factory that shall
119: * be used by the whole system, or null to use the system default.
120: *
121: * <p>Default: null - it means {@link org.zkoss.xel.el.ELFactory}.
122: *
123: * <p>Note: you can only specify an implementation that is compatible
124: * with JSP EL here, since all builtin pages depend on it.
125: *
126: * @param expfcls the implemtation class, or null to use the default.
127: * Note: expfcls must implement {@link ExpressionFactory}.
128: * If null, the system default is used.
129: */
130: public static final void setExpressionFactoryClass(Class expfcls) {
131: if (expfcls != null
132: && !ExpressionFactory.class.isAssignableFrom(expfcls))
133: throw new IllegalArgumentException(expfcls
134: + " must implement " + ExpressionFactory.class);
135: _expfcls = expfcls;
136: }
137:
138: /** Returns the implementation of the expression factory that
139: * is used by the whole system, or null to use the system default.
140: *
141: * @see #setExpressionFactoryClass
142: */
143: public static final Class getExpressionFactoryClass() {
144: return _expfcls;
145: }
146: }
147:
148: /*package*/class EmptyMapper implements FunctionMapper,
149: java.io.Serializable {
150: //-- FunctionMapper --//
151: public Function resolveFunction(String prefix, String name) {
152: return null;
153: }
154:
155: public Collection getClassNames() {
156: return Collections.EMPTY_LIST;
157: }
158:
159: public Class resolveClass(String name) {
160: return null;
161: }
162: }
163:
164: /*package*/class EmptyResolver implements VariableResolver,
165: java.io.Serializable {
166: public Object resolveVariable(String name) {
167: return null;
168: }
169: }
170:
171: /*package*/class DummyExpr implements Expression, java.io.Serializable {
172: public Object evaluate(XelContext ctx) throws XelException {
173: return null;
174: }
175: };
|