KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsf > application > JsfExpressionFactoryImpl


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, version 2,
11  * as published by the Free Software Foundation.
12  *
13  * Resin Open Source is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
16  * of NON-INFRINGEMENT. See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Resin Open Source; if not, write to the
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsf.application;
30
31 import java.math.*;
32 import java.util.*;
33
34 import javax.el.*;
35
36 import com.caucho.el.*;
37
38 /**
39  * Represents an EL expression factory
40  */

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