KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15
16 import org.apache.log4j.Logger;
17
18 import com.inversoft.util.ReflectionException;
19 import com.inversoft.util.ReflectionTools;
20 import com.inversoft.util.typeconverter.TypeConversionException;
21 import com.inversoft.verge.mvc.MVCException;
22 import com.inversoft.verge.mvc.MVCRegistry;
23 import com.inversoft.verge.mvc.MVCRequest;
24 import com.inversoft.verge.mvc.controller.Action;
25 import com.inversoft.verge.util.WebBeanProperty;
26
27
28 /**
29  * <p>
30  * This class is the default implemenation of the validator
31  * parser. This class locates and handles the validation
32  * of the model.
33  * </p>
34  *
35  * @author Brian Pontarelli
36  * @since 2.0
37  * @version 2.0
38  */

39 public class DefaultValidatorParser implements ValidatorParser {
40
41     /**
42      * This classes logger
43      */

44     private static final Logger logger = Logger.getLogger(DefaultValidatorParser.class);
45
46
47     /**
48      * Sets up the MVCRequest with all the Validator objects from the request as
49      * well as the configuration. This method has NO knowledge of the Form-Based
50      * MVC configuration since the FormControlerHandler sets up the validators
51      * for that. This is a bit out of order, but that is life right?
52      *
53      * @param mvcRequest The MVCRequest to set the validators on
54      * @throws com.inversoft.verge.mvc.MVCException If there were any errors encountered
55      */

56     public void preExecute(MVCRequest mvcRequest)
57     throws MVCException {
58         HttpServletRequest JavaDoc request = mvcRequest.getRequest();
59         String JavaDoc [] validatorNames = request.getParameterValues(ValidatorConstants.VALIDATOR_PARAMETER);
60
61         if (validatorNames == null || validatorNames.length == 0) {
62             return;
63         }
64
65         List JavaDoc validators = new ArrayList JavaDoc();
66         if (validatorNames != null) {
67             for (int i = 0; i < validatorNames.length; i++) {
68                 validators.add(ValidatorTools.findValidator(validatorNames[i]));
69             }
70         }
71
72         if (validators.size() > 0) {
73             Iterator JavaDoc iter = validators.iterator();
74             Class JavaDoc validatorClass;
75             while (iter.hasNext()) {
76                 validatorClass = (Class JavaDoc) iter.next();
77                 try {
78                     mvcRequest.addValidator(
79                         (Validator) ReflectionTools.instantiate(validatorClass));
80                 } catch (ReflectionException re) {
81                     throw new MVCException(re);
82                 }
83             }
84         }
85     }
86
87     /**
88      * Calls all the validators for this request
89      *
90      * @param mvcRequest Used to retrieve all the information needed
91      * @throws com.inversoft.verge.mvc.MVCException Never
92      */

93     public void execute(MVCRequest mvcRequest) throws MVCException {
94
95         if (!mvcRequest.isValidationEnabled()) {
96             logger.debug("Validation is disabled");
97             return;
98         }
99
100         Action action = mvcRequest.getAction();
101         if (action == null) {
102             action = new Action(null, mvcRequest.getRequest(),
103                 mvcRequest.getResponse(), mvcRequest.getRequestContext());
104         }
105
106         // Loop over all the validator handlers setup to be called. The initial
107
// state of the valid flag could be false from the handle of conversion
108
// errors below
109
boolean valid = mvcRequest.getRequestContext().isValid();
110         List JavaDoc handlers = mvcRequest.getValidatorHandlersToCall();
111         if (handlers.size() > 0) {
112             Iterator JavaDoc iter = handlers.iterator();
113             String JavaDoc name;
114             ValidatorHandler handler;
115
116             while (iter.hasNext()) {
117                 name = (String JavaDoc) iter.next();
118                 handler = MVCRegistry.lookupValidatorHandler(name);
119
120                 if (handler == null) {
121                     throw new MVCException("Invalid handler named: " + name);
122                 }
123
124                 valid &= handler.validate(mvcRequest);
125             }
126         }
127
128         // Execute on the fly validation
129
valid &= ValidatorTools.callValidate(mvcRequest.getValidators(),
130             mvcRequest.getModelObjects(), action);
131         mvcRequest.getRequestContext().setValid(valid);
132     }
133
134     /**
135      * Calls all the validators for this request to handle a conversion error
136      * that occurred during model handling.
137      *
138      * @param mvcRequest Used to retrieve all the information needed
139      * @throws com.inversoft.verge.mvc.MVCException Never
140      */

141     public void executeHandle(MVCRequest mvcRequest, TypeConversionException tce,
142             Object JavaDoc model, WebBeanProperty wbp)
143     throws MVCException {
144
145         if (!mvcRequest.hasValidators() && mvcRequest.getConfiguration() == null) {
146              throw new MVCException(tce);
147         }
148
149         if (!mvcRequest.isValidationEnabled()) {
150             logger.debug("Validation is disabled");
151             return;
152         }
153
154         // Loop over all the validator handlers setup to be called
155
List JavaDoc handlers = mvcRequest.getValidatorHandlersToCall();
156         if (handlers.size() > 0) {
157             Iterator JavaDoc iter = handlers.iterator();
158             String JavaDoc name;
159             ValidatorHandler handler;
160
161             while (iter.hasNext()) {
162                 name = (String JavaDoc) iter.next();
163                 handler = MVCRegistry.lookupValidatorHandler(name);
164
165                 if (handler == null) {
166                     throw new MVCException("Invalid handler named: " + name);
167                 }
168
169                 handler.handle(mvcRequest, tce, model, wbp);
170             }
171         }
172
173         Action action = mvcRequest.getAction();
174         if (action == null) {
175             action = new Action(null, mvcRequest.getRequest(),
176                 mvcRequest.getResponse(), mvcRequest.getRequestContext());
177         }
178
179         // Execute on the fly validators
180
ValidatorTools.callHandle(mvcRequest.getValidators(), model, wbp,
181             action);
182         mvcRequest.getRequestContext().setValid(false);
183     }
184 }
Popular Tags