1 23 24 27 28 package com.sun.enterprise.tools.common.validation; 29 30 import java.lang.reflect.Method ; 31 import java.text.MessageFormat ; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.HashMap ; 35 36 import com.sun.enterprise.tools.common.validation.Constants; 37 import com.sun.enterprise.tools.common.validation.constraints.Constraint; 38 import com.sun.enterprise.tools.common.validation.util.BundleReader; 39 import com.sun.enterprise.tools.common.validation.util.Utils; 40 import com.sun.enterprise.tools.common.validation.Validatee; 41 42 78 class Validator { 79 80 81 private static String SIZE_PREFIX = "size"; 83 87 private HashMap elementToConstraints = null; 88 89 private Utils utils = null; 90 91 92 Validator() { 93 elementToConstraints = new HashMap (); 94 utils = new Utils(); 95 } 96 97 98 108 void addElementConstraint(String element, Constraint constraint){ 109 ArrayList list = (ArrayList ) elementToConstraints.get(element); 110 if(null == list) { 111 list = new ArrayList (); 112 } 113 list.add(constraint); 114 elementToConstraints.put(element, list); 115 } 116 117 118 131 Collection validate(String elementName,String elementDtdName, 132 Validatee validatee){ 133 ArrayList failures = new ArrayList (); 134 boolean isIndexed = validatee.isIndexed(elementName); 135 136 if(isIndexed){ 137 failures.addAll(validateProperties(elementName, 138 elementDtdName, validatee)); 139 } else { 140 failures.addAll(validateProperty(elementName, 141 elementDtdName, validatee)); 142 } 143 return failures; 144 } 145 146 147 159 Collection validateIndividualProperty(String property, 160 String absoluteDtdName, String fieldName){ 161 164 ArrayList failures = new ArrayList (); 165 String elementDtdName = utils.getName(absoluteDtdName, 166 Constants.XPATH_DELIMITER_CHAR); 167 168 ArrayList constraintList = 169 (ArrayList )elementToConstraints.get(elementDtdName); 170 171 if(constraintList != null) { 172 failures.addAll(validate(property, constraintList, fieldName)); 173 } else { 174 } 180 return failures; 181 } 182 183 184 196 private Collection validateProperty(String elementName, 197 String elementDtdName, Validatee validatee){ 198 202 ArrayList failures = new ArrayList (); 203 ArrayList constraintList = 204 (ArrayList )elementToConstraints.get(elementDtdName); 205 String name = validatee.getXPath() + Constants.XPATH_DELIMITER + 206 elementDtdName; 207 if(constraintList != null) { 208 String property = (String )validatee.getElement(elementName); 209 failures.addAll(validate(property, constraintList, name)); 210 } else { 211 } 216 return failures; 217 } 218 219 220 232 private Collection validateProperties(String elementName, 233 String elementDtdName, Validatee validatee){ 234 ArrayList failures = new ArrayList (); 235 int noOfElements = 0; 236 String sizeMethodName = utils.methodNameFromBeanName( 237 elementName, SIZE_PREFIX); 238 Method sizeMethod = validatee.getMethod(sizeMethodName); 239 noOfElements = ((Integer )validatee.invoke(sizeMethod)).intValue(); 240 241 ArrayList constraintList = 242 (ArrayList )elementToConstraints.get(elementDtdName); 243 if(constraintList != null) { 244 for(int i=0; i<noOfElements; i++){ 245 String property = (String )validatee.getElement(elementName, 246 i); 247 String name = validatee.getXPath() + Constants.XPATH_DELIMITER + 248 utils.getIndexedName(elementDtdName, i); 249 failures.addAll(validate(property, constraintList, name)); 250 } 251 } else { 252 } 258 return failures; 259 } 260 261 262 274 private Collection validate(String value, ArrayList constraints, 275 String name){ 276 ArrayList failed_constrains = new ArrayList (); 277 int size = constraints.size(); 278 Constraint constraint = null; 279 for(int i=0; i<size; i++) { 280 constraint = (Constraint)constraints.get(i); 281 failed_constrains.addAll(constraint.match(value, name)); 282 } 283 return failed_constrains; 284 } 285 } 286 | Popular Tags |