1 package org.sapia.validator; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileNotFoundException ; 6 import java.io.IOException ; 7 import java.io.InputStream ; 8 import java.util.Collections ; 9 import java.util.HashMap ; 10 import java.util.Locale ; 11 import java.util.Map ; 12 13 import org.sapia.util.xml.ProcessingException; 14 import org.sapia.util.xml.confix.ConfigurationException; 15 import org.sapia.util.xml.confix.Dom4jProcessor; 16 import org.sapia.util.xml.confix.ObjectHandlerIF; 17 import org.sapia.validator.config.ConfigException; 18 import org.sapia.validator.config.Def; 19 import org.sapia.validator.config.VladObjectFactory; 20 21 43 public class Vlad implements ObjectHandlerIF { 44 public static final String VLAD_RULES_XML = "vlad_rules.xml"; 45 private Map _rules = new HashMap (); 46 private Map _ruleSets = new HashMap (); 47 private VladObjectFactory _factory; 48 private Map _globals = new HashMap (); 49 50 53 public Vlad() { 54 _factory = new VladObjectFactory(this); 55 56 InputStream is = loadResource(VLAD_RULES_XML); 57 58 if (is != null) { 59 Dom4jProcessor p = new Dom4jProcessor(_factory); 60 61 try { 62 p.process(is); 63 } catch (ProcessingException e) { 64 e.printStackTrace(); 65 } 66 } 67 } 68 69 82 public synchronized void addGlobal(String name, Object value){ 83 Map globals = new HashMap (_globals); 84 globals.put(name, value); 85 _globals = globals; 86 } 87 88 92 public synchronized void addGlobals(Map values){ 93 Map globals = new HashMap (_globals); 94 globals.putAll(values); 95 _globals = globals; 96 } 97 98 103 public Object getGlobal(String name){ 104 return _globals.get(name); 105 } 106 107 111 public Map getGlobals(){ 112 return Collections.unmodifiableMap(_globals); 113 } 114 115 116 121 public void addRuleSet(RuleSet set) { 122 _ruleSets.put(set.getId(), set); 123 } 124 125 134 public RuleSet getRuleSet(String id) 135 throws IllegalArgumentException { 136 RuleSet set = (RuleSet) _ruleSets.get(id); 137 138 if (set == null) { 139 throw new IllegalArgumentException ("No rule set for: " + id); 140 } 141 142 return set; 143 } 144 145 150 public void add(Rule rule) { 151 if (rule.getId() == null) { 152 throw new IllegalArgumentException ("rule " + rule.getClass().getName() 153 + " does not have an ID"); 154 } 155 156 _rules.put(rule.getId(), rule); 157 } 158 159 164 public void addNamespace(Namespace ns) { 165 if (ns.getPrefix() == null) { 166 throw new IllegalArgumentException ( 167 "Attribute 'prefix' not defined on 'namepsace' element"); 168 } 169 170 Def d; 171 172 for (int i = 0; i < ns.getRuleDefs().size(); i++) { 173 d = (Def) ns.getRuleDefs().get(i); 174 _factory.registerDef(ns.getPrefix(), d.getName(), d); 175 } 176 } 177 178 186 public Rule getRule(String id) 187 throws IllegalArgumentException { 188 Rule rule = (Rule) _rules.get(id); 189 190 if (rule == null) { 191 throw new IllegalArgumentException ("No rule for: " + id); 192 } 193 194 return rule; 195 } 196 197 209 public Status validate(String ruleSetId, Object obj, Locale locale) { 210 RuleSet set = getRuleSet(ruleSetId); 211 ValidationContext ctx = new ValidationContext(_globals, obj, this, locale); 212 213 set.validate(ctx); 214 215 return ctx.getStatus(); 216 } 217 218 232 public Status validate(String ruleSetId, Object obj, Locale locale, Map contextMap) { 233 RuleSet set = getRuleSet(ruleSetId); 234 ValidationContext ctx = new ValidationContext(_globals, obj, this, locale, contextMap); 235 236 set.validate(ctx); 237 238 return ctx.getStatus(); 239 } 240 250 public Vlad load(String resource) 251 throws IOException , ConfigException { 252 InputStream is = loadResource(resource); 253 254 if (is == null) { 255 throw new FileNotFoundException (resource); 256 } 257 258 return load(is); 259 } 260 261 270 public Vlad load(File f) throws IOException , ConfigException { 271 return load(new FileInputStream (f)); 272 } 273 274 283 public Vlad load(InputStream is) 284 throws IOException , ConfigException { 285 Dom4jProcessor p = new Dom4jProcessor(_factory); 286 287 try { 288 p.process(is); 289 290 return this; 291 } catch (ProcessingException e) { 292 throw new ConfigException("Could not process configuration", e); 293 } 294 } 295 296 305 public Vlad loadDefs(File f) 306 throws IOException , ConfigException { 307 return loadDefs(new FileInputStream (f)); 308 } 309 310 311 321 322 public Vlad loadDefs(String resource) 323 throws IOException , ConfigException { 324 InputStream is = loadResource(resource); 325 326 if (is == null) { 327 throw new FileNotFoundException (resource); 328 } 329 330 return loadDefs(is); 331 } 332 333 342 public Vlad loadDefs(InputStream is) 343 throws IOException , ConfigException { 344 if (is != null) { 345 Dom4jProcessor p = new Dom4jProcessor(_factory); 346 347 try { 348 p.process(is); 349 } catch (ProcessingException e) { 350 throw new ConfigException("Could not load definitions", e); 351 } 352 } 353 354 return this; 355 } 356 357 360 public void handleObject(String name, Object ruleOrRuleSet) 361 throws ConfigurationException { 362 if (ruleOrRuleSet instanceof Rule) { 363 add((Rule) ruleOrRuleSet); 364 } else if (ruleOrRuleSet instanceof RuleSet) { 365 addRuleSet((RuleSet) ruleOrRuleSet); 366 } else { 367 throw new ConfigurationException("Unexpected element: " + name); 368 } 369 } 370 371 private InputStream loadResource(String name) { 372 373 InputStream is = Thread.currentThread().getContextClassLoader() 374 .getResourceAsStream(name); 375 376 if (is == null) { 377 is = getClass().getClassLoader().getResourceAsStream(name); 378 } 379 380 if (is == null) { 381 is = Vlad.class.getResourceAsStream(name); 382 } 383 384 if (is == null) { 385 is = ClassLoader.getSystemResourceAsStream(name); 386 } 387 388 return is; 389 } 390 } 391 | Popular Tags |