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  package org.itracker.web.taglib;
19  
20  import org.apache.log4j.Logger;
21  import org.apache.struts.taglib.TagUtils;
22  import org.itracker.core.resources.ITrackerResources;
23  import org.itracker.web.util.Constants;
24  import org.itracker.web.util.LoginUtilities;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.jsp.JspException;
28  import javax.servlet.jsp.tagext.TagSupport;
29  import java.util.Arrays;
30  import java.util.Locale;
31  
32  
33  public class FormatMessageTag extends TagSupport {
34  
35      private static final long serialVersionUID = 1L;
36      private String key = null;
37      private String arg0 = null;
38      private String arg1 = null;
39      private String arg2 = null;
40      private String locale = null;
41      private String localeKey = Constants.LOCALE_KEY;
42  
43      private static final Logger log = Logger.getLogger(FormatMessageTag.class);
44  
45      protected static final Locale defaultLocale = ITrackerResources.getLocale();
46  
47      public String getArg0() {
48          return arg0;
49      }
50  
51      public void setArg0(String value) {
52          arg0 = value;
53      }
54  
55      public String getArg1() {
56          return arg1;
57      }
58  
59      public void setArg1(String value) {
60          arg1 = value;
61      }
62  
63      public String getArg2() {
64          return arg2;
65      }
66  
67      public void setArg2(String value) {
68          arg2 = value;
69      }
70  
71      public String getKey() {
72          return key;
73      }
74  
75      public void setKey(String value) {
76          key = value;
77      }
78  
79      public String getLocaleKey() {
80          return localeKey;
81      }
82  
83      public void setLocaleKey(String value) {
84          localeKey = value;
85      }
86  
87      public String getLocale() {
88          return locale;
89      }
90  
91      public void setLocale(String value) {
92          locale = value;
93      }
94  
95      public int doStartTag() throws JspException {
96          return SKIP_BODY;
97      }
98  
99      public int doEndTag() throws JspException {
100         String message = null;
101         Locale messageLocale = defaultLocale;
102         messageLocale = (Locale) LoginUtilities.getCurrentLocale((HttpServletRequest) pageContext.getRequest());
103 //        messageLocale = (Locale) (pageContext.getSession().getAttribute(getLocaleKey()));
104 
105         if (locale != null) {
106             messageLocale = ITrackerResources.getLocale(locale);
107             if (log.isDebugEnabled()) {
108                 log.debug("doEndTag: locale resolved to " + messageLocale);
109             }
110         }
111         Object[] args = null;
112         if (getArg0() == null) {
113             message = ITrackerResources.getString(key, messageLocale);
114         } else if (getArg2() != null) {
115             args = new Object[]{getArg0(), getArg1(), getArg2()};
116             message = ITrackerResources.getString(key, messageLocale, args);
117         } else if (getArg1() != null) {
118             args = new Object[]{getArg0(), getArg1()};
119             message = ITrackerResources.getString(key, messageLocale, args);
120         } else {
121             args = new Object[]{getArg0()};
122             message = ITrackerResources.getString(key, messageLocale, args);
123         }
124 
125         if (log.isDebugEnabled()) {
126             StringBuffer sb = new StringBuffer("doEndTag: resolved ").append(key).append(" for locale: ").append(" to '").append(message).append("', args_: ").append((null == args ? null : Arrays.asList(args)));
127             log.debug(sb.toString());
128         }
129 
130         //ResponseUtils.write(pageContext, message);
131         TagUtils.getInstance().write(pageContext, message);
132 
133         clearState();
134         return EVAL_PAGE;
135     }
136 
137 
138     public void release() {
139         super.release();
140         localeKey = Constants.LOCALE_KEY;
141     }
142 
143     private void clearState() {
144         arg0 = null;
145         arg1 = null;
146         arg2 = null;
147         key = null;
148         locale = null;
149     }
150 }