KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > Or


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: Or.java,v 1.14 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms;
21
22 import java.util.*;
23
24 import org.enhydra.barracuda.core.forms.*;
25 import org.enhydra.barracuda.plankton.*;
26
27 /**
28  * This validator ensures that at least one of the given validators is valid,
29  * (effectively acting as an "OR" or "||") or a ValidationException will
30  * be generated
31  * <p>
32  * The sub-validators can be specified as array, list or by using the convenience constructors.
33  */

34 public class Or extends AbstractFormValidator {
35
36     protected FormValidator _fv[];
37
38     /**
39      * Public constructor.
40      *
41      * @param fv the list of validators
42      */

43     public Or(List fv) {
44         this(fv, null);
45     }
46
47     /**
48      * Public constructor.
49      *
50      * @param fv the list of validators
51      * @param ierrmsg the message associated with this error
52      */

53     public Or(List fv, String JavaDoc ierrmsg) {
54         this((FormValidator[])fv.toArray(new FormValidator[1]), ierrmsg);
55     }
56
57     /**
58      * Public constructor.
59      *
60      * @param fv the array of validators
61      */

62     public Or(FormValidator [] fv) {
63         this(fv, null);
64     }
65
66
67     /**
68      * Public constructor.
69      *
70      * @param fv the array of validators
71      * @param ierrmsg the message associated with this error
72      */

73     public Or(FormValidator [] fv, String JavaDoc ierrmsg) {
74         setErrorMessage(ierrmsg);
75         _fv = fv;
76     }
77
78     /**
79      * Public constructor.
80      *
81      * @param fv1 the first subvalidator
82      * @param fv2 the second subvalidator
83      */

84     public Or(FormValidator fv1, FormValidator fv2) {
85         this(new FormValidator[] {fv1, fv2}, null);
86     }
87
88     /**
89      * Public constructor.
90      *
91      * @param fv1 the first subvalidator
92      * @param fv2 the second subvalidator
93      * @param ierrmsg the message associated with this error
94      */

95     public Or(FormValidator fv1, FormValidator fv2, String JavaDoc ierrmsg) {
96         this(new FormValidator[] {fv1, fv2}, ierrmsg);
97     }
98
99     /**
100      * Public constructor.
101      *
102      * @param fv1 the first subvalidator
103      * @param fv2 the second subvalidator
104      * @param fv3 the third subvalidator
105      */

106     public Or(FormValidator fv1, FormValidator fv2, FormValidator fv3) {
107         this(new FormValidator[] {fv1, fv2, fv3}, null);
108     }
109
110     /**
111      * Public constructor.
112      *
113      * @param fv1 the first subvalidator
114      * @param fv2 the second subvalidator
115      * @param fv3 the third subvalidator
116      * @param ierrmsg the message associated with this error
117      */

118     public Or(FormValidator fv1, FormValidator fv2, FormValidator fv3, String JavaDoc ierrmsg) {
119         this(new FormValidator[] {fv1, fv2, fv3}, ierrmsg);
120     }
121
122
123     /**
124      * Public constructor.
125      *
126      * @param fv1 the first subvalidator
127      * @param fv2 the second subvalidator
128      * @param fv3 the third subvalidator
129      * @param fv4 the fourth subvalidator
130      */

131     public Or(FormValidator fv1, FormValidator fv2, FormValidator fv3, FormValidator fv4) {
132         this(new FormValidator[] {fv1, fv2, fv3, fv4}, null);
133     }
134
135     /**
136      * Public constructor.
137      *
138      * @param fv1 the first subvalidator
139      * @param fv2 the second subvalidator
140      * @param fv3 the third subvalidator
141      * @param fv4 the fourth subvalidator
142      * @param ierrmsg the message associated with this error
143      */

144     public Or(FormValidator fv1, FormValidator fv2, FormValidator fv3, FormValidator fv4, String JavaDoc ierrmsg) {
145         this(new FormValidator[] {fv1, fv2, fv3, fv4}, ierrmsg);
146     }
147
148
149
150
151     //bw_102501.1 - added
152
/**
153      * Return the sub-validators that are or'ed together
154      * Do not assume this array is of length 2, in order to allow for future
155      * use. Also, do not change the returned array, as it maybe cached
156      * in the future.
157      *
158      * @return Sub-validators as an array
159      */

160     public FormValidator[] getSubValidators() {
161         return _fv;
162     }
163
164     /**
165      * Rather than calling all the sub validate methods, we make sure
166      * at least one of the sub-validators is valid
167      *
168      * @param element the form element to be validated (null indicates
169      * we want to perform form level validation)
170      * @param map the map to which the element belongs (sometimes necessary
171      * to validate elements by comparing them with other elements)
172      * @param deferExceptions do we want to deferValidation exceptions
173      * and attempt to validate all elements so that we can process
174      * all the exceptions at once
175      * @throws ValidationException if the element is not valid
176      */

177     public void validate(FormElement element, FormMap map, boolean deferExceptions) throws ValidationException {
178         if (localLogger.isDebugEnabled()) {
179             localLogger.debug("Making sure validators" + _fv + " are valid");
180         }
181
182         ValidationException veNew = null;
183         int exCount = 0;
184         for(int i = 0; i < _fv.length; i++) {
185             try {
186                 if (_fv[i]!=null) {
187                     _fv[i].validate(element, map, deferExceptions);
188                     // The first valid subvalidator will leave the loop
189
break;
190                 } else {
191                     veNew = this.generateException(element, deferExceptions, "A given Validator was null");
192                     exCount = _fv.length;
193                     break;
194                 }
195             } catch (ValidationException ve) {
196                 if (veNew==null) {
197                     veNew = this.generateException(element, deferExceptions, "Validators were invalid");
198                 }
199                 veNew.addSubException(ve);
200                 exCount++;
201             }
202         }
203         // If and only if all validators have failed, we throw an exceptio too
204
if (exCount==_fv.length) {
205             throw veNew;
206         }
207     }
208 }
209
Popular Tags