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