01: /*
02: ItsNat Java Web Application Framework
03: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
04: Author: Jose Maria Arranz Santamaria
05:
06: This program is free software: you can redistribute it and/or modify
07: it under the terms of the GNU Affero General Public License as published by
08: the Free Software Foundation, either version 3 of the License, or
09: (at your option) any later version. See the GNU Affero General Public
10: License for more details. See the copy of the GNU Affero General Public License
11: included in this program. If not, see <http://www.gnu.org/licenses/>.
12: */
13:
14: package org.itsnat.impl.core.conv;
15:
16: import org.itsnat.core.ItsNatException;
17: import java.util.HashMap;
18: import java.util.Map;
19:
20: /**
21: *
22: * @author jmarranz
23: */
24: public abstract class StringToObjectConverter {
25: public static final Map converters = new HashMap(); // No sincronizamos porque sólo leeremos
26:
27: static {
28: addConverter(new StringToStringConverter());
29: addConverter(new StringToBooleanConverter());
30: addConverter(new StringToByteConverter());
31: addConverter(new StringToCharacterConverter());
32: addConverter(new StringToShortConverter());
33: addConverter(new StringToIntegerConverter());
34: addConverter(new StringToLongConverter());
35: addConverter(new StringToFloatConverter());
36: addConverter(new StringToDoubleConverter());
37: }
38:
39: /** Creates a new instance of StringCoverter */
40: public StringToObjectConverter() {
41: }
42:
43: public static void addConverter(StringToObjectConverter conv) {
44: converters.put(conv.getClassTarget(), conv);
45: Class wrapper = conv.getClassTargetWrapper();
46: if (wrapper != null)
47: converters.put(wrapper, conv);
48: }
49:
50: public static Object convert(String value, Class type) {
51: if (value == null)
52: throw new ItsNatException("Unexpected null value");
53: StringToObjectConverter conv = (StringToObjectConverter) converters
54: .get(type);
55: if (conv == null)
56: throw new ItsNatException("Class type not supported: "
57: + type.getName());
58: return conv.convert(value);
59: }
60:
61: public abstract Class getClassTarget();
62:
63: public abstract Class getClassTargetWrapper();
64:
65: public abstract Object convert(String value);
66: }
|