KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > tags > LinkTag


1 /*
2  * $Header: /cvs/roller/roller/src/org/roller/presentation/tags/LinkTag.java,v 1.3 2004/09/23 02:15:36 snoopdave Exp $
3  * $Revision: 1.3 $
4  * $Date: 2004/09/23 02:15:36 $
5  *
6  * ====================================================================
7  */

8 package org.roller.presentation.tags;
9 import java.lang.reflect.InvocationTargetException JavaDoc;
10 import java.net.MalformedURLException JavaDoc;
11 import java.net.URLEncoder JavaDoc;
12 import java.util.Map JavaDoc;
13 import javax.servlet.jsp.JspException JavaDoc;
14 import org.apache.commons.beanutils.PropertyUtils;
15 import org.apache.log4j.Category;
16 import org.apache.struts.taglib.TagUtils;
17 import org.apache.struts.util.RequestUtils;
18 import org.apache.struts.util.ResponseUtils;
19
20 /**
21  * Generates an HTML link. This class adds the parameter feature to the Struts
22  * html link tag. <br/>It should be use as follow:
23  *
24  * <pre>
25  *
26  * &lt;hm:link HREF=&quot;http://java.sun.com&quot;&gt;
27  * &lt;hm:linkparam name=&quot;action&quot; value=&quot;submit&quot; /&gt;
28  * &lt;hm:linkparam name=&quot;ref&quot; value=&quot;144532&quot; /&gt;
29  * &lt;/hm:linl&gt;
30  *
31  * </pre>
32  *
33  * This will produce the equivalent of
34  *
35  * <pre>
36  * &lt;href=&quot;http://java.sun.com?_action=submit&amp;ref=144532&quot;&gt;
37  * </pre>
38  *
39  * Title: BSquare Description: Bsquare Projects Copyright: Copyright (c) 2001
40  * Company: HubMethods
41  *
42  * @author $Author: snoopdave $
43  * @version $Revision: 1.3 $
44  */

45 public class LinkTag extends org.apache.struts.taglib.html.LinkTag
46 {
47     // ------------------------------------------------------ Logging
48
static Category cat = Category.getInstance(LinkTag.class);
49     // ------------------------------------------------------ Instance Vartables
50
/**
51      * The full HREF URL
52      */

53     private StringBuffer JavaDoc hrefURL = new StringBuffer JavaDoc();
54
55     // ------------------------------------------------------------- Properties
56
//--------------------------------------------------------- Public Methods
57
/**
58      * Intialize the hyperlink.
59      *
60      * @exception JspException
61      * if a JSP exception has occurred
62      */

63     public int doStartTag() throws JspException JavaDoc
64     {
65         // Special case for name anchors
66
if (linkName != null)
67         {
68             StringBuffer JavaDoc results = new StringBuffer JavaDoc("<a name=\"");
69             results.append(linkName);
70             results.append("\">");
71             return (EVAL_BODY_TAG);
72         }
73         // Generate the hyperlink URL
74
Map JavaDoc params = RequestUtils.computeParameters(pageContext, paramId,
75                         paramName, paramProperty, paramScope, name, property,
76                         scope, transaction);
77         String JavaDoc url = null;
78         try
79         {
80             url = RequestUtils.computeURL(pageContext, forward, href, page,
81                             params, anchor, false);
82         }
83         catch (MalformedURLException JavaDoc e)
84         {
85             RequestUtils.saveException(pageContext, e);
86             throw new JspException JavaDoc(messages.getMessage("rewrite.url", e
87                             .toString()));
88         }
89         // Generate the opening anchor element
90
hrefURL = new StringBuffer JavaDoc("<a HREF=\"");
91         hrefURL.append(url);
92         if (cat.isDebugEnabled())
93             cat.debug("hrefURL = '" + hrefURL.toString());
94         // Evaluate the body of this tag
95
this.text = null;
96         return (EVAL_BODY_TAG);
97     }
98
99     /**
100      * Add a new parameter to the request
101      *
102      * @param name
103      * the name of the request parameter
104      * @param value
105      * the value of the request parameter
106      */

107     public void addRequestParameter(String JavaDoc name, String JavaDoc value)
108     {
109         if (cat.isDebugEnabled())
110             cat.debug("Adding '" + name + "' with value '" + value + "'");
111         boolean question = (hrefURL.toString().indexOf('?') >= 0);
112         if (question)
113         { // There are request parameter already
114
hrefURL.append('&');
115         }
116         else
117             hrefURL.append('?');
118         hrefURL.append(name);
119         hrefURL.append('=');
120         hrefURL.append(URLEncoder.encode(value));
121         if (cat.isDebugEnabled())
122             cat.debug("hrefURL = '" + hrefURL.toString() + "'");
123     }
124
125     /**
126      * Render the href reference
127      *
128      * @exception JspException
129      * if a JSP exception has occurred
130      */

131     public int doEndTag() throws JspException JavaDoc
132     {
133         hrefURL.append("\"");
134         if (target != null)
135         {
136             hrefURL.append(" target=\"");
137             hrefURL.append(target);
138             hrefURL.append("\"");
139         }
140         hrefURL.append(prepareStyles());
141         hrefURL.append(prepareEventHandlers());
142         hrefURL.append(">");
143         if (text != null)
144             hrefURL.append(text);
145         hrefURL.append("</a>");
146         if (cat.isDebugEnabled())
147             cat.debug("Total request is = '" + hrefURL.toString() + "'");
148         // Print this element to our output writer
149
ResponseUtils.write(pageContext, hrefURL.toString());
150         return (EVAL_PAGE);
151     }
152
153     /**
154      * Release any acquired resources.
155      */

156     public void release()
157     {
158         super.release();
159         forward = null;
160         href = null;
161         name = null;
162         property = null;
163         target = null;
164     }
165
166     // ----------------------------------------------------- Protected Methods
167
/**
168      * Return the specified hyperlink, modified as necessary with optional
169      * request parameters.
170      *
171      * @exception JspException
172      * if an error occurs preparing the hyperlink
173      */

174     protected String JavaDoc hyperlink() throws JspException JavaDoc
175     {
176         String JavaDoc href = this.href;
177         // If "forward" was specified, compute the "href" to forward to
178
// if (forward != null)
179
// {
180
// ActionForwards forwards = (ActionForwards) pageContext
181
// .getAttribute(Action.FORWARDS_KEY,
182
// PageContext.APPLICATION_SCOPE);
183
// ActionMapping mapping = (ActionMapping)pageContext.getAttribute(ActionConfig.)
184
// if (forwards == null)
185
// throw new JspException(messages.getMessage("linkTag.forwards"));
186
// ActionForward forward = forwards.findForward(this.forward);
187
// if (forward == null)
188
// throw new JspException(messages.getMessage("linkTag.forward"));
189
// HttpServletRequest request = (HttpServletRequest) pageContext
190
// .getRequest();
191
// href = request.getContextPath() + forward.getPath();
192
// }
193
// Just return the "href" attribute if there is no bean to look up
194
if ((property != null) && (name == null))
195             throw new JspException JavaDoc(messages.getMessage("getter.name"));
196         if (name == null)
197             return (href);
198         // Look up the map we will be using
199
Object JavaDoc bean = pageContext.findAttribute(name);
200         if (bean == null)
201             throw new JspException JavaDoc(messages.getMessage("getter.bean", name));
202         Map JavaDoc map = null;
203         if (property == null)
204         {
205             try
206             {
207                 map = (Map JavaDoc) bean;
208             }
209             catch (ClassCastException JavaDoc e)
210             {
211                 throw new JspException JavaDoc(messages.getMessage("linkTag.type"));
212             }
213         }
214         else
215         {
216             try
217             {
218                 map = (Map JavaDoc) PropertyUtils.getProperty(bean, property);
219                 if (map == null)
220                     throw new JspException JavaDoc(messages.getMessage(
221                                     "getter.property", property));
222             }
223             catch (IllegalAccessException JavaDoc e)
224             {
225                 throw new JspException JavaDoc(messages.getMessage("getter.access",
226                                 property, name));
227             }
228             catch (InvocationTargetException JavaDoc e)
229             {
230                 Throwable JavaDoc t = e.getTargetException();
231                 throw new JspException JavaDoc(messages.getMessage("getter.result",
232                                 property, t.toString()));
233             }
234             catch (ClassCastException JavaDoc e)
235             {
236                 throw new JspException JavaDoc(messages.getMessage("linkTag.type"));
237             }
238             catch (NoSuchMethodException JavaDoc e)
239             {
240                 throw new JspException JavaDoc(messages.getMessage("getter.method",
241                                 property, name));
242             }
243         }
244             // Append the required query parameters
245
// StringBuffer sb = new StringBuffer(href);
246
// boolean question = (href.indexOf("?") >= 0);
247
// Iterator keys = map.keySet().iterator();
248
// while (keys.hasNext())
249
// {
250
// String key = (String) keys.next();
251
// Object value = map.get(key);
252
// if (value instanceof String[])
253
// {
254
// String values[] = (String[]) value;
255
// for (int i = 0; i < values.length; i++)
256
// {
257
// if (question)
258
// sb.append('&');
259
// else
260
// {
261
// sb.append('?');
262
// question = true;
263
// }
264
// sb.append(key);
265
// sb.append('=');
266
// sb.append(URLEncoder.encode(values[i]));
267
// }
268
// }
269
// else
270
// {
271
// if (question)
272
// sb.append('&');
273
// else
274
// {
275
// sb.append('?');
276
// question = true;
277
// }
278
// sb.append(key);
279
// sb.append('=');
280
// sb.append(URLEncoder.encode(value.toString()));
281
// }
282
// }
283
// // Return the final result
284
// return (sb.toString());
285
try
286         {
287             return TagUtils.getInstance().computeURL(
288                             pageContext,
289                             forward,
290                             href,
291                             null,
292                             null,
293                             null,
294                             map,
295                             null,
296                             false
297                             );
298         }
299         catch (MalformedURLException JavaDoc e)
300         {
301             throw new JspException JavaDoc(e);
302         }
303     }
304 }
305
Popular Tags