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.logging.log4j.util.Strings;
23  import org.apache.struts.taglib.TagUtils;
24  import org.itracker.core.resources.ITrackerResources;
25  import org.itracker.model.UserPreferences;
26  import org.itracker.web.util.Constants;
27  import org.itracker.web.util.HTMLUtilities;
28  
29  import javax.servlet.http.HttpSession;
30  import javax.servlet.jsp.JspException;
31  import javax.servlet.jsp.PageContext;
32  import javax.servlet.jsp.tagext.BodyTagSupport;
33  import java.net.MalformedURLException;
34  import java.util.Locale;
35  
36  public final class FormatIconActionTag extends BodyTagSupport {
37     /**
38      *
39      */
40     private static final long serialVersionUID = 1L;
41  
42     private String action = null;
43     private String forward = null;
44     private String module = null;
45     private String paramName = null;
46     private String paramValue = null;
47     private String icon = null;
48     private String styleClass = null;
49     private String iconClass = null;
50     private String styleId = null;
51     private String arg0 = null;
52     private String textActionKey = null;
53     private String info = null;
54     private String caller = null;
55     private String targetAction = null;
56     private String target = null;
57  
58     public String getAction() {
59        return action;
60     }
61  
62     public void setAction(String value) {
63        action = value;
64     }
65  
66     public String getForward() {
67        return forward;
68     }
69  
70     public void setForward(String value) {
71        forward = value;
72     }
73  
74     public String getModule() {
75        return module;
76     }
77  
78     public void setModule(String module) {
79        this.module = module;
80     }
81  
82     public String getParamName() {
83        return paramName;
84     }
85  
86     public void setParamName(String value) {
87        paramName = value;
88     }
89  
90     public Object getParamValue() {
91        return paramValue;
92     }
93  
94     public void setParamValue(Object value) {
95        paramValue = (value != null ? value.toString() : null);
96     }
97  
98     public String getIcon() {
99        return icon;
100    }
101 
102    public void setIcon(String icon) {
103       this.icon = icon;
104    }
105 
106    public String getStyleClass() {
107       return styleClass;
108    }
109 
110    public void setStyleClass(String styleClass) {
111       this.styleClass = styleClass;
112    }
113 
114    public String getIconClass() {
115       return iconClass;
116    }
117 
118    public void setIconClass(String iconClass) {
119       this.iconClass = iconClass;
120    }
121 
122    public String getStyleId() {
123       return styleId;
124    }
125 
126    public void setStyleId(String styleId) {
127       this.styleId = styleId;
128    }
129 
130    public Object getArg0() {
131       return arg0;
132    }
133 
134    public void setArg0(Object value) {
135       arg0 = (value != null ? value.toString() : null);
136    }
137 
138    public String getTextActionKey() {
139       return textActionKey;
140    }
141 
142    public void setTextActionKey(String value) {
143       textActionKey = value;
144    }
145 
146    public String getInfo() {
147       return info;
148    }
149 
150    public void setInfo(String info) {
151       this.info = info;
152    }
153 
154    public String getCaller() {
155       return caller;
156    }
157 
158    public void setCaller(String value) {
159       caller = value;
160    }
161 
162    public String getTargetAction() {
163       return targetAction;
164    }
165 
166    public void setTargetAction(String value) {
167       targetAction = value;
168    }
169 
170    public String getTarget() {
171       return target;
172    }
173 
174    public void setTarget(String value) {
175       target = value;
176    }
177 
178    public int doStartTag() throws JspException {
179       boolean useTextActions = false;
180 
181       HttpSession session = pageContext.getSession();
182       if (session != null) {
183          UserPreferencesitracker/model/UserPreferences.html#UserPreferences">UserPreferences currUserPrefs = (UserPreferences) session
184                  .getAttribute(Constants.PREFERENCES_KEY);
185          useTextActions = (currUserPrefs != null
186                  && currUserPrefs.getUseTextActions());
187       }
188 
189       if (!useTextActions) {
190          textActionKey = null;
191       }
192       if (Strings.isBlank(textActionKey))
193          return EVAL_BODY_BUFFERED;
194       return SKIP_BODY;
195    }
196 
197    public int doEndTag() throws JspException {
198       Locale locale = null;
199       HttpSession session = pageContext.getSession();
200       if (session != null) {
201          locale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
202       }
203       styleClass = StringUtils.defaultIfEmpty(styleClass, "");
204 
205       StringBuilder buf = new StringBuilder();
206       appendStartTag(locale, buf);
207       appendBody(locale, buf);
208       appendEndTag(buf);
209       return EVAL_PAGE;
210    }
211 
212    private void appendBody(Locale locale, StringBuilder buf) {
213       boolean hasBody = bodyContent != null && !StringUtils.isBlank(bodyContent.getString());
214       if (hasBody) {
215          icon = null;
216          buf.append(bodyContent.getString());
217       } else {
218          if (!StringUtils.isBlank(textActionKey)) {
219             buf.append(HTMLUtilities.escapeTags(ITrackerResources.getString(textActionKey, locale,
220                     (arg0 == null ? "" : arg0))));
221          } else if (!StringUtils.isBlank(icon)) {
222             appendIcon(locale, buf);
223          } else {
224             buf.append("???");
225          }
226       }
227    }
228 
229    private void appendEndTag(StringBuilder buf) throws JspException {
230       buf.append("</a>");
231       TagUtils.getInstance().write(pageContext, buf.toString());
232       clearState();
233    }
234 
235    private void appendIcon(Locale locale, StringBuilder buf) {
236 
237       buf.append("<i class=\"fa fa-").append(icon).append(null != iconClass ? " " + iconClass : "")
238               .append("\" aria-hidden=\"true\"")
239               .append(" ></i>");
240 
241       buf.append("<span class=\"sr-only\">")
242               .append(HTMLUtilities.escapeTags(ITrackerResources
243                       .getString(textActionKey, locale,
244                               (arg0 == null ? "" : arg0))))
245               .append("</span>");
246    }
247 
248    private void appendStartTag(Locale locale, StringBuilder buf) {
249       buf.append("<a href=\"");
250       appendUrl(buf, pageContext, forward, action, module, paramName, paramValue, caller, targetAction);
251       buf.append("\"");
252       if (!StringUtils.isBlank(target)) {
253          buf.append(" target=\"" + target + "\"");
254       }
255       if (!StringUtils.isBlank(styleId)) {
256          buf.append(" id=\"" + styleId + "\"");
257       }
258 
259       buf.append(" title=\"").append(
260               HTMLUtilities.escapeTags(
261                       ITrackerResources.getString(info, locale, StringUtils.trimToEmpty(arg0))
262               ))
263               .append("\"");
264 
265       styleClass += " action";
266       if (!Strings.isBlank(icon) && Strings.isBlank(textActionKey)) {
267          styleClass += " icon " + icon;
268       }
269       buf.append(!StringUtils.isBlank(StringUtils.trimToNull(styleClass)) ? " class=\"" + styleClass + "\"" : "")
270               .append(">");
271    }
272 
273    private static void appendUrl(StringBuilder buf, PageContext pageContext, String forward, String action, String module, String paramName, String paramValue, String caller, String targetAction) {
274       boolean hasParams = false;
275       try {
276          buf.append(TagUtils.getInstance().computeURL(pageContext, forward,
277                  null, null, action, module, null, null, false));
278       } catch (MalformedURLException x) {
279          buf.append(HTMLUtilities.escapeTags(forward));
280       }
281       if (paramName != null && paramValue != null) {
282          buf.append("?" + paramName + "=" + paramValue);
283          hasParams = true;
284       }
285       if (caller != null) {
286          buf.append(hasParams ? "&amp;" : "?")
287                  .append("caller=")
288                  .append(HTMLUtilities.escapeTags(caller));
289          hasParams = true;
290       }
291       if (targetAction != null) {
292          buf.append(hasParams ? "&amp;" : "?")
293                  .append("action=")
294                  .append(HTMLUtilities.escapeTags(targetAction));
295       }
296    }
297 
298 
299    public void release() {
300       super.release();
301       clearState();
302    }
303 
304    private void clearState() {
305       action = null;
306       forward = null;
307       paramName = null;
308       paramValue = null;
309       icon = null;
310       styleClass = null;
311       iconClass = null;
312       styleId = null;
313       arg0 = null;
314       textActionKey = null;
315       caller = null;
316       target = null;
317       targetAction = null;
318       module = null;
319    }
320 }