KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.validator.examples;
2
3 import org.sapia.validator.*;
4 import org.sapia.validator.rules.*;
5
6 import java.util.*;
7
8 /**
9  * @author Yanick Duchesne
10  * <dl>
11  * <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>
12  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
13  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
14  * </dl>
15  */

16 public class MinSizeEg {
17   /**
18    * Constructor for MinSizeEg.
19    */

20   public MinSizeEg() {
21     super();
22   }
23
24   public static void main(String JavaDoc[] args) {
25     RuleSet rules = new RuleSet();
26
27     rules.setId("checkMinEmployees");
28
29     MinSize m = new MinSize();
30
31     m.setSize(2);
32     m.setId("minEmployees");
33     m.setAttribute("employees");
34     rules.addValidatable(m);
35
36     Vlad c = new Vlad();
37     ErrorMessage msg = m.createMessage();
38
39     msg.setValue("Company should have at least 2 employees");
40     c.addRuleSet(rules);
41
42     Vlad v = new Vlad();
43     Company comp = new Company("ACME");
44
45     comp.addEmployee(new Employee("Foo"));
46
47     Status st = v.validate("checkMinEmployees", comp,
48         Locale.getDefault());
49     List errs = st.getErrors();
50     ValidationErr err;
51
52     for (int i = 0; i < errs.size(); i++) {
53       err = (ValidationErr) errs.get(i);
54
55       if (err.isThrowable()) {
56         System.out.println("id :" + err.getId());
57         err.getThrowable().printStackTrace();
58       } else {
59         System.out.println("id :" + err.getId());
60         System.out.println(err.getMsg());
61       }
62     }
63   }
64 }
65
Popular Tags