1 7 package com.inversoft.verge.mvc.validator; 8 9 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Map ; 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 34 public class ValidatorTools { 35 36 39 public ValidatorTools() { 40 super(); 41 } 42 43 44 54 public static Class findValidator(String validatorClass) throws MVCException { 55 assert (validatorClass != null) : "validatorClass == null"; 56 57 Class 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 80 public static void callHandle(List validators, Object model, 81 WebBeanProperty property, Action action) { 82 Iterator 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 103 public static void callHandle(BaseConfigStruct struct, Object model, 104 WebBeanProperty property, Action action) 105 throws MVCException { 106 BaseFormConfig formConfig = struct.baseFormConfig; 107 Iterator 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 141 public static boolean callValidate(List validators, Map modelObjects, 142 Action action) { 143 Iterator 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 |