Coverage details for org.chwf.taglib.jhtml.LinkTag

LineHitsSource
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>&lt;link&gt;</code> tag.
48  *
49  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
50  */
5134public class LinkTag extends LifeCycleBodyTagSupport {
52  
53   /** Link href. */
54   private String href;
55  
56   /** Buffer to generate the encoded link. */
5717  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() {
651    super.cleanup();
661    this.href = null;
671  }
68  
69   /**
70    * The link href.
71    *
72    * @param href The link href.
73    */
74   public void setHref(String href) {
757    this.href = href;
767  }
77  
78   /**
79    * The link href.
80    *
81    * @return The link href.
82    */
83   public String getHref() {
847    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) {
951    addAttribute("class", value);
961  }
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) {
1041    addAttribute("id", value);
1051  }
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) {
1131    addAttribute("lang", value);
1141  }
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) {
1221    addAttribute("style", value);
1231  }
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) {
1311    addAttribute("title", value);
1321  }
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) {
1421    addAttribute("accesskey", value);
1431  }
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) {
1511    addAttribute("name", value);
1521  }
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) {
1601    addAttribute("target", value);
1611  }
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:
1715    JspWriter out = getPreviousOut();
1725    out.print("<a href='");
1735    out.print(deriveLink());
1745    out.print("'");
175  
1765    printAttributes();
177  
178     // Finish element start tag:
1795    out.print(">");
180  
181     // Print (trimmed) tag body content:
1825    BodyContent bodyContent = getBodyContent();
1835    if (bodyContent != null) {
1845      out.print(bodyContent.getString().trim());
185     }
186  
187     // Print <a> end tag:
1885    out.print("</a>");
189  
1905    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() {
1995    link.setLength(0); // Clear buffer
2005    String href = getHref();
2015    if (href.charAt(0) == '/') {
2021      HttpServletRequest request =
203         (HttpServletRequest) pageContext.getRequest();
2041      link.append(request.getContextPath());
205     }
2065    link.append(href);
2075    Iterator parameters = this.getParameterNames();
208  
2095    if (parameters.hasNext()) {
2102      String name = (String) parameters.next();
2112      link.append('?');
2122      link.append(ServletUtils.encode(name));
2132      link.append('=');
2142      link.append(ServletUtils.encode(getParameter(name)));
215     }
216  
2176    while (parameters.hasNext()) {
2181      String name = (String) parameters.next();
2191      link.append('&');
2201      link.append(ServletUtils.encode(name));
2211      link.append('=');
2221      link.append(ServletUtils.encode(getParameter(name)));
223     }
224  
2255    HttpServletResponse response =
226       (HttpServletResponse) pageContext.getResponse();
2275    return response.encodeURL(link.toString());
228   }
229 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.