KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jdom.Element;
11
12 import com.inversoft.config.ConfigurationException;
13 import com.inversoft.error.ErrorList;
14 import com.inversoft.util.ReflectionException;
15 import com.inversoft.verge.mvc.config.BaseConfig;
16 import com.inversoft.verge.mvc.config.BaseFormConfig;
17 import com.inversoft.verge.mvc.config.BaseValidatorConfig;
18 import com.inversoft.verge.mvc.config.BaseValidatorConfigBuilder;
19 import com.inversoft.verge.mvc.config.FormConfigRegistry;
20 import com.inversoft.verge.mvc.config.OverrideValidator;
21 import com.inversoft.verge.mvc.config.PropertyConfig;
22 import com.inversoft.verge.mvc.validator.Validator;
23
24
25 /**
26  * <p>
27  * This class is the configuration builder for the validator
28  * configuration objects for the Form-Based MVC.
29  * </p>
30  *
31  * @author Brian Pontarelli
32  */

33 public class ValidatorConfigBuilder extends BaseValidatorConfigBuilder {
34
35     /**
36      * Gets the name of the element that defines the failure definition. This
37      * allows systems that extend this builder to change the name of the attribute.
38      *
39      * @return The name of the element
40      */

41     public String JavaDoc getFailureElementName() {
42         return Constants.FAILURE_MAPPING_ATTRIBUTE;
43     }
44
45     /**
46      * {@inheritDoc}
47      */

48     public BaseConfig build(Element element) throws ConfigurationException {
49         BaseValidatorConfig base = (BaseValidatorConfig) super.build(element);
50         ValidatorConfig validator = new ValidatorConfig(base);
51
52         return validator;
53     }
54
55     /**
56      * {@inheritDoc}
57      */

58     public void validate(BaseValidatorConfig validator, BaseFormConfig config,
59             FormConfigRegistry registry)
60     throws ConfigurationException {
61
62         // Grab the mapping first so that we can use it in validation if needed
63
ErrorList errors = new ErrorList();
64         ValidatorConfig valConfig = (ValidatorConfig) validator;
65         FormMVCConfigRegistry formRegistry = (FormMVCConfigRegistry) registry;
66         String JavaDoc name = valConfig.getFailureDefinition();
67         if (name != null) {
68             MappingConfig mapping = formRegistry.lookupMapping(name,
69                 (FormConfig) config);
70             if (mapping == null) {
71                 errors.addError("Unable to locate mapping named: " + name +
72                     " for validator: " + validator.getName());
73             }
74
75             valConfig.setFailureMapping(mapping);
76         }
77
78         try {
79             super.validate(validator, config, registry);
80         } catch (ConfigurationException ce) {
81             errors.addErrorList(ce.getErrors());
82         }
83
84         if (!errors.isEmpty()) {
85             throw new ConfigurationException(errors);
86         }
87     }
88
89     /**
90      * Overrides the BaseValidatorConfigBuilder implementation so that the returned
91      * ValidatorConfig is a sub-class of the ValidatorConfig for the Form-Based
92      * MVC system.
93      *
94      * @param config The ValidatorConfig that proxies to the given validator
95      * configuration or copies it. It must act identical except for
96      * creating the validator class itself.
97      * @param propConfig The PropertyConfiguration if needed
98      * @return The proxy and never null
99      */

100     protected BaseValidatorConfig createProxy(BaseValidatorConfig config,
101             final PropertyConfig propConfig) {
102
103         ValidatorConfig valConfig = (ValidatorConfig) config;
104         return new ValidatorConfig(valConfig) {
105             // Is always validating
106
public boolean isValidating() {
107                 return true;
108             }
109
110             // Returns the Override validator as needed
111
public Validator newValidator() throws ReflectionException {
112                 return new OverrideValidator(propConfig);
113             }
114         };
115     }
116 }
Popular Tags