1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.ivata.mask.field;
39
40 import java.io.Serializable;
41 import java.math.BigDecimal;
42 import java.util.Date;
43 import java.util.HashMap;
44 import java.util.Map;
45
46 import javax.servlet.ServletException;
47
48 import com.ivata.mask.field.date.DateFieldValueConvertor;
49 import com.ivata.mask.field.number.NumberFieldValueConvertor;
50 import com.ivata.mask.util.SystemException;
51
52 /***
53 * @since ivata groupware 0.11 (17-Mar-2005)
54 * @author Colin MacLeod
55 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
56 * @version $Revision: 1.1 $
57 */
58
59 public class DefaultFieldValueConvertorFactory implements
60 FieldValueConvertorFactory, Serializable {
61 /***
62 * Convert a primitive type (such as <code>int</code>, <code>char</code>) to
63 * the wrapper class associated with it (such as <code>Integer</code>,
64 * <code>Character</code>).
65 *
66 * @param type The type to be converted.
67 * @return Wrapper class for the type provided.
68 * @throws ServletException if an unknown type is encountered.
69 */
70 public static Class convertPrimitiveType(final Class type)
71 throws SystemException {
72 String typeName = type.getName();
73 if ("boolean".equals(typeName)) {
74 return Boolean.class;
75 } else if ("byte".equals(typeName)) {
76 return Byte.class;
77 } else if ("char".equals(typeName)) {
78 return Character.class;
79 } else if ("double".equals(typeName)) {
80 return Double.class;
81 } else if ("float".equals(typeName)) {
82 return Float.class;
83 } else if ("int".equals(typeName)) {
84 return Integer.class;
85 } else if ("long".equals(typeName)) {
86 return Long.class;
87 } else if ("short".equals(typeName)) {
88 return Short.class;
89 } else {
90 throw new SystemException (
91 "Unknown primitive type: "
92 + type);
93 }
94 }
95 /***
96 * <p>
97 * Stores all the field value convertors, indexed by the class name they
98 * operate on.
99 * </p>
100 */
101 private Map fieldValueConvertors = new HashMap();
102
103 /***
104 * Constructor.
105 */
106 public DefaultFieldValueConvertorFactory() {
107 super();
108
109
110
111 fieldValueConvertors.put(BigDecimal.class.getName(),
112 new NumberFieldValueConvertor(
113 FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
114 fieldValueConvertors.put(Date.class.getName(),
115 new DateFieldValueConvertor(
116 FieldValueConvertorConstants.DEFAULT_DATE_PATTERN));
117 fieldValueConvertors.put(Class.class.getName(),
118 new ClassFieldValueConvertor());
119 fieldValueConvertors.put(Double.class.getName(),
120 new NumberFieldValueConvertor(
121 FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
122 fieldValueConvertors.put(Float.class.getName(),
123 new NumberFieldValueConvertor(
124 FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
125 fieldValueConvertors.put(Integer.class.getName(),
126 new NumberFieldValueConvertor(
127 FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
128 fieldValueConvertors.put(Object.class.getName(),
129 new FieldValueConvertor());
130 fieldValueConvertors.put(Short.class.getName(),
131 new NumberFieldValueConvertor(
132 FieldValueConvertorConstants.DEFAULT_NUMBER_PATTERN));
133 }
134 /***
135 * <p>
136 * Find the appropriate convertor for the field value class provided.
137 * This method goes thro' all the parent classes of the class provided
138 * till it finds a suitable convertor.
139 * </p>
140 *
141 * @param fieldValueClass Class of the field value we are tryinh to convert
142 * from a string.
143 * @return value field value convertor.
144 * @throws SystemException thrown if there is a class in the hierarchy
145 * for which we have no field value convertor.
146 */
147 public FieldValueConvertor getFieldValueConvertorForClass(
148 final Class fieldValueClass)
149 throws SystemException {
150 return getFieldValueConvertorForClass(fieldValueClass, fieldValueClass);
151 }
152 /***
153 * <p>
154 * Find the appropriate convertor for the field value class provided.
155 * This method goes thro' all the parents till it finds a suitable
156 * convertor.
157 * </p>
158 *
159 * @param originalClass
160 * original class we searched for (used in error reporting).
161 * @param fieldValueClass
162 * class of the field value to convert.
163 * @return value field value convertor.
164 * @throws ServletException thrown if there is a class in the hierarchy
165 * for which we have no field value convertor.
166 */
167 protected FieldValueConvertor getFieldValueConvertorForClass(
168 final Class originalClass,
169 final Class fieldValueClassParam) throws SystemException {
170 Class fieldValueClass = fieldValueClassParam;
171 String className = fieldValueClass.getName();
172 FieldValueConvertor convertor =
173 (FieldValueConvertor) fieldValueConvertors
174 .get(className);
175 if (convertor != null) {
176 return convertor;
177 }
178
179
180 if (fieldValueClass.isPrimitive()) {
181 fieldValueClass = DefaultFieldValueConvertorFactory
182 .convertPrimitiveType(fieldValueClass);
183 className = fieldValueClass.getName();
184 convertor =
185 (FieldValueConvertor) fieldValueConvertors
186 .get(className);
187 if (convertor != null) {
188 return convertor;
189 }
190 }
191
192 Class parentClass = fieldValueClass.getSuperclass();
193
194 if ("java.lang.Object".equals(className) || (parentClass == null)) {
195 throw new SystemException(
196 "ERROR: no field value convertor defined for '"
197 + originalClass.getName() + "'.");
198 }
199 return getFieldValueConvertorForClass(originalClass, parentClass);
200 }
201 /***
202 * <p>
203 * Set the field value convertor to use for a particular class.
204 * </p>
205 *
206 * @param fieldClass
207 * class to be converted.
208 * @param convertor
209 * convertor to use for this class.
210 */
211 public void setFieldValueConvertor(final Class fieldClass,
212 final FieldValueConvertor convertor) {
213 fieldValueConvertors.put(fieldClass.getName(), convertor);
214 }
215
216 }