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