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.Rule; 7 import org.sapia.validator.Validatable; 8 import org.sapia.validator.ValidationContext; 9 10 25 public class IfDefined extends Rule implements ObjectHandlerIF{ 26 private String _attribute; 27 private Validatable _validatable; 28 29 32 public IfDefined() { 33 } 34 35 41 public void setAttribute(String attr) { 42 _attribute = attr; 43 } 44 45 48 public void handleObject(String anElementName, Object anObject) 49 throws ConfigurationException { 50 if (_validatable != null) { 51 throw new IllegalArgumentException ( 52 qualifiedName() + " rule can only take one rule/ruleset"); 53 } 54 55 if (!(anObject instanceof Validatable)) { 56 throw new IllegalArgumentException ( 57 qualifiedName() + " rule only takes a validatable object"); 58 } 59 60 _validatable = (Validatable) anObject; 61 } 62 63 66 public void validate(ValidationContext ctx) { 67 Object item; 68 69 if (_attribute == null) { 70 item = ctx.get(); 71 } else { 72 try { 73 if (ctx.get() == null) { 74 throw new IllegalStateException ( 75 "No object on validation context stack at " + qualifiedName()); 76 } 77 78 item = PropertyUtils.getProperty(ctx.get(), _attribute); 79 } catch (Throwable err) { 80 ctx.getStatus().error(this, err); 81 82 return; 83 } 84 } 85 86 if ((_validatable != null) && (item != null)) { 87 _validatable.validate(ctx); 88 } 89 } 90 } 91 | Popular Tags |