KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > examples > PositiveRule


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 JavaDoc;
10
11 import java.util.List JavaDoc;
12
13 /**
14  * @author Yanick Duchesne
15  * <dl>
16  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
17  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
18  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
19  * </dl>
20  */

21 public class PositiveRule extends Rule {
22   /**
23    * Constructor for PositiveRule.
24    */

25   public PositiveRule() {
26     super();
27   }
28
29   /**
30    * @see org.sapia.validator.Rule#validate(ValidationContext)
31    */

32   public void validate(ValidationContext context) {
33     Integer JavaDoc intg = (Integer JavaDoc) context.get();
34
35     if (intg.intValue() < 0) {
36       context.getStatus().error(this);
37     }
38   }
39
40   public static void main(String JavaDoc[] args) {
41     try {
42       Vlad v = new Vlad().loadDefs(
43           "org/sapia/validator/examples/positivedefs.xml")
44                          .load(new File JavaDoc("positive.xml")).load(new File JavaDoc(
45             "vlad.xml"));
46       Status s = v.validate("checkPositive", new Integer JavaDoc(-1),
47           java.util.Locale.getDefault());
48
49       if (s.isError()) {
50         List JavaDoc 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 JavaDoc e) {
66       e.printStackTrace();
67     }
68   }
69 }
70
Popular Tags