KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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.woody.FormContext;
26 import org.apache.cocoon.woody.event.FormHandler;
27 import org.apache.cocoon.woody.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 30932 2004-07-29 17:35:38Z vgritsenko $
49  */

50 public class HandleFormSubmitAction extends AbstractWoodyAction 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         Source source = resolver.resolveURI(formSource);
64         try {
65             Form form = formManager.createForm(source);
66
67             Request request = ObjectModelHelper.getRequest(objectModel);
68             FormHandler formHandler = null;
69
70             if (formHandlerClassName != null) {
71                 // TODO cache these classes
72
Class JavaDoc clazz = Class.forName(formHandlerClassName);
73                 formHandler = (FormHandler)clazz.newInstance();
74                 LifecycleHelper.setupComponent(formHandler, null, null, manager, null, null);
75                 form.setFormHandler(formHandler);
76             }
77
78             FormContext formContext = new FormContext(request, locale);
79
80             boolean finished = form.process(formContext);
81             request.setAttribute(formAttribute, form);
82
83             if (finished)
84                 return Collections.EMPTY_MAP;
85             else
86                 return null;
87         } finally {
88             resolver.release(source);
89         }
90     }
91 }
92
Popular Tags