KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > typevalidator > RequiredTypeValidator


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.typevalidator;
8
9
10 import java.util.Locale JavaDoc;
11
12 import com.inversoft.util.StringTools;
13
14
15 /**
16  * This class ensures that a value is not empty or null and
17  * can be used to ensure that something required was specified.
18  *
19  * @author Brian Pontarelli
20  */

21 public class RequiredTypeValidator extends BaseTypeValidator {
22
23     /**
24      * The default error message for the required validator
25      */

26     public static final String JavaDoc DEFAULT_MESSAGE = "Missing required value";
27
28
29     /**
30      * Validates the required value is not null or empty.
31      *
32      * @param value The value to validate
33      * @param params (Optional) A Map of parameters
34      * @param message (Optional) The error message to return
35      * @param locale Not used
36      * @param mesgParams Parameters to the message (if any are needed)
37      * @return The error message
38      */

39     protected String JavaDoc internalValidate(Object JavaDoc value, Object JavaDoc params, String JavaDoc message,
40             Locale JavaDoc locale, Object JavaDoc[] mesgParams) {
41
42         boolean valid = true;
43         if (value == null) {
44             valid = false;
45         } else {
46             String JavaDoc str = value.toString();
47             valid = !StringTools.isEmpty(str);
48         }
49
50         String JavaDoc error = null;
51         if (!valid) {
52             error = getErrorMessage(message, DEFAULT_MESSAGE, mesgParams);
53         }
54
55         return error;
56     }
57 }
58
Popular Tags