KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > FormatDateTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 com.blandware.atleap.webapp.taglib.core;
17
18 import com.blandware.atleap.common.util.DateUtil;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.struts.Globals;
22 import org.apache.struts.taglib.TagUtils;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspTagException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
28 import java.text.DateFormat JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Locale JavaDoc;
32
33 /**
34  * <p>Formats date according to specified pattern. If variable is specified,
35  * formatted date is saved to it, else it's written to the page.
36  * If no pattern is specified, it is taken according to locale, stored in session under
37  * <code>org.apache.struts.Globals.LOCALE_KEY</code>
38  * </p>
39  * <p>This tag supports instances of following classes as input value:
40  * <ul type="disc">
41  * <li>java.lang.String (see below for information about reserved words)</li>
42  * <li>java.util.Date</li>
43  * <li>java.sql.Date</li>
44  * <li>java.sql.Timestamp</li>
45  * <li>java.util.Calendar</li>
46  * </ul>
47  * </p>
48  * You can specify following reserved words as string value:
49  * <ul type="disc">
50  * <li><em>now</em> - value will be treated as today (you can also omit <code>value</code> attribute to gain the same effect)</li>
51  * <li><em>epoch</em> - value will be the date when Unix epoch has started: January 1, 1970</li>
52  * </ul>
53  * <p>
54  * Full list of allowed attributes:
55  * <ul>
56  * <li>
57  * <b>value</b> - represents date that needs to be formatted (see above)
58  * </li>
59  * <li>
60  * <b>locale</b> - represents locale to format date to. Can be
61  * <code>String</code> (for example, "en_US") or <code>java.util.Locale</code>
62  * </li>
63  * <li>
64  * <b>type</b> - type of formatter: date, time or datetime. Default is date
65  * </li>
66  * <li>
67  * <b>dateStyle</b> - date style. Can be either SHORT, MEDIUM, LONG or FULL.
68  * See <code>java.text.DateFormat</code> for more information
69  * </li>
70  * <li>
71  * <b>timeStyle</b> - time style. Can be either SHORT, MEDIUM, LONG or FULL.
72  * See <code>java.text.DateFormat</code> for more infomation
73  * </li>
74  * <li>
75  * <b>var</b> - name of variable to export formatted date. If not given, result
76  * is not exported but written to the page
77  * </li>
78  * <li>
79  * <b>scope</b> - scope of variable to export formatted date. If <code>var</code>
80  * is not specified, this attribute is ignored
81  * </li>
82  * </ul>
83  * </p>
84  * <p>
85  * Example of use is <code>&lt;atleap:formatDate value="now" type="datetime" /&gt;</code>.
86  * This will render current date and time for current locale (which is stored in
87  * session).
88  * </p>
89  * <p><a HREF="FormatDateTag.java.htm"><i>View Source</i></a></p>
90  *
91  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
92  * @version $Revision: 1.16 $ $Date: 2005/10/12 13:34:53 $
93  * @jsp.tag name="formatDate"
94  * body-content="empty"
95  * @see org.apache.struts.Globals#LOCALE_KEY
96  * @see java.text.SimpleDateFormat
97  */

98 public class FormatDateTag extends SimpleTagSupport JavaDoc {
99
100     protected transient final Log log = LogFactory.getLog(FormatDateTag.class);
101
102     /**
103      * Object contains date to be formatted.
104      * Supports instances of <code>java.util.Date</code>, <code>java.sql.Date</code>,
105      * <code>java.sql.Timestamp</code> and <code>java.util.Calendar</code>.
106      */

107     protected Object JavaDoc value;
108
109     /**
110      * Locale to format date according to. Supports instances of <code>java.lang.String</code> or <code>java.util.Locale</code>
111      */

112     protected Object JavaDoc locale;
113
114     /**
115      * Type of formatter: date, time or datetime. Default is date.
116      */

117     protected String JavaDoc type = "date";
118
119     /**
120      * Date style. Can be either SHORT, MEDIUM, LONG or FULL.
121      * See <code>java.text.DateFormat</code> for more infomation
122      *
123      * @see java.text.DateFormat
124      */

125     protected String JavaDoc dateStyle = "medium";
126
127     /**
128      * Time style. Can be either SHORT, MEDIUM, LONG or FULL.
129      * See <code>java.text.DateFormat</code> for more infomation
130      *
131      * @see java.text.DateFormat
132      */

133     protected String JavaDoc timeStyle = "medium";
134
135     /**
136      * Name of variable to export formatted date
137      */

138     protected String JavaDoc var;
139
140     /**
141      * Scope to export variable to
142      */

143     protected String JavaDoc scope;
144
145     /**
146      * Returns date to be formatted
147      *
148      * @return date to be formatted
149      * @see #value
150      * @jsp.attribute required="false"
151      * rtexprvalue="true"
152      * type="java.lang.Object"
153      * description="Object contains date to be formatted."
154      */

155     public Object JavaDoc getValue() {
156         return value;
157     }
158
159     /**
160      * Sets date to be formatted
161      *
162      * @param value date to be formatted
163      * @see #value
164      */

165     public void setValue(Object JavaDoc value) {
166         this.value = value;
167     }
168
169     /**
170      * Returns locale
171      *
172      * @return locale
173      * @see #locale
174      * @jsp.attribute required="false"
175      * rtexprvalue="true"
176      * type="java.lang.Object"
177      * description="Locale to format date according to"
178      */

179     public Object JavaDoc getLocale() {
180         return locale;
181     }
182
183     /**
184      * Sets locale
185      *
186      * @param locale locale to set
187      * @see #locale
188      */

189     public void setLocale(Object JavaDoc locale) {
190         this.locale = locale;
191     }
192
193     /**
194      * Returns type of formatter
195      *
196      * @return type of formatter
197      * @see #type
198      * @jsp.attribute required="false"
199      * rtexprvalue="true"
200      * type="java.lang.String"
201      * description="Type of formatter: date, time or datetime. Default is date"
202      */

203     public String JavaDoc getType() {
204         return type;
205     }
206
207     /**
208      * Sets type of formatter
209      *
210      * @param type type of formatter to set
211      * @see #type
212      */

213     public void setType(String JavaDoc type) {
214         this.type = type;
215     }
216
217     /**
218      * Returns style of date
219      *
220      * @return style of date
221      * @see #dateStyle
222      * @jsp.attribute required="false"
223      * rtexprvalue="true"
224      * type="java.lang.String"
225      * description="Date style"
226      */

227     public String JavaDoc getDateStyle() {
228         return dateStyle;
229     }
230
231     /**
232      * Sets style of date
233      *
234      * @param dateStyle style of date to set
235      * @see #dateStyle
236      */

237     public void setDateStyle(String JavaDoc dateStyle) {
238         this.dateStyle = dateStyle;
239     }
240
241     /**
242      * Returns style of time
243      *
244      * @return style of time
245      * @see #timeStyle
246      * @jsp.attribute required="false"
247      * rtexprvalue="true"
248      * type="java.lang.String"
249      * description="Time style"
250      */

251     public String JavaDoc getTimeStyle() {
252         return timeStyle;
253     }
254
255     /**
256      * Sets style of time
257      *
258      * @param timeStyle style of time to set
259      * @see #timeStyle
260      */

261     public void setTimeStyle(String JavaDoc timeStyle) {
262         this.timeStyle = timeStyle;
263     }
264
265     /**
266      * Returns name of variable that will accept formatted date
267      *
268      * @return name of variable
269      * @see #var
270      * @jsp.attribute required="false"
271      * rtexprvalue="true"
272      * type="java.lang.String"
273      * description="Name of variable to export formatted date"
274      */

275     public String JavaDoc getVar() {
276         return var;
277     }
278
279     /**
280      * Sets name of variable that will accept formatted date
281      *
282      * @param var name of variable to set
283      * @see #var
284      */

285     public void setVar(String JavaDoc var) {
286         this.var = var;
287     }
288
289     /**
290      * Returns scope of variable
291      *
292      * @return scope of variable
293      * @see #scope
294      * @jsp.attribute required="false"
295      * rtexprvalue="true"
296      * type="java.lang.String"
297      * description="Scope to export variable to"
298      */

299     public String JavaDoc getScope() {
300         return scope;
301     }
302
303     /**
304      * Sets scope of variable
305      *
306      * @param scope scope of variable to set
307      * @see #scope
308      */

309     public void setScope(String JavaDoc scope) {
310         this.scope = scope;
311     }
312
313     /**
314      * Formats date and writes the result to the page or saves it to variable
315      * of specified scope
316      *
317      * @throws JspException
318      */

319     public void doTag() throws JspException JavaDoc {
320
321         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
322
323         if ( value == null ) {
324             value = new Date JavaDoc();
325         }
326
327         int dateStyle = DateFormat.MEDIUM;
328
329         if ( "short".equalsIgnoreCase(this.dateStyle) ) {
330             dateStyle = DateFormat.SHORT;
331         } else if ( "long".equalsIgnoreCase(this.dateStyle) ) {
332             dateStyle = DateFormat.LONG;
333         } else if ( "full".equalsIgnoreCase(this.dateStyle) ) {
334             dateStyle = DateFormat.FULL;
335         }
336
337         int timeStyle = DateFormat.MEDIUM;
338
339         if ( "short".equalsIgnoreCase(this.timeStyle) ) {
340             timeStyle = DateFormat.SHORT;
341         } else if ( "long".equalsIgnoreCase(this.timeStyle) ) {
342             timeStyle = DateFormat.LONG;
343         } else if ( "full".equalsIgnoreCase(this.timeStyle) ) {
344             timeStyle = DateFormat.FULL;
345         }
346
347
348         if ( !"date".equalsIgnoreCase(type) && !"time".equalsIgnoreCase(type) && !"datetime".equalsIgnoreCase(type) ) {
349             type = "date";
350         }
351
352         Locale JavaDoc locale = null;
353         TagUtils tagUtils = TagUtils.getInstance();
354         if ( this.locale == null ) {
355             locale = (Locale JavaDoc) pageContext.getAttribute(Globals.LOCALE_KEY, PageContext.SESSION_SCOPE);
356         } else if ( this.locale instanceof Locale JavaDoc ) {
357             locale = (Locale JavaDoc) this.locale;
358         } else if ( this.locale instanceof String JavaDoc ) {
359             String JavaDoc localeIdentifier = (String JavaDoc) this.locale;
360             String JavaDoc[] localeElements = localeIdentifier.split("_");
361             if ( localeElements.length >= 1 ) {
362                 String JavaDoc language = localeElements[0];
363                 if ( localeElements.length >= 2 ) {
364                     String JavaDoc country = localeElements[1];
365                     if ( localeElements.length >= 3 ) {
366                         String JavaDoc variant = localeElements[2];
367                         locale = new Locale JavaDoc(language, country, variant);
368                     } else {
369                         locale = new Locale JavaDoc(language, country);
370                     }
371                 } else {
372                     locale = new Locale JavaDoc(language);
373                 }
374             }
375         } else {
376             JspTagException JavaDoc e = new JspTagException JavaDoc("Only instances of java.lang.String and java.util.Locale are supported in 'locale' attribute");
377             tagUtils.saveException(pageContext, e);
378             throw e;
379         }
380
381         String JavaDoc result = new String JavaDoc();
382         if ( value != null ) {
383             Date JavaDoc date = null;
384             if ( value instanceof String JavaDoc ) {
385                 String JavaDoc s = (String JavaDoc) value;
386                 if ( s.equalsIgnoreCase("now") ) {
387                     date = new Date JavaDoc();
388                 } else if ( s.equalsIgnoreCase("epoch") ) {
389                     date = new Date JavaDoc(0);
390                 } else {
391                     JspTagException JavaDoc e = new JspTagException JavaDoc("Can't produce java.util.Date object for string \"" + s + "\"");
392                     tagUtils.saveException(pageContext, e);
393                     throw e;
394                 }
395             } else if ( value instanceof Date JavaDoc ) {
396                 date = (Date JavaDoc) value;
397             } else if ( value instanceof Calendar JavaDoc ) {
398                 date = ((Calendar JavaDoc) value).getTime();
399             } else {
400                 JspTagException JavaDoc e = new JspTagException JavaDoc("Instances of class " + value.getClass().getName() + " are not supported by this tag");
401                 tagUtils.saveException(pageContext, e);
402                 throw e;
403             }
404             if ( "date".equalsIgnoreCase(type) ) {
405                 result = DateUtil.formatDate(date, locale, dateStyle);
406             } else if ( "time".equalsIgnoreCase(type) ) {
407                 result = DateUtil.formatTime(date, locale, timeStyle);
408             } else /* datetime */ {
409                 result = DateUtil.formatDateTime(date, locale, dateStyle, timeStyle);
410             }
411         }
412
413         if ( var != null ) {
414             int varScope = PageContext.PAGE_SCOPE;
415             if ( scope != null ) {
416                 varScope = tagUtils.getScope(scope);
417             }
418             pageContext.setAttribute(var, result, varScope);
419         } else {
420             tagUtils.write(pageContext, result);
421         }
422     }
423
424 }
425
Popular Tags