KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > LinkTag


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

18
19 package org.apache.struts.taglib.html;
20
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26
27 import org.apache.struts.taglib.TagUtils;
28 import org.apache.struts.taglib.logic.IterateTag;
29 import org.apache.struts.util.MessageResources;
30
31 /**
32  * Generate a URL-encoded hyperlink to the specified URI.
33  *
34  * @version $Rev: 164530 $ $Date: 2005-04-25 04:11:07 +0100 (Mon, 25 Apr 2005) $
35  */

36 public class LinkTag extends BaseHandlerTag {
37
38
39     // ----------------------------------------------------- Instance Variables
40

41
42     /**
43      * The body content of this tag (if any).
44      */

45     protected String JavaDoc text = null;
46
47     // ----------------------------------------------------- Constructor
48

49     public LinkTag() {
50         super();
51         doDisabled = false;
52     }
53
54
55     // ------------------------------------------------------------- Properties
56

57
58     /**
59      * The anchor to be added to the end of the generated hyperlink.
60      */

61     protected String JavaDoc anchor = null;
62
63     public String JavaDoc getAnchor() {
64         return (this.anchor);
65     }
66
67     public void setAnchor(String JavaDoc anchor) {
68         this.anchor = anchor;
69     }
70
71
72     /**
73      * <p>The logical forward name from which to retrieve the hyperlink URI.</p>
74      * <p>Usage note: If a forward config is used in a hyperlink,
75      * and a module is specified, the path must lead to another
76      * action and not directly to a page. This is in keeping with
77      * rule that in a modular application all links must be to
78      * an action rather than a page.
79      * </p>
80      */

81     protected String JavaDoc forward = null;
82
83     public String JavaDoc getForward() {
84         return (this.forward);
85     }
86
87     public void setForward(String JavaDoc forward) {
88         this.forward = forward;
89     }
90
91
92     /**
93      * The hyperlink URI.
94      */

95     protected String JavaDoc href = null;
96
97     public String JavaDoc getHref() {
98         return (this.href);
99     }
100
101     public void setHref(String JavaDoc href) {
102         this.href = href;
103     }
104
105
106     /**
107      * The link name for named links.
108      */

109     protected String JavaDoc linkName = null;
110
111     public String JavaDoc getLinkName() {
112         return (this.linkName);
113     }
114
115     public void setLinkName(String JavaDoc linkName) {
116         this.linkName = linkName;
117     }
118
119
120     /**
121      * The message resources for this package.
122      */

123     protected static MessageResources messages =
124      MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
125
126
127     /**
128      * The JSP bean name for query parameters.
129      */

130     protected String JavaDoc name = null;
131
132     public String JavaDoc getName() {
133         return (this.name);
134     }
135
136     public void setName(String JavaDoc name) {
137         this.name = name;
138     }
139
140
141     /**
142      * The module-relative page URL (beginning with a slash) to which
143      * this hyperlink will be rendered.
144      */

145     protected String JavaDoc page = null;
146
147     public String JavaDoc getPage() {
148         return (this.page);
149     }
150
151     public void setPage(String JavaDoc page) {
152         this.page = page;
153     }
154
155
156     /**
157      * The module-relative action (beginning with a slash) which will be
158      * called by this link
159      */

160     protected String JavaDoc action = null;
161
162     public String JavaDoc getAction() {
163         return (this.action);
164     }
165
166     public void setAction(String JavaDoc action) {
167         this.action = action;
168     }
169
170
171     /**
172      * The module prefix (beginning with a slash) which will be
173      * used to find the action for this link.
174      */

175     protected String JavaDoc module = null;
176
177     public String JavaDoc getModule() {
178         return (this.module);
179     }
180
181     public void setModule(String JavaDoc module) {
182         this.module = module;
183     }
184
185
186     /**
187       * The single-parameter request parameter name to generate.
188       */

189      protected String JavaDoc paramId = null;
190
191     public String JavaDoc getParamId() {
192         return (this.paramId);
193     }
194
195     public void setParamId(String JavaDoc paramId) {
196         this.paramId = paramId;
197     }
198
199
200     /**
201      * The single-parameter JSP bean name.
202      */

203     protected String JavaDoc paramName = null;
204
205     public String JavaDoc getParamName() {
206         return (this.paramName);
207     }
208
209     public void setParamName(String JavaDoc paramName) {
210         this.paramName = paramName;
211     }
212
213
214     /**
215      * The single-parameter JSP bean property.
216      */

217     protected String JavaDoc paramProperty = null;
218
219     public String JavaDoc getParamProperty() {
220         return (this.paramProperty);
221     }
222
223     public void setParamProperty(String JavaDoc paramProperty) {
224         this.paramProperty = paramProperty;
225     }
226
227
228     /**
229      * The single-parameter JSP bean scope.
230      */

231     protected String JavaDoc paramScope = null;
232
233     public String JavaDoc getParamScope() {
234         return (this.paramScope);
235     }
236
237     public void setParamScope(String JavaDoc paramScope) {
238         this.paramScope = paramScope;
239     }
240
241
242     /**
243      * The JSP bean property name for query parameters.
244      */

245     protected String JavaDoc property = null;
246
247     public String JavaDoc getProperty() {
248         return (this.property);
249     }
250
251     public void setProperty(String JavaDoc property) {
252         this.property = property;
253     }
254
255
256     /**
257      * The scope of the bean specified by the name property, if any.
258      */

259     protected String JavaDoc scope = null;
260
261     public String JavaDoc getScope() {
262         return (this.scope);
263     }
264
265     public void setScope(String JavaDoc scope) {
266         this.scope = scope;
267     }
268
269
270     /**
271      * The window target.
272      */

273     protected String JavaDoc target = null;
274
275     public String JavaDoc getTarget() {
276         return (this.target);
277     }
278
279     public void setTarget(String JavaDoc target) {
280         this.target = target;
281     }
282
283
284     /**
285      * Include transaction token (if any) in the hyperlink?
286      */

287     protected boolean transaction = false;
288
289     public boolean getTransaction() {
290         return (this.transaction);
291     }
292
293     public void setTransaction(boolean transaction) {
294         this.transaction = transaction;
295     }
296
297     /**
298      * Name of parameter to generate to hold index number
299      */

300     protected String JavaDoc indexId = null;
301
302     public String JavaDoc getIndexId() {
303        return (this.indexId);
304     }
305
306     public void setIndexId(String JavaDoc indexId) {
307         this.indexId = indexId;
308     }
309
310     protected boolean useLocalEncoding = false;
311     
312     public boolean isUseLocalEncoding() {
313        return useLocalEncoding;
314     }
315
316     public void setUseLocalEncoding(boolean b) {
317        useLocalEncoding = b;
318     }
319
320     // --------------------------------------------------------- Public Methods
321

322
323     /**
324      * Render the beginning of the hyperlink.
325      * <p>
326      * Support for indexed property since Struts 1.1
327      *
328      * @exception JspException if a JSP exception has occurred
329      */

330     public int doStartTag() throws JspException JavaDoc {
331
332         // Generate the opening anchor element
333
StringBuffer JavaDoc results = new StringBuffer JavaDoc("<a");
334
335         // Special case for name anchors
336
prepareAttribute(results, "name", getLinkName());
337
338         // * @since Struts 1.1
339
if (getLinkName() == null || getForward() != null || getHref() != null ||
340             getPage() != null || getAction() != null) {
341             prepareAttribute(results, "href", calculateURL());
342         }
343         prepareAttribute(results, "target", getTarget());
344         prepareAttribute(results, "accesskey", getAccesskey());
345         prepareAttribute(results, "tabindex", getTabindex());
346         results.append(prepareStyles());
347         results.append(prepareEventHandlers());
348         prepareOtherAttributes(results);
349         results.append(">");
350
351         TagUtils.getInstance().write(pageContext, results.toString());
352
353         // Evaluate the body of this tag
354
this.text = null;
355         return (EVAL_BODY_TAG);
356
357     }
358
359
360
361     /**
362      * Save the associated label from the body content.
363      *
364      * @exception JspException if a JSP exception has occurred
365      */

366     public int doAfterBody() throws JspException JavaDoc {
367
368         if (bodyContent != null) {
369             String JavaDoc value = bodyContent.getString().trim();
370             if (value.length() > 0)
371                 text = value;
372         }
373         return (SKIP_BODY);
374
375     }
376
377
378     /**
379      * Render the end of the hyperlink.
380      *
381      * @exception JspException if a JSP exception has occurred
382      */

383     public int doEndTag() throws JspException JavaDoc {
384
385         // Prepare the textual content and ending element of this hyperlink
386
StringBuffer JavaDoc results = new StringBuffer JavaDoc();
387         if (text != null) {
388             results.append(text);
389         }
390         results.append("</a>");
391
392         TagUtils.getInstance().write(pageContext, results.toString());
393
394         return (EVAL_PAGE);
395
396     }
397
398
399     /**
400      * Release any acquired resources.
401      */

402     public void release() {
403
404         super.release();
405         anchor = null;
406         forward = null;
407         href = null;
408         linkName = null;
409         name = null;
410         page = null;
411         action = null;
412         module = null;
413         paramId = null;
414         paramName = null;
415         paramProperty = null;
416         paramScope = null;
417         property = null;
418         scope = null;
419         target = null;
420         text = null;
421         transaction = false;
422         indexId = null;
423         useLocalEncoding = false;
424
425     }
426
427
428     // ------------------------------------------------------ Protected Methods
429

430
431     /**
432      * Return the complete URL to which this hyperlink will direct the user.
433      * Support for indexed property since Struts 1.1
434      *
435      * @exception JspException if an exception is thrown calculating the value
436      */

437     protected String JavaDoc calculateURL() throws JspException JavaDoc {
438
439         // Identify the parameters we will add to the completed URL
440
Map JavaDoc params = TagUtils.getInstance().computeParameters
441             (pageContext, paramId, paramName, paramProperty, paramScope,
442              name, property, scope, transaction);
443
444         // if "indexed=true", add "index=x" parameter to query string
445
// * @since Struts 1.1
446
if( indexed ) {
447
448            int indexValue = getIndexValue();
449
450            //calculate index, and add as a parameter
451
if (params == null) {
452                params = new HashMap JavaDoc(); //create new HashMap if no other params
453
}
454            if (indexId != null) {
455               params.put(indexId, Integer.toString(indexValue));
456            } else {
457               params.put("index", Integer.toString(indexValue));
458            }
459         }
460
461         String JavaDoc url = null;
462         try {
463             url = TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, href,
464                                           page, action, module, params, anchor, false, useLocalEncoding);
465         } catch (MalformedURLException JavaDoc e) {
466             TagUtils.getInstance().saveException(pageContext, e);
467             throw new JspException JavaDoc
468                 (messages.getMessage("rewrite.url", e.toString()));
469         }
470         return (url);
471
472     }
473
474
475 }
476
Popular Tags