1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.convert; |
20 | |
|
21 | |
import java.text.DateFormat; |
22 | |
import java.text.ParseException; |
23 | |
import java.text.SimpleDateFormat; |
24 | |
import java.util.Date; |
25 | |
import java.util.Locale; |
26 | |
import java.util.TimeZone; |
27 | |
|
28 | |
import javax.faces.component.StateHolder; |
29 | |
import javax.faces.component.UIComponent; |
30 | |
import javax.faces.context.FacesContext; |
31 | |
|
32 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFConverter; |
33 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty; |
34 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
@JSFConverter( |
47 | |
name="f:convertDateTime", |
48 | |
bodyContent="empty", |
49 | |
tagClass="org.apache.myfaces.taglib.core.ConvertDateTimeTag") |
50 | |
@JSFJspProperty( |
51 | |
name="binding", |
52 | |
returnType = "javax.faces.convert.DateTimeConverter", |
53 | |
longDesc = "A ValueExpression that evaluates to a DateTimeConverter.") |
54 | |
public class DateTimeConverter |
55 | |
implements Converter, StateHolder |
56 | |
{ |
57 | |
|
58 | |
|
59 | |
public static final String CONVERTER_ID = "javax.faces.DateTime"; |
60 | |
public static final String DATE_ID = "javax.faces.converter.DateTimeConverter.DATE"; |
61 | |
public static final String DATETIME_ID = "javax.faces.converter.DateTimeConverter.DATETIME"; |
62 | |
public static final String STRING_ID = "javax.faces.converter.STRING"; |
63 | |
public static final String TIME_ID = "javax.faces.converter.DateTimeConverter.TIME"; |
64 | |
|
65 | |
|
66 | |
private static final String TYPE_DATE = "date"; |
67 | |
private static final String TYPE_TIME = "time"; |
68 | |
private static final String TYPE_BOTH = "both"; |
69 | |
private static final String STYLE_DEFAULT = "default"; |
70 | |
private static final String STYLE_MEDIUM = "medium"; |
71 | |
private static final String STYLE_SHORT = "short"; |
72 | |
private static final String STYLE_LONG = "long"; |
73 | |
private static final String STYLE_FULL = "full"; |
74 | 0 | private static final TimeZone TIMEZONE_DEFAULT = TimeZone.getTimeZone("GMT"); |
75 | |
|
76 | |
private String _dateStyle; |
77 | |
private Locale _locale; |
78 | |
private String _pattern; |
79 | |
private String _timeStyle; |
80 | |
private TimeZone _timeZone; |
81 | |
private String _type; |
82 | |
private boolean _transient; |
83 | |
|
84 | |
|
85 | |
public DateTimeConverter() |
86 | 0 | { |
87 | 0 | } |
88 | |
|
89 | |
|
90 | |
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) |
91 | |
{ |
92 | 0 | if (facesContext == null) throw new NullPointerException("facesContext"); |
93 | 0 | if (uiComponent == null) throw new NullPointerException("uiComponent"); |
94 | |
|
95 | 0 | if (value != null) |
96 | |
{ |
97 | 0 | value = value.trim(); |
98 | 0 | if (value.length() > 0) |
99 | |
{ |
100 | 0 | DateFormat format = getDateFormat(); |
101 | 0 | TimeZone tz = getTimeZone(); |
102 | 0 | if( tz != null ) |
103 | 0 | format.setTimeZone( tz ); |
104 | |
try |
105 | |
{ |
106 | 0 | return format.parse(value); |
107 | |
} |
108 | 0 | catch (ParseException e) |
109 | |
{ |
110 | 0 | String type = getType(); |
111 | 0 | Object[] args = new Object[]{value,format.format(new Date()),_MessageUtils.getLabel(facesContext, uiComponent)}; |
112 | |
|
113 | 0 | if(type.equals(TYPE_DATE)) |
114 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,DATE_ID,args)); |
115 | 0 | else if (type.equals(TYPE_TIME)) |
116 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,TIME_ID,args)); |
117 | 0 | else if (type.equals(TYPE_BOTH)) |
118 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,DATETIME_ID,args)); |
119 | |
else |
120 | 0 | throw new ConverterException("invalid type '" + _type + "'"); |
121 | |
} |
122 | |
} |
123 | |
} |
124 | 0 | return null; |
125 | |
} |
126 | |
|
127 | |
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) |
128 | |
{ |
129 | 0 | if (facesContext == null) throw new NullPointerException("facesContext"); |
130 | 0 | if (uiComponent == null) throw new NullPointerException("uiComponent"); |
131 | |
|
132 | 0 | if (value == null) |
133 | |
{ |
134 | 0 | return ""; |
135 | |
} |
136 | 0 | if (value instanceof String) |
137 | |
{ |
138 | 0 | return (String)value; |
139 | |
} |
140 | |
|
141 | 0 | DateFormat format = getDateFormat(); |
142 | 0 | TimeZone tz = getTimeZone(); |
143 | 0 | if (tz != null) |
144 | |
{ |
145 | 0 | format.setTimeZone(tz); |
146 | |
} |
147 | |
try |
148 | |
{ |
149 | 0 | return format.format(value); |
150 | |
} |
151 | 0 | catch (Exception e) |
152 | |
{ |
153 | 0 | throw new ConverterException(_MessageUtils.getErrorMessage(facesContext, STRING_ID, new Object[]{value,_MessageUtils.getLabel(facesContext, uiComponent)}),e); |
154 | |
} |
155 | |
} |
156 | |
|
157 | |
private DateFormat getDateFormat() |
158 | |
{ |
159 | 0 | String type = getType(); |
160 | |
DateFormat format; |
161 | 0 | if (_pattern != null) |
162 | |
{ |
163 | |
try |
164 | |
{ |
165 | 0 | format = new SimpleDateFormat(_pattern, getLocale()); |
166 | |
} |
167 | 0 | catch (IllegalArgumentException iae) |
168 | |
{ |
169 | 0 | throw new ConverterException("Invalid pattern", iae); |
170 | 0 | } |
171 | |
} |
172 | 0 | else if (type.equals(TYPE_DATE)) |
173 | |
{ |
174 | 0 | format = DateFormat.getDateInstance(calcStyle(getDateStyle()), getLocale()); |
175 | |
} |
176 | 0 | else if (type.equals(TYPE_TIME)) |
177 | |
{ |
178 | 0 | format = DateFormat.getTimeInstance(calcStyle(getTimeStyle()), getLocale()); |
179 | |
} |
180 | 0 | else if (type.equals(TYPE_BOTH)) |
181 | |
{ |
182 | 0 | format = DateFormat.getDateTimeInstance(calcStyle(getDateStyle()), |
183 | |
calcStyle(getTimeStyle()), |
184 | |
getLocale()); |
185 | |
} |
186 | |
else |
187 | |
{ |
188 | 0 | throw new ConverterException("invalid type '" + _type + "'"); |
189 | |
} |
190 | |
|
191 | |
|
192 | 0 | format.setLenient(false); |
193 | 0 | return format; |
194 | |
} |
195 | |
|
196 | |
private int calcStyle(String name) |
197 | |
{ |
198 | 0 | if (name.equals(STYLE_DEFAULT)) |
199 | |
{ |
200 | 0 | return DateFormat.DEFAULT; |
201 | |
} |
202 | 0 | if (name.equals(STYLE_MEDIUM)) |
203 | |
{ |
204 | 0 | return DateFormat.MEDIUM; |
205 | |
} |
206 | 0 | if (name.equals(STYLE_SHORT)) |
207 | |
{ |
208 | 0 | return DateFormat.SHORT; |
209 | |
} |
210 | 0 | if (name.equals(STYLE_LONG)) |
211 | |
{ |
212 | 0 | return DateFormat.LONG; |
213 | |
} |
214 | 0 | if (name.equals(STYLE_FULL)) |
215 | |
{ |
216 | 0 | return DateFormat.FULL; |
217 | |
} |
218 | |
|
219 | 0 | throw new ConverterException("invalid style '" + name + "'"); |
220 | |
} |
221 | |
|
222 | |
|
223 | |
public void restoreState(FacesContext facesContext, Object state) |
224 | |
{ |
225 | 0 | Object[] values = (Object[])state; |
226 | 0 | _dateStyle = (String)values[0]; |
227 | 0 | _locale = (Locale)values[1]; |
228 | 0 | _pattern = (String)values[2]; |
229 | 0 | _timeStyle = (String)values[3]; |
230 | 0 | _timeZone = (TimeZone)values[4]; |
231 | 0 | _type = (String)values[5]; |
232 | 0 | } |
233 | |
|
234 | |
public Object saveState(FacesContext facesContext) |
235 | |
{ |
236 | 0 | Object[] values = new Object[6]; |
237 | 0 | values[0] = _dateStyle; |
238 | 0 | values[1] = _locale; |
239 | 0 | values[2] = _pattern; |
240 | 0 | values[3] = _timeStyle; |
241 | 0 | values[4] = _timeZone; |
242 | 0 | values[5] = _type; |
243 | 0 | return values; |
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
@JSFProperty |
254 | |
public String getDateStyle() |
255 | |
{ |
256 | 0 | return _dateStyle != null ? _dateStyle : STYLE_DEFAULT; |
257 | |
} |
258 | |
|
259 | |
public void setDateStyle(String dateStyle) |
260 | |
{ |
261 | |
|
262 | 0 | _dateStyle = dateStyle; |
263 | 0 | } |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
@JSFProperty |
270 | |
public Locale getLocale() |
271 | |
{ |
272 | 0 | if (_locale != null) return _locale; |
273 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
274 | 0 | return context.getViewRoot().getLocale(); |
275 | |
} |
276 | |
|
277 | |
public void setLocale(Locale locale) |
278 | |
{ |
279 | 0 | _locale = locale; |
280 | 0 | } |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
@JSFProperty |
287 | |
public String getPattern() |
288 | |
{ |
289 | 0 | return _pattern; |
290 | |
} |
291 | |
|
292 | |
public void setPattern(String pattern) |
293 | |
{ |
294 | 0 | _pattern = pattern; |
295 | 0 | } |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
@JSFProperty |
303 | |
public String getTimeStyle() |
304 | |
{ |
305 | 0 | return _timeStyle != null ? _timeStyle : STYLE_DEFAULT; |
306 | |
} |
307 | |
|
308 | |
public void setTimeStyle(String timeStyle) |
309 | |
{ |
310 | |
|
311 | 0 | _timeStyle = timeStyle; |
312 | 0 | } |
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
@JSFProperty |
323 | |
public TimeZone getTimeZone() |
324 | |
{ |
325 | 0 | return _timeZone != null ? _timeZone : TIMEZONE_DEFAULT; |
326 | |
} |
327 | |
|
328 | |
public void setTimeZone(TimeZone timeZone) |
329 | |
{ |
330 | 0 | _timeZone = timeZone; |
331 | 0 | } |
332 | |
|
333 | |
public boolean isTransient() |
334 | |
{ |
335 | 0 | return _transient; |
336 | |
} |
337 | |
|
338 | |
public void setTransient(boolean aTransient) |
339 | |
{ |
340 | 0 | _transient = aTransient; |
341 | 0 | } |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
@JSFProperty |
350 | |
public String getType() |
351 | |
{ |
352 | 0 | return _type != null ? _type : TYPE_DATE; |
353 | |
} |
354 | |
|
355 | |
public void setType(String type) |
356 | |
{ |
357 | |
|
358 | 0 | _type = type; |
359 | 0 | } |
360 | |
} |