KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.MessageFormat JavaDoc;
11 import java.util.Locale JavaDoc;
12
13 import com.inversoft.beans.BeanException;
14 import com.inversoft.beans.NestedBeanProperty;
15 import com.inversoft.error.BasicError;
16 import com.inversoft.error.ErrorRegistry;
17 import com.inversoft.error.PropertyError;
18
19
20 /**
21  * <p>
22  * This class
23  * </p>
24  *
25  * @author bpontarelli
26  * @version 2.0
27  * @since 2.0
28  */

29 public abstract class BaseTypeValidator implements TypeValidator {
30
31     /**
32      * Constructs a new <code>BaseTypeValidator</code>
33      */

34     public BaseTypeValidator() {
35         // Empty
36
}
37
38
39     /**
40      * Validates the value given (which is either a String or converted to a
41      * String using the toString method).
42      *
43      * @param value The value to validate
44      * @param params (Optional) An Object that can be used to pass parameters
45      * to a validator that can help determine how to validation is to
46      * take place
47      * @param message (Optional) The error message to use for failed validation.
48      * If this is null, a default message should be supplied by
49      * implementations of this interface
50      * @param mesgParams (Optional) An array of Objects that is passed to the
51      * java.text.MessageFormat class to format the message passed in.
52      * If this is null, no formatting takes place
53      * @return The result of the validation or null if the validation was
54      * successfull
55      */

56     public BasicError validate(Object JavaDoc value, Object JavaDoc params, String JavaDoc message,
57             Object JavaDoc [] mesgParams) {
58         String JavaDoc error = internalValidate(value, params, message, null, mesgParams);
59         BasicError be = null;
60         if (error != null) {
61             be = new BasicError(error);
62         }
63
64         return be;
65     }
66
67     /**
68      * Validates the value given (which is either a String or converted to a
69      * String using the toString method).
70      *
71      * @param value The value to validate
72      * @param params (Optional) An Object that can be used to pass parameters
73      * to a validator that can help determine how to validation is to
74      * take place
75      * @param bundleName (Optional) The name of the resource bundle that the
76      * error is stored in. If this or key are null, a default error
77      * message should be used by implemenations of this interface
78      * @param key (Optional) The key of the error mesage in the bundle
79      * @param locale (Optional) The Locale of the bundle. If null, the default
80      * Locale is used
81      * @param mesgParams (Optional) An array of Objects that is passed to the
82      * java.text.MessageFormat class to format the message passed in.
83      * If this is null, no formatting takes place
84      * @param caller (Optional) The caller object, which is passed to the
85      * ErrorRegistry and prepended to the key when looking up the error
86      * from the bundle. If this is null, then only the key is used
87      * @return The result of the validation or null if the validation was
88      * successfull
89      */

90     public BasicError validate(Object JavaDoc value, Object JavaDoc params, String JavaDoc bundleName,
91             String JavaDoc key, Locale JavaDoc locale, Object JavaDoc [] mesgParams, Object JavaDoc caller) {
92         String JavaDoc error = ErrorRegistry.getError(bundleName, key, locale, caller);
93         error = internalValidate(value, params, error, null, mesgParams);
94
95         BasicError be = null;
96         if (error != null) {
97             be = new BasicError(error);
98         }
99
100         return be;
101     }
102
103     /**
104      * Validates the value given (which is either a String or converted to a
105      * String using the toString method).
106      *
107      * @param propertyName The name of the property to use when creating the
108      * PropertyError object, if necessary
109      * @param value The value to validate
110      * @param params (Optional) An Object that can be used to pass parameters
111      * to a validator that can help determine how to validation is to
112      * take place
113      * @param message (Optional) The error message to use for failed validation.
114      * If this is null, a default message should be supplied by
115      * implementations of this interface
116      * @param mesgParams (Optional) An array of Objects that is passed to the
117      * java.text.MessageFormat class to format the message passed in.
118      * If this is null, no formatting takes place
119      * @return The result of the validation or null if the validation was
120      * successfull
121      */

122     public PropertyError validate(String JavaDoc propertyName, Object JavaDoc value, Object JavaDoc params,
123             String JavaDoc message, Object JavaDoc [] mesgParams) {
124         String JavaDoc error = internalValidate(value, params, message, null, mesgParams);
125         PropertyError pe = null;
126         if (error != null) {
127             pe = new PropertyError(propertyName, error);
128         }
129
130         return pe;
131     }
132
133     /**
134      * Validates the value given (which is either a String or converted to a
135      * String using the toString method).
136      *
137      * @param propertyName The name of the property to use when creating the
138      * PropertyError object, if necessary
139      * @param value The value to validate
140      * @param params (Optional) An Object that can be used to pass parameters
141      * to a validator that can help determine how to validation is to
142      * take place
143      * @param bundleName (Optional) The name of the resource bundle that the
144      * error is stored in. If this or key are null, a default error
145      * message should be used by implemenations of this interface
146      * @param key (Optional) The key of the error mesage in the bundle
147      * @param locale (Optional) The Locale of the bundle. If null, the default
148      * Locale is used
149      * @param mesgParams (Optional) An array of Objects that is passed to the
150      * java.text.MessageFormat class to format the message passed in.
151      * If this is null, no formatting takes place
152      * @param caller (Optional) The caller object, which is passed to the
153      * ErrorRegistry and prepended to the key when looking up the error
154      * from the bundle. If this is null, then only the key is used
155      * @return The result of the validation or null if the validation was
156      * successfull
157      */

158     public PropertyError validate(String JavaDoc propertyName, Object JavaDoc value, Object JavaDoc params,
159             String JavaDoc bundleName, String JavaDoc key, Locale JavaDoc locale, Object JavaDoc [] mesgParams,
160             Object JavaDoc caller) {
161         String JavaDoc error = ErrorRegistry.getError(bundleName, key, locale, caller);
162         error = internalValidate(value, params, error, locale, mesgParams);
163
164         PropertyError pe = null;
165         if (error != null) {
166             pe = new PropertyError(propertyName, error);
167         }
168
169         return pe;
170     }
171
172     /**
173      * Validates the value given (which is either a String or converted to a
174      * String using the toString method).
175      *
176      * @param propertyName The name of the property to retrieve the value to be
177      * validated
178      * @param bean The JavaBean instance to retrieve the value from
179      * @param params (Optional) An Object that can be used to pass parameters
180      * to a validator that can help determine how to validation is to
181      * take place
182      * @param message (Optional) The error message to use for failed validation.
183      * If this is null, a default message should be supplied by
184      * implementations of this interface
185      * @param mesgParams (Optional) An array of Objects that is passed to the
186      * java.text.MessageFormat class to format the message passed in.
187      * If this is null, no formatting takes place
188      * @return The result of the validation or null if the validation was
189      * successfull
190      * @throws BeanException If the propertyName is invalid or not a property on
191      * the given bean Object
192      */

193     public PropertyError fetchAndValidate(String JavaDoc propertyName, Object JavaDoc bean, Object JavaDoc params,
194             String JavaDoc message, Object JavaDoc [] mesgParams)
195     throws BeanException {
196         NestedBeanProperty nbp = new NestedBeanProperty(propertyName, bean.getClass());
197         String JavaDoc error = internalValidate(nbp.getPropertyValue(bean), params,
198             message, null, mesgParams);
199         PropertyError pe = null;
200         if (error != null) {
201             pe = new PropertyError(propertyName, error);
202         }
203
204         return pe;
205     }
206
207     /**
208      * Validates the value given (which is either a String or converted to a
209      * String using the toString method).
210      *
211      * @param propertyName The name of the property to retrieve the value to be
212      * validated
213      * @param bean The JavaBean instance to retrieve the value from
214      * @param params (Optional) An Object that can be used to pass parameters
215      * to a validator that can help determine how to validation is to
216      * take place
217      * @param bundleName (Optional) The name of the resource bundle that the
218      * error is stored in. If this or key are null, a default error
219      * message should be used by implemenations of this interface
220      * @param key (Optional) The key of the error mesage in the bundle
221      * @param locale (Optional) The Locale of the bundle. If null, the default
222      * Locale is used
223      * @param mesgParams (Optional) An array of Objects that is passed to the
224      * java.text.MessageFormat class to format the message passed in.
225      * If this is null, no formatting takes place
226      * @param caller (Optional) The caller object, which is passed to the
227      * ErrorRegistry and prepended to the key when looking up the error
228      * from the bundle. If this is null, then only the key is used
229      * @return The result of the validation or null if the validation was
230      * successfull
231      * @throws BeanException If the propertyName is invalid or not a property on
232      * the given bean Object
233      */

234     public PropertyError fetchAndValidate(String JavaDoc propertyName, Object JavaDoc bean, Object JavaDoc params,
235             String JavaDoc bundleName, String JavaDoc key, Locale JavaDoc locale, Object JavaDoc [] mesgParams,
236             Object JavaDoc caller)
237     throws BeanException {
238         NestedBeanProperty nbp = new NestedBeanProperty(propertyName, bean.getClass());
239         String JavaDoc error = ErrorRegistry.getError(bundleName, key, locale, caller);
240         error = internalValidate(nbp.getPropertyValue(bean), params, error,
241             locale, mesgParams);
242
243         PropertyError pe = null;
244         if (error != null) {
245             pe = new PropertyError(propertyName, error);
246         }
247
248         return pe;
249     }
250
251     /**
252      * Does the work of validating the type. Subclasses must implement this method
253      * to do validation. All values passed in are values from the validate methods
254      * except the message. For those validate methods that use resource bundles,
255      * this is the resolved error from the resource bundle. This value could be
256      * null.
257      *
258      * @param value The value to validate
259      * @param params (Optional) An Object that can be used to pass parameters
260      * to a validator that can help determine how to validation is to
261      * take place
262      * @param message (Optional) The error message to use for failed validation.
263      * If this is null, a default message should be supplied by
264      * implementations of this interface
265      * @param locale (Optional) The Locale of the bundle. If null, the default
266      * Locale is used
267      * @param mesgParams (Optional) An array of Objects that is passed to the
268      * java.text.MessageFormat class to format the message passed in.
269      * If this is null, no formatting takes place
270      * @return The error message supplied and formatted (if mesgParams is not
271      * null) or null if the validation was successfull
272      */

273     protected abstract String JavaDoc internalValidate(Object JavaDoc value, Object JavaDoc params,
274             String JavaDoc message, Locale JavaDoc locale, Object JavaDoc [] mesgParams);
275
276     /**
277      * Returns the error message. This uses the message parameter and optionally
278      * formats this message. If this parameter is null, it uses the defaultMesg
279      * parameter and optionally formats this message.
280      *
281      * @param message The error to use
282      * @param defaultMesg The default to use if message is null
283      * @param mesgParams Parameters used for formatting the message
284      * @return The error message and null only if message and defaultMesg are null
285      */

286     protected String JavaDoc getErrorMessage(String JavaDoc message, String JavaDoc defaultMesg,
287             Object JavaDoc [] mesgParams) {
288         String JavaDoc error = message;
289         if (error == null) {
290             error = defaultMesg;
291         }
292
293         // Optionally format the message
294
if (mesgParams != null && error != null) {
295             error = MessageFormat.format(error, mesgParams);
296         }
297
298         return error;
299     }
300 }
Popular Tags