KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import org.jdom.Attribute;
15 import org.jdom.Element;
16
17 import com.inversoft.beans.BeanException;
18 import com.inversoft.beans.JavaBeanTools;
19 import com.inversoft.config.ConfigurationException;
20 import com.inversoft.error.ErrorList;
21 import com.inversoft.util.ReflectionException;
22 import com.inversoft.verge.mvc.MVCException;
23 import com.inversoft.verge.mvc.validator.Validator;
24 import com.inversoft.verge.mvc.validator.ValidatorTools;
25 import com.inversoft.verge.util.WebBean;
26 import com.inversoft.verge.util.WebBeanProperty;
27
28
29 /**
30  * <p>
31  * This class is the building for the validator configuration
32  * objects.
33  * </p>
34  *
35  * @author Brian Pontarelli
36  * @since 2.0
37  * @version 2.0
38  */

39 public class BaseValidatorConfigBuilder extends BaseConfigBuilder {
40
41
42     /**
43      * Gets the name of the element that defines the failure definition. This
44      * allows systems that extend this builder to change the name of the attribute.
45      *
46      * @return The name of the element
47      */

48     public String JavaDoc getFailureElementName() {
49         return Constants.FAILURE_ATTRIBUTE;
50     }
51
52     /**
53      * Builds the ValidatorConfig object from the given XML element.
54      *
55      * @param element The XML element that contains the validator information
56      * @return The ValidatorConfig object
57      * @throws ConfigurationException If the building fails
58      */

59     public BaseConfig build(Element element) throws ConfigurationException {
60
61         ErrorList errors = new ErrorList();
62         String JavaDoc validator = element.getAttributeValue(Constants.CLASS_ATTRIBUTE);
63         Class JavaDoc validatorClass = null;
64         if (validator != null) {
65             try {
66                 validatorClass = ValidatorTools.findValidator(validator);
67             } catch (MVCException mvce) {
68                 errors.addError("The validator class: " + validator +
69                     " is invalid");
70             }
71         }
72
73         String JavaDoc failureDef = element.getAttributeValue(getFailureElementName());
74
75         // Get all the automatic validators
76
BaseValidatorConfig validatorConfig = new BaseValidatorConfig(validatorClass,
77             failureDef);
78
79         buildProperties(element, validatorConfig, errors);
80
81         if (!errors.isEmpty()) {
82             throw new ConfigurationException(errors);
83         }
84
85         return validatorConfig;
86     }
87
88     /**
89      * Builds the property validation configuration for this validator
90      */

91     protected void buildProperties(Element element, BaseValidatorConfig validatorConfig,
92             ErrorList errors) {
93         Iterator JavaDoc iter = element.getChildren(Constants.PROPERTY_ELEMENT).iterator();
94         Element elem;
95         while (iter.hasNext()) {
96             elem = (Element) iter.next();
97
98             Iterator JavaDoc attrIter = elem.getAttributes().iterator();
99             Attribute attr;
100             String JavaDoc definition = null;
101             String JavaDoc id = null;
102             String JavaDoc property = null;
103             String JavaDoc type = null;
104             String JavaDoc key = null;
105             String JavaDoc bundleName = null;
106             String JavaDoc errorMsg = null;
107             String JavaDoc name;
108             JavaBeanTools.NameInfo ni;
109             Map JavaDoc parameters = new HashMap JavaDoc();
110             boolean valid = true;
111             while (attrIter.hasNext()) {
112
113                 attr = (Attribute) attrIter.next();
114                 name = attr.getName();
115                 if (name.equals(Constants.NAME_ATTRIBUTE)) {
116                     definition = attr.getValue();
117                     ni = JavaBeanTools.splitNameFront(definition);
118                     if (!ni.nested) {
119                         errors.addError("Property name must include the form " +
120                             "bean name and the property name (i.e. id.property)");
121                     } else {
122                         id = ni.localPropertyName;
123                         property = ni.nestedPropertyName;
124                     }
125                 } else if (name.equals(Constants.TYPE_ATTRIBUTE)) {
126                     type = attr.getValue();
127                 } else if (name.equals(Constants.KEY_ATTRIBUTE)) {
128                     key = attr.getValue();
129                 } else if (name.equals(Constants.BUNDLE_NAME_ATTRIBUTE)) {
130                     bundleName = attr.getValue();
131                 } else if (name.equals(Constants.ERROR_MSG_ATTRIBUTE)) {
132                     errorMsg = attr.getValue();
133                 } else {
134                     parameters.put(name, attr.getValue());
135                 }
136             }
137
138             if (property == null) {
139                 errors.addError("The property validation must specify a property");
140                 valid = false;
141             }
142             if (type == null) {
143                 errors.addError("The property validation for the property named: " +
144                     id + "." + property + " must specify a validator type");
145                 valid = false;
146             }
147
148             if (valid) {
149                 validatorConfig.addProperty(new PropertyConfig(id, property, type,
150                     key, bundleName, errorMsg, parameters));
151             }
152         }
153     }
154
155     /**
156      * Validates and finishes building the ValidatorConfig (if need be)
157      */

158     public void validate(BaseValidatorConfig validator, BaseFormConfig config,
159             FormConfigRegistry registry)
160     throws ConfigurationException {
161         // Validate that the properties are associated with valid form beans
162
ErrorList errors = new ErrorList();
163         Iterator JavaDoc iter = validator.getProperties().iterator();
164         PropertyConfig propConfig;
165         while (iter.hasNext()) {
166             propConfig = (PropertyConfig) iter.next();
167             if (config.getFormBean(propConfig.getID()) == null) {
168                 errors.addError("The form named: " + config.getName() +
169                     " does not have a form bean named: " + propConfig.getID());
170                 continue;
171             }
172
173             // If the type is one of the special primitive or wrapper types,
174
// validate that and possibly set things up to work for auto-validation
175
if (propConfig.isPrimitiveOrWrapper()) {
176                 validatePrimitive(validator, config, propConfig, errors);
177             }
178         }
179     }
180
181     /**
182      * Validates the primitive types and is only called if the type is one of the
183      * special primitive/wrapper types.
184      */

185     private void validatePrimitive(BaseValidatorConfig validator,
186                 BaseFormConfig config, PropertyConfig propConfig, ErrorList errors) {
187
188         // I need to construct the BeanProperty here so that I can find out
189
// what the type is
190
WebBean wb = config.getFormBean(propConfig.getID());
191         WebBeanProperty wbp = null;
192         try {
193             wbp = wb.getWebBeanProperty(propConfig.getProperty());
194         } catch (BeanException be) {
195             errors.addError("Invalid bean property: " + be.toString());
196             return;
197         }
198
199         Class JavaDoc propType = wbp.getPropertyType();
200         boolean needsConv = false;
201         double min = 0;
202         double max = 0;
203         if (propConfig.getType().equals("char")) {
204             needsConv = (propType == Character.TYPE || propType == Character JavaDoc.class);
205         } else if (propConfig.getType().equals("byte")) {
206             needsConv = (propType == Byte.TYPE || propType == Byte JavaDoc.class);
207             min = Byte.MIN_VALUE;
208             max = Byte.MAX_VALUE;
209         } else if (propConfig.getType().equals("short")) {
210             needsConv = (propType == Short.TYPE || propType == Short JavaDoc.class);
211             min = Short.MIN_VALUE;
212             max = Short.MAX_VALUE;
213         } else if (propConfig.getType().equals("int")) {
214             needsConv = (propType == Integer.TYPE || propType == Integer JavaDoc.class);
215             min = Integer.MIN_VALUE;
216             max = Integer.MAX_VALUE;
217         } else if (propConfig.getType().equals("long")) {
218             needsConv = (propType == Long.TYPE || propType == Long JavaDoc.class);
219             min = Long.MIN_VALUE;
220             max = Long.MAX_VALUE;
221         } else if (propConfig.getType().equals("float")) {
222             needsConv = (propType == Float.TYPE || propType == Float JavaDoc.class);
223             min = Float.MIN_VALUE;
224             max = Float.MAX_VALUE;
225         } else if (propConfig.getType().equals("double")) {
226             needsConv = (propType == Double.TYPE || propType == Double JavaDoc.class);
227             min = Double.MIN_VALUE;
228             max = Double.MAX_VALUE;
229         }
230
231         // Since the property will be converted, we need to add a new validator
232
// that will use the OverrideValidator
233
if (needsConv) {
234             validator.removeProperty(propConfig);
235             config.addValidatorConfig(createProxy(validator, propConfig));
236         } else if (min != 0 && max != 0) {
237             // Change the type to number with min and max for the value
238
Map JavaDoc map = propConfig.getLiveParameters();
239             if (map != null) {
240                 map = new HashMap JavaDoc();
241             }
242
243             if (map.get("min") == null) {
244                 map.put("min", new Double JavaDoc(min));
245             }
246
247             if (map.get("max") == null) {
248                 map.put("max", new Double JavaDoc(max));
249             }
250
251             propConfig.setType("number");
252             propConfig.setParameters(map);
253         } else {
254             // This case is a character that is not a primitive or wrapper
255
Map JavaDoc map = new HashMap JavaDoc();
256             map.put("min", new Double JavaDoc(0d));
257             map.put("max", new Double JavaDoc(1d));
258             propConfig.setType("string");
259             propConfig.setParameters(map);
260         }
261     }
262
263     /**
264      * Constructs a proxy to the given validator config and property configuration.
265      *
266      * @param config The ValidatorConfig that proxies to the given validator
267      * configuration or copies it. It must act identical except for
268      * creating the validator class itself.
269      * @param propConfig The PropertyConfiguration if needed
270      * @return The proxy and never null
271      */

272     protected BaseValidatorConfig createProxy(BaseValidatorConfig config,
273             final PropertyConfig propConfig) {
274
275         return new BaseValidatorConfig(null, config.getFailureDefinition()) {
276             // Is always validating
277
public boolean isValidating() {
278                 return true;
279             }
280
281             // Returns the Override validator as needed
282
public Validator newValidator() throws ReflectionException {
283                 return new OverrideValidator(propConfig);
284             }
285         };
286     }
287 }
Popular Tags