KickJava   Java API By Example, From Geeks To Geeks.

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


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: Not.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 something is NOT valid. We do this
29  * by validating another validator; if it does NOT generate a ValidationException
30  * then we throw an exception because it should have. If it does throw a
31  * ValidationException, then all is well. This validator effectively acts as
32  * a "NOT" or "!" on another FormValidator.
33  *
34  * @author Christian Cryder [christianc@granitepeaks.com]
35  * @author Bill Wallace <Bill_Wallace@elementk.com>
36  * @author Diez B. Roggisch <diez.roggisch@artnology.com>
37  * @author Jacob Kjome <hoju@visi.com>
38  * @version %I%, %G%
39  * @since 1.0
40  */

41 public class Not extends AbstractFormValidator {
42
43     protected FormValidator fv = null;
44
45     /**
46      * Public constructor.
47      *
48      * @param ifv the validator we wish to make sure is not valid
49      */

50     public Not(FormValidator ifv) {
51         this(ifv, null);
52     }
53
54     /**
55      * Public constructor.
56      *
57      * @param ifv the validator we wish to make sure is not valid
58      * @param ierrmsg the message associated with this error
59      */

60     public Not(FormValidator ifv, String JavaDoc ierrmsg) {
61         setErrorMessage(ierrmsg);
62         fv = ifv;
63     }
64
65     /**
66      * Return the value that is being not'ed
67      *
68      * @return The form element to negate
69      */

70     public FormValidator getSubValidator() {
71        return fv;
72     }
73
74     /**
75      * Rather than calling all the sub validate methods, we instead validate
76      * the validator we contain--because this validator is effectively doing
77      * a "NOT", we expect to receive a ValidationException; if we dont, then
78      * we will throw a ValidationException
79      *
80      * @param element the form element to be validated (null indicates
81      * we want to perform form level validation)
82      * @param map the map to which the element belongs (sometimes necessary
83      * to validate elements by comparing them with other elements)
84      * @param deferExceptions do we want to deferValidation exceptions
85      * and attempt to validate all elements so that we can process
86      * all the exceptions at once
87      * @throws ValidationException if the element is not valid
88      */

89     public void validate(FormElement element, FormMap map, boolean deferExceptions) throws ValidationException {
90         if (localLogger.isDebugEnabled()) localLogger.debug("Making sure "+fv+" is not valid");
91
92         boolean throwEx = true;
93
94         //if fv==true, no error occurs, since a NOT of a null would be considered true...
95
if (fv!=null) try {
96             //validate the internal validator
97
fv.validate(element, map, deferExceptions);
98         } catch (ValidationException ve) {
99             if (localLogger.isDebugEnabled()) localLogger.debug(fv+" is not valid, so we continue.");
100             throwEx = false;
101             //ok to continue
102
}
103
104         if (throwEx) {
105             //uh-oh; we didn't get an exception
106
throw this.generateException(element, deferExceptions, "Validator "+fv+" was valid (and it shouldn't have been!)");
107         }
108     }
109
110 }
111
Popular Tags