001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsp.el;
031:
032: import com.caucho.el.Expr;
033:
034: import javax.el.ELContext;
035: import javax.el.ELResolver;
036: import javax.servlet.jsp.el.Expression;
037: import javax.servlet.jsp.el.ExpressionEvaluator;
038: import javax.servlet.jsp.el.FunctionMapper;
039: import java.lang.reflect.Method;
040:
041: /**
042: * Implementation of the expression evaluator.
043: */
044: public class ExpressionEvaluatorImpl extends ExpressionEvaluator {
045: private ELContext _elContext;
046:
047: /**
048: * Creates the expression evaluator.
049: */
050: public ExpressionEvaluatorImpl(ELContext elContext) {
051: _elContext = elContext;
052: }
053:
054: /**
055: * Evaluates an expression.
056: */
057: public Object evaluate(String expression, Class expectedType,
058: javax.servlet.jsp.el.VariableResolver resolver,
059: FunctionMapper funMapper)
060: throws javax.servlet.jsp.el.ELException {
061: Expression expr = parseExpression(expression, expectedType,
062: funMapper);
063:
064: return expr.evaluate(resolver);
065: }
066:
067: /**
068: * Parses an expression.
069: */
070: public Expression parseExpression(String expression,
071: Class expectedType, FunctionMapper funMapper)
072: throws javax.servlet.jsp.el.ELException {
073: ELContext elContext;
074:
075: if (funMapper != null)
076: elContext = new ParseELContext(funMapper);
077: else
078: elContext = _elContext;
079:
080: JspELParser parser = new JspELParser(elContext, expression);
081:
082: // parser.setFunctionMapper(funMapper);
083:
084: try {
085: Expr expr = parser.parse();
086:
087: return new ExpressionImpl(expr);
088: } catch (com.caucho.el.ELParseException e) {
089: throw new javax.servlet.jsp.el.ELParseException(e
090: .getMessage());
091: }
092: }
093:
094: public class ParseELContext extends ELContext {
095: private javax.el.FunctionMapper _funMapper;
096:
097: ParseELContext(FunctionMapper funMapper) {
098: _funMapper = new FunctionMapperAdapter(funMapper);
099: }
100:
101: public ELResolver getELResolver() {
102: return _elContext.getELResolver();
103: }
104:
105: public javax.el.FunctionMapper getFunctionMapper() {
106: return _funMapper;
107: }
108:
109: public javax.el.VariableMapper getVariableMapper() {
110: return _elContext.getVariableMapper();
111: }
112: }
113:
114: public class FunctionMapperAdapter extends javax.el.FunctionMapper {
115: private FunctionMapper _funMap;
116:
117: FunctionMapperAdapter(FunctionMapper funMap) {
118: _funMap = funMap;
119: }
120:
121: public Method resolveFunction(String prefix, String localName) {
122: return _funMap.resolveFunction(prefix, localName);
123: }
124: }
125: }
|