KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > taglib > FormatDateTag


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.taglib;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26 import javax.servlet.jsp.*;
27 import javax.servlet.jsp.tagext.*;
28
29 import org.apache.struts.Globals;
30 import org.apache.struts.action.*;
31 import org.apache.struts.util.*;
32
33 import cowsultants.itracker.ejb.client.resources.*;
34 import cowsultants.itracker.ejb.client.util.*;
35 import cowsultants.itracker.web.util.*;
36
37 public class FormatDateTag extends TagSupport {
38     private String JavaDoc emptyKey = "itracker.web.generic.unavailable";
39     private String JavaDoc format;
40     private Date date;
41
42     public String JavaDoc getFormat() {
43         return format;
44     }
45
46     public void setFormat(String JavaDoc value) {
47         format = value;
48     }
49
50     public Date getDate() {
51         return date;
52     }
53
54     public void setDate(Date value) {
55         date = value;
56     }
57
58     public String JavaDoc getEmptyKey() {
59         return emptyKey;
60     }
61
62     public void setEmptyKey(String JavaDoc value) {
63         emptyKey = value;
64     }
65
66     public int doStartTag() throws JspException {
67         return SKIP_BODY;
68     }
69
70     public int doEndTag() throws JspException {
71         String JavaDoc value = "";
72         SimpleDateFormat sdf;
73         Locale currLocale = null;
74
75         HttpSession session = pageContext.getSession();
76         if(session != null) {
77             currLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
78         }
79
80         if(currLocale == null) {
81             currLocale = ITrackerResources.getLocale();
82         }
83
84         if(date == null) {
85             value = ITrackerResources.getString(emptyKey, currLocale);
86         } else {
87             try {
88                 if("short".equalsIgnoreCase(format)) {
89                     sdf = new SimpleDateFormat(ITrackerResources.getString("itracker.dateformat.short", currLocale), currLocale);
90                 } else if("notime".equalsIgnoreCase(format)) {
91                     sdf = new SimpleDateFormat(ITrackerResources.getString("itracker.dateformat.dateonly", currLocale), currLocale);
92                 } else {
93                     sdf = new SimpleDateFormat(ITrackerResources.getString("itracker.dateformat.full", currLocale), currLocale);
94                 }
95                 value = sdf.format(date);
96             } catch(Exception JavaDoc e) {
97                 value = ITrackerResources.getString(emptyKey, currLocale);
98             }
99         }
100         ResponseUtils.write(pageContext, value);
101
102         clearState();
103         return EVAL_PAGE;
104     }
105
106     public void release() {
107         super.release();
108         clearState();
109     }
110
111     private void clearState() {
112         emptyKey = "itracker.web.generic.unavailable";
113         format = null;
114         date = null;
115     }
116
117 }
118
Popular Tags