KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 1998-2003 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.*;
33
34 import javax.el.ELContext;
35 import javax.el.ELException;
36 import javax.el.ExpressionFactory;
37 import javax.el.MethodExpression;
38 import javax.el.ValueExpression;
39 import java.math.BigDecimal JavaDoc;
40 import java.math.BigInteger JavaDoc;
41 import java.util.HashMap JavaDoc;
42
43 /**
44  * Represents an EL expression factory
45  */

46 public class JspExpressionFactoryImpl extends ExpressionFactory {
47   private static final HashMap JavaDoc<Class JavaDoc,CoerceType> _coerceMap
48     = new HashMap JavaDoc<Class JavaDoc,CoerceType>();
49   
50   private final JspApplicationContextImpl _jspApplicationContext;
51
52   JspExpressionFactoryImpl(JspApplicationContextImpl jspApplicationContext)
53   {
54     _jspApplicationContext = jspApplicationContext;
55   }
56   
57   public Object JavaDoc coerceToType(Object JavaDoc obj, Class JavaDoc<?> targetType)
58     throws ELException
59   {
60     CoerceType type = _coerceMap.get(targetType);
61
62     if (type == null)
63       return obj;
64
65     switch (type) {
66     case BOOLEAN:
67       return Expr.toBoolean(obj, null) ? Boolean.FALSE : Boolean.TRUE;
68     case CHARACTER:
69       return Expr.toCharacter(obj, null);
70     case BYTE:
71       return new Byte JavaDoc((byte) Expr.toLong(obj, null));
72     case SHORT:
73       return new Short JavaDoc((short) Expr.toLong(obj, null));
74     case INTEGER:
75       return new Integer JavaDoc((int) Expr.toLong(obj, null));
76     case LONG:
77       return new Long JavaDoc(Expr.toLong(obj, null));
78     case FLOAT:
79       return new Float JavaDoc((float) Expr.toDouble(obj, null));
80     case DOUBLE:
81       return new Double JavaDoc(Expr.toDouble(obj, null));
82     case STRING:
83       if (obj == null)
84     return "";
85       else
86     return obj.toString();
87     case BIG_DECIMAL:
88       return Expr.toBigDecimal(obj, null);
89     case BIG_INTEGER:
90       return Expr.toBigInteger(obj, null);
91     }
92
93     return null;
94   }
95
96   public MethodExpression
97     createMethodExpression(ELContext context,
98                String JavaDoc expression,
99                Class JavaDoc<?> expectedReturnType,
100                Class JavaDoc<?>[] expectedParamTypes)
101     throws ELException
102   {
103     JspELParser parser = new JspELParser(context, expression);
104
105     Expr expr = parser.parse();
106
107     return new MethodExpressionImpl(expr, expression,
108                     expectedReturnType,
109                     expectedParamTypes);
110   }
111
112   public ValueExpression
113     createValueExpression(ELContext context,
114               String JavaDoc expression,
115               Class JavaDoc<?> expectedType)
116     throws ELException
117   {
118     JspELParser parser = new JspELParser(context, expression);
119
120     Expr expr = parser.parse();
121
122     return createValueExpression(expr, expression, expectedType);
123   }
124
125   public static ValueExpression createValueExpression(Expr expr,
126                               String JavaDoc expression,
127                               Class JavaDoc<?> expectedType)
128   {
129     CoerceType type = _coerceMap.get(expectedType);
130
131     if (type == null)
132       return new ObjectValueExpression(expr, expression);
133
134     switch (type) {
135     case BOOLEAN:
136       return new BooleanValueExpression(expr, expression);
137     case CHARACTER:
138       return new CharacterValueExpression(expr, expression);
139     case BYTE:
140       return new ByteValueExpression(expr, expression);
141     case SHORT:
142       return new ShortValueExpression(expr, expression);
143     case INTEGER:
144       return new IntegerValueExpression(expr, expression);
145     case LONG:
146       return new LongValueExpression(expr, expression);
147     case FLOAT:
148       return new FloatValueExpression(expr, expression);
149     case DOUBLE:
150       return new DoubleValueExpression(expr, expression);
151     case STRING:
152       return new StringValueExpression(expr, expression);
153     case BIG_DECIMAL:
154       return new BigDecimalValueExpression(expr, expression);
155     case BIG_INTEGER:
156       return new BigIntegerValueExpression(expr, expression);
157     }
158
159     return new ObjectValueExpression(expr, expression);
160   }
161
162   public ValueExpression
163     createValueExpression(Object JavaDoc instance,
164               Class JavaDoc<?> expectedType)
165     throws ELException
166   {
167     throw new UnsupportedOperationException JavaDoc();
168   }
169
170   public String JavaDoc toString()
171   {
172     return "JspExpressionFactoryImpl[" + _jspApplicationContext.getWebApp() + "]";
173   }
174
175   private enum CoerceType {
176     BOOLEAN,
177     CHARACTER,
178     STRING,
179     INTEGER,
180     DOUBLE,
181     LONG,
182     FLOAT,
183     SHORT,
184     BYTE,
185     BIG_INTEGER,
186     BIG_DECIMAL,
187     VOID
188   };
189
190   static {
191     _coerceMap.put(boolean.class, CoerceType.BOOLEAN);
192     _coerceMap.put(Boolean JavaDoc.class, CoerceType.BOOLEAN);
193     
194     _coerceMap.put(byte.class, CoerceType.BYTE);
195     _coerceMap.put(Byte JavaDoc.class, CoerceType.BYTE);
196     
197     _coerceMap.put(short.class, CoerceType.SHORT);
198     _coerceMap.put(Short JavaDoc.class, CoerceType.SHORT);
199     
200     _coerceMap.put(int.class, CoerceType.INTEGER);
201     _coerceMap.put(Integer JavaDoc.class, CoerceType.INTEGER);
202     
203     _coerceMap.put(long.class, CoerceType.LONG);
204     _coerceMap.put(Long JavaDoc.class, CoerceType.LONG);
205     
206     _coerceMap.put(float.class, CoerceType.FLOAT);
207     _coerceMap.put(Float JavaDoc.class, CoerceType.FLOAT);
208     
209     _coerceMap.put(double.class, CoerceType.DOUBLE);
210     _coerceMap.put(Double JavaDoc.class, CoerceType.DOUBLE);
211     
212     _coerceMap.put(char.class, CoerceType.CHARACTER);
213     _coerceMap.put(Character JavaDoc.class, CoerceType.CHARACTER);
214     
215     _coerceMap.put(String JavaDoc.class, CoerceType.STRING);
216     
217     _coerceMap.put(BigDecimal JavaDoc.class, CoerceType.BIG_DECIMAL);
218     _coerceMap.put(BigInteger JavaDoc.class, CoerceType.BIG_INTEGER);
219     
220     _coerceMap.put(void.class, CoerceType.VOID);
221   }
222 }
223
Popular Tags