View Javadoc

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 org.itracker.web.taglib;
20  
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.util.Locale;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.jsp.JspException;
27  import javax.servlet.jsp.tagext.TagSupport;
28  
29  import org.apache.struts.taglib.TagUtils;
30  import org.itracker.core.resources.ITrackerResources;
31  import org.itracker.web.util.LoginUtilities;
32  
33  public class FormatDateTag extends TagSupport {
34  	/**
35  	 * 
36  	 */
37  	private static final long serialVersionUID = 1L;
38  	private String emptyKey = "itracker.web.generic.unavailable";
39  	private String format;
40  	private Date date;
41  
42  	public String getFormat() {
43  		return format;
44  	}
45  
46  	public void setFormat(String value) {
47  		format = value;
48  	}
49  
50  	public Date getDate() {
51  		if (null == date)
52  			return null;
53  		return new Date(date.getTime());
54  	}
55  
56  	public void setDate(Date value) {
57  		if (null == value)
58  			this.date = null;
59  		else
60  			date = new Date(value.getTime());
61  	}
62  
63  	public String getEmptyKey() {
64  		return emptyKey;
65  	}
66  
67  	public void setEmptyKey(String value) {
68  		emptyKey = value;
69  	}
70  
71  	public int doStartTag() throws JspException {
72  		return SKIP_BODY;
73  	}
74  
75  	public int doEndTag() throws JspException {
76  		String value = "";
77  		SimpleDateFormat sdf;
78  		Locale locale = null;
79  		if (pageContext.getRequest() instanceof HttpServletRequest) {
80  			locale = LoginUtilities.getCurrentLocale((HttpServletRequest)pageContext.getRequest());
81  		}
82  		if (locale == null) {
83  			locale = ITrackerResources.getLocale();
84  		}
85  		
86  		if (date == null) {
87  			value = ITrackerResources.getString(emptyKey, locale);
88  		} else {
89  			try {
90  				if ("short".equalsIgnoreCase(format)) {
91  					sdf = new SimpleDateFormat(ITrackerResources.getString(
92  							"itracker.dateformat.short", locale), locale);
93  				} else if ("notime".equalsIgnoreCase(format)) {
94  					sdf = new SimpleDateFormat(ITrackerResources.getString(
95  							"itracker.dateformat.dateonly", locale), locale);
96  				} else {
97  					sdf = new SimpleDateFormat(ITrackerResources.getString(
98  							"itracker.dateformat.full", locale), locale);
99  				}
100 				value = sdf.format(date);
101 			} catch (Exception e) {
102 				value = ITrackerResources.getString(emptyKey, locale);
103 			}
104 		}
105 		// ResponseUtils.write(pageContext, value);
106 		TagUtils.getInstance().write(pageContext, value);
107 		clearState();
108 		return EVAL_PAGE;
109 	}
110 
111 	public void release() {
112 		super.release();
113 		clearState();
114 	}
115 
116 	private void clearState() {
117 		emptyKey = "itracker.web.generic.unavailable";
118 		format = null;
119 		date = null;
120 	}
121 
122 }