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 | 0 | 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 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
66 | 0 | if (facesContext != null) |
67 | |
{ |
68 | 0 | CONVERTER_ID = |
69 | |
facesContext.getApplication().getExpressionFactory().createValueExpression( |
70 | |
facesContext.getELContext(), "javax.faces.DateTime", String.class); |
71 | |
} |
72 | |
else |
73 | |
{ |
74 | |
|
75 | |
|
76 | |
|
77 | 0 | CONVERTER_ID = null; |
78 | |
} |
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public void release() |
83 | |
{ |
84 | 0 | super.release(); |
85 | 0 | _dateStyle = null; |
86 | 0 | _locale = null; |
87 | 0 | _pattern = null; |
88 | 0 | _timeStyle = null; |
89 | 0 | _timeZone = null; |
90 | 0 | _type = null; |
91 | 0 | } |
92 | |
|
93 | |
public void setDateStyle(ValueExpression dateStyle) |
94 | |
{ |
95 | 0 | _dateStyle = dateStyle; |
96 | 0 | } |
97 | |
|
98 | |
public void setLocale(ValueExpression locale) |
99 | |
{ |
100 | 0 | _locale = locale; |
101 | 0 | } |
102 | |
|
103 | |
public void setPattern(ValueExpression pattern) |
104 | |
{ |
105 | 0 | _pattern = pattern; |
106 | 0 | } |
107 | |
|
108 | |
public void setTimeStyle(ValueExpression timeStyle) |
109 | |
{ |
110 | 0 | _timeStyle = timeStyle; |
111 | 0 | } |
112 | |
|
113 | |
public void setTimeZone(ValueExpression timeZone) |
114 | |
{ |
115 | 0 | _timeZone = timeZone; |
116 | 0 | } |
117 | |
|
118 | |
public void setType(ValueExpression type) |
119 | |
{ |
120 | 0 | _type = type; |
121 | 0 | } |
122 | |
|
123 | |
@Override |
124 | |
public int doStartTag() throws JspException |
125 | |
{ |
126 | 0 | super.setConverterId(CONVERTER_ID); |
127 | 0 | return super.doStartTag(); |
128 | |
} |
129 | |
|
130 | |
@Override |
131 | |
protected Converter createConverter() throws JspException |
132 | |
{ |
133 | 0 | DateTimeConverter converter = (DateTimeConverter)super.createConverter(); |
134 | |
|
135 | 0 | ELContext elContext = FacesContext.getCurrentInstance().getELContext(); |
136 | 0 | setConverterDateStyle(elContext, converter, _dateStyle); |
137 | 0 | setConverterLocale(elContext, converter, _locale); |
138 | 0 | setConverterPattern(elContext, converter, _pattern); |
139 | 0 | setConverterTimeStyle(elContext, converter, _timeStyle); |
140 | 0 | setConverterTimeZone(elContext, converter, _timeZone); |
141 | 0 | setConverterType(elContext, converter, _type); |
142 | |
|
143 | 0 | return converter; |
144 | |
} |
145 | |
|
146 | |
private void setConverterLocale(ELContext eLContext, DateTimeConverter converter, ValueExpression value) |
147 | |
{ |
148 | 0 | if (value == null) |
149 | |
{ |
150 | 0 | return; |
151 | |
} |
152 | |
|
153 | 0 | Object objLocale = UIComponentELTagUtils.evaluateValueExpression(eLContext, value); |
154 | |
Locale locale; |
155 | |
|
156 | 0 | if (objLocale == null) |
157 | |
{ |
158 | 0 | return; |
159 | |
} |
160 | |
|
161 | 0 | if (objLocale instanceof Locale) |
162 | |
{ |
163 | 0 | locale = (Locale)objLocale; |
164 | |
} |
165 | |
else |
166 | |
{ |
167 | 0 | locale = LocaleUtils.toLocale(objLocale.toString()); |
168 | |
} |
169 | 0 | converter.setLocale(locale); |
170 | 0 | } |
171 | |
|
172 | |
private void setConverterDateStyle(ELContext elContext, DateTimeConverter converter, ValueExpression value) |
173 | |
{ |
174 | 0 | if (value == null) |
175 | |
{ |
176 | 0 | return; |
177 | |
} |
178 | |
|
179 | 0 | String dateStyle = (String)UIComponentELTagUtils.evaluateValueExpression(elContext, value); |
180 | |
|
181 | 0 | if (dateStyle == null) |
182 | |
{ |
183 | 0 | dateStyle = DEFAULT_DATE_STYLE; |
184 | |
} |
185 | |
|
186 | 0 | converter.setDateStyle(dateStyle); |
187 | 0 | } |
188 | |
|
189 | |
private void setConverterPattern(ELContext elContext, DateTimeConverter converter, ValueExpression value) |
190 | |
{ |
191 | 0 | if (value == null) |
192 | |
{ |
193 | 0 | return; |
194 | |
} |
195 | |
|
196 | 0 | String pattern = (String)UIComponentELTagUtils.evaluateValueExpression(elContext, value); |
197 | 0 | converter.setPattern(pattern); |
198 | 0 | } |
199 | |
|
200 | |
private void setConverterTimeStyle(ELContext elContext, DateTimeConverter converter, ValueExpression value) |
201 | |
{ |
202 | 0 | if (value == null) |
203 | |
{ |
204 | 0 | return; |
205 | |
} |
206 | |
|
207 | 0 | String timeStyle = (String)UIComponentELTagUtils.evaluateValueExpression(elContext, value); |
208 | |
|
209 | 0 | if (timeStyle == null) |
210 | |
{ |
211 | 0 | timeStyle = DEFAULT_TIME_STYLE; |
212 | |
} |
213 | |
|
214 | 0 | converter.setTimeStyle(timeStyle); |
215 | 0 | } |
216 | |
|
217 | |
private void setConverterTimeZone(ELContext elContext, DateTimeConverter converter, ValueExpression value) |
218 | |
{ |
219 | 0 | if (value == null) |
220 | |
{ |
221 | 0 | return; |
222 | |
} |
223 | |
|
224 | 0 | Object objTimeZone = UIComponentELTagUtils.evaluateValueExpression(elContext, value); |
225 | |
TimeZone timeZone; |
226 | |
|
227 | 0 | if (objTimeZone == null) |
228 | |
{ |
229 | 0 | return; |
230 | |
} |
231 | |
|
232 | 0 | if (objTimeZone instanceof TimeZone) |
233 | |
{ |
234 | 0 | timeZone = (TimeZone)objTimeZone; |
235 | |
} |
236 | |
else |
237 | |
{ |
238 | 0 | timeZone = TimeZone.getTimeZone(objTimeZone.toString()); |
239 | |
} |
240 | 0 | converter.setTimeZone(timeZone); |
241 | 0 | } |
242 | |
|
243 | |
private void setConverterType(ELContext elContext, DateTimeConverter converter, ValueExpression value) |
244 | |
{ |
245 | |
String type; |
246 | |
|
247 | 0 | if (value == null) |
248 | |
{ |
249 | 0 | type = null; |
250 | |
} |
251 | |
else |
252 | |
{ |
253 | 0 | type = (String)UIComponentELTagUtils.evaluateValueExpression(elContext, value); |
254 | |
} |
255 | |
|
256 | 0 | if (type == null) |
257 | |
{ |
258 | |
|
259 | |
|
260 | 0 | String timeStyle = |
261 | |
(_timeStyle == null) ? null : (String)UIComponentELTagUtils.evaluateValueExpression(elContext, |
262 | |
_timeStyle); |
263 | 0 | String dateStyle = |
264 | |
(_dateStyle == null) ? null : (String)UIComponentELTagUtils.evaluateValueExpression(elContext, |
265 | |
_dateStyle); |
266 | 0 | if (dateStyle == null) |
267 | |
{ |
268 | 0 | if (timeStyle == null) |
269 | |
{ |
270 | |
|
271 | 0 | type = DEFAULT_TYPE; |
272 | |
} |
273 | |
else |
274 | |
{ |
275 | |
|
276 | 0 | type = TYPE_TIME; |
277 | |
} |
278 | |
} |
279 | |
else |
280 | |
{ |
281 | 0 | if (timeStyle == null) |
282 | |
{ |
283 | |
|
284 | 0 | type = TYPE_DATE; |
285 | |
} |
286 | |
else |
287 | |
{ |
288 | |
|
289 | 0 | type = TYPE_BOTH; |
290 | |
} |
291 | |
} |
292 | 0 | } |
293 | |
else |
294 | |
{ |
295 | 0 | if (!TYPE_DATE.equals(type) && !TYPE_TIME.equals(type) && !TYPE_BOTH.equals(type)) |
296 | |
{ |
297 | 0 | type = DEFAULT_TYPE; |
298 | |
} |
299 | |
} |
300 | |
|
301 | 0 | converter.setType(type); |
302 | 0 | } |
303 | |
|
304 | |
} |