001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.mock.web;
018
019import javax.servlet.jsp.JspException;
020import javax.servlet.jsp.PageContext;
021
022import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
023
024/**
025 * Mock implementation of the JSP 2.0 {@link javax.servlet.jsp.el.ExpressionEvaluator}
026 * interface, delegating to the Apache JSTL ExpressionEvaluatorManager.
027 *
028 * <p>Used for testing the web framework; only necessary for testing applications
029 * when testing custom JSP tags.
030 *
031 * <p>Note that the Apache JSTL implementation (jstl.jar, standard.jar) has to be
032 * available on the class path to use this expression evaluator.
033 *
034 * @author Juergen Hoeller
035 * @since 1.1.5
036 * @see org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager
037 */
038@SuppressWarnings("deprecation")
039public class MockExpressionEvaluator extends javax.servlet.jsp.el.ExpressionEvaluator {
040
041        private final PageContext pageContext;
042
043
044        /**
045         * Create a new MockExpressionEvaluator for the given PageContext.
046         * @param pageContext the JSP PageContext to run in
047         */
048        public MockExpressionEvaluator(PageContext pageContext) {
049                this.pageContext = pageContext;
050        }
051
052
053        @Override
054        @SuppressWarnings("rawtypes")
055        public javax.servlet.jsp.el.Expression parseExpression(final String expression, final Class expectedType,
056                        final javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException {
057
058                return new javax.servlet.jsp.el.Expression() {
059                        @Override
060                        public Object evaluate(javax.servlet.jsp.el.VariableResolver variableResolver) throws javax.servlet.jsp.el.ELException {
061                                return doEvaluate(expression, expectedType, functionMapper);
062                        }
063                };
064        }
065
066        @Override
067        @SuppressWarnings("rawtypes")
068        public Object evaluate(String expression, Class expectedType, javax.servlet.jsp.el.VariableResolver variableResolver,
069                        javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException {
070
071                if (variableResolver != null) {
072                        throw new IllegalArgumentException("Custom VariableResolver not supported");
073                }
074                return doEvaluate(expression, expectedType, functionMapper);
075        }
076
077        @SuppressWarnings("rawtypes")
078        protected Object doEvaluate(String expression, Class expectedType, javax.servlet.jsp.el.FunctionMapper functionMapper)
079                        throws javax.servlet.jsp.el.ELException {
080
081                if (functionMapper != null) {
082                        throw new IllegalArgumentException("Custom FunctionMapper not supported");
083                }
084                try {
085                        return ExpressionEvaluatorManager.evaluate("JSP EL expression", expression, expectedType, this.pageContext);
086                }
087                catch (JspException ex) {
088                        throw new javax.servlet.jsp.el.ELException("Parsing of JSP EL expression \"" + expression + "\" failed", ex);
089                }
090        }
091
092}