KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > FmtParseDateTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jstl.rt;
30
31 import com.caucho.jsp.PageContextImpl;
32 import com.caucho.util.L10N;
33
34 import javax.servlet.jsp.JspException JavaDoc;
35 import javax.servlet.jsp.JspWriter JavaDoc;
36 import javax.servlet.jsp.jstl.core.Config;
37 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.text.DateFormat JavaDoc;
40 import java.text.ParseException JavaDoc;
41 import java.text.SimpleDateFormat JavaDoc;
42 import java.util.Locale JavaDoc;
43 import java.util.TimeZone JavaDoc;
44
45 /**
46  * Formats an i18n date and prints it.
47  */

48 public class FmtParseDateTag extends BodyTagSupport JavaDoc {
49   private static L10N L = new L10N(FmtParseDateTag.class);
50
51   private String JavaDoc _value;
52   
53   private String JavaDoc _type;
54   private String JavaDoc _dateStyle;
55   private String JavaDoc _timeStyle;
56   
57   private Object JavaDoc _parseLocale;
58   
59   private String JavaDoc _pattern;
60   private Object JavaDoc _timeZone;
61   
62   private String JavaDoc _var;
63   private String JavaDoc _scope;
64
65   /**
66    * Sets the formatting value.
67    *
68    * @param value the string value.
69    */

70   public void setValue(String JavaDoc value)
71   {
72     _value = value;
73   }
74
75   /**
76    * Sets the date/time type.
77    *
78    * @param type the date/time type.
79    */

80   public void setType(String JavaDoc type)
81   {
82     _type = type;
83   }
84
85   /**
86    * Sets the date style (full, short, etc.)
87    *
88    * @param style the date style
89    */

90   public void setDateStyle(String JavaDoc style)
91   {
92     _dateStyle = style;
93   }
94
95   /**
96    * Sets the time style (full, short, etc.)
97    *
98    * @param style the time style
99    */

100   public void setTimeStyle(String JavaDoc style)
101   {
102     _timeStyle = style;
103   }
104
105   /**
106    * Sets the formatting pattern.
107    *
108    * @param pattern the formatting pattern.
109    */

110   public void setPattern(String JavaDoc pattern)
111   {
112     _pattern = pattern;
113   }
114
115   /**
116    * Sets the time zone.
117    *
118    * @param zone the time zone expression
119    */

120   public void setTimeZone(Object JavaDoc zone)
121   {
122     _timeZone = zone;
123   }
124
125   /**
126    * Sets the parse locale
127    *
128    * @param locale the locale
129    */

130   public void setParseLocale(Object JavaDoc locale)
131   {
132     _parseLocale = locale;
133   }
134
135   /**
136    * Sets the variable name.
137    *
138    * @param var the variable name to store the value in.
139    */

140   public void setVar(String JavaDoc var)
141   {
142     _var = var;
143   }
144
145   /**
146    * Sets the variable scope.
147    *
148    * @param scope the variable scope to store the value in.
149    */

150   public void setScope(String JavaDoc scope)
151   {
152     _scope = scope;
153   }
154
155   /**
156    * Process the tag.
157    */

158   public int doEndTag()
159     throws JspException JavaDoc
160   {
161     try {
162       PageContextImpl pc = (PageContextImpl) pageContext;
163       
164       JspWriter JavaDoc out = pc.getOut();
165
166       String JavaDoc string;
167
168       if (_value != null)
169         string = _value;
170       else
171         string = bodyContent.getString().trim();
172       
173       DateFormat JavaDoc format = getFormat();
174
175       Object JavaDoc value = format.parse(string);
176
177       if (_var == null)
178         out.print(value);
179       else
180         CoreSetTag.setValue(pageContext, _var, _scope, value);
181     } catch (IOException JavaDoc e) {
182     } catch (ParseException JavaDoc e) {
183       throw new JspException JavaDoc(e);
184     }
185
186     return EVAL_PAGE;
187   }
188
189   protected DateFormat JavaDoc getFormat()
190     throws JspException JavaDoc
191   {
192     PageContextImpl pc = (PageContextImpl) pageContext;
193     
194     DateFormat JavaDoc format = null;
195     Locale JavaDoc locale = null;
196
197     if (_parseLocale != null) {
198       Object JavaDoc localeObj = _parseLocale;
199
200       if (localeObj instanceof Locale JavaDoc)
201         locale = (Locale JavaDoc) localeObj;
202       else if (localeObj instanceof String JavaDoc)
203         locale = pc.getLocale((String JavaDoc) localeObj, null);
204     }
205     
206     if (locale == null)
207       locale = pc.getLocale();
208
209     int dateStyle = DateFormat.DEFAULT;
210     if (_dateStyle != null)
211       dateStyle = getDateStyle(_dateStyle);
212
213     int timeStyle = DateFormat.DEFAULT;
214     if (_timeStyle != null)
215       timeStyle = getDateStyle(_timeStyle);
216
217     if (locale != null) {
218       if (_type == null || _type.equals("date"))
219         format = DateFormat.getDateInstance(dateStyle, locale);
220       else if (_type.equals("both"))
221         format = DateFormat.getDateTimeInstance(dateStyle,
222                                                 timeStyle,
223                                                 locale);
224       else if (_type.equals("time"))
225         format = DateFormat.getTimeInstance(timeStyle, locale);
226       else
227         throw new JspException JavaDoc(L.l("illegal type `{0}'", _type));
228     }
229     else {
230       if (_type == null || _type.equals("date"))
231         format = DateFormat.getDateInstance(dateStyle);
232       else if (_type.equals("both"))
233         format = DateFormat.getDateTimeInstance(dateStyle, timeStyle);
234       else if (_type.equals("time"))
235         format = DateFormat.getTimeInstance(timeStyle);
236       else
237         throw new JspException JavaDoc(L.l("illegal type `{0}'", _type));
238     }
239
240     if (format == null)
241       return null;
242
243     if (_pattern != null) {
244       try {
245         ((SimpleDateFormat JavaDoc) format).applyPattern(_pattern);
246       } catch (ClassCastException JavaDoc e) {
247         format = new SimpleDateFormat JavaDoc(_pattern, locale);
248       }
249     }
250     
251     TimeZone JavaDoc timeZone = getTimeZone(_timeZone);
252
253     if (timeZone == null)
254       timeZone = (TimeZone JavaDoc) pageContext.getAttribute("com.caucho.time-zone");
255         
256     if (timeZone == null)
257       timeZone = getTimeZone(Config.find(pageContext, Config.FMT_TIME_ZONE));
258
259     if (timeZone != null)
260       format.setTimeZone(timeZone);
261
262     return format;
263   }
264
265   private TimeZone JavaDoc getTimeZone(Object JavaDoc timeZoneObj)
266   {
267     if (timeZoneObj instanceof TimeZone JavaDoc)
268       return (TimeZone JavaDoc) timeZoneObj;
269     else if (timeZoneObj instanceof String JavaDoc) {
270       String JavaDoc timeZoneString = (String JavaDoc) timeZoneObj;
271
272       return TimeZone.getTimeZone(timeZoneString);
273     }
274
275     return null;
276   }
277
278   public static int getDateStyle(String JavaDoc style)
279     throws JspException JavaDoc
280   {
281     if (style == null || style.equals("default"))
282       return DateFormat.DEFAULT;
283     else if (style.equals("short"))
284       return DateFormat.SHORT;
285     else if (style.equals("medium"))
286       return DateFormat.MEDIUM;
287     else if (style.equals("long"))
288       return DateFormat.LONG;
289     else if (style.equals("full"))
290       return DateFormat.FULL;
291     else
292       throw new JspException JavaDoc(L.l("illegal date style `{0}'", style));
293   }
294 }
295
Popular Tags