KickJava   Java API By Example, From Geeks To Geeks.

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


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: And.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 all of the given validators are valid,
29  * (effectively acting as an "AND" 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 And extends AbstractFormValidator {
35
36     protected FormValidator _fv[];
37
38     /**
39      * Public constructor.
40      *
41      * @param fv the list of validators
42      */

43     public And(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 And(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 And(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 And(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 And(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 And(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 And(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 And(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 And(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 And(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
152     //bw_102501.1 - added
153
/**
154      * Return the sub-validators that are or'ed together
155      * Do not assume this array is of length 2, in order to allow for future
156      * use. Also, do not change the returned array, as it maybe cached
157      * in the future.
158      *
159      * @return Sub-validators as an array
160      */

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

178     public void validate(FormElement element, FormMap map, boolean deferExceptions) throws ValidationException {
179         if (localLogger.isDebugEnabled()) {
180             localLogger.debug("Making sure validators" + _fv + " are valid");
181         }
182         
183         try {
184             for(int i = 0; i < _fv.length; i++) {
185                 if (_fv[i]!=null) {
186                     _fv[i].validate(element, map, deferExceptions);
187                 } else {
188                     throw this.generateException(element, deferExceptions, "A given Validator was null");
189                 }
190             }
191         } catch (ValidationException ve) {
192             ValidationException veNew = this.generateException(element, deferExceptions, "Validators were invalid");
193             veNew.addSubException(ve);
194             throw veNew;
195         }
196     }
197 }
198
Popular Tags