001/*
002 * Copyright 2002-2018 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 {@link ExpressionEvaluatorManager}.
027 * Only necessary for testing applications when testing custom JSP tags.
028 *
029 * <p>Note that the Apache JSTL implementation (jstl.jar, standard.jar) has to be
030 * available on the classpath to use this expression evaluator.
031 *
032 * @author Juergen Hoeller
033 * @since 1.1.5
034 * @see org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager
035 */
036@SuppressWarnings("deprecation")
037public class MockExpressionEvaluator extends javax.servlet.jsp.el.ExpressionEvaluator {
038
039        private final PageContext pageContext;
040
041
042        /**
043         * Create a new MockExpressionEvaluator for the given PageContext.
044         * @param pageContext the JSP PageContext to run in
045         */
046        public MockExpressionEvaluator(PageContext pageContext) {
047                this.pageContext = pageContext;
048        }
049
050
051        @Override
052        @SuppressWarnings("rawtypes")
053        public javax.servlet.jsp.el.Expression parseExpression(final String expression, final Class expectedType,
054                        final javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException {
055
056                return new javax.servlet.jsp.el.Expression() {
057                        @Override
058                        public Object evaluate(javax.servlet.jsp.el.VariableResolver variableResolver) throws javax.servlet.jsp.el.ELException {
059                                return doEvaluate(expression, expectedType, functionMapper);
060                        }
061                };
062        }
063
064        @Override
065        @SuppressWarnings("rawtypes")
066        public Object evaluate(String expression, Class expectedType, javax.servlet.jsp.el.VariableResolver variableResolver,
067                        javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException {
068
069                return doEvaluate(expression, expectedType, functionMapper);
070        }
071
072        @SuppressWarnings("rawtypes")
073        protected Object doEvaluate(String expression, Class expectedType, javax.servlet.jsp.el.FunctionMapper functionMapper)
074                        throws javax.servlet.jsp.el.ELException {
075
076                try {
077                        return ExpressionEvaluatorManager.evaluate("JSP EL expression", expression, expectedType, this.pageContext);
078                }
079                catch (JspException ex) {
080                        throw new javax.servlet.jsp.el.ELException("Parsing of JSP EL expression \"" + expression + "\" failed", ex);
081                }
082        }
083
084}