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