1 19 20 package org.netbeans.modules.j2ee.sun.validation; 21 22 import java.lang.reflect.Method ; 23 import java.text.MessageFormat ; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 28 import org.netbeans.modules.j2ee.sun.validation.Constants; 29 import org.netbeans.modules.j2ee.sun.validation.constraints.Constraint; 30 import org.netbeans.modules.j2ee.sun.validation.util.BundleReader; 31 import org.netbeans.modules.j2ee.sun.validation.util.Utils; 32 import org.netbeans.modules.j2ee.sun.validation.Validatee; 33 34 70 class Validator { 71 72 73 private static String SIZE_PREFIX = "size"; 75 79 private HashMap elementToConstraints = null; 80 81 private Utils utils = null; 82 83 84 Validator() { 85 elementToConstraints = new HashMap (); 86 utils = new Utils(); 87 } 88 89 90 100 void addElementConstraint(String element, Constraint constraint){ 101 ArrayList list = (ArrayList ) elementToConstraints.get(element); 102 if(null == list) { 103 list = new ArrayList (); 104 } 105 list.add(constraint); 106 elementToConstraints.put(element, list); 107 } 108 109 110 123 Collection validate(String elementName,String elementDtdName, 124 Validatee validatee){ 125 ArrayList failures = new ArrayList (); 126 boolean isIndexed = validatee.isIndexed(elementName); 127 128 if(isIndexed){ 129 failures.addAll(validateProperties(elementName, 130 elementDtdName, validatee)); 131 } else { 132 failures.addAll(validateProperty(elementName, 133 elementDtdName, validatee)); 134 } 135 return failures; 136 } 137 138 139 151 Collection validateIndividualProperty(String property, 152 String absoluteDtdName, String fieldName){ 153 156 ArrayList failures = new ArrayList (); 157 String elementDtdName = utils.getName(absoluteDtdName, 158 Constants.XPATH_DELIMITER_CHAR); 159 160 ArrayList constraintList = 161 (ArrayList )elementToConstraints.get(elementDtdName); 162 163 if(constraintList != null) { 164 failures.addAll(validate(property, constraintList, fieldName)); 165 } else { 166 } 172 return failures; 173 } 174 175 176 188 private Collection validateProperty(String elementName, 189 String elementDtdName, Validatee validatee){ 190 194 ArrayList failures = new ArrayList (); 195 ArrayList constraintList = 196 (ArrayList )elementToConstraints.get(elementDtdName); 197 String name = validatee.getXPath() + Constants.XPATH_DELIMITER + 198 elementDtdName; 199 if(constraintList != null) { 200 String property = (String )validatee.getElement(elementName); 201 failures.addAll(validate(property, constraintList, name)); 202 } else { 203 } 208 return failures; 209 } 210 211 212 224 private Collection validateProperties(String elementName, 225 String elementDtdName, Validatee validatee){ 226 ArrayList failures = new ArrayList (); 227 int noOfElements = 0; 228 String sizeMethodName = utils.methodNameFromBeanName( 229 elementName, SIZE_PREFIX); 230 Method sizeMethod = validatee.getMethod(sizeMethodName); 231 noOfElements = ((Integer )validatee.invoke(sizeMethod)).intValue(); 232 233 ArrayList constraintList = 234 (ArrayList )elementToConstraints.get(elementDtdName); 235 if(constraintList != null) { 236 for(int i=0; i<noOfElements; i++){ 237 String property = (String )validatee.getElement(elementName, 238 i); 239 String name = validatee.getXPath() + Constants.XPATH_DELIMITER + 240 utils.getIndexedName(elementDtdName, i); 241 failures.addAll(validate(property, constraintList, name)); 242 } 243 } else { 244 } 250 return failures; 251 } 252 253 254 266 private Collection validate(String value, ArrayList constraints, 267 String name){ 268 ArrayList failed_constrains = new ArrayList (); 269 int size = constraints.size(); 270 Constraint constraint = null; 271 for(int i=0; i<size; i++) { 272 constraint = (Constraint)constraints.get(i); 273 failed_constrains.addAll(constraint.match(value, name)); 274 } 275 return failed_constrains; 276 } 277 } 278 | Popular Tags |