KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > html > ATag


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.html;
8
9
10 import java.io.IOException JavaDoc;
11 import java.io.Writer JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import javax.servlet.jsp.JspException JavaDoc;
17
18 import org.apache.log4j.Logger;
19
20 import com.inversoft.verge.mvc.view.HtmlConstants;
21 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
22 import com.inversoft.verge.mvc.view.jsp.JspTools;
23
24
25 /**
26  * This class is an anchor tag for the framework that allows
27  * pages to insert anchors into the page that use the context
28  * page of the application. Mostly a convience tag. The syntax
29  * is like:<br>
30  * &lt;html:a HREF="../../../../../../../../somePage.jsp"&gt;<br>
31  * This will print to the page:<br>
32  * &lt;a HREF="../../../../../../../../ContextPath/somePage.jsp"&gt;<br>
33  *
34  * @author Brian Pontarelli
35  */

36 public class ATag extends ContextBasedTag {
37
38     private static final Logger logger = Logger.getLogger(ATag.class);
39
40     private String JavaDoc href;
41     protected String JavaDoc localHref;
42     private String JavaDoc rel;
43     protected String JavaDoc localRel;
44     private String JavaDoc rev;
45     protected String JavaDoc localRev;
46     private String JavaDoc shape;
47     protected String JavaDoc localShape;
48     private String JavaDoc coords;
49     protected String JavaDoc localCoords;
50     private String JavaDoc onfocus;
51     protected String JavaDoc localOnfocus;
52     private String JavaDoc onblur;
53     protected String JavaDoc localOnblur;
54     private Map JavaDoc params = new HashMap JavaDoc();
55
56
57     /**
58      * Retrieves the tags href attribute
59      *
60      * @return The tags href attribute
61      */

62     public String JavaDoc getHref() {
63         return href;
64     }
65
66     /**
67      * Populates the tags href attribute
68      *
69      * @param href The tags href attribute
70      */

71     public void setHref(String JavaDoc href) {
72         this.href = href;
73     }
74
75     /**
76      * Retrieves the tag's rel attribute
77      *
78      * @return Returns the tag's rel attribute
79      */

80     public String JavaDoc getRel() {
81         return rel;
82     }
83
84     /**
85      * Populates the tag's rel attribute
86      *
87      * @param rel The value of the tag's rel attribute
88      */

89     public void setRel(String JavaDoc rel) {
90         this.rel = rel;
91     }
92
93     /**
94      * Retrieves the tag's rev attribute
95      *
96      * @return Returns the tag's rev attribute
97      */

98     public String JavaDoc getRev() {
99         return rev;
100     }
101
102     /**
103      * Populates the tag's rev attribute
104      *
105      * @param rev The value of the tag's rev attribute
106      */

107     public void setRev(String JavaDoc rev) {
108         this.rev = rev;
109     }
110
111     /**
112      * Retrieves the tag's coords attribute
113      *
114      * @return Returns the tag's coords attribute
115      */

116     public String JavaDoc getCoords() {
117         return coords;
118     }
119
120     /**
121      * Populates the tag's coords attribute
122      *
123      * @param coords The value of the tag's coords attribute
124      */

125     public void setCoords(String JavaDoc coords) {
126         this.coords = coords;
127     }
128
129     /**
130      * Retrieves the tag's shape attribute
131      *
132      * @return Returns the tag's shape attribute
133      */

134     public String JavaDoc getShape() {
135         return shape;
136     }
137
138     /**
139      * Populates the tag's shape attribute
140      *
141      * @param shape The value of the tag's shape attribute
142      */

143     public void setShape(String JavaDoc shape) {
144         this.shape = shape;
145     }
146
147     /**
148      * Retrieves the tag's onfocus attribute
149      *
150      * @return Returns the tag's onfocus attribute
151      */

152     public String JavaDoc getOnfocus() {
153         return onfocus;
154     }
155
156     /**
157      * Populates the tag's onfocus attribute
158      *
159      * @param onfocus The value of the tag's onfocus attribute
160      */

161     public void setOnfocus(String JavaDoc onfocus) {
162         this.onfocus = onfocus;
163     }
164
165     /**
166      * Retrieves the tag's onblur attribute
167      *
168      * @return Returns the tag's onblur attribute
169      */

170     public String JavaDoc getOnblur() {
171         return onblur;
172     }
173
174     /**
175      * Populates the tag's onblur attribute
176      *
177      * @param onblur The value of the tag's onblur attribute
178      */

179     public void setOnblur(String JavaDoc onblur) {
180         this.onblur = onblur;
181     }
182
183     /**
184      * Returns the parameters Map that is used when drawing the anchor tag.
185      */

186     public Map JavaDoc getParameterMap() {
187         return params;
188     }
189
190     /**
191      * Expand the variables if not JSP 2.0 or greater.
192      *
193      * @throws JspException If there was any problem with the expansion
194      */

195     protected void initialize() throws JspException JavaDoc {
196         super.initialize();
197
198         localHref = href;
199         localRel = rel;
200         localRev = rev;
201         localCoords = coords;
202         localShape = shape;
203         localOnfocus = onfocus;
204         localOnblur = onblur;
205
206         // Expand the attributes if not JSP 2.0 or greate
207
if (!JspTools.JSP_20) {
208             localHref = (String JavaDoc) JspTools.expand("href", href, String JavaDoc.class, this,
209                 pageContext);
210             localRel = (String JavaDoc) JspTools.expand("rel", rel, String JavaDoc.class, this,
211                 pageContext);
212             localRev = (String JavaDoc) JspTools.expand("rev", rev, String JavaDoc.class, this,
213                 pageContext);
214             localCoords = (String JavaDoc) JspTools.expand("coords", coords, String JavaDoc.class,
215                 this, pageContext);
216             localShape = (String JavaDoc) JspTools.expand("shape", shape, String JavaDoc.class,
217                 this, pageContext);
218             localOnfocus = (String JavaDoc) JspTools.expand("onfocus", onfocus,
219                 String JavaDoc.class, this, pageContext);
220             localOnblur = (String JavaDoc) JspTools.expand("onblur", onblur, String JavaDoc.class,
221                 this, pageContext);
222         }
223
224         attributes.put(HtmlConstants.REL, localRel);
225         attributes.put(HtmlConstants.REV, localRev);
226         attributes.put(HtmlConstants.COORDS, localCoords);
227         attributes.put(HtmlConstants.SHAPE, localShape);
228         attributes.put(HtmlConstants.ON_FOCUS, localOnfocus);
229         attributes.put(HtmlConstants.ON_BLUR, localOnblur);
230     }
231
232     /**
233      * Returns EVAL_BODY_BUFFERED. doAfeterBody outputs the tag itself.
234      *
235      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
236      */

237     public int doStartTag() throws JspException JavaDoc {
238         initialize();
239         logger.debug("Inside doStartBody, returning buffered");
240
241         return EVAL_BODY_BUFFERED;
242     }
243
244     /**
245      * Outputs the tag, the body and the closing tag.
246      *
247      * @return Always SKIP_BODY
248      * @throws JspException If there was any problem outputting the tag
249      */

250     public int doAfterBody() throws JspException JavaDoc {
251         logger.debug("Inside doAfterBody, outputting tag");
252
253         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(128);
254         Writer JavaDoc writer = bodyContent.getEnclosingWriter();
255         createATag(buf, getId(), localName, localHref, getParameterMap());
256
257         try {
258             writer.write(buf.toString());
259
260             if (bodyContent != null) {
261                 bodyContent.writeOut(writer);
262             }
263
264             writer.write("</a>");
265         } catch (IOException JavaDoc ioe) {
266             throw new JspException JavaDoc(ioe.toString());
267         }
268
269         return SKIP_BODY;
270     }
271
272     /**
273      * Clears the parameters map and then returns EVAL_PAGE.
274      */

275     public int doEndTag() throws JspException JavaDoc {
276
277         // Clear out the parameters
278
getParameterMap().clear();
279
280         return EVAL_PAGE;
281     }
282
283     /**
284      * Does the work of rendering the anchor tag using the href, the parameters
285      * map and the rest of the attributes and such.
286      *
287      * @param buf The StringBuffer to append the anchor tag to
288      * @param href The action of the anchor tag
289      * @throws JspException If there was a problem generating the URLs for the
290      * anchor tag
291      */

292     protected void createATag(StringBuffer JavaDoc buf, String JavaDoc id, String JavaDoc name, String JavaDoc href,
293             Map JavaDoc parameters)
294     throws JspException JavaDoc {
295
296         // Append all the URL parameters
297
if (href != null) {
298
299             if (parameters.size() > 0) {
300                 StringBuffer JavaDoc url = new StringBuffer JavaDoc();
301                 url.append(href);
302                 if (url.indexOf("?") == -1) {
303                     url.append("?");
304                 } else {
305                     url.append("&");
306                 }
307
308                 Iterator JavaDoc iter = parameters.entrySet().iterator();
309                 Map.Entry JavaDoc entry;
310                 while (iter.hasNext()) {
311                     entry = (Map.Entry JavaDoc) iter.next();
312                     url.append(entry.getKey().toString());
313                     url.append("=");
314                     url.append(entry.getValue().toString());
315
316                     if (iter.hasNext()) {
317                         url.append("&");
318                     }
319                 }
320
321                 href = url.toString();
322             }
323
324             // Creates the HREF
325
StringBuffer JavaDoc tempHref = new StringBuffer JavaDoc();
326             appendContextPlusURL(tempHref, href);
327             href = tempHref.toString();
328         }
329
330         // Output the URL and other attributes
331
HtmlViewToolkit.createAnchorTag(buf, id, name, href, attributes, singleAttrs);
332     }
333 }
Popular Tags