1 7 package com.inversoft.verge.mvc.validator; 8 9 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import javax.servlet.http.HttpServletRequest ; 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 39 public class DefaultValidatorParser implements ValidatorParser { 40 41 44 private static final Logger logger = Logger.getLogger(DefaultValidatorParser.class); 45 46 47 56 public void preExecute(MVCRequest mvcRequest) 57 throws MVCException { 58 HttpServletRequest request = mvcRequest.getRequest(); 59 String [] validatorNames = request.getParameterValues(ValidatorConstants.VALIDATOR_PARAMETER); 60 61 if (validatorNames == null || validatorNames.length == 0) { 62 return; 63 } 64 65 List validators = new ArrayList (); 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 iter = validators.iterator(); 74 Class validatorClass; 75 while (iter.hasNext()) { 76 validatorClass = (Class ) 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 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 boolean valid = mvcRequest.getRequestContext().isValid(); 110 List handlers = mvcRequest.getValidatorHandlersToCall(); 111 if (handlers.size() > 0) { 112 Iterator iter = handlers.iterator(); 113 String name; 114 ValidatorHandler handler; 115 116 while (iter.hasNext()) { 117 name = (String ) 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 valid &= ValidatorTools.callValidate(mvcRequest.getValidators(), 130 mvcRequest.getModelObjects(), action); 131 mvcRequest.getRequestContext().setValid(valid); 132 } 133 134 141 public void executeHandle(MVCRequest mvcRequest, TypeConversionException tce, 142 Object 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 List handlers = mvcRequest.getValidatorHandlersToCall(); 156 if (handlers.size() > 0) { 157 Iterator iter = handlers.iterator(); 158 String name; 159 ValidatorHandler handler; 160 161 while (iter.hasNext()) { 162 name = (String ) 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 ValidatorTools.callHandle(mvcRequest.getValidators(), model, wbp, 181 action); 182 mvcRequest.getRequestContext().setValid(false); 183 } 184 } | Popular Tags |