1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.tags;
17
18 import static org.seasar.cubby.CubbyConstants.ATTR_CONTEXT_PATH;
19 import static org.seasar.cubby.tags.TagUtils.toAttr;
20
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import javax.servlet.http.HttpServletResponse;
26 import javax.servlet.jsp.JspException;
27 import javax.servlet.jsp.JspWriter;
28 import javax.servlet.jsp.PageContext;
29 import javax.servlet.jsp.tagext.BodyContent;
30 import javax.servlet.jsp.tagext.BodyTagSupport;
31 import javax.servlet.jsp.tagext.DynamicAttributes;
32
33
34
35
36
37
38
39 public class LinkTag extends BodyTagSupport implements DynamicAttributes,
40 ParamParent {
41
42
43 private static final long serialVersionUID = 1L;
44
45
46 private final Map<String, Object> attrs = new HashMap<String, Object>();
47
48
49 private final LinkSupport linkSupport = new LinkSupport();
50
51
52 private String tag;
53
54
55 private String attr;
56
57
58 private boolean encodeURL = true;
59
60
61
62
63 public void setDynamicAttribute(final String uri, final String localName,
64 final Object value) throws JspException {
65 this.attrs.put(localName, value);
66 }
67
68
69
70
71
72
73
74 public void setTag(final String tag) {
75 this.tag = tag;
76 }
77
78
79
80
81
82
83
84 public void setAttr(final String attr) {
85 this.attr = attr;
86 }
87
88
89
90
91
92
93
94 public void setActionClass(final String actionClass) {
95 linkSupport.setActionClassName(actionClass);
96 }
97
98
99
100
101
102
103
104 public void setActionMethod(final String actionMethod) {
105 linkSupport.setActionMethodName(actionMethod);
106 }
107
108
109
110
111
112
113
114
115 public void setEncodeURL(final boolean encodeURL) {
116 this.encodeURL = encodeURL;
117 }
118
119
120
121
122
123
124
125
126
127 public void addParameter(final String name, final String value) {
128 linkSupport.addParameter(name, value);
129 }
130
131
132
133
134 @Override
135 public int doStartTag() throws JspException {
136 return EVAL_BODY_BUFFERED;
137 }
138
139
140
141
142 @Override
143 public int doEndTag() throws JspException {
144 final String contextPath = (String) pageContext.getAttribute(
145 ATTR_CONTEXT_PATH, PageContext.REQUEST_SCOPE);
146 final String url;
147 if (encodeURL) {
148 final HttpServletResponse response = (HttpServletResponse) pageContext
149 .getResponse();
150 url = response.encodeURL(contextPath + linkSupport.getPath());
151 } else {
152 url = contextPath + linkSupport.getPath();
153 }
154
155 try {
156 final JspWriter out = pageContext.getOut();
157 if (tag == null) {
158 out.write(url);
159 final BodyContent bodyContent = getBodyContent();
160 if (bodyContent != null) {
161 bodyContent.writeOut(out);
162 }
163 } else {
164 attrs.put(attr, url);
165 out.write("<");
166 out.write(tag);
167 out.write(" ");
168 out.write(toAttr(attrs));
169 out.write(">");
170 final BodyContent bodyContent = getBodyContent();
171 if (bodyContent != null) {
172 bodyContent.writeOut(out);
173 }
174 out.write("</");
175 out.write(tag);
176 out.write(">");
177 }
178 } catch (final IOException e) {
179 throw new JspException(e);
180 }
181 reset();
182 return EVAL_PAGE;
183 }
184
185
186
187
188 private void reset() {
189 linkSupport.clear();
190 attrs.clear();
191 tag = null;
192 attr = null;
193 encodeURL = true;
194 }
195
196 }