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