KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > tags > LinkTag


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Get a date from the ValueStack and format it in a nice way. Defaults to "yyyy-MM-dd" format.
28  */

29
30 package net.killingar.forum.tags;
31
32 import javax.servlet.jsp.JspException JavaDoc;
33 import javax.servlet.jsp.JspTagException JavaDoc;
34
35 public class LinkTag extends webwork.view.taglib.URLTag
36 {
37     // Attributes ----------------------------------------------------
38
protected String JavaDoc nameAttr = null;
39     protected String JavaDoc classAttr = null;
40     protected String JavaDoc targetAttr = null;
41     protected String JavaDoc anchorAttr = null;
42
43     // Public --------------------------------------------------------
44
public void setName(String JavaDoc in) { nameAttr = in; }
45     public void setCssclass(String JavaDoc in) { classAttr = in; }
46     public void setTarget(String JavaDoc in) { targetAttr = in; }
47     public void setAnchor(String JavaDoc in) { anchorAttr = in; }
48
49     // BodyTag implementation ----------------------------------------
50
public int doStartTag() throws JspException JavaDoc
51     {
52         try
53         {
54             pageContext.getOut().write("<a HREF=\"");
55             int r = super.doStartTag();
56             return r;
57         }
58         catch (Exception JavaDoc e)
59         {
60             e.printStackTrace();
61             throw new JspTagException JavaDoc("Could not show value " + valueAttr + ":" + e);
62         }
63     }
64
65     public int doEndTag() throws JspException JavaDoc
66     {
67         try
68         {
69             int r = super.doEndTag();
70
71             if (anchorAttr != null)
72             {
73                 pageContext.getOut().write("#");
74                 pageContext.getOut().write(anchorAttr);
75             }
76
77             if (classAttr != null)
78             {
79                 pageContext.getOut().write("\" class=\"");
80                 pageContext.getOut().write(classAttr);
81             }
82
83       // get access key
84
String JavaDoc accessKey = (String JavaDoc)findValue("text('"+nameAttr+".accesskey')");
85             if (accessKey != null && !"".equals(accessKey))
86             {
87                 accessKey = accessKey.toUpperCase();
88                 pageContext.getOut().write("\" accesskey=\"");
89                 pageContext.getOut().write(accessKey);
90             }
91
92             if (targetAttr != null)
93             {
94                 pageContext.getOut().write("\" target=\"");
95                 pageContext.getOut().write(targetAttr);
96             }
97
98             pageContext.getOut().write("\">");
99
100             // set accesskey CSS class
101
String JavaDoc name = (String JavaDoc)findValue("text('"+nameAttr+"')");
102
103             if (name == null)
104             {
105                 System.err.println("ERROR: could not find resource string \""+nameAttr+"\"");
106                 pageContext.getOut().write("[ERROR: could not find resource string \""+nameAttr+"\"]</a>");
107                 return r;
108             }
109
110             String JavaDoc nameUpper = name.toUpperCase();
111             int index = accessKey == null || "".equals(accessKey)? -1: nameUpper.indexOf(accessKey);
112             if (index != -1)
113             {
114                 pageContext.getOut().write(name.substring(0, index));
115                 pageContext.getOut().write("<span class=\"accesskey\">");
116                 pageContext.getOut().write(name.charAt(index));
117                 pageContext.getOut().write("</span>");
118                 pageContext.getOut().write(name.substring(index+accessKey.length()));
119             }
120             else
121                 pageContext.getOut().write(name);
122
123             // end link
124
pageContext.getOut().write("</a>");
125             return r;
126         }
127         catch (Exception JavaDoc e)
128         {
129             e.printStackTrace();
130             throw new JspTagException JavaDoc("Could not show value " + valueAttr + ":" + e);
131         }
132     }
133 }
Popular Tags