1 package org.sapia.validator; 2 3 import org.apache.commons.beanutils.PropertyUtils; 4 5 37 public abstract class BeanRule extends Rule { 38 protected String _attribute; 39 private boolean _throw; 40 41 48 public void setAttribute(String attr) { 49 _attribute = attr; 50 } 51 52 63 protected void throwExceptionOnNull(boolean throwExc) { 64 _throw = throwExc; 65 } 66 67 90 public final void validate(ValidationContext context) { 91 Object toValidate; 92 93 if (_attribute == null) { 94 toValidate = context.get(); 95 96 if ((toValidate == null) && _throw) { 97 throw new IllegalStateException ("No object on validation context stack at " + qualifiedName()); 98 } 99 } else { 100 try { 101 toValidate = PropertyUtils.getProperty(context.get(), _attribute); 102 103 if ((toValidate == null) && _throw) { 104 throw new IllegalStateException ("Attribute " + _attribute 105 + " evaluates to null on rule " + qualifiedName()); 106 } 107 } catch (Throwable err) { 108 context.getStatus().error(this, err); 109 110 return; 111 } 112 } 113 114 if (!doValidate(toValidate)) { 115 context.getStatus().error(this); 116 } 117 } 118 119 131 protected abstract boolean doValidate(Object toValidate); 132 } 133 | Popular Tags |