KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > html > LinkTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.html;
17
18 import com.blandware.atleap.webapp.taglib.core.BaseHandlerTag;
19 import com.blandware.atleap.webapp.taglib.core.ParamParent;
20 import com.blandware.atleap.webapp.util.core.RequestUtil;
21 import com.blandware.atleap.webapp.util.core.WebappUtil;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.struts.Globals;
25 import org.apache.struts.taglib.TagUtils;
26 import org.apache.struts.taglib.html.Constants;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.http.HttpSession JavaDoc;
31 import javax.servlet.jsp.JspException JavaDoc;
32 import javax.servlet.jsp.JspTagException JavaDoc;
33 import javax.servlet.jsp.PageContext JavaDoc;
34 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.StringWriter JavaDoc;
37 import java.net.MalformedURLException JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * <p>Generates an HTML hyperlink. Generation of HTML element is divided on
43  * number of methods, which may be overridden in subclass to provide different
44  * or additional functionality or even render another HTML element, which
45  * behaviour is the same as behaviour of HTML Anchor.
46  * </p>
47  * <p>
48  * If <b>name</b> is specified, than anchor with given name will be generated.
49  * Else, link with rewritten URL will be produced. For definition of 'URL
50  * rewriting', see {@link RewriteUrlTag}.
51  * </p>
52  * <p>
53  * Allowed attributes are:
54  * <ul>
55  * <li>
56  * <b>action</b> - name of Action to construct URL for
57  * </li>
58  * <li>
59  * <b>forward</b> - name of Global Action forward to construct URL for
60  * </li>
61  * <li>
62  * <b>href</b> - URL to rewrite
63  * </li>
64  * <li>
65  * <b>name</b> - link name for named links
66  * </li>
67  * <li>
68  * <b>anchor</b> - the anchor to be added to the end of the generated hyperlink
69  * </li>
70  * <li>
71  * <b>target</b> - window target
72  * </li>
73  * <li>
74  * <b>transaction</b> - whether to include transaction token (if any) in the
75  * URL. May be "false" or "true", default is "false"
76  * </li>
77  * <li>
78  * <b>addLocaleSuffix</b> - whether to add locale suffix before extension. May
79  * be "false" or "true", default is "true".
80  * </li>
81  * </ul>
82  * </p>
83  * <p>
84  * This tag may also accept parameters through &lt;atleap:param&gt; tags. They
85  * are added to resulting URL.
86  * </p>
87  * <p>
88  * Only one of <b>action</b>, <b>forward</b>, <b>name</b> or <b>href</b> must be
89  * specified.
90  * </p>
91  * <p>
92  * Here's an example:
93  * <pre>
94  * &lt;atleap:link action="login"&gt;
95  * &lt;atleap:param name="error" value="true"&gt;
96  * &lt;/atleap:link&gt;
97  * </pre>
98  * This code will generate a link to page corresponding to 'login' action,
99  * URL will contain a parameter with name 'error' and value 'true'.
100  * </p>
101  * <p><a HREF="LinkTag.java.htm"><i>View Source</i></a></p>
102  *
103  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
104  * @version $Revision: 1.8 $ $Date: 2005/10/12 13:35:00 $
105  * @see com.blandware.atleap.webapp.taglib.core.ParamTag
106  * @jsp.tag name="link"
107  * body-content="scriptless"
108  */

109 public class LinkTag extends BaseHandlerTag implements ParamParent {
110
111     protected transient final Log log = LogFactory.getLog(getClass());
112
113     /**
114      * Name of Action to construct URL for
115      */

116     protected String JavaDoc action;
117
118     /**
119      * Name of Global Action forward to construct URL for
120      */

121     protected String JavaDoc forward;
122
123     /**
124      * HREF to rewrite
125      */

126     protected String JavaDoc href;
127
128     /**
129      * The link name for named links.
130      */

131     protected String JavaDoc name;
132
133     /**
134      * The anchor to be added to the end of the generated hyperlink.
135      */

136     protected String JavaDoc anchor;
137
138     /**
139      * The window target.
140      */

141     protected String JavaDoc target;
142
143     /**
144      * Include transaction token (if any) in the hyperlink?
145      */

146     protected Boolean JavaDoc transaction = Boolean.FALSE;
147
148     /**
149      * Whether or not to add locale suffix before extension
150      */

151     protected Boolean JavaDoc addLocaleSuffix = Boolean.TRUE;
152
153     /**
154      * Local parameters
155      */

156     protected Map JavaDoc parameters = new HashMap JavaDoc();
157
158     /**
159      * If <code>true</code>, no check is performed that action or forward or
160      * href or name exist. In current tag this is always <code>false</code>,
161      * but this can be modified in derived classes to prevent parameter
162      * validation.
163      */

164     protected boolean suppressValidation = false;
165
166     /**
167      * Returns action
168      *
169      * @return action
170      * @see #action
171      * @jsp.attribute required="false"
172      * rtexprvalue="true"
173      * type="java.lang.String"
174      * description="Name of Action to construct URL for"
175      */

176     public String JavaDoc getAction() {
177         return action;
178     }
179
180     /**
181      * Sets action
182      *
183      * @param action action to set
184      * @see #action
185      */

186     public void setAction(String JavaDoc action) {
187         this.action = action;
188     }
189
190     /**
191      * Returns forward
192      *
193      * @return forward
194      * @see #forward
195      * @jsp.attribute required="false"
196      * rtexprvalue="true"
197      * type="java.lang.String"
198      * description="Name of Global Action forward to construct URL for"
199      */

200     public String JavaDoc getForward() {
201         return forward;
202     }
203
204     /**
205      * Sets forward
206      *
207      * @param forward forward to set
208      * @see #forward
209      */

210     public void setForward(String JavaDoc forward) {
211         this.forward = forward;
212     }
213
214     /**
215      * Returns href
216      *
217      * @return href
218      * @see #href
219      * @jsp.attribute required="false"
220      * rtexprvalue="true"
221      * type="java.lang.String"
222      * description="HREF to rewrite"
223      */

224     public String JavaDoc getHref() {
225         return href;
226     }
227
228     /**
229      * Sets href
230      *
231      * @param href href to set
232      * @see #href
233      */

234     public void setHref(String JavaDoc href) {
235         this.href = href;
236     }
237
238     /**
239      * Returns link name
240      *
241      * @return link name
242      * @see #name
243      * @jsp.attribute required="false"
244      * rtexprvalue="true"
245      * type="java.lang.String"
246      * description="Link name for named links"
247      */

248     public String JavaDoc getName() {
249         return name;
250     }
251
252     /**
253      * Sets link name
254      *
255      * @param name link name to set
256      * @see #name
257      */

258     public void setName(String JavaDoc name) {
259         this.name = name;
260     }
261
262     /**
263      * Returns anchor
264      *
265      * @return anchor
266      * @see #anchor
267      * @jsp.attribute required="false"
268      * rtexprvalue="true"
269      * type="java.lang.String"
270      * description="Anchor to append to the name of generated hyperlink"
271      */

272     public String JavaDoc getAnchor() {
273         return anchor;
274     }
275
276     /**
277      * Sets anchor
278      *
279      * @param anchor anchor to set
280      * @see #anchor
281      */

282     public void setAnchor(String JavaDoc anchor) {
283         this.anchor = anchor;
284     }
285
286     /**
287      * Returns window target
288      *
289      * @return window target
290      * @see #target
291      * @jsp.attribute required="false"
292      * rtexprvalue="true"
293      * type="java.lang.String"
294      * description="The window target"
295      */

296     public String JavaDoc getTarget() {
297         return target;
298     }
299
300     /**
301      * Sets window target
302      *
303      * @param target window target to set
304      * @see #target
305      */

306     public void setTarget(String JavaDoc target) {
307         this.target = target;
308     }
309
310     /**
311      * Returns whether or not to include transaction token in the hyperlink
312      *
313      * @return whether to include transaction token in the hyperlink
314      * @jsp.attribute required="false"
315      * rtexprvalue="true"
316      * type="java.lang.Boolean"
317      * description="Whether or not to include transaction token in the generated hyperlink"
318      */

319     public Boolean JavaDoc getTransaction() {
320         return transaction;
321     }
322
323     /**
324      * Sets whether or not to include transaction token in the hyperlink
325      *
326      * @param transaction whether to include transaction token in the hyperlink
327      */

328     public void setTransaction(Boolean JavaDoc transaction) {
329         this.transaction = transaction;
330     }
331
332     /**
333      * Returns whether or not to add locale suffix before extension
334      *
335      * @return whether to add locale suffix before extension
336      * @jsp.attribute required="false"
337      * rtexprvalue="true"
338      * type="java.lang.Boolean"
339      * description="Whether or not to add locale suffix before extension"
340      */

341     public Boolean JavaDoc getAddLocaleSuffix() {
342         return addLocaleSuffix;
343     }
344
345     /**
346      * Sets whether or not to add locale suffix before extension
347      *
348      * @param addLocaleSuffix whether to add locale suffix before extension
349      */

350     public void setAddLocaleSuffix(Boolean JavaDoc addLocaleSuffix) {
351         this.addLocaleSuffix = addLocaleSuffix;
352     }
353
354     /**
355      * Processes the tag
356      *
357      * @throws JspException
358      * @throws IOException
359      */

360     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
361
362         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
363         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
364         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
365
366         // check for necessary attributes
367
if (!suppressValidation) {
368             if ( name == null && action == null && forward == null && href == null ) {
369                 JspTagException JavaDoc e = new JspTagException JavaDoc("Because 'name' attribute is not specified, either 'action' or 'forward' or 'href' attrbute must be specified");
370                 throw e;
371             }
372         }
373
374         if ( transaction == null ) {
375             transaction = Boolean.FALSE;
376         }
377
378         if ( addLocaleSuffix == null ) {
379             addLocaleSuffix = Boolean.TRUE;
380         }
381
382         // invoke body content
383
JspFragment JavaDoc body = getJspBody();
384         String JavaDoc bodyContent = null;
385         if ( body != null ) {
386             StringWriter JavaDoc sw = new StringWriter JavaDoc();
387             body.invoke(sw);
388             bodyContent = sw.toString();
389         }
390
391         // calculate transaction token if requested
392
if ( transaction.booleanValue() ) {
393             HttpSession JavaDoc session = pageContext.getSession();
394             String JavaDoc token = null;
395             if ( session != null ) {
396                 token = (String JavaDoc) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
397             }
398
399             if ( token != null ) {
400                 parameters.put(Constants.TOKEN_KEY, token);
401             }
402         }
403
404         // calculate href
405
String JavaDoc url = null;
406         try {
407             url = WebappUtil.computeURL(action, forward, href, anchor, parameters, request, response, true, false, addLocaleSuffix.booleanValue());
408         } catch ( MalformedURLException JavaDoc e ) {
409             throw new JspTagException JavaDoc(e);
410         }
411
412         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
413         openElement(result);
414         appendLocation(result, url);
415         appendAttributes(result);
416
417         closeOpeningTag(result);
418
419         // append body content and closing tag
420
if ( bodyContent != null && bodyContent.trim().length() > 0 ) {
421             appendBody(result, bodyContent);
422         }
423         closeElement(result);
424
425         // write result to output
426
TagUtils.getInstance().write(pageContext, result.toString());
427
428     }
429
430     /**
431      * Appends opening tag of HTML element, which renders a hyperlink, to the specified StringBuffer
432      *
433      * @param element String buffer to append opening tag to
434      */

435     protected void openElement(StringBuffer JavaDoc element) {
436         element.append("<a ");
437     }
438
439     /**
440      * Appends location to the specified StringBuffer
441      *
442      * @param element String buffer to append location to
443      * @param location
444      */

445     protected void appendLocation(StringBuffer JavaDoc element, String JavaDoc location) {
446         if ( name == null ) {
447             element.append("href=\"").append(location).append("\" ");
448         } else {
449             element.append("name=\"").append(name).append("\" ");
450         }
451     }
452
453     /**
454      * Appends the rest of attributes, including ones from BaseHandlerTag, to the specified StringBuffer
455      *
456      * @param element String buffer to append attributes to
457      */

458     protected void appendAttributes(StringBuffer JavaDoc element) {
459         if ( target != null ) {
460             element.append("target=\"").append(target).append("\" ");
461         }
462         element.append(prepareAttributes());
463     }
464
465     /**
466      * Appends HTML code that closes opening tag
467      *
468      * @param element String buffer to append to
469      */

470     protected void closeOpeningTag(StringBuffer JavaDoc element) {
471         element.append(">");
472     }
473
474     /**
475      * Appends body content
476      * @param element String buffer to append body content to
477      * @param body Body content to append to buffer
478      */

479     protected void appendBody(StringBuffer JavaDoc element, String JavaDoc body) {
480         element.append(body);
481     }
482
483     /**
484      * Appends closing tag to the specified StringBuffer
485      * @param element String buffer to append closing tag to
486      */

487     protected void closeElement(StringBuffer JavaDoc element) {
488         element.append("</a>");
489     }
490
491     /**
492      * @see com.blandware.atleap.webapp.taglib.core.ParamParent#addParameter(String, Object)
493      */

494     public void addParameter(String JavaDoc name, Object JavaDoc value) {
495         parameters.put(name, RequestUtil.mergeValues(parameters.get(name), value));
496     }
497
498 }
499
Popular Tags