1 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 41 public class JsfExpressionFactoryImpl extends ExpressionFactory { 42 private static final HashMap<Class ,CoerceType> _coerceMap 43 = new HashMap<Class ,CoerceType>(); 44 45 JsfExpressionFactoryImpl() 46 { 47 } 48 49 public Object coerceToType(Object obj, Class <?> 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 ((byte) Expr.toLong(obj, null)); 64 case SHORT: 65 return new Short ((short) Expr.toLong(obj, null)); 66 case INTEGER: 67 return new Integer ((int) Expr.toLong(obj, null)); 68 case LONG: 69 return new Long (Expr.toLong(obj, null)); 70 case FLOAT: 71 return new Float ((float) Expr.toDouble(obj, null)); 72 case DOUBLE: 73 return new Double (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 expression, 91 Class <?> expectedReturnType, 92 Class <?>[] 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 expression, 107 Class <?> 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 expression, 119 Class <?> 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 instance, 156 Class <?> expectedType) 157 throws ELException 158 { 159 throw new UnsupportedOperationException (); 160 } 161 162 public String 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 .class, CoerceType.BOOLEAN); 185 186 _coerceMap.put(byte.class, CoerceType.BYTE); 187 _coerceMap.put(Byte .class, CoerceType.BYTE); 188 189 _coerceMap.put(short.class, CoerceType.SHORT); 190 _coerceMap.put(Short .class, CoerceType.SHORT); 191 192 _coerceMap.put(int.class, CoerceType.INTEGER); 193 _coerceMap.put(Integer .class, CoerceType.INTEGER); 194 195 _coerceMap.put(long.class, CoerceType.LONG); 196 _coerceMap.put(Long .class, CoerceType.LONG); 197 198 _coerceMap.put(float.class, CoerceType.FLOAT); 199 _coerceMap.put(Float .class, CoerceType.FLOAT); 200 201 _coerceMap.put(double.class, CoerceType.DOUBLE); 202 _coerceMap.put(Double .class, CoerceType.DOUBLE); 203 204 _coerceMap.put(char.class, CoerceType.CHARACTER); 205 _coerceMap.put(Character .class, CoerceType.CHARACTER); 206 207 _coerceMap.put(String .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 |