01: package org.mdarad.framework.util.struts;
02:
03: import java.net.URI;
04: import java.util.Currency;
05: import java.util.Iterator;
06: import java.util.Locale;
07: import java.util.SortedMap;
08: import java.util.TreeMap;
09:
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: // do nothing, leave null
37: }
38: }
39: }
40: }
41:
42: return output;
43: }
44:
45: static public SortedMap convertStringMap(SortedMap map,
46: SortedMap oldMap, Class c) {
47: SortedMap output = null;
48: if (map != null) {
49: output = new TreeMap(map.comparator());
50: Iterator keyIterator = map.keySet().iterator();
51: while (keyIterator.hasNext()) {
52: Object key = keyIterator.next();
53: Object value = map.get(key);
54: if (value instanceof String) {
55: value = ConvertUtils.convert((String) value, c);
56: } else if (value instanceof FormFile) {
57: value = PrimitiveToStrutsBeanFormTypeMappings
58: .getBlobValue((Blob) oldMap.get(key),
59: (FormFile) value);
60: }
61: output.put(key, value);
62: }
63: }
64: return output;
65: }
66: }
|