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