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