1 21 22 package org.apache.commons.validator.example; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.text.MessageFormat ; 27 import java.util.Iterator ; 28 import java.util.Locale ; 29 import java.util.Map ; 30 import java.util.ResourceBundle ; 31 32 import org.apache.commons.validator.Field; 33 import org.apache.commons.validator.Form; 34 import org.apache.commons.validator.Validator; 35 import org.apache.commons.validator.ValidatorAction; 36 import org.apache.commons.validator.ValidatorException; 37 import org.apache.commons.validator.ValidatorResources; 38 import org.apache.commons.validator.ValidatorResult; 39 import org.apache.commons.validator.ValidatorResults; 40 import org.xml.sax.SAXException ; 41 42 52 public class ValidateExample extends Object { 53 54 59 private static ResourceBundle apps = 60 ResourceBundle.getBundle( 61 "org.apache.commons.validator.example.applicationResources"); 62 63 67 public static void main(String [] args) 68 throws ValidatorException, IOException , SAXException { 69 70 InputStream in = null; 71 ValidatorResources resources = null; 72 73 try { 74 75 in = ValidateExample.class.getResourceAsStream("validator-example.xml"); 81 resources = new ValidatorResources(in); 82 83 } finally { 84 if (in != null) { 86 in.close(); 87 } 88 } 89 90 ValidateBean bean = new ValidateBean(); 92 93 Validator validator = new Validator(resources, "ValidateBean"); 96 97 validator.setParameter(Validator.BEAN_PARAM, bean); 99 100 ValidatorResults results = null; 101 102 106 results = validator.validate(); 107 printResults(bean, results, resources); 108 109 bean.setLastName("Tester"); 113 bean.setFirstName("John"); 114 bean.setStreet1("1 Test Street"); 115 bean.setCity("Testville"); 116 bean.setState("TE"); 117 bean.setPostalCode("12345"); 118 bean.setAge("Too Old"); 119 results = validator.validate(); 120 printResults(bean, results, resources); 121 122 validator.setOnlyReturnErrors(true); 124 results = validator.validate(); 125 printResults(bean, results, resources); 126 127 validator.setOnlyReturnErrors(false); 129 bean.setAge("123"); 130 results = validator.validate(); 131 printResults(bean, results, resources); 132 } 133 134 137 public static void printResults( 138 ValidateBean bean, 139 ValidatorResults results, 140 ValidatorResources resources) { 141 142 boolean success = true; 143 144 Form form = resources.getForm(Locale.getDefault(), "ValidateBean"); 146 147 System.out.println("\n\nValidating:"); 148 System.out.println(bean); 149 150 Iterator propertyNames = results.getPropertyNames().iterator(); 152 while (propertyNames.hasNext()) { 153 String propertyName = (String ) propertyNames.next(); 154 155 Field field = form.getField(propertyName); 157 158 String prettyFieldName = apps.getString(field.getArg(0).getKey()); 160 161 ValidatorResult result = results.getValidatorResult(propertyName); 163 164 Map actionMap = result.getActionMap(); 166 Iterator keys = actionMap.keySet().iterator(); 167 while (keys.hasNext()) { 168 String actName = (String ) keys.next(); 169 170 ValidatorAction action = resources.getValidatorAction(actName); 172 173 System.out.println( 175 propertyName 176 + "[" 177 + actName 178 + "] (" 179 + (result.isValid(actName) ? "PASSED" : "FAILED") 180 + ")"); 181 182 if (!result.isValid(actName)) { 184 success = false; 185 String message = apps.getString(action.getMsg()); 186 Object [] args = { prettyFieldName }; 187 System.out.println( 188 " Error message will be: " 189 + MessageFormat.format(message, args)); 190 191 } 192 } 193 } 194 if (success) { 195 System.out.println("FORM VALIDATION PASSED"); 196 } else { 197 System.out.println("FORM VALIDATION FAILED"); 198 } 199 200 } 201 202 } 203 | Popular Tags |