KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > convert > DateTimeConverter


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package javax.faces.convert;
17
18 import javax.faces.component.UIComponent;
19 import javax.faces.component.StateHolder;
20 import javax.faces.context.FacesContext;
21 import java.text.DateFormat JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.TimeZone JavaDoc;
26
27 /**
28  * @author Thomas Spiegl (latest modification by $Author: svieujot $)
29  * @version $Revision: 1.13 $ $Date: 2004/07/11 05:23:24 $
30  * $Log: DateTimeConverter.java,v $
31  * Revision 1.13 2004/07/11 05:23:24 svieujot
32  * Remove the DEFAULT_TIME_ZONE
33  *
34  * Revision 1.12 2004/07/01 22:00:51 mwessendorf
35  * ASF switch
36  *
37  * Revision 1.11 2004/06/07 13:40:37 mwessendorf
38  * solved Feature Request #966892
39  *
40  * Revision 1.10 2004/04/16 09:08:41 royalts
41  * fixed getAsString: getTimeZone() may not be used here!!.
42  *
43  * Revision 1.9 2004/04/01 10:47:00 royalts
44  * bugfix in restoreState
45  *
46  * Revision 1.8 2004/04/01 10:39:53 royalts
47  * implements StateHoder was missing
48  *
49  * Revision 1.7 2004/03/26 12:08:42 manolito
50  * Exceptions in getAsString now catched and
51  * more relaxed Number casting in all number converters
52  *
53  */

54 public class DateTimeConverter
55         implements Converter, StateHolder
56 {
57     private static final String JavaDoc CONVERSION_MESSAGE_ID = "javax.faces.convert.DateTimeConverter.CONVERSION";
58
59     // FIELDS
60
public static final String JavaDoc CONVERTER_ID = "javax.faces.DateTime";
61     private static final String JavaDoc TYPE_DATE = "date";
62     private static final String JavaDoc TYPE_TIME = "time";
63     private static final String JavaDoc TYPE_BOTH = "both";
64     private static final String JavaDoc STYLE_DEFAULT = "default";
65     private static final String JavaDoc STYLE_MEDIUM = "medium";
66     private static final String JavaDoc STYLE_SHORT = "short";
67     private static final String JavaDoc STYLE_LONG = "long";
68     private static final String JavaDoc STYLE_FULL = "full";
69
70     private String JavaDoc _dateStyle;
71     private Locale JavaDoc _locale;
72     private String JavaDoc _pattern;
73     private String JavaDoc _timeStyle;
74     private TimeZone JavaDoc _timeZone;
75     private String JavaDoc _type;
76     private boolean _transient;
77
78     // CONSTRUCTORS
79
public DateTimeConverter()
80     {
81     }
82
83     // METHODS
84
public Object JavaDoc getAsObject(FacesContext facesContext, UIComponent uiComponent, String JavaDoc value)
85     {
86         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
87         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
88
89         if (value != null)
90         {
91             value = value.trim();
92             if (value.length() > 0)
93             {
94                 DateFormat JavaDoc format = getDateFormat();
95                 format.setLenient(true);
96                 TimeZone JavaDoc tz = getTimeZone();
97                 if( tz != null )
98                     format.setTimeZone( tz );
99                 try
100                 {
101                     return format.parse(value);
102                 }
103                 catch (ParseException JavaDoc e)
104                 {
105                     throw new ConverterException(_MessageUtils.getErrorMessage(facesContext,
106                                                                                CONVERSION_MESSAGE_ID,
107                                                                                new Object JavaDoc[]{value,uiComponent.getId()}), e);
108                 }
109             }
110         }
111         return null;
112     }
113
114     public String JavaDoc getAsString(FacesContext facesContext, UIComponent uiComponent, Object JavaDoc value)
115     {
116         if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
117         if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
118
119         if (value == null)
120         {
121             return "";
122         }
123         if (value instanceof String JavaDoc)
124         {
125             return (String JavaDoc)value;
126         }
127
128         DateFormat JavaDoc format = getDateFormat();
129         if (_timeZone != null)
130         {
131             format.setTimeZone(_timeZone);
132         }
133         try
134         {
135             return format.format(value);
136         }
137         catch (Exception JavaDoc e)
138         {
139             throw new ConverterException("Cannot convert value '" + value + "'");
140         }
141     }
142
143     private DateFormat JavaDoc getDateFormat()
144     {
145         String JavaDoc type = getType();
146         DateFormat JavaDoc format = null;
147         if (_pattern != null)
148         {
149             format = new SimpleDateFormat JavaDoc(_pattern, getLocale());
150         }
151         else if (type.equals(TYPE_DATE))
152         {
153             format = DateFormat.getDateInstance(calcStyle(getDateStyle()), getLocale());
154         }
155         else if (type.equals(TYPE_TIME))
156         {
157             format = DateFormat.getTimeInstance(calcStyle(getTimeStyle()), getLocale());
158         }
159         else if (type.equals(TYPE_BOTH))
160         {
161             format = DateFormat.getDateTimeInstance(calcStyle(getDateStyle()),
162                                                     calcStyle(getTimeStyle()),
163                                                     getLocale());
164         }
165         else
166         {
167             throw new ConverterException("invalid type '" + _type + "'");
168         }
169         return format;
170     }
171
172     private int calcStyle(String JavaDoc name)
173     {
174         if (name.equals(STYLE_DEFAULT))
175         {
176             return DateFormat.DEFAULT;
177         }
178         if (name.equals(STYLE_MEDIUM))
179         {
180             return DateFormat.MEDIUM;
181         }
182         if (name.equals(STYLE_SHORT))
183         {
184             return DateFormat.SHORT;
185         }
186         if (name.equals(STYLE_LONG))
187         {
188             return DateFormat.LONG;
189         }
190         if (name.equals(STYLE_FULL))
191         {
192             return DateFormat.FULL;
193         }
194
195         throw new ConverterException("invalid style '" + name + "'");
196     }
197
198     // STATE SAVE/RESTORE
199
public void restoreState(FacesContext facesContext, Object JavaDoc state)
200     {
201         Object JavaDoc[] values = (Object JavaDoc[])state;
202         _dateStyle = (String JavaDoc)values[0];
203         _locale = (Locale JavaDoc)values[1];
204         _pattern = (String JavaDoc)values[2];
205         _timeStyle = (String JavaDoc)values[3];
206         _timeZone = (TimeZone JavaDoc)values[4];
207         _type = (String JavaDoc)values[5];
208     }
209
210     public Object JavaDoc saveState(FacesContext facesContext)
211     {
212         Object JavaDoc[] values = new Object JavaDoc[6];
213         values[0] = _dateStyle;
214         values[1] = _locale;
215         values[2] = _pattern;
216         values[3] = _timeStyle;
217         values[4] = _timeZone;
218         values[5] = _type;
219         return values;
220     }
221
222     // GETTER & SETTER
223
public String JavaDoc getDateStyle()
224     {
225         return _dateStyle != null ? _dateStyle : STYLE_DEFAULT;
226     }
227
228     public void setDateStyle(String JavaDoc dateStyle)
229     {
230         //TODO: validate timeStyle
231
_dateStyle = dateStyle;
232     }
233
234     public Locale JavaDoc getLocale()
235     {
236         if (_locale != null) return _locale;
237         FacesContext context = FacesContext.getCurrentInstance();
238         return context.getViewRoot().getLocale();
239     }
240
241     public void setLocale(Locale JavaDoc locale)
242     {
243         _locale = locale;
244     }
245
246     public String JavaDoc getPattern()
247     {
248         return _pattern;
249     }
250
251     public void setPattern(String JavaDoc pattern)
252     {
253         _pattern = pattern;
254     }
255
256     public String JavaDoc getTimeStyle()
257     {
258         return _timeStyle != null ? _timeStyle : STYLE_DEFAULT;
259     }
260
261     public void setTimeStyle(String JavaDoc timeStyle)
262     {
263         //TODO: validate timeStyle
264
_timeStyle = timeStyle;
265     }
266
267     public TimeZone JavaDoc getTimeZone()
268     {
269         return _timeZone;
270     }
271
272     public void setTimeZone(TimeZone JavaDoc timeZone)
273     {
274         _timeZone = timeZone;
275     }
276
277     public boolean isTransient()
278     {
279         return _transient;
280     }
281
282     public void setTransient(boolean aTransient)
283     {
284         _transient = aTransient;
285     }
286
287     public String JavaDoc getType()
288     {
289         return _type != null ? _type : TYPE_DATE;
290     }
291
292     public void setType(String JavaDoc type)
293     {
294         //TODO: validate type
295
_type = type;
296     }
297 }
298
Popular Tags