01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.mock.web;
18:
19: import javax.servlet.jsp.JspException;
20: import javax.servlet.jsp.PageContext;
21: import javax.servlet.jsp.el.ELException;
22: import javax.servlet.jsp.el.Expression;
23: import javax.servlet.jsp.el.ExpressionEvaluator;
24: import javax.servlet.jsp.el.FunctionMapper;
25: import javax.servlet.jsp.el.VariableResolver;
26:
27: import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
28:
29: /**
30: * Mock implementation of the JSP 2.0 {@link javax.servlet.jsp.el.ExpressionEvaluator}
31: * interface, delegating to the Jakarta JSTL ExpressionEvaluatorManager.
32: *
33: * <p>Used for testing the web framework; only necessary for testing
34: * applications when testing custom JSP tags.
35: *
36: * <p>Note that the Jakarta JSTL implementation (jstl.jar, standard.jar)
37: * has to be available on the class path to use this expression evaluator.
38: *
39: * @author Juergen Hoeller
40: * @since 1.1.5
41: * @see org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager
42: */
43: public class MockExpressionEvaluator extends ExpressionEvaluator {
44:
45: private final PageContext pageContext;
46:
47: /**
48: * Create a new MockExpressionEvaluator for the given PageContext.
49: * @param pageContext the JSP PageContext to run in
50: */
51: public MockExpressionEvaluator(PageContext pageContext) {
52: this .pageContext = pageContext;
53: }
54:
55: public Expression parseExpression(final String expression,
56: final Class expectedType,
57: final FunctionMapper functionMapper) throws ELException {
58:
59: return new Expression() {
60: public Object evaluate(VariableResolver variableResolver)
61: throws ELException {
62: return doEvaluate(expression, expectedType,
63: functionMapper);
64: }
65: };
66: }
67:
68: public Object evaluate(String expression, Class expectedType,
69: VariableResolver variableResolver,
70: FunctionMapper functionMapper) throws ELException {
71:
72: if (variableResolver != null) {
73: throw new IllegalArgumentException(
74: "Custom VariableResolver not supported");
75: }
76: return doEvaluate(expression, expectedType, functionMapper);
77: }
78:
79: protected Object doEvaluate(String expression, Class expectedType,
80: FunctionMapper functionMapper) throws ELException {
81:
82: if (functionMapper != null) {
83: throw new IllegalArgumentException(
84: "Custom FunctionMapper not supported");
85: }
86: try {
87: return ExpressionEvaluatorManager.evaluate(
88: "JSP EL expression", expression, expectedType,
89: this .pageContext);
90: } catch (JspException ex) {
91: throw new ELException("Parsing of JSP EL expression \""
92: + expression + "\" failed", ex);
93: }
94: }
95:
96: }
|