1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.taglib.core;
20
21 import org.apache.myfaces.shared.taglib.UIComponentELTagUtils;
22 import org.apache.myfaces.shared.util.LocaleUtils;
23
24 import javax.el.ELContext;
25 import javax.el.ValueExpression;
26 import javax.faces.context.FacesContext;
27 import javax.faces.convert.Converter;
28 import javax.faces.convert.DateTimeConverter;
29 import javax.servlet.jsp.JspException;
30 import java.util.Locale;
31 import java.util.TimeZone;
32
33
34
35
36
37 public class ConvertDateTimeTag extends ConverterTag
38 {
39
40
41
42
43 private static final long serialVersionUID = 54366768002181L;
44
45 private static final String DEFAULT_DATE_STYLE = "default";
46 private static final String DEFAULT_TIME_STYLE = "default";
47
48 private static final String TYPE_DATE = "date";
49 private static final String TYPE_TIME = "time";
50 private static final String TYPE_BOTH = "both";
51
52 private static final String DEFAULT_TYPE = TYPE_DATE;
53
54 private ValueExpression _dateStyle;
55 private ValueExpression _locale;
56 private ValueExpression _pattern;
57 private ValueExpression _timeStyle;
58 private ValueExpression _timeZone;
59 private ValueExpression _type;
60
61 private static final ValueExpression CONVERTER_ID;
62
63 static
64 {
65 FacesContext facesContext = FacesContext.getCurrentInstance();
66 if (facesContext != null)
67 {
68 CONVERTER_ID =
69 facesContext.getApplication().getExpressionFactory().createValueExpression(
70 facesContext.getELContext(), "javax.faces.DateTime", String.class);
71 }
72 else
73 {
74
75
76
77 CONVERTER_ID = null;
78 }
79 }
80
81 @Override
82 public void release()
83 {
84 super.release();
85 _dateStyle = null;
86 _locale = null;
87 _pattern = null;
88 _timeStyle = null;
89 _timeZone = null;
90 _type = null;
91 }
92
93 public void setDateStyle(ValueExpression dateStyle)
94 {
95 _dateStyle = dateStyle;
96 }
97
98 public void setLocale(ValueExpression locale)
99 {
100 _locale = locale;
101 }
102
103 public void setPattern(ValueExpression pattern)
104 {
105 _pattern = pattern;
106 }
107
108 public void setTimeStyle(ValueExpression timeStyle)
109 {
110 _timeStyle = timeStyle;
111 }
112
113 public void setTimeZone(ValueExpression timeZone)
114 {
115 _timeZone = timeZone;
116 }
117
118 public void setType(ValueExpression type)
119 {
120 _type = type;
121 }
122
123 @Override
124 public int doStartTag() throws JspException
125 {
126 super.setConverterId(CONVERTER_ID);
127 return super.doStartTag();
128 }
129
130 @Override
131 protected Converter createConverter() throws JspException
132 {
133 DateTimeConverter converter = (DateTimeConverter)super.createConverter();
134
135 ELContext elContext = FacesContext.getCurrentInstance().getELContext();
136 setConverterDateStyle(elContext, converter, _dateStyle);
137 setConverterLocale(elContext, converter, _locale);
138 setConverterPattern(elContext, converter, _pattern);
139 setConverterTimeStyle(elContext, converter, _timeStyle);
140 setConverterTimeZone(elContext, converter, _timeZone);
141 setConverterType(elContext, converter, _type);
142
143 return converter;
144 }
145
146 private void setConverterLocale(ELContext eLContext, DateTimeConverter converter, ValueExpression value)
147 {
148 if (value == null)
149 {
150 return;
151 }
152
153 Object objLocale = UIComponentELTagUtils.evaluateValueExpression(eLContext, value);
154 Locale locale;
155
156 if (objLocale == null)
157 {
158 return;
159 }
160
161 if (objLocale instanceof Locale)
162 {
163 locale = (Locale)objLocale;
164 }
165 else
166 {
167 locale = LocaleUtils.toLocale(objLocale.toString());
168 }
169 converter.setLocale(locale);
170 }
171
172 private void setConverterDateStyle(ELContext elContext, DateTimeConverter converter, ValueExpression value)
173 {
174 if (value == null)
175 {
176 return;
177 }
178
179 String dateStyle = (String)UIComponentELTagUtils.evaluateValueExpression(elContext, value);
180
181 if (dateStyle == null)
182 {
183 dateStyle = DEFAULT_DATE_STYLE;
184 }
185
186 converter.setDateStyle(dateStyle);
187 }
188
189 private void setConverterPattern(ELContext elContext, DateTimeConverter converter, ValueExpression value)
190 {
191 if (value == null)
192 {
193 return;
194 }
195
196 String pattern = (String)UIComponentELTagUtils.evaluateValueExpression(elContext, value);
197 converter.setPattern(pattern);
198 }
199
200 private void setConverterTimeStyle(ELContext elContext, DateTimeConverter converter, ValueExpression value)
201 {
202 if (value == null)
203 {
204 return;
205 }
206
207 String timeStyle = (String)UIComponentELTagUtils.evaluateValueExpression(elContext, value);
208
209 if (timeStyle == null)
210 {
211 timeStyle = DEFAULT_TIME_STYLE;
212 }
213
214 converter.setTimeStyle(timeStyle);
215 }
216
217 private void setConverterTimeZone(ELContext elContext, DateTimeConverter converter, ValueExpression value)
218 {
219 if (value == null)
220 {
221 return;
222 }
223
224 Object objTimeZone = UIComponentELTagUtils.evaluateValueExpression(elContext, value);
225 TimeZone timeZone;
226
227 if (objTimeZone == null)
228 {
229 return;
230 }
231
232 if (objTimeZone instanceof TimeZone)
233 {
234 timeZone = (TimeZone)objTimeZone;
235 }
236 else
237 {
238 timeZone = TimeZone.getTimeZone(objTimeZone.toString());
239 }
240 converter.setTimeZone(timeZone);
241 }
242
243 private void setConverterType(ELContext elContext, DateTimeConverter converter, ValueExpression value)
244 {
245 String type;
246
247 if (value == null)
248 {
249 type = null;
250 }
251 else
252 {
253 type = (String)UIComponentELTagUtils.evaluateValueExpression(elContext, value);
254 }
255
256 if (type == null)
257 {
258
259
260 String timeStyle =
261 (_timeStyle == null) ? null : (String)UIComponentELTagUtils.evaluateValueExpression(elContext,
262 _timeStyle);
263 String dateStyle =
264 (_dateStyle == null) ? null : (String)UIComponentELTagUtils.evaluateValueExpression(elContext,
265 _dateStyle);
266 if (dateStyle == null)
267 {
268 if (timeStyle == null)
269 {
270
271 type = DEFAULT_TYPE;
272 }
273 else
274 {
275
276 type = TYPE_TIME;
277 }
278 }
279 else
280 {
281 if (timeStyle == null)
282 {
283
284 type = TYPE_DATE;
285 }
286 else
287 {
288
289 type = TYPE_BOTH;
290 }
291 }
292 }
293 else
294 {
295 if (!TYPE_DATE.equals(type) && !TYPE_TIME.equals(type) && !TYPE_BOTH.equals(type))
296 {
297 type = DEFAULT_TYPE;
298 }
299 }
300
301 converter.setType(type);
302 }
303
304 }