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> dynamicAttributes = new HashMap<String, Object>();
51
52
53 private transient 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.dynamicAttributes.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 HttpServletRequest request = (HttpServletRequest) pageContext
175 .getRequest();
176 final String characterEncoding = request.getCharacterEncoding();
177 if (encodeURL) {
178 final HttpServletResponse response = (HttpServletResponse) pageContext
179 .getResponse();
180 actionPath = response.encodeURL(contextPath
181 + linkSupport.getPath(characterEncoding));
182 } else {
183 actionPath = contextPath + linkSupport.getPath(characterEncoding);
184 }
185
186 final String url;
187 try {
188 url = linkBuilder.file(actionPath).toLink(request);
189 } catch (final MalformedURLException e) {
190 throw new JspException(e);
191 }
192
193 try {
194 final JspWriter out = pageContext.getOut();
195 if (tag == null) {
196 out.write(url);
197 final BodyContent bodyContent = getBodyContent();
198 if (bodyContent != null) {
199 bodyContent.writeOut(out);
200 }
201 } else {
202 dynamicAttributes.put(attr, url);
203 out.write("<");
204 out.write(tag);
205 out.write(" ");
206 out.write(toAttr(dynamicAttributes));
207 out.write(">");
208 final BodyContent bodyContent = getBodyContent();
209 if (bodyContent != null) {
210 bodyContent.writeOut(out);
211 }
212 out.write("</");
213 out.write(tag);
214 out.write(">");
215 }
216 } catch (final IOException e) {
217 throw new JspException(e);
218 }
219 reset();
220 return EVAL_PAGE;
221 }
222
223
224
225
226 private void reset() {
227 linkSupport.clear();
228 linkBuilder.clear();
229 dynamicAttributes.clear();
230 tag = null;
231 attr = null;
232 encodeURL = true;
233 }
234
235 }