1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.exolab.castor.xml.parsing.primitive.objects;
16
17 import java.math.BigDecimal;
18 import java.math.BigInteger;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25
26
27
28 public class PrimitiveObjectFactory {
29
30 private Map<Class<?>, Class<? extends PrimitiveObject>> typeHandlers =
31 new HashMap<Class<?>, Class<? extends PrimitiveObject>>();
32
33 private final Log logger = LogFactory.getLog(this.getClass());
34
35 private static PrimitiveObjectFactory primitiveObjectFactory;
36
37 public static synchronized PrimitiveObjectFactory getInstance() {
38 if (primitiveObjectFactory == null) {
39 primitiveObjectFactory = new PrimitiveObjectFactory();
40 }
41 return primitiveObjectFactory;
42 }
43
44 private PrimitiveObjectFactory() {
45 typeHandlers.put(String.class, PrimitiveString.class);
46
47 typeHandlers.put(Enum.class, PrimitiveEnum.class);
48
49 typeHandlers.put(Integer.TYPE, PrimitiveInteger.class);
50 typeHandlers.put(Integer.class, PrimitiveInteger.class);
51
52 typeHandlers.put(Boolean.TYPE, PrimitiveBoolean.class);
53 typeHandlers.put(Boolean.class, PrimitiveBoolean.class);
54
55 typeHandlers.put(Double.TYPE, PrimitiveDouble.class);
56 typeHandlers.put(Double.class, PrimitiveDouble.class);
57
58 typeHandlers.put(Long.TYPE, PrimitiveLong.class);
59 typeHandlers.put(Long.class, PrimitiveLong.class);
60
61 typeHandlers.put(Character.TYPE, PrimitiveChar.class);
62 typeHandlers.put(Character.class, PrimitiveChar.class);
63
64 typeHandlers.put(Short.TYPE, PrimitiveShort.class);
65 typeHandlers.put(Short.class, PrimitiveShort.class);
66
67 typeHandlers.put(Float.TYPE, PrimitiveFloat.class);
68 typeHandlers.put(Float.class, PrimitiveFloat.class);
69
70 typeHandlers.put(Byte.TYPE, PrimitiveByte.class);
71 typeHandlers.put(Byte.class, PrimitiveByte.class);
72
73 typeHandlers.put(BigInteger.class, PrimitiveBigInteger.class);
74
75 typeHandlers.put(BigDecimal.class, PrimitiveBigDecimal.class);
76 }
77
78
79
80
81
82
83 public Object getObject(Class<?> type, String value) {
84
85 PrimitiveObject handler = lookupHandler(type);
86
87 if (handler == null) {
88 handler = getDefaultHandler();
89 }
90
91 if (type != String.class) {
92 value = trimNumericValues(value);
93 }
94
95 return handler.getObject(type, value);
96 }
97
98
99
100
101
102
103
104
105 private PrimitiveObject lookupHandler(Class<?> type) {
106
107 PrimitiveObject instance = null;
108
109 if (type == null) {
110 return null;
111 }
112
113 Class<? extends PrimitiveObject> result = typeHandlers.get(type);
114
115 if (result == null) {
116 result = typeHandlers.get(type.getSuperclass());
117 }
118
119 if (result != null) {
120 try {
121 instance = result.newInstance();
122 } catch (InstantiationException e) {
123 this.logger.error("Problem instantiating an instance of " + result.getName());
124 e.printStackTrace();
125 } catch (IllegalAccessException e) {
126 this.logger.error("Problem accessing default constructor of " + result.getName());
127 e.printStackTrace();
128 }
129 }
130
131 return instance;
132 }
133
134
135
136
137
138
139
140 private String trimNumericValues(String value) {
141 if (value == null) {
142 return null;
143 }
144
145 return value.trim();
146 }
147
148
149
150
151
152
153 private PrimitiveObject getDefaultHandler() {
154 return new PrimitiveObject();
155 }
156
157 }