KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > 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.el;
30
31 import com.caucho.el.Expr;
32 import com.caucho.jsp.PageContextImpl;
33 import com.caucho.util.L10N;
34
35 import javax.el.ELContext;
36 import javax.el.ELException;
37 import javax.servlet.jsp.JspException JavaDoc;
38 import javax.servlet.jsp.JspWriter JavaDoc;
39 import javax.servlet.jsp.jstl.core.Config;
40 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
41 import java.text.DateFormat JavaDoc;
42 import java.text.SimpleDateFormat JavaDoc;
43 import java.util.Date JavaDoc;
44 import java.util.Locale JavaDoc;
45 import java.util.TimeZone JavaDoc;
46
47 /**
48  * Formats an i18n date and prints it.
49  */

50 public class FormatDateTag extends BodyTagSupport JavaDoc {
51   private static L10N L = new L10N(FormatDateTag.class);
52   
53   private Expr _valueExpr;
54   
55   private Expr _typeExpr;
56   private Expr _dateStyleExpr;
57   private Expr _timeStyleExpr;
58   
59   private Expr _patternExpr;
60   private Expr _timeZoneExpr;
61
62   private String JavaDoc _var;
63   private String JavaDoc _scope;
64
65   /**
66    * Sets the formatting value.
67    *
68    * @param value the JSP-EL expression for the value.
69    */

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

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

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

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

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

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

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

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

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