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