KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > ParseDateTag


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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

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

74   public void setValue(Expr value)
75   {
76     _valueExpr = value;
77   }
78
79   /**
80    * Sets the date/time type.
81    *
82    * @param type the date/time type.
83    */

84   public void setType(Expr type)
85   {
86     _typeExpr = type;
87   }
88
89   /**
90    * Sets the date style (full, short, etc.)
91    *
92    * @param style the date style
93    */

94   public void setDateStyle(Expr style)
95   {
96     _dateStyleExpr = style;
97   }
98
99   /**
100    * Sets the time style (full, short, etc.)
101    *
102    * @param style the time style
103    */

104   public void setTimeStyle(Expr style)
105   {
106     _timeStyleExpr = style;
107   }
108
109   /**
110    * Sets the formatting pattern.
111    *
112    * @param pattern the formatting pattern.
113    */

114   public void setPattern(Expr pattern)
115   {
116     _patternExpr = pattern;
117   }
118
119   /**
120    * Sets the time zone.
121    *
122    * @param zone the time zone expression
123    */

124   public void setTimeZone(Expr zone)
125   {
126     _timeZoneExpr = zone;
127   }
128
129   /**
130    * Sets the parse locale
131    *
132    * @param locale the locale
133    */

134   public void setParseLocale(Expr locale)
135   {
136     _parseLocaleExpr = locale;
137   }
138
139   /**
140    * Sets the variable name.
141    *
142    * @param var the variable name to store the value in.
143    */

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

154   public void setScope(String JavaDoc scope)
155   {
156     _scope = scope;
157   }
158
159   /**
160    * Process the tag.
161    */

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