KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.validator;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 /**
7  * Models a validation status. An instance of this class is encapsulated
8  * by a <code>ValidationContext</code>. It holds the current validation
9  * errors (if any).
10  * <p>
11  * From within the <code>Validate</code> method of a <code>Rule</code>, validation
12  * errors must be signaled through one of this class' <code>error()</code>
13  * methods, as in the following:
14  * <pre>
15  * // 'this' is the current Rule instance.
16  * ...
17  * public void validate(ValidationContext ctx){
18  * ctx.getStatus().error(this);
19  * }
20  * ...
21  * </pre>
22  *
23  * @author Yanick Duchesne
24  * <dl>
25  * <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>
26  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
27  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
28  * </dl>
29  */

30 public class Status {
31   private ValidationContext _ctx;
32   private List JavaDoc _validationErrs = new ArrayList JavaDoc();
33
34   /**
35    * Constructor for Status.
36    */

37   public Status(ValidationContext ctx) {
38     _ctx = ctx;
39   }
40   
41   /**
42    * Registers a validation error for the given rule.
43    *
44    * @param r a <code>Rule</code>.
45    * @see ValidationErr
46    */

47   public void error(Rule r) {
48     String JavaDoc msg = r.getErrorMessageFor(_ctx.getLocale());
49     String JavaDoc id = r.getId();
50     addError(new ValidationErr(id, msg));
51   }
52   
53   /**
54    * Registers an error for the given <code>Rule</code>. Internally
55    * creates a <code>ValidationException</code> that is assigned
56    * to a <code>ValidationErr</code> that is added to this instance's list
57    * of <code>ValidationErr</code>s.
58    *
59    * @see ValidationErr
60    */

61   public void error(Rule r, String JavaDoc msg) {
62     String JavaDoc id = r.getId();
63     addError(new ValidationErr(id, new ValidationException(msg, id)));
64   }
65   
66   /**
67    * Registers an error for the given <code>Rule</code>. The <code>Throwable</code>
68    * instance is is assigned to a <code>ValidationErr</code> that is added to this instance's list
69    * of <code>ValidationErr</code>s.
70    *
71    * @see ValidationErr
72    */

73   public void error(Rule r, Throwable JavaDoc err) {
74     String JavaDoc id = r.getId();
75     addError(new ValidationErr(id, err));
76   }
77   
78   /**
79    * Returns this instance's list of validation errors.
80    *
81    * @return a <code>List</code> of <code>ValidationErr</code>.
82    */

83   public List JavaDoc getErrors() {
84     return _validationErrs;
85   }
86   
87   /**
88    * Returns <code>true</code> if this instance contains one or more
89    * validation errors.
90    *
91    * @return <code>true</code> if this instance contains one or more
92    * validation errors.
93    */

94   public boolean isError() {
95     return _validationErrs.size() > 0;
96   }
97   
98   /**
99    * @param err a <code>ValidationErr</code>
100    */

101   public void addError(ValidationErr err){
102     _validationErrs.add(err);
103   }
104   
105   /**
106    * @param errs a <code>List</code> of <code>ValidationErr</code>s.
107    */

108   public void addErrors(List JavaDoc errs){
109     for(int i = 0; i < errs.size(); i++){
110       _validationErrs.add((ValidationErr)errs.get(i));
111     }
112   }
113   
114   /**
115    * Returns the validation errors that this instance holds, and whose
116    * identifier starts with the given one. The returned error objects are at
117    * the same time removed from this instance.
118    *
119    * @return a <code>List</code> of <code>ValidationErr</code>s.
120    */

121   public List JavaDoc removeErrorsFor(String JavaDoc id){
122     List JavaDoc toReturn = new ArrayList JavaDoc();
123     for(int i = 0; i < _validationErrs.size(); i++){
124       ValidationErr err = (ValidationErr)_validationErrs.get(i);
125       if(err.getId() != null && err.getId().startsWith(id)){
126         toReturn.add(err);
127         _validationErrs.remove(i--);
128       }
129     }
130     return toReturn;
131   }
132   
133   /**
134    * Returns the validation errors that this instance holds. The returned
135    * error objects are at the same time removed from this instance.
136    *
137    * @return a <code>List</code> of <code>ValidationErr</code>s.
138    */

139   public List JavaDoc removeErrors(){
140     List JavaDoc lst = new ArrayList JavaDoc(_validationErrs);
141     _validationErrs.clear();
142     return lst;
143   }
144
145   /**
146    * Returns the validation errors that do not have an ID defined.
147    * The returned error objects are at the same time removed from this instance.
148    *
149    * @return a <code>List</code> of <code>ValidationErr</code>s.
150    */

151   public List JavaDoc removeAnonymousErrors(){
152     List JavaDoc toReturn = new ArrayList JavaDoc();
153     for(int i = 0; i < _validationErrs.size(); i++){
154       ValidationErr err = (ValidationErr)_validationErrs.get(i);
155       if(err.getId() == null){
156         toReturn.add(err);
157         _validationErrs.remove(i--);
158       }
159     }
160     return toReturn;
161   }
162 }
163
Popular Tags