1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.morph.transform.converters;
17
18 import java.text.DateFormat;
19 import java.util.Calendar;
20 import java.util.Locale;
21
22 import net.sf.morph.Defaults;
23 import net.sf.morph.transform.Converter;
24 import net.sf.morph.transform.DecoratedConverter;
25 import net.sf.morph.transform.ImpreciseTransformer;
26 import net.sf.morph.transform.transformers.BaseTransformer;
27 import net.sf.morph.util.TransformerUtils;
28
29 /**
30 * Converts the basic time types ({@link java.util.Date} and
31 * {@link java.util.Calendar}) to one of the text types (
32 * {@link java.lang.String}, {@link java.lang.StringBuffer} and
33 * {@link java.lang.Character}).
34 *
35 * @author Matt Sgarlata
36 * @since Dec 31, 2004
37 */
38 public class TimeToTextConverter extends BaseTransformer implements DecoratedConverter,
39 ImpreciseTransformer {
40
41 private DateFormat dateFormat;
42 private Converter timeConverter;
43 private Converter textConverter;
44
45 /**
46 * {@inheritDoc}
47 */
48 protected Object convertImpl(Class destinationClass, Object source,
49 Locale locale) throws Exception {
50
51 Calendar calendar = (Calendar) getTimeConverter().convert(Calendar.class, source, locale);
52
53 String string = getDateFormat(calendar).format(calendar.getTime());
54
55 return getTextConverter().convert(destinationClass, string, locale);
56 }
57
58 /**
59 * {@inheritDoc}
60 */
61 protected boolean isImpreciseTransformationImpl(Class destinationClass, Class sourceClass) {
62 return TransformerUtils.isImpreciseTransformation(getTextConverter(), destinationClass, String.class);
63 }
64
65 /**
66 * {@inheritDoc}
67 */
68 protected boolean isWrappingRuntimeExceptions() {
69 return true;
70 }
71
72 /**
73 * {@inheritDoc}
74 */
75 protected Class[] getSourceClassesImpl() throws Exception {
76 return getTimeConverter().getSourceClasses();
77 }
78
79 /**
80 * {@inheritDoc}
81 */
82 protected Class[] getDestinationClassesImpl() throws Exception {
83 return getTextConverter().getDestinationClasses();
84 }
85
86 /**
87 * Get the DateFormat used by this TimeToTextConverter.
88 * @return DateFormat
89 */
90 public DateFormat getDateFormat() {
91 return getDateFormat(null);
92 }
93
94 /**
95 * Get a DateFormat for the specified Calendar.
96 * @param calendar
97 * @return DateFormat
98 */
99 protected DateFormat getDateFormat(Calendar calendar) {
100 if (dateFormat != null) {
101 return dateFormat;
102 }
103 DateFormat result = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
104 if (calendar != null) {
105 result.setCalendar(calendar);
106 }
107 return result;
108 }
109
110 /**
111 * Set the default DateFormat used by this TimeToTextConverter.
112 * @param dateFormat
113 */
114 public void setDateFormat(DateFormat dateFormat) {
115 this.dateFormat = dateFormat;
116 }
117
118 /**
119 * Get the text converter used by this TimeToTextConverter.
120 * @return Converter
121 */
122 public Converter getTextConverter() {
123 if (textConverter == null) {
124 setTextConverter(Defaults.createTextConverter());
125 }
126 return textConverter;
127 }
128
129 /**
130 * Set the text converter used by this TimeToTextConverter.
131 * @param textConverter
132 */
133 public void setTextConverter(Converter textConverter) {
134 this.textConverter = textConverter;
135 }
136
137 /**
138 * Get the time converter used by this TimeToTextConverter.
139 * @return Converter
140 */
141 public Converter getTimeConverter() {
142 if (timeConverter == null) {
143 setTimeConverter(Defaults.createTimeConverter());
144 }
145 return timeConverter;
146 }
147
148 /**
149 * Set the time converter used by this TimeToTextConverter.
150 * @param timeConverter
151 */
152 public void setTimeConverter(Converter timeConverter) {
153 this.timeConverter = timeConverter;
154 }
155 }