1 29 30 package com.caucho.el; 31 32 import javax.el.ELContext; 33 import javax.el.ELException; 34 import java.util.HashMap ; 35 36 39 abstract public class Marshall { 40 private static final HashMap <Class ,Marshall> ARG_MAP 41 = new HashMap <Class ,Marshall>(); 42 43 public static Marshall create(Class arg) 44 { 45 Marshall marshall = ARG_MAP.get(arg); 46 47 if (marshall != null) 48 return marshall; 49 else 50 return OBJECT; 51 } 52 53 abstract public Object marshall(Expr expr, ELContext env) 54 throws ELException; 55 56 public static final Marshall BOOLEAN = new Marshall() { 57 public Object marshall(Expr expr, ELContext env) 58 throws ELException 59 { 60 return new Boolean ((boolean) expr.evalBoolean(env)); 61 } 62 }; 63 64 public static final Marshall BYTE = new Marshall() { 65 public Object marshall(Expr expr, ELContext env) 66 throws ELException 67 { 68 return new Byte ((byte) expr.evalLong(env)); 69 } 70 }; 71 72 public static final Marshall SHORT = new Marshall() { 73 public Object marshall(Expr expr, ELContext env) 74 throws ELException 75 { 76 return new Short ((short) expr.evalLong(env)); 77 } 78 }; 79 80 public static final Marshall INTEGER = new Marshall() { 81 public Object marshall(Expr expr, ELContext env) 82 throws ELException 83 { 84 return new Integer ((int) expr.evalLong(env)); 85 } 86 }; 87 88 public static final Marshall LONG = new Marshall() { 89 public Object marshall(Expr expr, ELContext env) 90 throws ELException 91 { 92 return new Long (expr.evalLong(env)); 93 } 94 }; 95 96 public static final Marshall FLOAT = new Marshall() { 97 public Object marshall(Expr expr, ELContext env) 98 throws ELException 99 { 100 return new Float ((float) expr.evalDouble(env)); 101 } 102 }; 103 104 public static final Marshall DOUBLE = new Marshall() { 105 public Object marshall(Expr expr, ELContext env) 106 throws ELException 107 { 108 return new Double (expr.evalDouble(env)); 109 } 110 }; 111 112 public static final Marshall STRING = new Marshall() { 113 public Object marshall(Expr expr, ELContext env) 114 throws ELException 115 { 116 return expr.evalString(env); 117 } 118 }; 119 120 public static final Marshall CHARACTER = new Marshall() { 121 public Object marshall(Expr expr, ELContext env) 122 throws ELException 123 { 124 String s = expr.evalString(env); 125 126 if (s == null || s.length() == 0) 127 return null; 128 else 129 return new Character (s.charAt(0)); 130 } 131 }; 132 133 public static final Marshall OBJECT = new Marshall() { 134 public Object marshall(Expr expr, ELContext env) 135 throws ELException 136 { 137 return expr.getValue(env); 138 } 139 }; 140 141 static { 142 ARG_MAP.put(boolean.class, BOOLEAN); 143 ARG_MAP.put(Boolean .class, BOOLEAN); 144 145 ARG_MAP.put(byte.class, BYTE); 146 ARG_MAP.put(Byte .class, BYTE); 147 148 ARG_MAP.put(short.class, SHORT); 149 ARG_MAP.put(Short .class, SHORT); 150 151 ARG_MAP.put(int.class, INTEGER); 152 ARG_MAP.put(Integer .class, INTEGER); 153 154 ARG_MAP.put(long.class, LONG); 155 ARG_MAP.put(Long .class, LONG); 156 157 ARG_MAP.put(float.class, FLOAT); 158 ARG_MAP.put(Float .class, FLOAT); 159 160 ARG_MAP.put(double.class, DOUBLE); 161 ARG_MAP.put(Double .class, DOUBLE); 162 163 ARG_MAP.put(char.class, CHARACTER); 164 ARG_MAP.put(Character .class, CHARACTER); 165 166 ARG_MAP.put(String .class, STRING); 167 } 168 } 169 | Popular Tags |