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.tags.TagUtils.getContextPath;
19 import static org.seasar.cubby.tags.TagUtils.toAttr;
20
21 import java.io.IOException;
22 import java.net.MalformedURLException;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.JspWriter;
30 import javax.servlet.jsp.tagext.BodyContent;
31 import javax.servlet.jsp.tagext.BodyTagSupport;
32 import javax.servlet.jsp.tagext.DynamicAttributes;
33
34 import org.seasar.cubby.util.LinkBuilder;
35
36
37
38
39
40
41 public class LinkTag extends BodyTagSupport implements DynamicAttributes,
42 ParamParent {
43
44
45 private static final long serialVersionUID = 1L;
46
47
48 private final Map<String, Object> dynamicAttributes = new HashMap<String, Object>();
49
50
51 private final LinkSupport linkSupport = new LinkSupport();
52
53
54 private final LinkBuilder linkBuilder = new LinkBuilder();
55
56
57 private String tag;
58
59
60 private String attr;
61
62
63 private boolean encodeURL = true;
64
65
66
67
68 public void setDynamicAttribute(final String uri, final String localName,
69 final Object value) throws JspException {
70 this.dynamicAttributes.put(localName, value);
71 }
72
73
74
75
76
77
78
79 public void setTag(final String tag) {
80 this.tag = tag;
81 }
82
83
84
85
86
87
88
89 public void setAttr(final String attr) {
90 this.attr = attr;
91 }
92
93
94
95
96
97
98
99 public void setActionClass(final String actionClass) {
100 linkSupport.setActionClassName(actionClass);
101 }
102
103
104
105
106
107
108
109 public void setActionMethod(final String actionMethod) {
110 linkSupport.setActionMethodName(actionMethod);
111 }
112
113
114
115
116
117
118
119
120 public void setEncodeURL(final boolean encodeURL) {
121 this.encodeURL = encodeURL;
122 }
123
124
125
126
127
128
129
130 public void setProtocol(final String protocol) {
131 linkBuilder.setProtocol(protocol);
132 }
133
134
135
136
137
138
139
140 public void setPort(final int port) {
141 linkBuilder.setPort(port);
142 }
143
144
145
146
147
148
149
150
151
152 public void addParameter(final String name, final String value) {
153 linkSupport.addParameter(name, value);
154 }
155
156
157
158
159 @Override
160 public int doStartTag() throws JspException {
161 return EVAL_BODY_BUFFERED;
162 }
163
164
165
166
167 @Override
168 public int doEndTag() throws JspException {
169 final String contextPath = getContextPath(pageContext);
170 final String actionPath;
171 final HttpServletRequest request = (HttpServletRequest) pageContext
172 .getRequest();
173 final String characterEncoding = request.getCharacterEncoding();
174 if (encodeURL) {
175 final HttpServletResponse response = (HttpServletResponse) pageContext
176 .getResponse();
177 actionPath = response.encodeURL(contextPath
178 + linkSupport.getPath(characterEncoding));
179 } else {
180 actionPath = contextPath + linkSupport.getPath(characterEncoding);
181 }
182
183 final String url;
184 try {
185 url = linkBuilder.file(actionPath).toLink(request);
186 } catch (final MalformedURLException e) {
187 throw new JspException(e);
188 }
189
190 try {
191 final JspWriter out = pageContext.getOut();
192 if (tag == null) {
193 out.write(url);
194 final BodyContent bodyContent = getBodyContent();
195 if (bodyContent != null) {
196 bodyContent.writeOut(out);
197 }
198 } else {
199 dynamicAttributes.put(attr, url);
200 out.write("<");
201 out.write(tag);
202 out.write(" ");
203 out.write(toAttr(dynamicAttributes));
204 out.write(">");
205 final BodyContent bodyContent = getBodyContent();
206 if (bodyContent != null) {
207 bodyContent.writeOut(out);
208 }
209 out.write("</");
210 out.write(tag);
211 out.write(">");
212 }
213 } catch (final IOException e) {
214 throw new JspException(e);
215 }
216 reset();
217 return EVAL_PAGE;
218 }
219
220
221
222
223 private void reset() {
224 linkSupport.clear();
225 linkBuilder.clear();
226 dynamicAttributes.clear();
227 tag = null;
228 attr = null;
229 encodeURL = true;
230 }
231
232 }