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 package org.exolab.castor.xml.schema.util;
37
38
39 import org.exolab.castor.types.Date;
40 import org.exolab.castor.types.Time;
41
42 import java.text.DateFormat;
43 import java.text.ParseException;
44 import java.text.SimpleDateFormat;
45
46
47
48
49
50
51
52
53 public class DatatypeHandler {
54
55
56
57
58 public static final String BOOLEAN_TYPE = "boolean";
59
60
61
62
63 public static final String DATE_TYPE = "date";
64
65
66
67
68 public static final String DATETIME_TYPE = "dateTime";
69
70
71
72
73 public static final String DOUBLE_TYPE = "double";
74
75
76
77
78 public static final String FLOAT_TYPE = "float";
79
80
81
82
83 public static final String INTEGER_TYPE = "integer";
84
85
86
87
88 public static final String LONG_TYPE = "long";
89
90
91
92
93 public static final String STRING_TYPE = "string";
94
95
96
97
98 public static final String TIME_TYPE = "time";
99
100
101
102 private static final String TRUE = "true";
103 private static final String FALSE = "false";
104
105 private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
106 private static final String DATE_FORMAT_2 = "yyyy-MM-dd'T'HH:mm:ss";
107
108
109
110
111
112
113 private DatatypeHandler() {
114 super();
115 }
116
117
118
119
120
121
122
123
124
125 public static String guessType(String value) {
126 if (value == null)
127 return null;
128
129
130
131 if (value.length() == 0)
132 return STRING_TYPE;
133
134
135 try {
136 Integer.parseInt(value);
137 return INTEGER_TYPE;
138 } catch (NumberFormatException nfe) {
139 }
140
141
142 try {
143 Long.parseLong(value);
144 return LONG_TYPE;
145 } catch (NumberFormatException nfe) {
146 }
147
148
149 try {
150 Float.valueOf(value);
151 return FLOAT_TYPE;
152 } catch (NumberFormatException nfe) {
153 }
154
155
156 try {
157 Double.valueOf(value);
158 return DOUBLE_TYPE;
159 } catch (NumberFormatException nfe) {
160 }
161
162
163 if (value.equals(TRUE) || value.equals(FALSE)) {
164 return BOOLEAN_TYPE;
165 }
166
167
168 try {
169 Date.parseDate(value);
170 return DATE_TYPE;
171 } catch (ParseException px) {
172 }
173
174
175 try {
176 Time.parseTime(value);
177 return TIME_TYPE;
178 } catch (ParseException px) {
179 } catch (IllegalArgumentException ex) {
180 }
181
182
183 DateFormat df = null;
184 if (value.indexOf('.') < 0)
185 df = new SimpleDateFormat(DATE_FORMAT);
186 else
187 df = new SimpleDateFormat(DATE_FORMAT_2);
188
189 try {
190 df.parse(value);
191 return DATETIME_TYPE;
192 } catch (java.text.ParseException ex) {
193 }
194
195
196
197 return STRING_TYPE;
198 }
199
200
201
202
203
204 protected static String whichType(String type1, String type2) {
205
206
207 if (type1.equals(type2))
208 return type1;
209
210 if (type1.equals(STRING_TYPE) || (type2.equals(STRING_TYPE)))
211 return STRING_TYPE;
212
213 if (INTEGER_TYPE.equals(type1)) {
214 if (LONG_TYPE.equals(type2))
215 return LONG_TYPE;
216 else if (FLOAT_TYPE.equals(type2))
217 return FLOAT_TYPE;
218 else if (DOUBLE_TYPE.equals(type2))
219 return DOUBLE_TYPE;
220 } else if (LONG_TYPE.equals(type1)) {
221 if (INTEGER_TYPE.equals(type2))
222 return LONG_TYPE;
223 else if (FLOAT_TYPE.equals(type2))
224 return DOUBLE_TYPE;
225 else if (DOUBLE_TYPE.equals(type2))
226 return DOUBLE_TYPE;
227 } else if (FLOAT_TYPE.equals(type1)) {
228 if (INTEGER_TYPE.equals(type2))
229 return FLOAT_TYPE;
230 else if (LONG_TYPE.equals(type2))
231 return DOUBLE_TYPE;
232 else if (DOUBLE_TYPE.equals(type2))
233 return DOUBLE_TYPE;
234 } else if (DOUBLE_TYPE.equals(type1)) {
235 if (INTEGER_TYPE.equals(type2))
236 return DOUBLE_TYPE;
237 else if (LONG_TYPE.equals(type2))
238 return DOUBLE_TYPE;
239 else if (FLOAT_TYPE.equals(type2))
240 return DOUBLE_TYPE;
241 }
242
243 return STRING_TYPE;
244 }
245
246
247 }