KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > runtime > ExpressionEvaluatorImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25
26 package org.apache.jasper.runtime;
27
28 import java.util.Iterator JavaDoc;
29
30 import javax.el.ELContext;
31 import javax.el.ELResolver;
32 import javax.el.ValueExpression;
33 import javax.el.ExpressionFactory;
34
35 import javax.servlet.jsp.PageContext JavaDoc;
36 import javax.servlet.jsp.el.Expression JavaDoc;
37 import javax.servlet.jsp.el.ELException JavaDoc;
38 import javax.servlet.jsp.el.FunctionMapper JavaDoc;
39 import javax.servlet.jsp.el.ExpressionEvaluator JavaDoc;
40 import javax.servlet.jsp.el.VariableResolver JavaDoc;
41
42 /**
43  * <p>This is the implementation of ExpreesioEvaluator
44  * using implementation of JSP2.1.
45  *
46  * @author Kin-man Chung
47  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
48  **/

49
50 public class ExpressionEvaluatorImpl extends ExpressionEvaluator JavaDoc
51 {
52     private PageContext JavaDoc pageContext;
53
54     //-------------------------------------
55
/**
56      * Constructor
57      **/

58     public ExpressionEvaluatorImpl (PageContext JavaDoc pageContext) {
59         this.pageContext = pageContext;
60     }
61   
62     //-------------------------------------
63
public Expression parseExpression(String JavaDoc expression,
64                                       Class JavaDoc expectedType,
65                                       FunctionMapper JavaDoc fMapper )
66             throws ELException JavaDoc {
67
68         ExpressionFactory fac = JspApplicationContextImpl.expressionFactory;
69         javax.el.ValueExpression expr;
70         ELContextImpl elContext = new ELContextImpl(null);
71         javax.el.FunctionMapper fm = new FunctionMapperWrapper(fMapper);
72         elContext.setFunctionMapper(fm);
73         try {
74             expr = fac.createValueExpression(
75                            elContext,
76                            expression, expectedType);
77         } catch (javax.el.ELException ex) {
78             throw new ELException JavaDoc(ex);
79         }
80         return new ExpressionImpl(expr, pageContext);
81     }
82
83      public Object JavaDoc evaluate(String JavaDoc expression,
84                             Class JavaDoc expectedType,
85                             VariableResolver JavaDoc vResolver,
86                             FunctionMapper JavaDoc fMapper )
87                 throws ELException JavaDoc {
88
89         ELContextImpl elContext;
90         if (vResolver instanceof VariableResolverImpl) {
91             elContext = (ELContextImpl) pageContext.getELContext();
92         }
93         else {
94             // The provided variable Resolver is a custom resolver,
95
// wrap it with a ELResolver
96
elContext = new ELContextImpl(new ELResolverWrapper(vResolver));
97         }
98
99         javax.el.FunctionMapper fm = new FunctionMapperWrapper(fMapper);
100         elContext.setFunctionMapper(fm);
101         ExpressionFactory fac = JspApplicationContextImpl.expressionFactory;
102         Object JavaDoc value;
103         try {
104             ValueExpression expr = fac.createValueExpression(
105                                  elContext,
106                                  expression,
107                                  expectedType);
108             value = expr.getValue(elContext);
109         } catch (javax.el.ELException ex) {
110             throw new ELException JavaDoc(ex);
111         }
112         return value;
113     }
114
115     static private class ExpressionImpl extends Expression {
116
117         private ValueExpression valueExpr;
118         private PageContext JavaDoc pageContext;
119
120         ExpressionImpl(ValueExpression valueExpr,
121                        PageContext JavaDoc pageContext) {
122             this.valueExpr = valueExpr;
123             this.pageContext = pageContext;
124         }
125
126         public Object JavaDoc evaluate(VariableResolver JavaDoc vResolver) throws ELException JavaDoc {
127
128             ELContext elContext;
129             if (vResolver instanceof VariableResolverImpl) {
130                 elContext = pageContext.getELContext();
131             }
132             else {
133                 // The provided variable Resolver is a custom resolver,
134
// wrap it with a ELResolver
135
elContext = new ELContextImpl(new ELResolverWrapper(vResolver));
136             }
137             try {
138                 return valueExpr.getValue(elContext);
139             } catch (javax.el.ELException ex) {
140                 throw new ELException JavaDoc(ex);
141             }
142         }
143     }
144
145     private static class FunctionMapperWrapper
146         extends javax.el.FunctionMapper {
147
148         private FunctionMapper JavaDoc mapper;
149
150         FunctionMapperWrapper(FunctionMapper JavaDoc mapper) {
151             this.mapper = mapper;
152         }
153
154         public java.lang.reflect.Method JavaDoc resolveFunction(String JavaDoc prefix,
155                                                         String JavaDoc localName) {
156             return mapper.resolveFunction(prefix, localName);
157         }
158     }
159
160     private static class ELResolverWrapper extends ELResolver {
161         private VariableResolver JavaDoc vResolver;
162
163         ELResolverWrapper(VariableResolver JavaDoc vResolver) {
164             this.vResolver = vResolver;
165         }
166
167         public Object JavaDoc getValue(ELContext context,
168                                Object JavaDoc base,
169                                Object JavaDoc property)
170                 throws javax.el.ELException {
171             if (base == null) {
172                 context.setPropertyResolved(true);
173                 try {
174                     return vResolver.resolveVariable(property.toString());
175                 } catch (ELException JavaDoc ex) {
176                     throw new javax.el.ELException(ex);
177                 }
178             }
179             return null;
180         }
181
182         public Class JavaDoc getType(ELContext context,
183                              Object JavaDoc base,
184                              Object JavaDoc property)
185                 throws javax.el.ELException {
186             return null;
187         }
188
189         public void setValue(ELContext context,
190                              Object JavaDoc base,
191                              Object JavaDoc property,
192                              Object JavaDoc value)
193                 throws javax.el.ELException {
194         }
195
196         public boolean isReadOnly(ELContext context,
197                                   Object JavaDoc base,
198                                   Object JavaDoc property)
199                 throws javax.el.ELException {
200             return false;
201         }
202
203         public Iterator JavaDoc getFeatureDescriptors(ELContext context,
204                                               Object JavaDoc base) {
205             return null;
206         }
207
208         public Class JavaDoc getCommonPropertyType(ELContext context,
209                                            Object JavaDoc base) {
210             return null;
211         }
212     }
213 }
214
Popular Tags