1 /**
2 * Licensed under the Artistic License; you may not use this file
3 * except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://displaytag.sourceforge.net/license.html
7 *
8 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 */
12 package org.displaytag.util;
13
14 /**
15 * Anchor object used to output an html link (an <a> tag).
16 * @author Fabrizio Giustina
17 * @version $Revision: 1081 $ ($Author: fgiust $)
18 */
19 public class Anchor
20 {
21
22 /**
23 * Href object to be written in the "href" html attribute.
24 */
25 private Href href;
26
27 /**
28 * link body text.
29 */
30 private String linkText;
31
32 /**
33 * HashMap containing all the html attributes.
34 */
35 private HtmlAttributeMap attributeMap = new HtmlAttributeMap();
36
37 /**
38 * Creates a new Anchor whit the supplied Href and body text.
39 * @param linkHref baseHref
40 * @param linkBody String link body
41 */
42 public Anchor(Href linkHref, String linkBody)
43 {
44 this.href = linkHref;
45 this.linkText = linkBody;
46 }
47
48 /**
49 * setter the anchor Href.
50 * @param linkHref Href
51 */
52 public void setHref(Href linkHref)
53 {
54 this.href = linkHref;
55 }
56
57 /**
58 * setter for the link body text.
59 * @param linkBody String
60 */
61 public void setText(String linkBody)
62 {
63 this.linkText = linkBody;
64 }
65
66 /**
67 * add a "class" attribute to the html link.
68 * @param cssClass String
69 */
70 public void setClass(String cssClass)
71 {
72 this.attributeMap.put(TagConstants.ATTRIBUTE_CLASS, cssClass);
73 }
74
75 /**
76 * add a "style" attribute to the html link.
77 * @param style String
78 */
79 public void setStyle(String style)
80 {
81 this.attributeMap.put(TagConstants.ATTRIBUTE_STYLE, style);
82 }
83
84 /**
85 * add a "title" attribute to the html link.
86 * @param title String
87 */
88 public void setTitle(String title)
89 {
90 this.attributeMap.put(TagConstants.ATTRIBUTE_TITLE, title);
91 }
92
93 /**
94 * returns the href attribute, surrounded by quotes and prefixed with " href=".
95 * @return String <code> href ="<em>href value</em>"</code> or an emty String if Href is null
96 */
97 private String getHrefString()
98 {
99 if (this.href == null)
100 {
101 return TagConstants.EMPTY_STRING;
102 }
103 return " href=\"" + this.href.toString() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
104 }
105
106 /**
107 * Returns the <a> tag, with rendered href and any html attribute.
108 * @return String
109 */
110 public String getOpenTag()
111 {
112
113 // shortcut for links with no attributes
114 if (this.attributeMap.size() == 0)
115 {
116 return TagConstants.TAG_OPEN + TagConstants.TAGNAME_ANCHOR + getHrefString() + TagConstants.TAG_CLOSE;
117 }
118
119 // append all attributes
120 StringBuffer buffer = new StringBuffer();
121
122 buffer.append(TagConstants.TAG_OPEN).append(TagConstants.TAGNAME_ANCHOR).append(getHrefString());
123
124 buffer.append(this.attributeMap);
125
126 buffer.append(TagConstants.TAG_CLOSE);
127
128 return buffer.toString();
129 }
130
131 /**
132 * returns the </a> tag.
133 * @return String
134 */
135 public String getCloseTag()
136 {
137 return TagConstants.TAG_OPENCLOSING + TagConstants.TAGNAME_ANCHOR + TagConstants.TAG_CLOSE;
138 }
139
140 /**
141 * returns the full <a href="">body</a>.
142 * @return String html link
143 */
144 public String toString()
145 {
146 return getOpenTag() + this.linkText + getCloseTag();
147 }
148
149 }