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.net.MalformedURLException;
22  import java.util.Locale;
23  
24  import javax.servlet.http.HttpSession;
25  import javax.servlet.jsp.JspException;
26  import javax.servlet.jsp.tagext.BodyTagSupport;
27  
28  import org.apache.struts.taglib.TagUtils;
29  import org.itracker.core.resources.ITrackerResources;
30  import org.itracker.services.util.HTMLUtilities;
31  import org.itracker.web.util.Constants;
32  
33  
34  public final class FormatLinkTag extends BodyTagSupport {
35      /**
36  	 * 
37  	 */
38  	private static final long serialVersionUID = 1L;
39  
40  	private String text = null;
41  
42      private String action = null;
43      private String forward = null;
44      private String paramName = null;
45      private String paramValue = null;
46      private String titleKey = null;
47      private String arg0 = null;
48      private String caller = null;
49      private String targetAction = null;
50      private String target = null;
51      private String styleClass = null;
52      private String queryString = null;
53  
54      public String getAction() {
55        	return action;
56      }
57  
58      public void setAction(String value) {
59      	  action = value;
60      }
61  
62      public String getForward() {
63        	return forward;
64      }
65  
66      public void setForward(String value) {
67      	  forward = value;
68      }
69  
70      public String getParamName() {
71        	return paramName;
72      }
73  
74      public void setParamName(String value) {
75      	  paramName = value;
76      }
77  
78      public Object getParamValue() {
79        	return paramValue;
80      }
81  
82      public void setParamValue(Object value) {
83      	  paramValue = (value != null ? value.toString() : null);
84      }
85  
86      public String getQueryString() {
87          return queryString;
88      }
89  
90      public void setQueryString(String value) {
91          queryString = value;
92      }
93  
94      public String getTitleKey() {
95        	return titleKey;
96      }
97  
98      public void setTitleKey(String value) {
99      	  titleKey = value;
100     }
101 
102     public Object getArg0() {
103       	return arg0;
104     }
105 
106     public void setArg0(Object value) {
107     	  arg0 = (value != null ? value.toString() : null);
108     }
109 
110     public String getCaller() {
111       	return caller;
112     }
113 
114     public void setCaller(String value) {
115     	  caller = value;
116     }
117 
118     public String getTargetAction() {
119       	return targetAction;
120     }
121 
122     public void setTargetAction(String value) {
123     	  targetAction = value;
124     }
125 
126     public String getTarget() {
127       	return target;
128     }
129 
130     public void setTarget(String value) {
131     	  target = value;
132     }
133 
134     public String getStyleClass() {
135       	return styleClass;
136     }
137 
138     public void setStyleClass(String value) {
139     	  styleClass = value;
140     }
141 
142     public int doStartTag() throws JspException {
143         text = null;
144         return EVAL_BODY_BUFFERED;
145     }
146 
147     public int doAfterBody() throws JspException {
148         if(bodyContent != null) {
149             String value = bodyContent.getString().trim();
150             if(value.length() > 0) {
151                 text = value;
152             }
153         }
154         return SKIP_BODY;
155     }
156 
157     public int doEndTag() throws JspException {
158         boolean hasParams = false;
159         Locale locale = null;
160 
161         HttpSession session = pageContext.getSession();
162         if(session != null) {
163             locale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
164         }
165 
166         StringBuffer buf = new StringBuffer("<a href=\"");
167         try {
168             buf.append(TagUtils.getInstance().computeURL(pageContext, forward, null, null, action, null, null, null, false));            
169         } catch(MalformedURLException murle) {
170             buf.append(HTMLUtilities.escapeTags(forward));
171         }
172         if(queryString != null) {
173             buf.append("?" + HTMLUtilities.escapeTags(queryString));
174             hasParams = true;
175         }
176         if(paramName != null && paramValue != null) {
177             buf.append((hasParams ? "&amp;" : "?") + paramName + "=" + paramValue);
178             hasParams = true;
179         }
180         if(caller != null) {
181             buf.append((hasParams ? "&amp;" : "?") + "caller=" + HTMLUtilities.escapeTags(caller));
182             hasParams = true;
183         }
184         if(targetAction != null) {
185             buf.append((hasParams ? "&amp;" : "?") + "action=" + HTMLUtilities.escapeTags(targetAction));
186             hasParams = true;
187         }
188         buf.append("\"");
189         if(target != null) {
190             buf.append(" target=\"" + HTMLUtilities.escapeTags(target) + "\"");
191         }
192         if(titleKey != null) {
193             buf.append(" title=\"" + HTMLUtilities.escapeTags(ITrackerResources.getString(titleKey, locale, (arg0 == null ? "" : arg0))) + "\"");
194         }
195         if(styleClass != null) {
196             buf.append(" class=\"" + HTMLUtilities.escapeTags(styleClass) + "\"");
197         }
198         buf.append(">");
199         buf.append(HTMLUtilities.escapeTags(text));
200         buf.append("</a>");
201         // ResponseUtils.write(pageContext, buf.toString());
202         TagUtils.getInstance().write(pageContext, buf.toString());
203         clearState();
204         return (EVAL_PAGE);
205     }
206 
207     public void release() {
208         super.release();
209         clearState();
210     }
211 
212     private void clearState() {
213         text = null;
214         action = null;
215         forward = null;
216         paramName = null;
217         paramValue = null;
218         titleKey = null;
219         arg0 = null;
220         caller = null;
221         target = null;
222         targetAction = null;
223         styleClass = null;
224         queryString = null;
225     }
226 }