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_impl.taglib.UIComponentELTagUtils; |
22 | |
import org.apache.myfaces.shared_impl.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 | |
|
38 | 0 | public class ConvertDateTimeTag |
39 | |
extends ConverterTag |
40 | |
{ |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
private static final long serialVersionUID = 54366768002181L; |
46 | |
|
47 | |
private static final String DEFAULT_DATE_STYLE = "default"; |
48 | |
private static final String DEFAULT_TIME_STYLE = "default"; |
49 | |
|
50 | |
private static final String TYPE_DATE = "date"; |
51 | |
private static final String TYPE_TIME = "time"; |
52 | |
private static final String TYPE_BOTH = "both"; |
53 | |
|
54 | |
private static final String DEFAULT_TYPE = TYPE_DATE; |
55 | |
|
56 | |
private ValueExpression _dateStyle; |
57 | |
private ValueExpression _locale; |
58 | |
private ValueExpression _pattern; |
59 | |
private ValueExpression _timeStyle; |
60 | |
private ValueExpression _timeZone; |
61 | |
private ValueExpression _type; |
62 | |
|
63 | |
private static final ValueExpression CONVERTER_ID; |
64 | |
|
65 | |
static |
66 | |
{ |
67 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
68 | 0 | if (facesContext != null) |
69 | |
{ |
70 | 0 | CONVERTER_ID = facesContext.getApplication().getExpressionFactory().createValueExpression( |
71 | |
facesContext.getELContext(),"javax.faces.DateTime",String.class); |
72 | |
} |
73 | |
else |
74 | |
{ |
75 | |
|
76 | |
|
77 | |
|
78 | 0 | CONVERTER_ID = null; |
79 | |
} |
80 | 0 | } |
81 | |
|
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 | |
public int doStartTag() throws JspException { |
124 | 0 | super.setConverterId(CONVERTER_ID); |
125 | 0 | return super.doStartTag(); |
126 | |
} |
127 | |
|
128 | |
protected Converter createConverter() throws JspException |
129 | |
{ |
130 | 0 | DateTimeConverter converter = (DateTimeConverter)super.createConverter(); |
131 | |
|
132 | 0 | ELContext elContext = FacesContext.getCurrentInstance().getELContext(); |
133 | 0 | setConverterDateStyle(elContext, converter, _dateStyle); |
134 | 0 | setConverterLocale(elContext, converter, _locale); |
135 | 0 | setConverterPattern(elContext, converter, _pattern); |
136 | 0 | setConverterTimeStyle(elContext, converter, _timeStyle); |
137 | 0 | setConverterTimeZone(elContext, converter, _timeZone); |
138 | 0 | setConverterType(elContext, converter, _type); |
139 | |
|
140 | 0 | return converter; |
141 | |
} |
142 | |
|
143 | |
private void setConverterLocale(ELContext eLContext, |
144 | |
DateTimeConverter converter, |
145 | |
ValueExpression value) |
146 | |
{ |
147 | 0 | if (value == null) return; |
148 | |
|
149 | 0 | Object objLocale = UIComponentELTagUtils.evaluateValueExpression(eLContext, value); |
150 | |
Locale locale; |
151 | |
|
152 | 0 | if (objLocale == null) return; |
153 | |
|
154 | 0 | if (objLocale instanceof Locale) |
155 | |
{ |
156 | 0 | locale = (Locale) objLocale; |
157 | |
} |
158 | |
else |
159 | |
{ |
160 | 0 | locale = LocaleUtils.toLocale(objLocale.toString()); |
161 | |
} |
162 | 0 | converter.setLocale(locale); |
163 | 0 | } |
164 | |
|
165 | |
private void setConverterDateStyle(ELContext elContext, |
166 | |
DateTimeConverter converter, |
167 | |
ValueExpression value) |
168 | |
{ |
169 | 0 | if (value == null) return; |
170 | |
|
171 | 0 | String dateStyle = (String) UIComponentELTagUtils.evaluateValueExpression(elContext, value); |
172 | |
|
173 | 0 | if (dateStyle == null) |
174 | |
{ |
175 | 0 | dateStyle = DEFAULT_DATE_STYLE; |
176 | |
} |
177 | |
|
178 | 0 | converter.setDateStyle(dateStyle); |
179 | 0 | } |
180 | |
|
181 | |
private void setConverterPattern(ELContext elContext, |
182 | |
DateTimeConverter converter, |
183 | |
ValueExpression value) |
184 | |
{ |
185 | 0 | if (value == null) return; |
186 | |
|
187 | 0 | String pattern = (String) UIComponentELTagUtils.evaluateValueExpression(elContext, value); |
188 | 0 | converter.setPattern(pattern); |
189 | 0 | } |
190 | |
|
191 | |
private void setConverterTimeStyle(ELContext elContext, |
192 | |
DateTimeConverter converter, |
193 | |
ValueExpression value) |
194 | |
{ |
195 | 0 | if (value == null) return; |
196 | |
|
197 | 0 | String timeStyle = (String) UIComponentELTagUtils.evaluateValueExpression(elContext, value); |
198 | |
|
199 | 0 | if (timeStyle == null) |
200 | |
{ |
201 | 0 | timeStyle = DEFAULT_TIME_STYLE; |
202 | |
} |
203 | |
|
204 | 0 | converter.setTimeStyle(timeStyle); |
205 | 0 | } |
206 | |
|
207 | |
private void setConverterTimeZone(ELContext elContext, |
208 | |
DateTimeConverter converter, |
209 | |
ValueExpression value) |
210 | |
{ |
211 | 0 | if (value == null) return; |
212 | |
|
213 | 0 | Object objTimeZone = UIComponentELTagUtils.evaluateValueExpression(elContext, value); |
214 | |
TimeZone timeZone; |
215 | |
|
216 | 0 | if (objTimeZone == null) return; |
217 | |
|
218 | 0 | if (objTimeZone instanceof TimeZone) |
219 | |
{ |
220 | 0 | timeZone = (TimeZone) objTimeZone; |
221 | |
} |
222 | |
else |
223 | |
{ |
224 | 0 | timeZone = TimeZone.getTimeZone(objTimeZone.toString()); |
225 | |
} |
226 | 0 | converter.setTimeZone(timeZone); |
227 | 0 | } |
228 | |
|
229 | |
private void setConverterType(ELContext elContext, |
230 | |
DateTimeConverter converter, |
231 | |
ValueExpression value) |
232 | |
{ |
233 | |
String type; |
234 | |
|
235 | 0 | if (value == null) |
236 | |
{ |
237 | 0 | type = null; |
238 | |
} |
239 | |
else |
240 | |
{ |
241 | 0 | type = (String) UIComponentELTagUtils.evaluateValueExpression(elContext, value); |
242 | |
} |
243 | |
|
244 | 0 | if (type == null) |
245 | |
{ |
246 | |
|
247 | |
|
248 | 0 | String timeStyle = (_timeStyle == null) ? null : |
249 | |
(String) UIComponentELTagUtils.evaluateValueExpression(elContext, _timeStyle); |
250 | 0 | String dateStyle = (_dateStyle == null) ? null : |
251 | |
(String) UIComponentELTagUtils.evaluateValueExpression(elContext, _dateStyle); |
252 | 0 | if (dateStyle == null) |
253 | |
{ |
254 | 0 | if (timeStyle == null) |
255 | |
{ |
256 | |
|
257 | 0 | type = DEFAULT_TYPE; |
258 | |
} |
259 | |
else |
260 | |
{ |
261 | |
|
262 | 0 | type = TYPE_TIME; |
263 | |
} |
264 | |
} |
265 | |
else |
266 | |
{ |
267 | 0 | if (timeStyle == null) |
268 | |
{ |
269 | |
|
270 | 0 | type = TYPE_DATE; |
271 | |
} |
272 | |
else |
273 | |
{ |
274 | |
|
275 | 0 | type = TYPE_BOTH; |
276 | |
} |
277 | |
} |
278 | 0 | }else { |
279 | 0 | if (!TYPE_DATE.equals(type) && |
280 | |
!TYPE_TIME.equals(type) && |
281 | |
!TYPE_BOTH.equals(type)) |
282 | |
{ |
283 | 0 | type = DEFAULT_TYPE; |
284 | |
} |
285 | |
} |
286 | |
|
287 | 0 | converter.setType(type); |
288 | 0 | } |
289 | |
|
290 | |
|
291 | |
} |