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.jsp.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 JspExpressionFactoryImpl extends ExpressionFactory {
047: private static final HashMap<Class, CoerceType> _coerceMap = new HashMap<Class, CoerceType>();
048:
049: private final JspApplicationContextImpl _jspApplicationContext;
050:
051: JspExpressionFactoryImpl(
052: JspApplicationContextImpl jspApplicationContext) {
053: _jspApplicationContext = jspApplicationContext;
054: }
055:
056: public Object coerceToType(Object obj, Class<?> targetType)
057: throws ELException {
058: CoerceType type = _coerceMap.get(targetType);
059:
060: if (type == null)
061: return obj;
062:
063: switch (type) {
064: case BOOLEAN:
065: return Expr.toBoolean(obj, null) ? Boolean.TRUE
066: : Boolean.FALSE;
067: case CHARACTER:
068: return Expr.toCharacter(obj, null);
069: case BYTE:
070: return new Byte((byte) Expr.toLong(obj, null));
071: case SHORT:
072: return new Short((short) Expr.toLong(obj, null));
073: case INTEGER:
074: return new Integer((int) Expr.toLong(obj, null));
075: case LONG:
076: return new Long(Expr.toLong(obj, null));
077: case FLOAT:
078: return new Float((float) Expr.toDouble(obj, null));
079: case DOUBLE:
080: return new Double(Expr.toDouble(obj, null));
081: case STRING:
082: if (obj == null)
083: return "";
084: else
085: return obj.toString();
086: case BIG_DECIMAL:
087: return Expr.toBigDecimal(obj, null);
088: case BIG_INTEGER:
089: return Expr.toBigInteger(obj, null);
090: }
091:
092: return null;
093: }
094:
095: public MethodExpression createMethodExpression(ELContext context,
096: String expression, Class<?> expectedReturnType,
097: Class<?>[] expectedParamTypes) throws ELException {
098: JspELParser parser = new JspELParser(context, expression);
099:
100: Expr expr = parser.parse();
101:
102: return new MethodExpressionImpl(expr, expression,
103: expectedReturnType, expectedParamTypes);
104: }
105:
106: public ValueExpression createValueExpression(ELContext context,
107: String expression, Class<?> expectedType)
108: throws ELException {
109: JspELParser parser = new JspELParser(context, expression);
110:
111: Expr expr = parser.parse();
112:
113: return createValueExpression(expr, expression, expectedType);
114: }
115:
116: public static ValueExpression createValueExpression(Expr expr,
117: String expression, Class<?> expectedType) {
118: CoerceType type = _coerceMap.get(expectedType);
119:
120: if (type == null)
121: return new ObjectValueExpression(expr, expression);
122:
123: switch (type) {
124: case BOOLEAN:
125: return new BooleanValueExpression(expr, expression);
126: case CHARACTER:
127: return new CharacterValueExpression(expr, expression);
128: case BYTE:
129: return new ByteValueExpression(expr, expression);
130: case SHORT:
131: return new ShortValueExpression(expr, expression);
132: case INTEGER:
133: return new IntegerValueExpression(expr, expression);
134: case LONG:
135: return new LongValueExpression(expr, expression);
136: case FLOAT:
137: return new FloatValueExpression(expr, expression);
138: case DOUBLE:
139: return new DoubleValueExpression(expr, expression);
140: case STRING:
141: return new StringValueExpression(expr, expression);
142: case BIG_DECIMAL:
143: return new BigDecimalValueExpression(expr, expression);
144: case BIG_INTEGER:
145: return new BigIntegerValueExpression(expr, expression);
146: }
147:
148: return new ObjectValueExpression(expr, expression);
149: }
150:
151: public ValueExpression createValueExpression(Object instance,
152: Class<?> expectedType) throws ELException {
153: throw new UnsupportedOperationException();
154: }
155:
156: public String toString() {
157: return "JspExpressionFactoryImpl["
158: + _jspApplicationContext.getWebApp() + "]";
159: }
160:
161: private enum CoerceType {
162: BOOLEAN, CHARACTER, STRING, INTEGER, DOUBLE, LONG, FLOAT, SHORT, BYTE, BIG_INTEGER, BIG_DECIMAL, VOID
163: };
164:
165: static {
166: _coerceMap.put(boolean.class, CoerceType.BOOLEAN);
167: _coerceMap.put(Boolean.class, CoerceType.BOOLEAN);
168:
169: _coerceMap.put(byte.class, CoerceType.BYTE);
170: _coerceMap.put(Byte.class, CoerceType.BYTE);
171:
172: _coerceMap.put(short.class, CoerceType.SHORT);
173: _coerceMap.put(Short.class, CoerceType.SHORT);
174:
175: _coerceMap.put(int.class, CoerceType.INTEGER);
176: _coerceMap.put(Integer.class, CoerceType.INTEGER);
177:
178: _coerceMap.put(long.class, CoerceType.LONG);
179: _coerceMap.put(Long.class, CoerceType.LONG);
180:
181: _coerceMap.put(float.class, CoerceType.FLOAT);
182: _coerceMap.put(Float.class, CoerceType.FLOAT);
183:
184: _coerceMap.put(double.class, CoerceType.DOUBLE);
185: _coerceMap.put(Double.class, CoerceType.DOUBLE);
186:
187: _coerceMap.put(char.class, CoerceType.CHARACTER);
188: _coerceMap.put(Character.class, CoerceType.CHARACTER);
189:
190: _coerceMap.put(String.class, CoerceType.STRING);
191:
192: _coerceMap.put(BigDecimal.class, CoerceType.BIG_DECIMAL);
193: _coerceMap.put(BigInteger.class, CoerceType.BIG_INTEGER);
194:
195: _coerceMap.put(void.class, CoerceType.VOID);
196: }
197: }
|