1 package jodd.bean; 2 3 import java.util.HashMap; 4 5 import jodd.bean.converters.BigDecimalConverter; 6 import jodd.bean.converters.BigIntegerConverter; 7 import jodd.bean.converters.BooleanConverter; 8 import jodd.bean.converters.ByteConverter; 9 import jodd.bean.converters.CharacterConverter; 10 import jodd.bean.converters.DoubleConverter; 11 import jodd.bean.converters.FloatConverter; 12 import jodd.bean.converters.IntegerConverter; 13 import jodd.bean.converters.LongConverter; 14 import jodd.bean.converters.SqlDateConverter; 15 import jodd.bean.converters.SqlTimeConverter; 16 import jodd.bean.converters.SqlTimestampConverter; 17 import jodd.bean.converters.StringArrayConverter; 18 import jodd.bean.converters.StringConverter; 19 import jodd.bean.converters.UploadedFileConverter; 20 21 27 public class ConvertersManager { 28 29 31 private static HashMap convmap = new HashMap(); 32 33 static { 34 registerDefaults(); 35 } 36 37 40 public static void registerDefaults() { 41 convmap.clear(); 42 register(String.class, new StringConverter()); 43 register(String[].class, new StringArrayConverter()); 44 register(Integer.class, new IntegerConverter()); 45 register(int.class, new IntegerConverter()); 46 register(Long.class, new LongConverter()); 47 register(long.class, new LongConverter()); 48 register(Byte.class, new ByteConverter()); 49 register(byte.class, new ByteConverter()); 50 register(Boolean.class, new BooleanConverter()); 51 register(boolean.class, new BooleanConverter()); 52 register(java.math.BigDecimal.class, new BigDecimalConverter()); 53 register(java.math.BigInteger.class, new BigIntegerConverter()); 54 register(Float.class, new FloatConverter()); 55 register(float.class, new FloatConverter()); 56 register(Double.class, new DoubleConverter()); 57 register(double.class, new DoubleConverter()); 58 register(Character.class, new CharacterConverter()); 59 register(char.class, new CharacterConverter()); 60 register(java.sql.Date.class, new SqlDateConverter()); 61 register(java.sql.Time.class, new SqlTimeConverter()); 62 register(java.sql.Timestamp.class, new SqlTimestampConverter()); 63 register(jodd.servlet.UploadedFile.class, new UploadedFileConverter()); 64 } 65 66 72 public static void register(Class key, Converter conv) { 73 convmap.put(key, conv); 74 } 75 76 83 public static Converter get(Class key) { 84 return (Converter) convmap.get(key); 85 } 86 87 89 98 public static Object convert(Object value, Class c) throws IllegalArgumentException { 99 Converter conv = (Converter) convmap.get(c); 100 if (conv == null) { 101 throw new IllegalArgumentException("No registered converter for " + c.getName()); 102 } 103 Object result = null; 104 try { 105 result = conv.convert(value); 106 } catch (IllegalArgumentException iaex) { 107 throw iaex; 108 } 109 return result; 110 } 111 112 113 } 114 | Popular Tags |