Line | Hits | Source |
---|---|---|
1 | /* | |
2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
4 | ||
5 | All rights reserved. | |
6 | ||
7 | Redistribution and use in source and binary forms, with or without | |
8 | modification, are permitted provided that the following conditions are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright notice, this | |
11 | list of conditions and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | this list of conditions and the following disclaimer in the documentation | |
15 | and/or other materials provided with the distribution. | |
16 | ||
17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
18 | may be used to endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package org.chwf.taglib.jhtml; | |
34 | ||
35 | import java.io.IOException; | |
36 | import java.util.Iterator; | |
37 | ||
38 | import javax.servlet.http.HttpServletRequest; | |
39 | import javax.servlet.http.HttpServletResponse; | |
40 | import javax.servlet.jsp.JspWriter; | |
41 | import javax.servlet.jsp.tagext.BodyContent; | |
42 | ||
43 | import org.chwf.servlet.ServletUtils; | |
44 | import org.chwf.taglib.base.LifeCycleBodyTagSupport; | |
45 | ||
46 | /** | |
47 | * Tag handler for the <code><link></code> tag. | |
48 | * | |
49 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
50 | */ | |
51 | 34 | public class LinkTag extends LifeCycleBodyTagSupport { |
52 | ||
53 | /** Link href. */ | |
54 | private String href; | |
55 | ||
56 | /** Buffer to generate the encoded link. */ | |
57 | 17 | private StringBuffer link = new StringBuffer(); |
58 | ||
59 | /** | |
60 | * Clean up data for tag handler reuse and pooling. Should be overridden in | |
61 | * tag handler. Subclasses should always invoke their superclass's cleanup | |
62 | * method: <code>super.cleanup()</code> | |
63 | */ | |
64 | public void cleanup() { | |
65 | 1 | super.cleanup(); |
66 | 1 | this.href = null; |
67 | 1 | } |
68 | ||
69 | /** | |
70 | * The link href. | |
71 | * | |
72 | * @param href The link href. | |
73 | */ | |
74 | public void setHref(String href) { | |
75 | 7 | this.href = href; |
76 | 7 | } |
77 | ||
78 | /** | |
79 | * The link href. | |
80 | * | |
81 | * @return The link href. | |
82 | */ | |
83 | public String getHref() { | |
84 | 7 | return this.href; |
85 | } | |
86 | ||
87 | // Generic html attributes | |
88 | ||
89 | /** | |
90 | * Pass-through attribute. Same meaning as "class" in HTML. | |
91 | * | |
92 | * @param value The attribute value. | |
93 | */ | |
94 | public void setStyleClass(String value) { | |
95 | 1 | addAttribute("class", value); |
96 | 1 | } |
97 | ||
98 | /** | |
99 | * Pass-through attribute. Same meaning as in HTML. | |
100 | * | |
101 | * @param value The attribute value. | |
102 | */ | |
103 | public void setId(String value) { | |
104 | 1 | addAttribute("id", value); |
105 | 1 | } |
106 | ||
107 | /** | |
108 | * Pass-through attribute. Same meaning as in HTML. | |
109 | * | |
110 | * @param value The attribute value. | |
111 | */ | |
112 | public void setLang(String value) { | |
113 | 1 | addAttribute("lang", value); |
114 | 1 | } |
115 | ||
116 | /** | |
117 | * Pass-through attribute. Same meaning as in HTML. | |
118 | * | |
119 | * @param value The attribute value. | |
120 | */ | |
121 | public void setStyle(String value) { | |
122 | 1 | addAttribute("style", value); |
123 | 1 | } |
124 | ||
125 | /** | |
126 | * Pass-through attribute. Same meaning as in HTML. | |
127 | * | |
128 | * @param value The attribute value. | |
129 | */ | |
130 | public void setTitle(String value) { | |
131 | 1 | addAttribute("title", value); |
132 | 1 | } |
133 | ||
134 | // <a>-specific html attributes | |
135 | ||
136 | /** | |
137 | * Pass-through attribute. Same meaning as in HTML. | |
138 | * | |
139 | * @param value The attribute value. | |
140 | */ | |
141 | public void setAccesskey(String value) { | |
142 | 1 | addAttribute("accesskey", value); |
143 | 1 | } |
144 | ||
145 | /** | |
146 | * Pass-through attribute. Same meaning as in HTML. | |
147 | * | |
148 | * @param value The attribute value. | |
149 | */ | |
150 | public void setName(String value) { | |
151 | 1 | addAttribute("name", value); |
152 | 1 | } |
153 | ||
154 | /** | |
155 | * Pass-through attribute. Same meaning as in HTML. | |
156 | * | |
157 | * @param value The attribute value. | |
158 | */ | |
159 | public void setTarget(String value) { | |
160 | 1 | addAttribute("target", value); |
161 | 1 | } |
162 | ||
163 | /** | |
164 | * Wraps the (trimmed) tag body with the defined element. | |
165 | * | |
166 | * @return EVAL_PAGE | |
167 | * @throws IOException For write errors. | |
168 | */ | |
169 | public int doEnd() throws IOException { | |
170 | // Begin <a> start tag: | |
171 | 5 | JspWriter out = getPreviousOut(); |
172 | 5 | out.print("<a href='"); |
173 | 5 | out.print(deriveLink()); |
174 | 5 | out.print("'"); |
175 | ||
176 | 5 | printAttributes(); |
177 | ||
178 | // Finish element start tag: | |
179 | 5 | out.print(">"); |
180 | ||
181 | // Print (trimmed) tag body content: | |
182 | 5 | BodyContent bodyContent = getBodyContent(); |
183 | 5 | if (bodyContent != null) { |
184 | 5 | out.print(bodyContent.getString().trim()); |
185 | } | |
186 | ||
187 | // Print <a> end tag: | |
188 | 5 | out.print("</a>"); |
189 | ||
190 | 5 | return EVAL_PAGE; |
191 | } | |
192 | ||
193 | /** | |
194 | * Derive and encode the link from the href and parameters. | |
195 | * | |
196 | * @return The link with parameters. | |
197 | */ | |
198 | private String deriveLink() { | |
199 | 5 | link.setLength(0); // Clear buffer |
200 | 5 | String href = getHref(); |
201 | 5 | if (href.charAt(0) == '/') { |
202 | 1 | HttpServletRequest request = |
203 | (HttpServletRequest) pageContext.getRequest(); | |
204 | 1 | link.append(request.getContextPath()); |
205 | } | |
206 | 5 | link.append(href); |
207 | 5 | Iterator parameters = this.getParameterNames(); |
208 | ||
209 | 5 | if (parameters.hasNext()) { |
210 | 2 | String name = (String) parameters.next(); |
211 | 2 | link.append('?'); |
212 | 2 | link.append(ServletUtils.encode(name)); |
213 | 2 | link.append('='); |
214 | 2 | link.append(ServletUtils.encode(getParameter(name))); |
215 | } | |
216 | ||
217 | 6 | while (parameters.hasNext()) { |
218 | 1 | String name = (String) parameters.next(); |
219 | 1 | link.append('&'); |
220 | 1 | link.append(ServletUtils.encode(name)); |
221 | 1 | link.append('='); |
222 | 1 | link.append(ServletUtils.encode(getParameter(name))); |
223 | } | |
224 | ||
225 | 5 | HttpServletResponse response = |
226 | (HttpServletResponse) pageContext.getResponse(); | |
227 | 5 | return response.encodeURL(link.toString()); |
228 | } | |
229 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |