KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > form > Form


1 /*
2  * $Header: /home/cvs/jakarta-slide/projector/src/java/org/apache/slide/projector/processor/form/Form.java,v 1.4 2004/08/03 14:29:56 dflorey Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/08/03 14:29:56 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.projector.processor.form;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.apache.slide.projector.ConfigurationException;
33 import org.apache.slide.projector.Constants;
34 import org.apache.slide.projector.ContentType;
35 import org.apache.slide.projector.Context;
36 import org.apache.slide.projector.Result;
37 import org.apache.slide.projector.Store;
38 import org.apache.slide.projector.URI;
39 import org.apache.slide.projector.descriptor.AnyValueDescriptor;
40 import org.apache.slide.projector.descriptor.BooleanValueDescriptor;
41 import org.apache.slide.projector.descriptor.LocaleValueDescriptor;
42 import org.apache.slide.projector.descriptor.MapValueDescriptor;
43 import org.apache.slide.projector.descriptor.ParameterDescriptor;
44 import org.apache.slide.projector.descriptor.ResultDescriptor;
45 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
46 import org.apache.slide.projector.descriptor.StateDescriptor;
47 import org.apache.slide.projector.descriptor.StringValueDescriptor;
48 import org.apache.slide.projector.descriptor.URIValueDescriptor;
49 import org.apache.slide.projector.engine.ProcessorManager;
50 import org.apache.slide.projector.i18n.DefaultMessage;
51 import org.apache.slide.projector.i18n.ParameterMessage;
52 import org.apache.slide.projector.processor.process.Process;
53 import org.apache.slide.projector.value.ArrayValue;
54 import org.apache.slide.projector.value.BooleanValue;
55 import org.apache.slide.projector.value.LocaleValue;
56 import org.apache.slide.projector.value.MapValue;
57 import org.apache.slide.projector.value.NullValue;
58 import org.apache.slide.projector.value.StreamableValue;
59 import org.apache.slide.projector.value.StringValue;
60 import org.apache.slide.projector.value.Value;
61
62 /**
63  * The Form class
64  *
65  */

66 public class Form extends ControlComposer {
67     public final static String JavaDoc CONTROL_IDENTIFIER = "control:";
68     public final static String JavaDoc TRIGGER_IDENTIFIER = "trigger:";
69
70     private ParameterDescriptor[] parameterDescriptors;
71     
72     private final static ResultDescriptor resultDescriptor = new ResultDescriptor(
73             new StateDescriptor[] {
74                 new StateDescriptor(DEFAULT_STATE, new DefaultMessage("form/state/default")),
75                 new StateDescriptor(VALID_STATE, new DefaultMessage("form/state/valid")),
76                 new StateDescriptor(INVALID_STATE, new DefaultMessage("form/state/invalid"))
77             },
78             new ResultEntryDescriptor[]{
79                 new ResultEntryDescriptor(OUTPUT, new DefaultMessage("form/output"), ContentType.DYNAMIC, false)
80             });
81
82     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
83         Object JavaDoc formResult = context.getStore(Store.FORM).get(FormHandler.RESULT);
84         if ( formResult instanceof Result ) {
85             return (Result)formResult;
86         }
87         Object JavaDoc a = parameter.get(ACTION);
88         URI actionUri = (URI)a;
89         Locale JavaDoc locale = ((LocaleValue)parameter.get(LOCALE)).getLocale();
90         List JavaDoc informations = context.getInformations();
91         parameter.put(HANDLER, ProcessorManager.getInstance().process(ProcessorManager.URL, parameter.get(HANDLER), context));
92         List JavaDoc controlDescriptions = new ArrayList JavaDoc();
93         List JavaDoc triggerDescriptions = new ArrayList JavaDoc();
94         for (int i = 0; i < parameterDescriptors.length; i++) {
95             String JavaDoc parameterName = parameterDescriptors[i].getName();
96             Value value = (Value)parameter.get(parameterName);
97             if (parameterName.startsWith(CONTROL_IDENTIFIER)) {
98                 ((MapValue)value).getMap().put(CONTROL_NAME, parameterName);
99                 controlDescriptions.add(value);
100             } else if (parameterName.startsWith(TRIGGER_IDENTIFIER)) {
101                 ((MapValue)value).getMap().put(TRIGGER_NAME, parameterName);
102                 triggerDescriptions.add(value);
103             }
104         }
105         parameter.put(ControlComposer.CONTROL_DESCRIPTIONS, new ArrayValue((Value[])controlDescriptions.toArray(new Value[controlDescriptions.size()])));
106         parameter.put(ControlComposer.TRIGGER_DESCRIPTIONS, new ArrayValue((Value[])triggerDescriptions.toArray(new Value[triggerDescriptions.size()])));
107         ProcessorManager.prepareValues(super.getParameterDescriptors(), parameter, context);
108         Result controlComposerResult = super.process(parameter, context);
109         Value[] generatedControls = ((ArrayValue)controlComposerResult.getResultEntries().get(GENERATED_CONTROLS)).getArray();
110         for ( int i = 0; i < generatedControls.length; i++ ) {
111             Iterator JavaDoc j = ((MapValue)generatedControls[i]).getMap().entrySet().iterator();
112             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)j.next();
113             parameter.put(entry.getKey(), entry.getValue());
114         }
115         Value[] generatedTriggers = ((ArrayValue)controlComposerResult.getResultEntries().get(GENERATED_TRIGGERS)).getArray();
116         for ( int i = 0; i < generatedTriggers.length; i++ ) {
117             Iterator JavaDoc j = ((MapValue)generatedTriggers[i]).getMap().entrySet().iterator();
118             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)j.next();
119             parameter.put(entry.getKey(), entry.getValue());
120         }
121         parameter.put(RENDERED_ERRORS, controlComposerResult.getResultEntries().get(RENDERED_ERRORS));
122         Template template = defaultTemplate;
123         String JavaDoc state = controlComposerResult.getState();
124         if ( state == VALID_STATE && validTemplate != null ) {
125             template = validTemplate;
126         } else if ( state == INVALID_STATE && invalidTemplate != null ) {
127             template = invalidTemplate;
128         }
129         return new Result(state, OUTPUT, renderFragment(template, parameter));
130     }
131
132     public void configure(StreamableValue config) throws ConfigurationException {
133         super.configure(config);
134         ParameterDescriptor[] parentParameterDescriptors = super.getParameterDescriptors();
135         int counter = 0;
136         List JavaDoc parameterList = new ArrayList JavaDoc();
137         for (int i = 0; i < parentParameterDescriptors.length; i++) {
138             if (parentParameterDescriptors[i].getName().startsWith(CONTROL_IDENTIFIER)) {
139                 parameterList.add(new ParameterDescriptor(parentParameterDescriptors[i].getName(),
140                         new ParameterMessage("form/control"),
141                         new MapValueDescriptor(new ParameterDescriptor[] {
142                             new ParameterDescriptor(CONTROL, new ParameterMessage("form/control"), new URIValueDescriptor()),
143                             new ParameterDescriptor(CONTROL_CONTAINER, new ParameterMessage("form/controlContainer"), new URIValueDescriptor(), NullValue.NULL),
144                             new ParameterDescriptor(Control.ACTION, new ParameterMessage("control/action"), new URIValueDescriptor(), NullValue.NULL),
145                             new ParameterDescriptor(Control.PARAMETER, new ParameterMessage("control/parameter"), new StringValueDescriptor())
146                         })));
147             } else if (parentParameterDescriptors[i].getName().startsWith(TRIGGER_IDENTIFIER)) {
148                     parameterList.add(new ParameterDescriptor(parentParameterDescriptors[i].getName(),
149                             new ParameterMessage("form/trigger"),
150                             new MapValueDescriptor(new ParameterDescriptor[] {
151                                 new ParameterDescriptor(TRIGGER, new ParameterMessage("form/trigger"), new URIValueDescriptor()),
152                                 new ParameterDescriptor(TRIGGER_CONTAINER, new ParameterMessage("form/triggerContainer"), new URIValueDescriptor(), NullValue.NULL),
153                                 new ParameterDescriptor(Trigger.ACTION, new ParameterMessage("trigger/action"), new URIValueDescriptor(), NullValue.NULL),
154                                 new ParameterDescriptor(Trigger.VALIDATE, new ParameterMessage("trigger/validate"), new BooleanValueDescriptor(), BooleanValue.TRUE),
155                                 new ParameterDescriptor(Process.STEP, new ParameterMessage("trigger/step"), new AnyValueDescriptor(), NullValue.NULL)
156                             })));
157             } else if (!parentParameterDescriptors[i].getName().equals(FRAGMENT)
158                     && !parentParameterDescriptors[i].getName().equals(CONTROL_DESCRIPTIONS)
159                     && !parentParameterDescriptors[i].getName().equals(TRIGGER_DESCRIPTIONS)
160                     && !parentParameterDescriptors[i].getName().equals(RENDERED_ERRORS)
161                     && !parentParameterDescriptors[i].getName().equals(HANDLER)
162                     && !parentParameterDescriptors[i].getName().equals(METHOD)) {
163                 parameterList.add(parentParameterDescriptors[i]);
164             }
165         }
166         parameterList.add(new ParameterDescriptor(HANDLER, new ParameterMessage("form/handler"), new URIValueDescriptor(), Constants.DEFAULT_FORM_HANDLER));
167         parameterList.add(new ParameterDescriptor(METHOD, new ParameterMessage("form/method"), new StringValueDescriptor(methods), new StringValue(POST)));
168         parameterList.add(new ParameterDescriptor(LOCALE, new ParameterMessage("form/locale"), new LocaleValueDescriptor()));
169         parameterList.add(new ParameterDescriptor(ACTION, new ParameterMessage("form/action"), new URIValueDescriptor()));
170         parameterDescriptors = (ParameterDescriptor[])parameterList.toArray(new ParameterDescriptor[parameterList.size()]);
171     }
172
173     public ParameterDescriptor[] getParameterDescriptors() {
174         return parameterDescriptors;
175     }
176
177     public ResultDescriptor getResultDescriptor() {
178         return resultDescriptor;
179     }
180 }
Popular Tags