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