1 package org.sapia.validator.rules; 2 3 import org.apache.commons.beanutils.PropertyUtils; 4 import org.sapia.util.xml.confix.ConfigurationException; 5 import org.sapia.util.xml.confix.ObjectHandlerIF; 6 import org.sapia.validator.*; 7 8 19 public class Select extends Rule implements ObjectHandlerIF { 20 private String _attribute; 21 private Validatable _validatable; 22 23 26 public Select() { 27 super(); 28 } 29 30 36 public void setAttribute(String attr) { 37 _attribute = attr; 38 } 39 40 43 public void handleObject(String anElementName, Object anObject) 44 throws ConfigurationException { 45 if (_validatable != null) { 46 throw new IllegalArgumentException ( 47 qualifiedName() + " rule can only take one rule/ruleset"); 48 } 49 50 if (!(anObject instanceof Validatable)) { 51 throw new IllegalArgumentException ( 52 qualifiedName() + " rule only takes a validatable object"); 53 } 54 55 _validatable = (Validatable) anObject; 56 } 57 58 61 public void validate(ValidationContext ctx) { 62 Object item; 63 64 if (_attribute == null) { 65 item = ctx.get(); 66 67 if (item == null) { 68 throw new IllegalStateException ("No object on validation context stack at " + qualifiedName()); 69 } 70 } else { 71 try { 72 if (ctx.get() == null) { 73 throw new IllegalStateException ( 74 "No object on validation context stack at " + qualifiedName()); 75 } 76 77 item = PropertyUtils.getProperty(ctx.get(), _attribute); 78 79 if (item == null) { 80 throw new IllegalStateException ("Attribute " + _attribute 81 + " evaluates to null at " + qualifiedName()); 82 } 83 } catch (Throwable err) { 84 ctx.getStatus().error(this, err); 85 86 return; 87 } 88 } 89 90 if (_validatable != null) { 91 ctx.push(item); 92 _validatable.validate(ctx); 93 ctx.pop(); 94 } 95 } 96 } 97 | Popular Tags |