001: /*
002: * Copyright (c) 1998-2003 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsf.el;
031:
032: import com.caucho.el.*;
033:
034: import javax.el.ELContext;
035: import javax.el.ELException;
036: import javax.el.ExpressionFactory;
037: import javax.el.MethodExpression;
038: import javax.el.ValueExpression;
039: import java.math.BigDecimal;
040: import java.math.BigInteger;
041: import java.util.HashMap;
042:
043: /**
044: * Represents an EL expression factory
045: */
046: public class JsfExpressionFactoryImpl extends ExpressionFactory {
047: private static final HashMap<Class, CoerceType> _coerceMap = new HashMap<Class, CoerceType>();
048:
049: public JsfExpressionFactoryImpl() {
050: }
051:
052: public Object coerceToType(Object obj, Class<?> targetType)
053: throws ELException {
054: CoerceType type = _coerceMap.get(targetType);
055:
056: if (type == null)
057: return obj;
058:
059: switch (type) {
060: case BOOLEAN:
061: return Expr.toBoolean(obj, null) ? Boolean.FALSE
062: : Boolean.TRUE;
063: case CHARACTER:
064: return Expr.toCharacter(obj, null);
065: case BYTE:
066: return new Byte((byte) Expr.toLong(obj, null));
067: case SHORT:
068: return new Short((short) Expr.toLong(obj, null));
069: case INTEGER:
070: return new Integer((int) Expr.toLong(obj, null));
071: case LONG:
072: return new Long(Expr.toLong(obj, null));
073: case FLOAT:
074: return new Float((float) Expr.toDouble(obj, null));
075: case DOUBLE:
076: return new Double(Expr.toDouble(obj, null));
077: case STRING:
078: if (obj == null)
079: return "";
080: else
081: return obj.toString();
082: case BIG_DECIMAL:
083: return Expr.toBigDecimal(obj, null);
084: case BIG_INTEGER:
085: return Expr.toBigInteger(obj, null);
086: }
087:
088: return null;
089: }
090:
091: public MethodExpression createMethodExpression(ELContext context,
092: String expression, Class<?> expectedReturnType,
093: Class<?>[] expectedParamTypes) throws ELException {
094: JsfELParser parser = new JsfELParser(context, expression);
095:
096: Expr expr = parser.parse();
097:
098: return new MethodExpressionImpl(expr, expression,
099: expectedReturnType, expectedParamTypes);
100: }
101:
102: public ValueExpression createValueExpression(ELContext context,
103: String expression, Class<?> expectedType)
104: throws ELException {
105: JsfELParser parser = new JsfELParser(context, expression);
106:
107: Expr expr = parser.parse();
108:
109: return createValueExpression(expr, expression, expectedType);
110: }
111:
112: public static ValueExpression createValueExpression(Expr expr,
113: String expression, Class<?> expectedType) {
114: CoerceType type = _coerceMap.get(expectedType);
115:
116: if (type == null)
117: return new ObjectValueExpression(expr, expression,
118: expectedType);
119:
120: switch (type) {
121: case BOOLEAN:
122: return new BooleanValueExpression(expr, expression);
123: case CHARACTER:
124: return new CharacterValueExpression(expr, expression);
125: case BYTE:
126: return new ByteValueExpression(expr, expression);
127: case SHORT:
128: return new ShortValueExpression(expr, expression);
129: case INTEGER:
130: return new IntegerValueExpression(expr, expression);
131: case LONG:
132: return new LongValueExpression(expr, expression);
133: case FLOAT:
134: return new FloatValueExpression(expr, expression);
135: case DOUBLE:
136: return new DoubleValueExpression(expr, expression);
137: case STRING:
138: return new StringValueExpression(expr, expression);
139: case BIG_DECIMAL:
140: return new BigDecimalValueExpression(expr, expression);
141: case BIG_INTEGER:
142: return new BigIntegerValueExpression(expr, expression);
143: }
144:
145: return new ObjectValueExpression(expr, expression);
146: }
147:
148: public ValueExpression createValueExpression(Object instance,
149: Class<?> expectedType) throws ELException {
150: throw new UnsupportedOperationException();
151: }
152:
153: public String toString() {
154: return "JsfExpressionFactoryImpl[]";
155: }
156:
157: private enum CoerceType {
158: BOOLEAN, CHARACTER, STRING, INTEGER, DOUBLE, LONG, FLOAT, SHORT, BYTE, BIG_INTEGER, BIG_DECIMAL, VOID
159: };
160:
161: static {
162: _coerceMap.put(boolean.class, CoerceType.BOOLEAN);
163: _coerceMap.put(Boolean.class, CoerceType.BOOLEAN);
164:
165: _coerceMap.put(byte.class, CoerceType.BYTE);
166: _coerceMap.put(Byte.class, CoerceType.BYTE);
167:
168: _coerceMap.put(short.class, CoerceType.SHORT);
169: _coerceMap.put(Short.class, CoerceType.SHORT);
170:
171: _coerceMap.put(int.class, CoerceType.INTEGER);
172: _coerceMap.put(Integer.class, CoerceType.INTEGER);
173:
174: _coerceMap.put(long.class, CoerceType.LONG);
175: _coerceMap.put(Long.class, CoerceType.LONG);
176:
177: _coerceMap.put(float.class, CoerceType.FLOAT);
178: _coerceMap.put(Float.class, CoerceType.FLOAT);
179:
180: _coerceMap.put(double.class, CoerceType.DOUBLE);
181: _coerceMap.put(Double.class, CoerceType.DOUBLE);
182:
183: _coerceMap.put(char.class, CoerceType.CHARACTER);
184: _coerceMap.put(Character.class, CoerceType.CHARACTER);
185:
186: _coerceMap.put(String.class, CoerceType.STRING);
187:
188: _coerceMap.put(BigDecimal.class, CoerceType.BIG_DECIMAL);
189: _coerceMap.put(BigInteger.class, CoerceType.BIG_INTEGER);
190:
191: _coerceMap.put(void.class, CoerceType.VOID);
192: }
193: }
|