KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Map JavaDoc;
12
13
14 /**
15  * <p>
16  * A String type validator that ensures that the object is a
17  * String or can be converted to a String and also optionally
18  * checks the length of the String using the <code>min</code>
19  * and <code>max</code> parameters.
20  * </p>
21  *
22  * @author Brian Pontarelli
23  */

24 public class StringTypeValidator extends BaseTypeValidator {
25
26     /**
27      * The default error message
28      */

29     public static final String JavaDoc DEFAULT_MESSAGE = "Invalid String";
30
31
32     /**
33      * Does the validation work on the String.
34      *
35      * @param value The Object value that must be a String or convertable
36      * @param params The parameters to get the min and max if they exist
37      * @param message The error message if the validation fails
38      * @param locale The locale to use when generating the error message
39      * @param mesgParams Message params to use in formating the message
40      * @return The message if the validation fails
41      * @throws NumberFormatException If the params min and/or max are not numbers
42      * or String representations of numbers
43      */

44     protected String JavaDoc internalValidate(Object JavaDoc value, Object JavaDoc params,
45             String JavaDoc message, Locale JavaDoc locale, Object JavaDoc[] mesgParams) {
46         if (value == null) {
47             return null;
48         }
49
50         String JavaDoc str = null;
51         boolean valid = true;
52         int length = 0;
53         try {
54             str = value.toString();
55             length = (str == null) ? 0 : str.length();
56         } catch (Exception JavaDoc e) {
57             valid = false;
58         }
59
60         if (valid && length > 0 && params != null) {
61             Object JavaDoc min = ((Map JavaDoc) params).get("min");
62             Object JavaDoc max = ((Map JavaDoc) params).get("max");
63             if (min != null) {
64                 if (!(min instanceof Number JavaDoc)) {
65                     try {
66                         min = Double.valueOf(min.toString());
67                     } catch (NumberFormatException JavaDoc nfe) {
68                         throw new NumberFormatException JavaDoc("Invalid min value: " +
69                             min);
70                     }
71                 }
72
73                 if (min != null) {
74                     if (length < (((Number JavaDoc) min).doubleValue())) {
75                         valid = false;
76                     }
77                 }
78             }
79
80             if (max != null) {
81                 if (!(max instanceof Number JavaDoc)) {
82                     try {
83                         max = Double.valueOf(max.toString());
84                     } catch (NumberFormatException JavaDoc nfe) {
85                         throw new NumberFormatException JavaDoc("Invalid max value: " +
86                             max);
87                     }
88                 }
89
90                 if (max != null) {
91                     if (length > (((Number JavaDoc) max).doubleValue())) {
92                         valid = false;
93                     }
94                 }
95             }
96         }
97
98         String JavaDoc error = null;
99         if (!valid) {
100             error = getErrorMessage(message, DEFAULT_MESSAGE, mesgParams);
101         }
102
103         return error;
104     }
105 }
Popular Tags