KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > CompositeRule


1 package org.sapia.validator;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.sapia.util.xml.confix.ConfigurationException;
7 import org.sapia.util.xml.confix.ObjectHandlerIF;
8
9 /**
10  * An instance of this class is intended to hold other validation rules.
11  *
12  * @author Yanick Duchesne
13  * <dl>
14  * <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>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class CompositeRule extends Rule implements ObjectHandlerIF {
20   private List JavaDoc _rules = new ArrayList JavaDoc();
21   protected boolean _stop = true;
22
23   /**
24    * Constructor for CompositeRule.
25    */

26   public CompositeRule() {
27   }
28
29   /**
30    * Adds a <code>Validatable</code> to this instance.
31    *
32    * @param a <code>Validatable</code>.
33    */

34   public void addValidatable(Validatable v) {
35     _rules.add(v);
36   }
37
38   /**
39    * Sets the "stop" flag to <code>true</code> or <code>false</code>. This
40    * flag meaning is as follows: when an instance of this class processes
41    * its nested rules, it will continue with the next rule even if the
42    * previous one generated an error, provided this flag is set to
43    * <code>false</code>.
44    * <p>
45    * This feature allows for subsequent rules to be processed even if
46    * validation errors have previously been created; thus, in such a case,
47    * the <code>ValidationContext</code> could potentially contain more
48    * than one instance of <code>ValidationErr</code>.
49    *
50    * @param stop this instance's "stop" flag.
51    * @see ValidationContext
52    * @see ValidationErr
53    */

54   public void setStop(boolean stop) {
55     _stop = stop;
56   }
57
58   /**
59    * Creates a rule set and returns it.
60    *
61    * @return a <code>RuleSet</code>.
62    */

63   public RuleSet createRuleSet() {
64     RuleSet rs = new RuleSet();
65
66     _rules.add(rs);
67
68     return rs;
69   }
70
71   /**
72    * @see org.sapia.util.xml.confix.ObjectHandlerIF#handleObject(String, Object)
73    */

74   public void handleObject(String JavaDoc name, Object JavaDoc validatable)
75     throws ConfigurationException {
76     if (validatable instanceof Validatable) {
77       addValidatable((Validatable) validatable);
78     } else {
79       throw new ConfigurationException("Unexpected element: " + name + " at " + qualifiedName());
80     }
81   }
82
83   /**
84    * @see org.sapia.validator.Rule#validate(ValidationContext)
85    */

86   public void validate(ValidationContext ctx) {
87     Validatable current;
88     boolean valid = true;
89
90     for (int i = 0; i < _rules.size(); i++) {
91       current = (Validatable) _rules.get(i);
92
93       current.validate(ctx);
94
95       if (ctx.getStatus().isError() && _stop) {
96         break;
97       }
98     }
99   }
100 }
101
Popular Tags