KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > MockExpressionEvaluator


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
20 import javax.servlet.jsp.PageContext JavaDoc;
21 import javax.servlet.jsp.el.ELException JavaDoc;
22 import javax.servlet.jsp.el.Expression JavaDoc;
23 import javax.servlet.jsp.el.ExpressionEvaluator JavaDoc;
24 import javax.servlet.jsp.el.FunctionMapper JavaDoc;
25 import javax.servlet.jsp.el.VariableResolver JavaDoc;
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 JavaDoc {
44
45     private final PageContext JavaDoc pageContext;
46
47
48     /**
49      * Create a new MockExpressionEvaluator for the given PageContext.
50      * @param pageContext the JSP PageContext to run in
51      */

52     public MockExpressionEvaluator(PageContext JavaDoc pageContext) {
53         this.pageContext = pageContext;
54     }
55
56     public Expression JavaDoc parseExpression(
57             final String JavaDoc expression, final Class JavaDoc expectedType, final FunctionMapper JavaDoc functionMapper)
58             throws ELException JavaDoc {
59
60         return new Expression JavaDoc() {
61             public Object JavaDoc evaluate(VariableResolver JavaDoc variableResolver) throws ELException JavaDoc {
62                 return doEvaluate(expression, expectedType, functionMapper);
63             }
64         };
65     }
66
67     public Object JavaDoc evaluate(
68             String JavaDoc expression, Class JavaDoc expectedType, VariableResolver JavaDoc variableResolver, FunctionMapper JavaDoc functionMapper)
69             throws ELException JavaDoc {
70
71         if (variableResolver != null) {
72             throw new IllegalArgumentException JavaDoc("Custom VariableResolver not supported");
73         }
74         return doEvaluate(expression, expectedType, functionMapper);
75     }
76
77     protected Object JavaDoc doEvaluate(
78             String JavaDoc expression, Class JavaDoc expectedType, FunctionMapper JavaDoc functionMapper)
79             throws ELException JavaDoc {
80
81         if (functionMapper != null) {
82             throw new IllegalArgumentException JavaDoc("Custom FunctionMapper not supported");
83         }
84         try {
85             return ExpressionEvaluatorManager.evaluate("JSP EL expression", expression, expectedType, this.pageContext);
86         }
87         catch (JspException JavaDoc ex) {
88             throw new ELException JavaDoc("Parsing of JSP EL expression \"" + expression + "\" failed", ex);
89         }
90     }
91
92 }
93
Popular Tags