001: /*
002: * $Id: LinkTag.java 499093 2007-01-23 18:24:41Z niallp $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.html;
022:
023: import org.apache.struts.taglib.TagUtils;
024: import org.apache.struts.util.MessageResources;
025:
026: import javax.servlet.jsp.JspException;
027:
028: import java.net.MalformedURLException;
029:
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: /**
034: * Generate a URL-encoded hyperlink to the specified URI.
035: *
036: * @version $Rev: 499093 $ $Date: 2005-04-06 02:37:00 -0400 (Wed, 06 Apr 2005)
037: * $
038: */
039: public class LinkTag extends BaseHandlerTag {
040: /**
041: * The message resources for this package.
042: */
043: protected static MessageResources messages = MessageResources
044: .getMessageResources(Constants.Package + ".LocalStrings");
045:
046: // ----------------------------------------------------- Instance Variables
047:
048: /**
049: * The body content of this tag (if any).
050: */
051: protected String text = null;
052:
053: // ------------------------------------------------------------- Properties
054:
055: /**
056: * The anchor to be added to the end of the generated hyperlink.
057: */
058: protected String anchor = null;
059:
060: /**
061: * <p>The logical forward name from which to retrieve the hyperlink
062: * URI.</p> <p>Usage note: If a forward config is used in a hyperlink, and
063: * a module is specified, the path must lead to another action and not
064: * directly to a page. This is in keeping with rule that in a modular
065: * application all links must be to an action rather than a page. </p>
066: */
067: protected String forward = null;
068:
069: /**
070: * The hyperlink URI.
071: */
072: protected String href = null;
073:
074: /**
075: * The link name for named links.
076: */
077: protected String linkName = null;
078:
079: /**
080: * The JSP bean name for query parameters.
081: */
082: protected String name = null;
083:
084: /**
085: * The module-relative page URL (beginning with a slash) to which this
086: * hyperlink will be rendered.
087: */
088: protected String page = null;
089:
090: /**
091: * The module-relative action (beginning with a slash) which will be
092: * called by this link
093: */
094: protected String action = null;
095:
096: /**
097: * The module prefix (beginning with a slash) which will be used to find
098: * the action for this link.
099: */
100: protected String module = null;
101:
102: /**
103: * The single-parameter request parameter name to generate.
104: */
105: protected String paramId = null;
106:
107: /**
108: * The single-parameter JSP bean name.
109: */
110: protected String paramName = null;
111:
112: /**
113: * The single-parameter JSP bean property.
114: */
115: protected String paramProperty = null;
116:
117: /**
118: * The single-parameter JSP bean scope.
119: */
120: protected String paramScope = null;
121:
122: /**
123: * The JSP bean property name for query parameters.
124: */
125: protected String property = null;
126:
127: /**
128: * The scope of the bean specified by the name property, if any.
129: */
130: protected String scope = null;
131:
132: /**
133: * The window target.
134: */
135: protected String target = null;
136:
137: /**
138: * Include transaction token (if any) in the hyperlink?
139: */
140: protected boolean transaction = false;
141:
142: /**
143: * Additional parameters included programatically.
144: */
145: protected Map parameters = new HashMap();
146:
147: /**
148: * Name of parameter to generate to hold index number
149: */
150: protected String indexId = null;
151: protected boolean useLocalEncoding = false;
152:
153: // ----------------------------------------------------- Constructor
154: public LinkTag() {
155: super ();
156: doDisabled = false;
157: }
158:
159: public String getAnchor() {
160: return (this .anchor);
161: }
162:
163: public void setAnchor(String anchor) {
164: this .anchor = anchor;
165: }
166:
167: public String getForward() {
168: return (this .forward);
169: }
170:
171: public void setForward(String forward) {
172: this .forward = forward;
173: }
174:
175: public String getHref() {
176: return (this .href);
177: }
178:
179: public void setHref(String href) {
180: this .href = href;
181: }
182:
183: public String getLinkName() {
184: return (this .linkName);
185: }
186:
187: public void setLinkName(String linkName) {
188: this .linkName = linkName;
189: }
190:
191: public String getName() {
192: return (this .name);
193: }
194:
195: public void setName(String name) {
196: this .name = name;
197: }
198:
199: public String getPage() {
200: return (this .page);
201: }
202:
203: public void setPage(String page) {
204: this .page = page;
205: }
206:
207: public String getAction() {
208: return (this .action);
209: }
210:
211: public void setAction(String action) {
212: this .action = action;
213: }
214:
215: public String getModule() {
216: return (this .module);
217: }
218:
219: public void setModule(String module) {
220: this .module = module;
221: }
222:
223: public String getParamId() {
224: return (this .paramId);
225: }
226:
227: public void setParamId(String paramId) {
228: this .paramId = paramId;
229: }
230:
231: public String getParamName() {
232: return (this .paramName);
233: }
234:
235: public void setParamName(String paramName) {
236: this .paramName = paramName;
237: }
238:
239: public String getParamProperty() {
240: return (this .paramProperty);
241: }
242:
243: public void setParamProperty(String paramProperty) {
244: this .paramProperty = paramProperty;
245: }
246:
247: public String getParamScope() {
248: return (this .paramScope);
249: }
250:
251: public void setParamScope(String paramScope) {
252: this .paramScope = paramScope;
253: }
254:
255: public String getProperty() {
256: return (this .property);
257: }
258:
259: public void setProperty(String property) {
260: this .property = property;
261: }
262:
263: public String getScope() {
264: return (this .scope);
265: }
266:
267: public void setScope(String scope) {
268: this .scope = scope;
269: }
270:
271: public String getTarget() {
272: return (this .target);
273: }
274:
275: public void setTarget(String target) {
276: this .target = target;
277: }
278:
279: public boolean getTransaction() {
280: return (this .transaction);
281: }
282:
283: public void setTransaction(boolean transaction) {
284: this .transaction = transaction;
285: }
286:
287: public String getIndexId() {
288: return (this .indexId);
289: }
290:
291: public void setIndexId(String indexId) {
292: this .indexId = indexId;
293: }
294:
295: public boolean isUseLocalEncoding() {
296: return useLocalEncoding;
297: }
298:
299: public void setUseLocalEncoding(boolean b) {
300: useLocalEncoding = b;
301: }
302:
303: // --------------------------------------------------------- Public Methods
304:
305: /**
306: * Render the beginning of the hyperlink. <p> Support for indexed property
307: * since Struts 1.1
308: *
309: * @throws JspException if a JSP exception has occurred
310: */
311: public int doStartTag() throws JspException {
312: this .text = null;
313: this .parameters.clear();
314:
315: // Evaluate the body of this tag
316: return (EVAL_BODY_TAG);
317: }
318:
319: /**
320: * Save the associated label from the body content.
321: *
322: * @throws JspException if a JSP exception has occurred
323: */
324: public int doAfterBody() throws JspException {
325: if (bodyContent != null) {
326: String value = bodyContent.getString().trim();
327:
328: if (value.length() > 0) {
329: text = value;
330: }
331: }
332:
333: return (SKIP_BODY);
334: }
335:
336: /**
337: * Render the end of the hyperlink.
338: *
339: * @throws JspException if a JSP exception has occurred
340: */
341: public int doEndTag() throws JspException {
342: // Generate the opening anchor element
343: StringBuffer results = new StringBuffer("<a");
344:
345: // Special case for name anchors
346: prepareAttribute(results, "name", getLinkName());
347:
348: // * @since Struts 1.1
349: if ((getLinkName() == null) || (getForward() != null)
350: || (getHref() != null) || (getPage() != null)
351: || (getAction() != null)) {
352: prepareAttribute(results, "href", calculateURL());
353: }
354:
355: prepareAttribute(results, "target", getTarget());
356: prepareAttribute(results, "accesskey", getAccesskey());
357: prepareAttribute(results, "tabindex", getTabindex());
358: results.append(prepareStyles());
359: results.append(prepareEventHandlers());
360: prepareOtherAttributes(results);
361: results.append(">");
362:
363: // Prepare the textual content and ending element of this hyperlink
364: if (text != null) {
365: results.append(text);
366: }
367: results.append("</a>");
368: TagUtils.getInstance().write(pageContext, results.toString());
369:
370: return (EVAL_PAGE);
371: }
372:
373: /**
374: * Release any acquired resources.
375: */
376: public void release() {
377: super .release();
378: anchor = null;
379: forward = null;
380: href = null;
381: linkName = null;
382: name = null;
383: page = null;
384: action = null;
385: module = null;
386: paramId = null;
387: paramName = null;
388: paramProperty = null;
389: paramScope = null;
390: parameters = null;
391: property = null;
392: scope = null;
393: target = null;
394: text = null;
395: transaction = false;
396: indexId = null;
397: useLocalEncoding = false;
398: }
399:
400: // ------------------------------------------------------ Protected Methods
401:
402: /**
403: * Return the complete URL to which this hyperlink will direct the user.
404: * Support for indexed property since Struts 1.1
405: *
406: * @throws JspException if an exception is thrown calculating the value
407: */
408: protected String calculateURL() throws JspException {
409: // Identify the parameters we will add to the completed URL
410: Map params = TagUtils.getInstance().computeParameters(
411: pageContext, paramId, paramName, paramProperty,
412: paramScope, name, property, scope, transaction);
413:
414: // Add parameters collected from the tag's inner body
415: if (!this .parameters.isEmpty()) {
416: if (params == null) {
417: params = new HashMap();
418: }
419: params.putAll(this .parameters);
420: }
421:
422: // if "indexed=true", add "index=x" parameter to query string
423: // * @since Struts 1.1
424: if (indexed) {
425: int indexValue = getIndexValue();
426:
427: //calculate index, and add as a parameter
428: if (params == null) {
429: params = new HashMap(); //create new HashMap if no other params
430: }
431:
432: if (indexId != null) {
433: params.put(indexId, Integer.toString(indexValue));
434: } else {
435: params.put("index", Integer.toString(indexValue));
436: }
437: }
438:
439: String url = null;
440:
441: try {
442: url = TagUtils.getInstance().computeURLWithCharEncoding(
443: pageContext, forward, href, page, action, module,
444: params, anchor, false, useLocalEncoding);
445: } catch (MalformedURLException e) {
446: TagUtils.getInstance().saveException(pageContext, e);
447: throw new JspException(messages.getMessage("rewrite.url", e
448: .toString()));
449: }
450:
451: return (url);
452: }
453:
454: /**
455: * <p>Adds a parameter to this link.</p>
456: *
457: * @param paramName the parameter name
458: * @param paramValue the parameter value
459: * @since Struts 1.3.6
460: */
461: public void addParameter(String paramName, Object paramValue) {
462: this.parameters.put(paramName, paramValue);
463: }
464: }
|