001: /* SimpleEvaluator.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Aug 30 22:43:08 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.zk.xel.impl;
020:
021: import org.zkoss.xel.XelContext;
022: import org.zkoss.xel.FunctionMapper;
023: import org.zkoss.xel.VariableResolver;
024: import org.zkoss.xel.Expression;
025: import org.zkoss.xel.Expressions;
026: import org.zkoss.xel.ExpressionFactory;
027: import org.zkoss.xel.XelException;
028: import org.zkoss.xel.util.SimpleXelContext;
029:
030: import org.zkoss.web.servlet.xel.RequestContext;
031: import org.zkoss.web.servlet.xel.RequestContexts;
032:
033: import org.zkoss.zk.ui.Execution;
034: import org.zkoss.zk.ui.Executions;
035: import org.zkoss.zk.ui.Page;
036: import org.zkoss.zk.ui.Component;
037: import org.zkoss.zk.xel.Evaluator;
038:
039: /**
040: * A simple implementation of {@link Evaluator}.
041: *
042: * @author tomyeh
043: * @since 3.0.0
044: */
045: public class SimpleEvaluator implements Evaluator {
046: private transient ExpressionFactory _expf;
047: private final Class _expfcls;
048: private final FunctionMapper _mapper;
049:
050: /**
051: * @param expfcls the class that implements the expression factory.
052: * If null, the default one is used.
053: */
054: public SimpleEvaluator(FunctionMapper mapper, Class expfcls) {
055: _expfcls = expfcls;
056: _mapper = mapper;
057: }
058:
059: //Evaluator//
060: public Expression parseExpression(String expression,
061: Class expectedType) throws XelException {
062: return getExpressionFactory().parseExpression(
063: getXelContext(null), expression, expectedType);
064: }
065:
066: public Object evaluate(Page page, Expression expression)
067: throws XelException {
068: return expression.evaluate(getXelContext(page));
069: }
070:
071: public Object evaluate(Component comp, Expression expression)
072: throws XelException {
073: return expression.evaluate(getXelContext(comp));
074: }
075:
076: /** Returns the implementation class of the expression factory,
077: * or null to use the default.
078: */
079: public Class getExpressionFactoryClass() {
080: return _expfcls;
081: }
082:
083: /** Returns the expression factory. */
084: private ExpressionFactory getExpressionFactory() {
085: if (_expf == null)
086: _expf = Expressions.newExpressionFactory(_expfcls);
087: return _expf;
088: }
089:
090: /** Returns the XEL context.
091: */
092: private XelContext getXelContext(Object ref) {
093: final FunctionMapper mapper = getFunctionMapper(ref);
094: final VariableResolver resolver = getVariableResolver(ref);
095: return new SimpleXelContext(resolver, mapper);
096: //Bug 1814838: don't cache the instance
097: }
098:
099: /** Returns the function mapper, or null if not available.
100: *
101: * <p>Default: returns the function mapper passed thru
102: * {@link #SimpleEvaluator}.
103: *
104: * @param ref the object,either page, component, or null.
105: * It is passed to {@link #evaluate}
106: */
107: public FunctionMapper getFunctionMapper(Object ref) {
108: return _mapper;
109: }
110:
111: /** Returns the variable resolver, or null if not available.
112: *
113: * <p>Default: it returns the variable resolver of the
114: * current execution, if any.
115: * Otherwise, it returns the current {@link RequestContext}, if any.
116: * If both not available, null is returned.
117: *
118: * @param ref the object,either page, component, or null.
119: * It is passed to {@link #evaluate}
120: */
121: public VariableResolver getVariableResolver(Object ref) {
122: final Execution exec = Executions.getCurrent();
123: VariableResolver resolver = exec.getVariableResolver();
124: if (resolver == null) {
125: final RequestContext rc = RequestContexts.getCurrent();
126: if (rc != null)
127: resolver = rc.getVariableResolver();
128: if (resolver == null)
129: return null;
130: }
131: if (resolver instanceof ExecutionResolver)
132: ((ExecutionResolver) resolver).setSelf(ref);
133: return resolver;
134: }
135: }
|