1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.dozer.converters; |
17 | |
|
18 | |
import org.apache.commons.beanutils.Converter; |
19 | |
import org.dozer.MappingException; |
20 | |
|
21 | |
import javax.xml.datatype.DatatypeConfigurationException; |
22 | |
import javax.xml.datatype.DatatypeFactory; |
23 | |
import javax.xml.datatype.XMLGregorianCalendar; |
24 | |
import java.text.DateFormat; |
25 | |
import java.text.ParseException; |
26 | |
import java.util.Calendar; |
27 | |
import java.util.Date; |
28 | |
import java.util.GregorianCalendar; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class XMLGregorianCalendarConverter implements Converter { |
52 | |
|
53 | |
private DateFormat dateFormat; |
54 | |
|
55 | 19 | public XMLGregorianCalendarConverter(DateFormat dateFormat) { |
56 | 19 | this.dateFormat = dateFormat; |
57 | 19 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private static DatatypeFactory dataTypeFactory; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
private static DatatypeFactory dataTypeFactory() { |
70 | 18 | if (dataTypeFactory == null) { |
71 | |
try { |
72 | 1 | dataTypeFactory = DatatypeFactory.newInstance(); |
73 | 0 | } catch (DatatypeConfigurationException e) { |
74 | 0 | throw new MappingException(e); |
75 | 1 | } |
76 | |
} |
77 | 18 | return dataTypeFactory; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public Object convert(Class destClass, Object srcObj) { |
84 | 19 | Class sourceClass = srcObj.getClass(); |
85 | 19 | Calendar result = new GregorianCalendar(); |
86 | |
|
87 | 19 | if (java.util.Date.class.isAssignableFrom(sourceClass)) { |
88 | |
|
89 | 7 | result.setTime((java.util.Date) srcObj); |
90 | 12 | } else if (Calendar.class.isAssignableFrom(sourceClass)) { |
91 | |
|
92 | 3 | Calendar c = (Calendar) srcObj; |
93 | 3 | result.setTime(c.getTime()); |
94 | 3 | result.setTimeZone(c.getTimeZone()); |
95 | 3 | } else if (XMLGregorianCalendar.class.isAssignableFrom(sourceClass)) { |
96 | 1 | result = ((XMLGregorianCalendar) srcObj).toGregorianCalendar(); |
97 | 8 | } else if (dateFormat != null && String.class.isAssignableFrom(sourceClass)) { |
98 | 7 | if ("".equals(srcObj)) { |
99 | 1 | return null; |
100 | |
} |
101 | |
try { |
102 | 6 | long time = dateFormat.parse((String) srcObj).getTime(); |
103 | 6 | result.setTime(new Date(time)); |
104 | 0 | } catch (ParseException e) { |
105 | 0 | throw new ConversionException("Unable to parse source object using specified date format", e); |
106 | 6 | } |
107 | |
} else { |
108 | |
try { |
109 | 1 | long time = Long.parseLong(srcObj.toString()); |
110 | 1 | result.setTime(new Date(time)); |
111 | 0 | } catch (NumberFormatException e) { |
112 | 0 | throw new ConversionException("Unable to determine time in millis of source object", e); |
113 | 1 | } |
114 | |
} |
115 | |
|
116 | 18 | return dataTypeFactory().newXMLGregorianCalendar((GregorianCalendar) result); |
117 | |
} |
118 | |
} |