KickJava   Java API By Example, From Geeks To Geeks.

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


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.SimpleDateFormat JavaDoc;
41 import java.util.Date 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 FormatDateTag extends BodyTagSupport JavaDoc {
49   private static L10N L = new L10N(FormatDateTag.class);
50
51   private Date JavaDoc _value;
52   
53   private String JavaDoc _type;
54   private String JavaDoc _dateStyle;
55   private String JavaDoc _timeStyle;
56   
57   private String JavaDoc _pattern;
58   private Object JavaDoc _timeZone;
59
60   private String JavaDoc _var;
61   private String JavaDoc _scope;
62
63   /**
64    * Sets the formatting value.
65    *
66    * @param value the date value.
67    */

68   public void setValue(Object JavaDoc value)
69   {
70     if (value instanceof Number JavaDoc)
71       _value = new Date JavaDoc(((Number JavaDoc) value).longValue());
72     else
73       _value = (Date JavaDoc) value;
74   }
75
76   /**
77    * Sets the date/time type.
78    *
79    * @param type the date/time type.
80    */

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

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

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

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

121   public void setTimeZone(Object JavaDoc zone)
122   {
123     _timeZone = zone;
124   }
125
126   /**
127    * Sets the variable name.
128    *
129    * @param var the variable name to store the value in.
130    */

131   public void setVar(String JavaDoc var)
132   {
133     _var = var;
134   }
135
136   /**
137    * Sets the variable scope.
138    *
139    * @param scope the variable scope to store the value in.
140    */

141   public void setScope(String JavaDoc scope)
142   {
143     _scope = scope;
144   }
145
146   /**
147    * Process the tag.
148    */

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