1 package org.sapia.validator.examples; 2 3 import org.sapia.validator.Rule; 4 import org.sapia.validator.Status; 5 import org.sapia.validator.ValidationContext; 6 import org.sapia.validator.ValidationErr; 7 import org.sapia.validator.Vlad; 8 9 import java.io.File ; 10 11 import java.util.List ; 12 13 21 public class PositiveRule extends Rule { 22 25 public PositiveRule() { 26 super(); 27 } 28 29 32 public void validate(ValidationContext context) { 33 Integer intg = (Integer ) context.get(); 34 35 if (intg.intValue() < 0) { 36 context.getStatus().error(this); 37 } 38 } 39 40 public static void main(String [] args) { 41 try { 42 Vlad v = new Vlad().loadDefs( 43 "org/sapia/validator/examples/positivedefs.xml") 44 .load(new File ("positive.xml")).load(new File ( 45 "vlad.xml")); 46 Status s = v.validate("checkPositive", new Integer (-1), 47 java.util.Locale.getDefault()); 48 49 if (s.isError()) { 50 List errs = s.getErrors(); 51 ValidationErr err; 52 53 for (int i = 0; i < errs.size(); i++) { 54 err = (ValidationErr) errs.get(i); 55 56 if (err.isThrowable()) { 57 err.getThrowable().printStackTrace(); 58 } else { 59 System.out.println(err.getMsg()); 60 } 61 } 62 } else { 63 System.out.println("No validation error."); 64 } 65 } catch (Exception e) { 66 e.printStackTrace(); 67 } 68 } 69 } 70 | Popular Tags |