1 21 22 package org.apache.commons.validator; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.Serializable ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 35 import org.apache.commons.collections.FastHashMap; import org.apache.commons.digester.Digester; 37 import org.apache.commons.digester.xmlrules.DigesterLoader; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 import org.xml.sax.SAXException ; 42 43 59 public class ValidatorResources implements Serializable { 60 61 66 private static final String registrations[] = { 67 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN", 68 "/org/apache/commons/validator/resources/validator_1_0.dtd", 69 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0.1//EN", 70 "/org/apache/commons/validator/resources/validator_1_0_1.dtd", 71 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN", 72 "/org/apache/commons/validator/resources/validator_1_1.dtd", 73 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.2.0//EN", 74 "/org/apache/commons/validator/resources/validator_1_2_0.dtd" 75 }; 76 77 private static final Log log = LogFactory.getLog(ValidatorResources.class); 78 79 84 protected FastHashMap hFormSets = new FastHashMap(); 85 86 91 protected FastHashMap hConstants = new FastHashMap(); 92 93 98 protected FastHashMap hActions = new FastHashMap(); 99 100 103 protected static Locale defaultLocale = Locale.getDefault(); 104 105 108 public ValidatorResources() { 109 super(); 110 } 111 112 122 public ValidatorResources(InputStream in) throws IOException , SAXException { 123 this(new InputStream []{in}); 124 } 125 126 137 public ValidatorResources(InputStream [] streams) 138 throws IOException , SAXException { 139 140 super(); 141 142 URL rulesUrl = this.getClass().getResource("digester-rules.xml"); 143 Digester digester = DigesterLoader.createDigester(rulesUrl); 144 digester.setNamespaceAware(true); 145 digester.setValidating(true); 146 digester.setUseContextClassLoader(true); 147 148 for (int i = 0; i < registrations.length; i += 2) { 150 URL url = this.getClass().getResource(registrations[i + 1]); 151 if (url != null) { 152 digester.register(registrations[i], url.toString()); 153 } 154 } 155 156 for (int i = 0; i < streams.length; i++) { 157 digester.push(this); 158 digester.parse(streams[i]); 159 } 160 161 this.process(); 162 } 163 164 170 public void addFormSet(FormSet fs) { 171 String key = this.buildKey(fs); 172 List formsets = (List ) hFormSets.get(key); 173 174 if (formsets == null) { 175 formsets = new ArrayList (); 176 hFormSets.put(key, formsets); 177 } 178 179 if (!formsets.contains(fs)) { 180 if (log.isDebugEnabled()) { 181 log.debug("Adding FormSet '" + fs.toString() + "'."); 182 } 183 formsets.add(fs); 184 } 185 } 186 187 190 public void addConstant(String name, String value) { 191 if (log.isDebugEnabled()) { 192 log.debug("Adding Global Constant: " + name + "," + value); 193 } 194 195 this.hConstants.put(name, value); 196 } 197 198 204 public void addValidatorAction(ValidatorAction va) { 205 va.init(); 206 207 this.hActions.put(va.getName(), va); 208 209 if (log.isDebugEnabled()) { 210 log.debug("Add ValidatorAction: " + va.getName() + "," + va.getClassname()); 211 } 212 } 213 214 217 public ValidatorAction getValidatorAction(String key) { 218 return (ValidatorAction) hActions.get(key); 219 } 220 221 224 public Map getValidatorActions() { 225 return Collections.unmodifiableMap(hActions); 226 } 227 228 232 protected String buildKey(FormSet fs) { 233 String locale = 234 this.buildLocale(fs.getLanguage(), fs.getCountry(), fs.getVariant()); 235 236 if (locale.length() == 0) { 237 locale = defaultLocale.toString(); 238 } 239 240 return locale; 241 } 242 243 246 private String buildLocale(String lang, String country, String variant) { 247 String key = ((lang != null && lang.length() > 0) ? lang : ""); 248 key += ((country != null && country.length() > 0) ? "_" + country : ""); 249 key += ((variant != null && variant.length() > 0) ? "_" + variant : ""); 250 return key; 251 } 252 253 265 public Form getForm(Locale locale, String formKey) { 266 return this.getForm(locale.getLanguage(), locale.getCountry(), locale 267 .getVariant(), formKey); 268 } 269 270 282 public Form getForm(String language, String country, String variant, 283 String formKey) { 284 285 String key = this.buildLocale(language, country, variant); 286 287 List v = (List ) hFormSets.get(key); 288 289 if (v == null) { 290 key = (language != null && language.length() > 0) ? language : ""; 291 key += (country != null && country.length() > 0) ? "_" + country : ""; 292 v = (List ) hFormSets.get(key); 293 } 294 295 if (v == null) { 296 key = (language != null && language.length() > 0) ? language : ""; 297 v = (List ) hFormSets.get(key); 298 } 299 300 if (v == null) { 301 key = defaultLocale.toString(); 302 v = (List ) hFormSets.get(key); 303 } 304 305 if (v == null) { 306 return null; 307 } 308 309 Iterator formsets = v.iterator(); 310 while (formsets.hasNext()) { 311 FormSet set = (FormSet) formsets.next(); 312 313 if ((set != null) && (set.getForm(formKey) != null)) { 314 return set.getForm(formKey); 315 } 316 317 } 318 return null; 319 } 320 321 328 public void process() { 329 hFormSets.setFast(true); 330 hConstants.setFast(true); 331 hActions.setFast(true); 332 333 this.processForms(); 334 } 335 336 341 private void processForms() { 342 String defaultKey = defaultLocale.toString(); 344 345 for (Iterator i = hFormSets.keySet().iterator(); i.hasNext();) { 347 String key = (String ) i.next(); 348 if (key.equals(defaultKey)) { 350 continue; 351 } 352 List formsets = (List ) hFormSets.get(key); 353 Iterator formsetsIterator = formsets.iterator(); 354 while (formsetsIterator.hasNext()) { 355 FormSet fs = (FormSet) formsetsIterator.next(); 356 357 for (Iterator x = fs.getForms().keySet().iterator(); x.hasNext();) { 359 String formKey = (String ) x.next(); 360 Form form = (Form) fs.getForms().get(formKey); 361 Form newForm = new Form(); 364 newForm.setName(form.getName()); 365 366 Form defaultForm = this.getForm(defaultLocale, formKey); 369 370 Iterator defaultFields = defaultForm.getFields().iterator(); 371 while (defaultFields.hasNext()) { 372 Field defaultField = (Field) defaultFields.next(); 373 String fieldKey = defaultField.getKey(); 374 375 if (form.containsField(fieldKey)) { 376 newForm.addField(form.getField(fieldKey)); 377 378 } else { 379 Field field = 380 getClosestLocaleField(fs, formKey, fieldKey); 381 382 newForm.addField((Field) field.clone()); 383 } 384 } 385 386 fs.addForm(newForm); 387 } 388 } 389 } 390 391 for (Iterator i = hFormSets.values().iterator(); i.hasNext();) { 393 List formsets = (List ) i.next(); 394 Iterator formsetsIterator = formsets.iterator(); 395 while (formsetsIterator.hasNext()) { 396 FormSet fs = (FormSet) formsetsIterator.next(); 397 398 if (!fs.isProcessed()) { 399 fs.process(hConstants); 400 } 401 } 402 } 403 } 404 405 411 protected Field getClosestLocaleField(FormSet fs, String formKey, 412 String fieldKey) { 413 414 Field field = null; 415 String language = fs.getLanguage(); 416 String country = fs.getCountry(); 417 String variant = fs.getVariant(); 418 419 if (!GenericValidator.isBlankOrNull(language) 420 && !GenericValidator.isBlankOrNull(country) 421 && !GenericValidator.isBlankOrNull(variant)) { 422 423 Form form = this.getForm(language, country, variant, formKey); 424 field = form.getField(fieldKey); 425 } 426 427 if (field == null) { 428 if (!GenericValidator.isBlankOrNull(language) 429 && !GenericValidator.isBlankOrNull(country)) { 430 431 Form form = this.getForm(language, country, null, formKey); 432 field = form.getField(fieldKey); 433 } 434 } 435 436 if (field == null) { 437 if (!GenericValidator.isBlankOrNull(language)) { 438 Form form = this.getForm(language, null, null, formKey); 439 field = form.getField(fieldKey); 440 } 441 } 442 443 if (field == null) { 444 Form form = this.getForm(defaultLocale, formKey); 445 field = form.getField(fieldKey); 446 } 447 448 return field; 449 } 450 451 455 protected Map getFormSets() { 456 return hFormSets; 457 } 458 459 463 protected Map getConstants() { 464 return hConstants; 465 } 466 467 471 protected Map getActions() { 472 return hActions; 473 } 474 475 } 476 | Popular Tags |