KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > form > config > FormConfigBuilder


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller.form.config;
8
9
10 import java.util.Iterator JavaDoc;
11
12 import org.jdom.Element;
13
14 import com.inversoft.config.ConfigurationException;
15 import com.inversoft.config.component.ComponentConfigBuilderRegistry;
16 import com.inversoft.error.BasicError;
17 import com.inversoft.error.ErrorList;
18 import com.inversoft.util.ReflectionException;
19 import com.inversoft.util.ReflectionTools;
20 import com.inversoft.verge.mvc.config.BaseConfig;
21 import com.inversoft.verge.mvc.config.BaseFormConfig;
22 import com.inversoft.verge.mvc.config.BaseFormConfigBuilder;
23 import com.inversoft.verge.mvc.controller.LongTxnSetup;
24
25
26 /**
27  * This class is responsible for building the FormConfig
28  * objects from XML Elements.
29  *
30  * @author Brian Pontarelli
31  */

32 public class FormConfigBuilder extends BaseFormConfigBuilder {
33
34     ActionConfigBuilder actionConfigBuilder;
35     MappingConfigBuilder mappingConfigBuilder;
36
37
38     /**
39      * Constructs a new form config builder using the given configuration repository
40      */

41     public FormConfigBuilder() {
42     }
43
44
45     /**
46      * Overrides the method from the Abstract parent class in order to retrieve
47      * the necessary builders form the registry
48      */

49     public void setRegistry(ComponentConfigBuilderRegistry registry) {
50         super.setRegistry(registry);
51         actionConfigBuilder =
52             (ActionConfigBuilder) registry.lookup(Constants.ACTION_BUILDER_NAME);
53         mappingConfigBuilder =
54             (MappingConfigBuilder) registry.lookup(Constants.MAPPING_BUILDER_NAME);
55     }
56
57     /**
58      * Parses out a single form configuration and constructs a FormConfig object
59      * that is returned. If there are any imbedded action elements, those are
60      * parsed out using the {@link ActionConfig ActionConfig} class.
61      *
62      * @param element The element to build from
63      * @return The FormConfig object and never null
64      * @throws ConfigurationException If there were any problems during parsing
65      * and building
66      */

67     public BaseConfig build(Element element) throws ConfigurationException {
68
69         ErrorList errors = new ErrorList();
70         BaseFormConfig base = null;
71         try {
72             base = (BaseFormConfig) super.build(element);
73         } catch (ConfigurationException ce) {
74             errors.addErrorList(ce.getErrors());
75         }
76
77         FormConfig formConfig = null;
78         if (errors.isEmpty()) {
79             formConfig = new FormConfig(base);
80
81             buildActions(element, formConfig, errors);
82             buildMappings(element, formConfig, errors);
83         }
84
85         String JavaDoc longTxnSetup = element.getAttributeValue(
86             Constants.LONG_TXN_SETUP_ATTRIBUTE);
87         LongTxnSetup longTxnSetupObj = null;
88         if (longTxnSetup != null) {
89             try {
90                 longTxnSetupObj =
91                     (LongTxnSetup) ReflectionTools.instantiate(longTxnSetup);
92             } catch (ReflectionException re) {
93                 errors.addError("Unable to instantiate LongTxnSetup class: " +
94                     longTxnSetup);
95             } catch (ClassCastException JavaDoc cce) {
96                 errors.addError("Class: " + longTxnSetup + " not an instance of " +
97                     "com.inversoft.verge.mvc.controller.LongTxnSetup");
98             }
99             formConfig.setLongTxnSetup(longTxnSetupObj);
100         }
101
102         if (!errors.isEmpty()) {
103             throw new ConfigurationException(errors);
104         }
105
106         return formConfig;
107     }
108
109     /**
110      * Builds the actions for the form from the XML Element and stores them in
111      * the given FormConfig Object
112      */

113     void buildActions(Element element, FormConfig formConfig, ErrorList errors) {
114
115         // Build the actions for the form (the action's mappings are built by
116
// the action config builder)
117
Iterator JavaDoc iter = element.getChildren(Constants.ACTION_ELEMENT).iterator();
118         Element actionElement;
119         ActionConfig actionConfig;
120         String JavaDoc actionName;
121         while (iter.hasNext()) {
122             actionElement = (Element) iter.next();
123             try {
124                 actionConfig = (ActionConfig) actionConfigBuilder.build(actionElement);
125             } catch (ConfigurationException ce) {
126                 Iterator JavaDoc errorIter = ce.getErrors().iterator();
127                 BasicError error;
128                 while (errorIter.hasNext()) {
129                     error = (BasicError) errorIter.next();
130                     error = new BasicError("For form named: "
131                         + formConfig.getName() + " -> " + error.getMessage());
132                     errors.addError(error);
133                 }
134                 continue;
135             }
136
137             actionName = actionConfig.getName();
138             if (formConfig.getActionConfig(actionName) != null) {
139                 errors.addError("action named: " + actionName + " is already" +
140                     " defined for the form: " + formConfig.getName());
141             }
142
143             formConfig.addActionConfig(actionName, actionConfig);
144         }
145     }
146
147     /**
148      * Builds the mappings for the form from the XML Element and stores them in
149      * the given FormConfig Object
150      */

151     void buildMappings(Element element, FormConfig formConfig, ErrorList errors) {
152
153         // Build the actions for the form (the action's mappings are built by
154
// the action config builder)
155
Iterator JavaDoc iter = element.getChildren(Constants.MAPPING_ELEMENT).iterator();
156         Element mappingElement;
157         MappingConfig mappingConfig;
158         String JavaDoc mappingName;
159         while (iter.hasNext()) {
160             mappingElement = (Element) iter.next();
161             try {
162                 mappingConfig = (MappingConfig) mappingConfigBuilder.build(mappingElement);
163             } catch (ConfigurationException ce) {
164                 Iterator JavaDoc errorIter = ce.getErrors().iterator();
165                 BasicError error;
166                 while (errorIter.hasNext()) {
167                     error = (BasicError) errorIter.next();
168                     error = new BasicError("For form named: "
169                         + formConfig.getName() + " -> " + error.getMessage());
170                     errors.addError(error);
171                 }
172                 continue;
173             }
174
175             mappingName = mappingConfig.getName();
176             if (formConfig.getMappingConfig(mappingName) != null) {
177                 errors.addError("mapping named: " + mappingName + " is already" +
178                     " defined for the form: " + formConfig.getName());
179             }
180
181             formConfig.addMappingConfig(mappingName, mappingConfig);
182         }
183     }
184 }
Popular Tags