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;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import javax.servlet.jsp.JspException;
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 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 anchor = null;
62
63 public String getAnchor() {
64 return (this.anchor);
65 }
66
67 public void setAnchor(String 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 forward = null;
82
83 public String getForward() {
84 return (this.forward);
85 }
86
87 public void setForward(String forward) {
88 this.forward = forward;
89 }
90
91
92 /**
93 * The hyperlink URI.
94 */
95 protected String href = null;
96
97 public String getHref() {
98 return (this.href);
99 }
100
101 public void setHref(String href) {
102 this.href = href;
103 }
104
105
106 /**
107 * The link name for named links.
108 */
109 protected String linkName = null;
110
111 public String getLinkName() {
112 return (this.linkName);
113 }
114
115 public void setLinkName(String 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 name = null;
131
132 public String getName() {
133 return (this.name);
134 }
135
136 public void setName(String 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 page = null;
146
147 public String getPage() {
148 return (this.page);
149 }
150
151 public void setPage(String 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 action = null;
161
162 public String getAction() {
163 return (this.action);
164 }
165
166 public void setAction(String 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 module = null;
176
177 public String getModule() {
178 return (this.module);
179 }
180
181 public void setModule(String module) {
182 this.module = module;
183 }
184
185
186 /**
187 * The single-parameter request parameter name to generate.
188 */
189 protected String paramId = null;
190
191 public String getParamId() {
192 return (this.paramId);
193 }
194
195 public void setParamId(String paramId) {
196 this.paramId = paramId;
197 }
198
199
200 /**
201 * The single-parameter JSP bean name.
202 */
203 protected String paramName = null;
204
205 public String getParamName() {
206 return (this.paramName);
207 }
208
209 public void setParamName(String paramName) {
210 this.paramName = paramName;
211 }
212
213
214 /**
215 * The single-parameter JSP bean property.
216 */
217 protected String paramProperty = null;
218
219 public String getParamProperty() {
220 return (this.paramProperty);
221 }
222
223 public void setParamProperty(String paramProperty) {
224 this.paramProperty = paramProperty;
225 }
226
227
228 /**
229 * The single-parameter JSP bean scope.
230 */
231 protected String paramScope = null;
232
233 public String getParamScope() {
234 return (this.paramScope);
235 }
236
237 public void setParamScope(String paramScope) {
238 this.paramScope = paramScope;
239 }
240
241
242 /**
243 * The JSP bean property name for query parameters.
244 */
245 protected String property = null;
246
247 public String getProperty() {
248 return (this.property);
249 }
250
251 public void setProperty(String 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 scope = null;
260
261 public String getScope() {
262 return (this.scope);
263 }
264
265 public void setScope(String scope) {
266 this.scope = scope;
267 }
268
269
270 /**
271 * The window target.
272 */
273 protected String target = null;
274
275 public String getTarget() {
276 return (this.target);
277 }
278
279 public void setTarget(String 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 indexId = null;
301
302 public String getIndexId() {
303 return (this.indexId);
304 }
305
306 public void setIndexId(String 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 {
331
332 // Generate the opening anchor element
333 StringBuffer results = new StringBuffer("<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 {
367
368 if (bodyContent != null) {
369 String 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 {
384
385 // Prepare the textual content and ending element of this hyperlink
386 StringBuffer results = new StringBuffer();
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 calculateURL() throws JspException {
438
439 // Identify the parameters we will add to the completed URL
440 Map 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(); //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 url = null;
462 try {
463 url = TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, href,
464 page, action, module, params, anchor, false, useLocalEncoding);
465 } catch (MalformedURLException e) {
466 TagUtils.getInstance().saveException(pageContext, e);
467 throw new JspException
468 (messages.getMessage("rewrite.url", e.toString()));
469 }
470 return (url);
471
472 }
473
474
475 }