KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > acting > HandleFormSubmitAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.acting;
17
18 import org.apache.avalon.framework.thread.ThreadSafe;
19 import org.apache.avalon.framework.parameters.Parameters;
20 import org.apache.cocoon.acting.Action;
21 import org.apache.cocoon.environment.Redirector;
22 import org.apache.cocoon.environment.SourceResolver;
23 import org.apache.cocoon.environment.Request;
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.cocoon.forms.FormContext;
26 import org.apache.cocoon.forms.event.FormHandler;
27 import org.apache.cocoon.forms.formmodel.Form;
28 import org.apache.cocoon.i18n.I18nUtils;
29 import org.apache.cocoon.components.LifecycleHelper;
30 import org.apache.excalibur.source.Source;
31
32 import java.util.Map JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.Locale JavaDoc;
35
36 /**
37  * An action that will create a form instance, let it handle the current request (and
38  * do validation), and will return not-null if validation was successfully or null when
39  * validation failed. In both cases, the created form instance is stored in a request attribute,
40  * so that it can be picked up later on by other components.
41  *
42  * <p>Required parameters:
43  * <ul>
44  * <li><strong>form-definition</strong>: filename (URL) pointing to the form definition file
45  * <li><strong>attribute-name</strong>: name of the request attribute in which the form instance should be stored
46  * </ul>
47  *
48  * @version $Id: HandleFormSubmitAction.java 157080 2005-03-11 14:04:07Z sylvain $
49  */

50 public class HandleFormSubmitAction extends AbstractFormsAction implements Action, ThreadSafe {
51
52     public Map JavaDoc act(Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters parameters)
53             throws Exception JavaDoc {
54         String JavaDoc formSource = parameters.getParameter("form-definition");
55         String JavaDoc formAttribute = parameters.getParameter("attribute-name");
56         String JavaDoc formHandlerClassName = parameters.getParameter("formhandler", null);
57
58         Locale JavaDoc locale = Locale.getDefault();
59         String JavaDoc localeStr = parameters.getParameter("locale", null);
60         if (localeStr != null) {
61             locale = I18nUtils.parseLocale(localeStr, locale);
62         }
63
64         Source source = resolver.resolveURI(formSource);
65         try {
66             Form form = formManager.createForm(source);
67
68             Request request = ObjectModelHelper.getRequest(objectModel);
69             FormHandler formHandler = null;
70
71             if (formHandlerClassName != null) {
72                 // TODO cache these classes
73
Class JavaDoc clazz = Class.forName(formHandlerClassName);
74                 formHandler = (FormHandler)clazz.newInstance();
75                 LifecycleHelper.setupComponent(formHandler, null, null, manager, null);
76                 form.setFormHandler(formHandler);
77             }
78
79             FormContext formContext = new FormContext(request, locale);
80
81             boolean finished = form.process(formContext);
82             request.setAttribute(formAttribute, form);
83
84             if (finished) {
85                 return Collections.EMPTY_MAP;
86             }
87             return null;
88         } finally {
89             resolver.release(source);
90         }
91     }
92 }
93
Popular Tags