KickJava   Java API By Example, From Geeks To Geeks.

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


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: AbstractFormValidator.java,v 1.15 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 import org.apache.log4j.*;
27
28 /**
29  * The root implementation of the FormValidator interface
30  */

31 public abstract class AbstractFormValidator implements FormValidator {
32
33     protected static final Logger localLogger = Logger.getLogger(AbstractFormValidator.class.getName());
34
35     private String JavaDoc myErrMsg = null;
36
37     /**
38      * Set the error message to be used in the event of an error.
39      *
40      * @param ierrmsg the error message to be used in the event
41      * of an error
42      */

43     public void setErrorMessage(String JavaDoc ierrmsg) {
44         this.myErrMsg = ierrmsg;
45     }
46
47     /**
48      * Get the error message to be used in the event of an error.
49      *
50      * @return the error message to be used in the event
51      * of an error
52      */

53     public String JavaDoc getErrorMessage() {
54         return this.myErrMsg;
55     }
56
57     /**
58      * This is a simple convenience method to return the appropriate type
59      * of exception based on whether we're deferring or not.
60      *
61      * @param source the source of the exception
62      * @param deferExceptions
63      * true if we want to generate a DeferredValidationException
64      * @param defaultMsg the error message to use if no error message specified in
65      * the constructor of the validator
66      * @return a validation exception
67      */

68     public ValidationException generateException(Object JavaDoc source, boolean deferExceptions, String JavaDoc defaultMsg) {
69         if (localLogger.isDebugEnabled()) localLogger.debug("Generating exception... defer="+deferExceptions);
70         String JavaDoc errmsg = (null==this.getErrorMessage()) ? defaultMsg : this.getErrorMessage();
71         if (deferExceptions) return new DeferredValidationException(source, errmsg);
72         else return new ValidationException(source, errmsg);
73     }
74
75     // ilc_02212002 start
76
// added new method to check for nullness
77
/**
78      * Check if val passed is null in a consistant manner.
79      * Most validators should call isNull(val) first thing
80      * and return if that is the case. Leave null validation
81      * up to NotNullValidator.
82      *
83      * @param val the value to test for nullness
84      */

85     public boolean isNull(Object JavaDoc val, FormElement element){
86       return ((val==null) || (val.toString().trim().length() < 1));
87     }
88     // ilc_02212001 End
89

90 }
91
Popular Tags