1 package org.mdarad.framework.util.struts; 2 3 import java.net.URI ; 4 import java.util.Currency ; 5 import java.util.Iterator ; 6 import java.util.Locale ; 7 import java.util.SortedMap ; 8 import java.util.TreeMap ; 9 10 import org.apache.commons.beanutils.ConvertUtils; 11 import org.apache.commons.beanutils.Converter; 12 import org.apache.struts.upload.FormFile; 13 import org.dataisland.primitives.datatype.Blob; 14 import org.dataisland.primitives.format.CurrencyDataIslandFormat; 15 import org.dataisland.primitives.format.LocaleDataIslandFormat; 16 import org.dataisland.primitives.format.URIDataIslandFormat; 17 18 public class StrutsConverter implements Converter { 19 20 public Object convert(Class c, Object o) { 21 Object output = null; 22 23 if(o instanceof String ) { 24 String string = (String ) o; 25 if(c == Locale .class) { 26 output = LocaleDataIslandFormat.deserialize(string); 27 } else if (c == Currency .class) { 28 output = CurrencyDataIslandFormat.deserialize(string); 29 } else if (c == URI .class) { 30 output = URIDataIslandFormat.deserialize(string); 31 } else if (c == Integer .class) { 32 if(string != null && !"".equals(string)) { 33 try { 34 output = new Integer (string); 35 } catch(NumberFormatException nfe) { 36 } 38 } 39 } 40 } 41 42 return output; 43 } 44 45 static public SortedMap convertStringMap(SortedMap map, SortedMap oldMap, Class c) { 46 SortedMap output = null; 47 if(map != null) { 48 output = new TreeMap (map.comparator()); 49 Iterator keyIterator = map.keySet().iterator(); 50 while(keyIterator.hasNext()) { 51 Object key = keyIterator.next(); 52 Object value = map.get(key); 53 if(value instanceof String ) { 54 value = ConvertUtils.convert((String ) value, c); 55 } else if(value instanceof FormFile) { 56 value = PrimitiveToStrutsBeanFormTypeMappings.getBlobValue((Blob) oldMap.get(key), (FormFile) value); 57 } 58 output.put(key, value); 59 } 60 } 61 return output; 62 } 63 } | Popular Tags |