KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > validator > ValidatorTools


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.validator;
8
9
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import com.inversoft.util.ReflectionException;
15 import com.inversoft.util.ReflectionTools;
16 import com.inversoft.verge.mvc.MVCException;
17 import com.inversoft.verge.mvc.config.BaseConfigStruct;
18 import com.inversoft.verge.mvc.config.BaseFormConfig;
19 import com.inversoft.verge.mvc.config.BaseValidatorConfig;
20 import com.inversoft.verge.mvc.controller.Action;
21 import com.inversoft.verge.util.WebBeanProperty;
22
23
24 /**
25  * <p>
26  * This class provides simple tools that help the model
27  * part of the MVC
28  * </p>
29  *
30  * @author Brian Pontarelli
31  * @since 2.0
32  * @version 2.0
33  */

34 public class ValidatorTools {
35
36     /**
37      * Constructor for ValidatorTools.
38      */

39     public ValidatorTools() {
40         super();
41     }
42
43
44     /**
45      * Using the name of a validator class, this attempts to locate the Class
46      * instance as well as validates the Class is the correct type. If all is
47      * well, this returns the Class. Otherwise, it throws an Exception
48      *
49      * @param validatorClass The name of the Validator class to find
50      * @return The Validator Class instance and never null
51      * @throws com.inversoft.verge.mvc.MVCException If the validator class does not implement {@link
52      * Validator Validator} or could not be found
53      */

54     public static Class JavaDoc findValidator(String JavaDoc validatorClass) throws MVCException {
55         assert (validatorClass != null) : "validatorClass == null";
56
57         Class JavaDoc klass = null;
58         try {
59             klass = ReflectionTools.findClass(validatorClass);
60         } catch (ReflectionException re) {
61             throw new MVCException(re);
62         }
63
64         if (!Validator.class.isAssignableFrom(klass)) {
65             throw new MVCException("validatorClass does not a implement" +
66                 " the Validator interface");
67         }
68
69         return klass;
70     }
71
72     /**
73      * Calls the handleConversion for each Validator obejct in the List.
74      *
75      * @param validators The list of validators to call
76      * @param model The Model object to pass to the method
77      * @param property The WebBeanProperty to pass to the method
78      * @param action The Action to pass to the method
79      */

80     public static void callHandle(List JavaDoc validators, Object JavaDoc model,
81             WebBeanProperty property, Action action) {
82         Iterator JavaDoc iter = validators.iterator();
83         Validator validator;
84         while (iter.hasNext()) {
85             validator = (Validator) iter.next();
86             validator.handleConversion(model, property, action);
87         }
88
89         action.getRequestContext().setValid(false);
90     }
91
92     /**
93      * Calls the handleConversion for each ValidatorConfig object in the BaseFormConfig.
94      * If any of those have mappings, then the first one encountered is used to
95      * setup for the controller so that the controller is not executed only the
96      * mapping is.
97      *
98      * @param struct BaseConfigStruct to get the ValidatorConfig objects from
99      * @param model The Model object to pass to the method
100      * @param property The WebBeanProperty to pass to the method
101      * @param action The Action to pass to the method
102      */

103     public static void callHandle(BaseConfigStruct struct, Object JavaDoc model,
104             WebBeanProperty property, Action action)
105     throws MVCException {
106         BaseFormConfig formConfig = struct.baseFormConfig;
107         Iterator JavaDoc iter = formConfig.getValidators().iterator();
108         BaseValidatorConfig config;
109         Validator validator;
110         while (iter.hasNext()) {
111             config = (BaseValidatorConfig) iter.next();
112
113             if (!config.isValidating()) {
114                 continue;
115             }
116
117             try {
118                 validator = config.newValidator();
119             } catch (ReflectionException re) {
120                 throw new MVCException(re);
121             }
122
123             validator.handleConversion(model, property, action);
124
125             if (config.getFailureDefinition() != null &&
126                     struct.failedValidatorConfig == null) {
127                 struct.failedValidatorConfig = config;
128             }
129         }
130
131         action.getRequestContext().setValid(false);
132     }
133
134     /**
135      * Calls the validate for each Validator obejct in the List.
136      *
137      * @param validators The list of validators to call
138      * @param modelObjects The Map of Model objects to pass to the method
139      * @param action The Action to pass to the method
140      */

141     public static boolean callValidate(List JavaDoc validators, Map JavaDoc modelObjects,
142             Action action) {
143         Iterator JavaDoc iter = validators.iterator();
144         Validator validator;
145         boolean valid = true;
146         while (iter.hasNext()) {
147             validator = (Validator) iter.next();
148             valid &= validator.validate(modelObjects, action);
149         }
150
151         return valid;
152     }
153 }
Popular Tags