1 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 ; 40 import java.math.BigInteger ; 41 import java.util.HashMap ; 42 43 46 public class JspExpressionFactoryImpl extends ExpressionFactory { 47 private static final HashMap <Class ,CoerceType> _coerceMap 48 = new HashMap <Class ,CoerceType>(); 49 50 private final JspApplicationContextImpl _jspApplicationContext; 51 52 JspExpressionFactoryImpl(JspApplicationContextImpl jspApplicationContext) 53 { 54 _jspApplicationContext = jspApplicationContext; 55 } 56 57 public Object coerceToType(Object obj, Class <?> 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 ((byte) Expr.toLong(obj, null)); 72 case SHORT: 73 return new Short ((short) Expr.toLong(obj, null)); 74 case INTEGER: 75 return new Integer ((int) Expr.toLong(obj, null)); 76 case LONG: 77 return new Long (Expr.toLong(obj, null)); 78 case FLOAT: 79 return new Float ((float) Expr.toDouble(obj, null)); 80 case DOUBLE: 81 return new Double (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 expression, 99 Class <?> expectedReturnType, 100 Class <?>[] 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 expression, 115 Class <?> 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 expression, 127 Class <?> 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 instance, 164 Class <?> expectedType) 165 throws ELException 166 { 167 throw new UnsupportedOperationException (); 168 } 169 170 public String 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 .class, CoerceType.BOOLEAN); 193 194 _coerceMap.put(byte.class, CoerceType.BYTE); 195 _coerceMap.put(Byte .class, CoerceType.BYTE); 196 197 _coerceMap.put(short.class, CoerceType.SHORT); 198 _coerceMap.put(Short .class, CoerceType.SHORT); 199 200 _coerceMap.put(int.class, CoerceType.INTEGER); 201 _coerceMap.put(Integer .class, CoerceType.INTEGER); 202 203 _coerceMap.put(long.class, CoerceType.LONG); 204 _coerceMap.put(Long .class, CoerceType.LONG); 205 206 _coerceMap.put(float.class, CoerceType.FLOAT); 207 _coerceMap.put(Float .class, CoerceType.FLOAT); 208 209 _coerceMap.put(double.class, CoerceType.DOUBLE); 210 _coerceMap.put(Double .class, CoerceType.DOUBLE); 211 212 _coerceMap.put(char.class, CoerceType.CHARACTER); 213 _coerceMap.put(Character .class, CoerceType.CHARACTER); 214 215 _coerceMap.put(String .class, CoerceType.STRING); 216 217 _coerceMap.put(BigDecimal .class, CoerceType.BIG_DECIMAL); 218 _coerceMap.put(BigInteger .class, CoerceType.BIG_INTEGER); 219 220 _coerceMap.put(void.class, CoerceType.VOID); 221 } 222 } 223 | Popular Tags |