KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > el > ExpressionEvaluatorImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp.el;
31
32 import com.caucho.el.Expr;
33
34 import javax.el.ELContext;
35 import javax.el.ELResolver;
36 import javax.servlet.jsp.el.Expression JavaDoc;
37 import javax.servlet.jsp.el.ExpressionEvaluator JavaDoc;
38 import javax.servlet.jsp.el.FunctionMapper JavaDoc;
39 import java.lang.reflect.Method JavaDoc;
40
41 /**
42  * Implementation of the expression evaluator.
43  */

44 public class ExpressionEvaluatorImpl extends ExpressionEvaluator JavaDoc {
45   private ELContext _elContext;
46   
47   /**
48    * Creates the expression evaluator.
49    */

50   public ExpressionEvaluatorImpl(ELContext elContext)
51   {
52     _elContext = elContext;
53   }
54
55   /**
56    * Evaluates an expression.
57    */

58   public Object JavaDoc evaluate(String JavaDoc expression, Class JavaDoc expectedType,
59              javax.servlet.jsp.el.VariableResolver JavaDoc resolver,
60              FunctionMapper JavaDoc funMapper)
61     throws javax.servlet.jsp.el.ELException JavaDoc
62   {
63     Expression JavaDoc expr = parseExpression(expression, expectedType, funMapper);
64       
65     return expr.evaluate(resolver);
66   }
67
68   /**
69    * Parses an expression.
70    */

71   public Expression JavaDoc parseExpression(String JavaDoc expression,
72                     Class JavaDoc expectedType,
73                     FunctionMapper JavaDoc funMapper)
74     throws javax.servlet.jsp.el.ELException JavaDoc
75   {
76     ELContext elContext;
77
78     if (funMapper != null)
79       elContext = new ParseELContext(funMapper);
80     else
81       elContext = _elContext;
82     
83     JspELParser parser = new JspELParser(elContext, expression);
84
85     // parser.setFunctionMapper(funMapper);
86

87     try {
88       Expr expr = parser.parse();
89
90       return new ExpressionImpl(expr);
91     } catch (com.caucho.el.ELParseException e) {
92       throw new javax.servlet.jsp.el.ELParseException JavaDoc(e.getMessage());
93     }
94   }
95
96   public class ParseELContext extends ELContext
97   {
98     private javax.el.FunctionMapper _funMapper;
99
100     ParseELContext(FunctionMapper JavaDoc funMapper)
101     {
102       _funMapper = new FunctionMapperAdapter(funMapper);
103     }
104     
105     public ELResolver getELResolver()
106     {
107       return _elContext.getELResolver();
108     }
109
110     public javax.el.FunctionMapper getFunctionMapper()
111     {
112       return _funMapper;
113     }
114
115     public javax.el.VariableMapper getVariableMapper()
116     {
117       return _elContext.getVariableMapper();
118     }
119   }
120
121   public class FunctionMapperAdapter extends javax.el.FunctionMapper
122   {
123     private FunctionMapper JavaDoc _funMap;
124
125     FunctionMapperAdapter(FunctionMapper JavaDoc funMap)
126     {
127       _funMap = funMap;
128     }
129     
130     public Method JavaDoc resolveFunction(String JavaDoc prefix, String JavaDoc localName)
131     {
132       return _funMap.resolveFunction(prefix, localName);
133     }
134   }
135 }
136
Popular Tags