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