KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > config > BaseFormConfigBuilder


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.config;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 import org.jdom.Element;
14
15 import com.inversoft.beans.BeanException;
16 import com.inversoft.config.ConfigurationException;
17 import com.inversoft.config.component.ComponentConfigBuilderRegistry;
18 import com.inversoft.error.BasicError;
19 import com.inversoft.error.ErrorList;
20 import com.inversoft.util.StringTools;
21 import com.inversoft.verge.repository.RepositoryBean;
22 import com.inversoft.verge.repository.config.Config;
23 import com.inversoft.verge.repository.config.RepositoryConfigRegistry;
24 import com.inversoft.verge.util.ScopeConstants;
25 import com.inversoft.verge.util.ScopeTools;
26 import com.inversoft.verge.util.WebBean;
27
28
29 /**
30  * This class is responsible for building the FormConfig
31  * objects from XML Elements.
32  *
33  * @author Brian Pontarelli
34  */

35 public class BaseFormConfigBuilder extends BaseConfigBuilder {
36
37     private BaseValidatorConfigBuilder validatorConfigBuilder;
38
39
40     /**
41      * Constructs a new form config builder using the given configuration
42      * repository
43      */

44     public BaseFormConfigBuilder() {
45     }
46
47
48     /**
49      * Overrides the method from the Abstract parent class in order to retrieve
50      * the necessary builders form the registry
51      */

52     public void setRegistry(ComponentConfigBuilderRegistry registry) {
53         super.setRegistry(registry);
54         validatorConfigBuilder = (BaseValidatorConfigBuilder)
55             registry.lookup(Constants.VALIDATOR_BUILDER_NAME);
56     }
57
58     /**
59      * Parses out a single form configuration and constructs a FormConfig object
60      * that is returned.
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         String JavaDoc name = element.getAttributeValue(Constants.NAME_ATTRIBUTE);
71
72         if (name == null) {
73             errors.addError("Forms must have a name");
74         } else if (name.indexOf(".") != -1) {
75             errors.addError("The form name: " + name +
76                 " is invalid because it contains the '.' character");
77         }
78
79         BaseFormConfig formConfig = null;
80         if (errors.isEmpty()) {
81             formConfig = new BaseFormConfig(name);
82             buildFormBeans(element, formConfig, errors);
83             buildValidators(element, formConfig, errors);
84         }
85
86         if (!errors.isEmpty()) {
87             throw new ConfigurationException(errors);
88         }
89
90         return formConfig;
91     }
92
93     /**
94      * Validates and sets the form bean configurations for a given form
95      */

96     void buildFormBeans(Element form, BaseFormConfig formConfig, ErrorList errors) {
97
98         List JavaDoc list = form.getChildren(Constants.FORM_BEAN_ELEMENT);
99         if (list.size() == 0) {
100             return;
101         }
102
103         Iterator JavaDoc iter = list.iterator();
104         while (iter.hasNext()) {
105             Element bean = (Element) iter.next();
106             String JavaDoc name = bean.getAttributeValue(Constants.NAME_ATTRIBUTE);
107             String JavaDoc beanClass = bean.getAttributeValue(Constants.CLASS_ATTRIBUTE);
108             String JavaDoc scope = bean.getAttributeValue(Constants.SCOPE_ATTRIBUTE);
109             String JavaDoc isRepository = bean.getAttributeValue(Constants.IS_REPOSITORY_ATTRIBUTE);
110
111             if (name == null) {
112                 errors.addError("All formBeans for the form: " + formConfig.getName() +
113                     " must have a name");
114             }
115
116             boolean isRepositoryBool = false;
117             if (isRepository != null && !StringTools.isValidBoolean(isRepository)) {
118                 errors.addError("The formBean named: " + name + " for the form: "
119                     + formConfig.getName() + " has an invalid isRepository " +
120                     "attribute value");
121             } else {
122                 isRepositoryBool = Boolean.valueOf(isRepository).booleanValue();
123             }
124
125             if (beanClass == null && !isRepositoryBool) {
126                 errors.addError("The formBean named: " + name + " for the form: " +
127                     formConfig.getName() + " must have a class attribute or " +
128                     "have isRepository set to true");
129             }
130
131             int scopeInt = ScopeConstants.REQUEST_INT;
132             if (scope != null) {
133                 try {
134                     scopeInt = ScopeTools.convertScope(scope);
135                 } catch (IllegalArgumentException JavaDoc iae) {
136                     errors.addError("The formBean for the form: " + formConfig.getName() +
137                         " has an invalid scope of: " + scope);
138                 }
139             }
140
141             if (errors.isEmpty()){
142                 if (!isRepositoryBool) {
143                     try {
144                         formConfig.addFormBean(new WebBean(name, scopeInt, beanClass));
145                     } catch (BeanException be) {
146                         errors.addError( new BasicError(be.toString()) );
147                     }
148                 } else {
149                     formConfig.addFormBeanRID(name);
150                 }
151             }
152         }
153     }
154
155     /**
156      * Builds the validators for the form from the XML Element and stores them in
157      * the given FormConfig Object
158      */

159     void buildValidators(Element element, BaseFormConfig formConfig,
160             ErrorList errors) {
161
162         // Build the actions for the form (the action's mappings are built by
163
// the action config builder)
164
Iterator JavaDoc iter = element.getChildren(Constants.VALIDATOR_ELEMENT).iterator();
165         Element validatorElement;
166         BaseValidatorConfig validatorConfig;
167         while (iter.hasNext()) {
168             validatorElement = (Element) iter.next();
169             try {
170                 validatorConfig = (BaseValidatorConfig) validatorConfigBuilder.build(
171                     validatorElement);
172             } catch (ConfigurationException ce) {
173                 Iterator JavaDoc errorIter = ce.getErrors().iterator();
174                 BasicError error;
175                 while (errorIter.hasNext()) {
176                     error = (BasicError) errorIter.next();
177                     error = new BasicError("For form named: "
178                         + formConfig.getName() + " -> " + error.getMessage());
179                     errors.addError(error);
180                 }
181                 continue;
182             }
183
184             formConfig.addValidatorConfig(validatorConfig);
185         }
186     }
187
188     /**
189      * Validates and finishes building the FormConfig (if need be)
190      */

191     public void validate(BaseFormConfig config, RepositoryConfigRegistry registry,
192             FormConfigRegistry formRegistry)
193     throws ConfigurationException {
194
195         // If this config does not already has a formBean check if it is a RID
196
ErrorList errors = new ErrorList();
197         Iterator JavaDoc iter = config.getFormBeanRIDs().iterator();
198         while (iter.hasNext()) {
199             String JavaDoc rid = (String JavaDoc) iter.next();
200             Config repositoryConfig = registry.lookup(rid);
201
202             if (repositoryConfig == null) {
203                 errors.addError("Invalid repository id: " + rid);
204             } else {
205
206                 try {
207                     config.addFormBean(new RepositoryBean(repositoryConfig));
208                 } catch (BeanException be) {
209                     errors.addError(be.getMessage());
210                 }
211             }
212         }
213
214         iter = config.getValidators().iterator();
215         BaseValidatorConfig validatorConfig;
216         while (iter.hasNext()) {
217             validatorConfig = (BaseValidatorConfig) iter.next();
218             try {
219                 validatorConfigBuilder.validate(validatorConfig, config,
220                     formRegistry);
221             } catch (ConfigurationException ce) {
222                 errors.addErrorList(ce.getErrors());
223             }
224         }
225
226         if (!errors.isEmpty()) {
227             throw new ConfigurationException(errors);
228         }
229     }
230 }
Popular Tags