KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > forms > validators > EqualsValidator


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: EqualsValidator.java,v 1.13 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.forms.validators;
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 the original value constitues a
29  * valid type
30  */

31 public class EqualsValidator extends DefaultFormValidator {
32
33     protected Object JavaDoc obj = null;
34
35     /**
36      * Public no-args constructor.
37      */

38     public EqualsValidator() {
39         this(null, null);
40     }
41
42     /**
43      * Public constructor.
44      *
45      * @param iobj the object with which we wish to compare equality
46      */

47     public EqualsValidator(Object JavaDoc iobj) {
48         this(iobj, null);
49     }
50
51     /**
52      * Public constructor.
53      *
54      * @param iobj the object with which we wish to compare equality
55      * @param ierrmsg the message associated with this error
56      */

57     public EqualsValidator(Object JavaDoc iobj, String JavaDoc ierrmsg) {
58         super(ierrmsg);
59         obj = iobj;
60     }
61
62     //bw_102501.1 - added
63
/**
64      * Return the value that this object must equal.
65      */

66     public Object JavaDoc getObject() {
67        return obj;
68     }
69
70     /**
71      * Validate a FormElement to make see if the element equals() a
72      * given object
73      *
74      * @param val the value to compare the element value to
75      * @param element the form element that contains the val
76      * to validate elements by comparing them with other elements)
77      * @param deferExceptions do we want to deferValidation exceptions
78      * and attempt to validate all elements so that we can process
79      * all the exceptions at once
80      * @throws ValidationException if the element is not valid
81      */

82     public void validateFormElement(Object JavaDoc val, FormElement element, boolean deferExceptions) throws ValidationException {
83         if (localLogger.isInfoEnabled()) localLogger.info("validating to see if val {"+val+"} .equals() {"+obj+"}");
84         // cannot validate a null value, use NotNullvalidator if you want to disallow nulls
85
// ilc_022502.1 start
86
if (isNull(val, element))
87           return;
88
89         if (val==obj) return;
90
91         if (element.getParseException()==null) {
92           if (element.getVal().equals(obj))
93             return;
94           else
95             throw this.generateException(element, deferExceptions, "Value {"+val+"} is not equal to {"+obj+"}");
96         }
97         else
98           throw this.generateException(element, deferExceptions, "Unable to parse: " + element.getParseException().getMessage());
99         // ilc_022502.1 end
100

101
102         // ilc_031902.2 start
103
/* use above
104         if ((val==null && obj!=null) || (val!=null && !val.equals(obj))) {
105             throw this.generateException(element, deferExceptions, "Value {"+val+"} is not equal to {"+obj+"}");
106         }
107         */

108         // ilc_031902.2 end;
109
}
110
111 }
112
Popular Tags