KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > validator > Resources


1 /*
2  * $Id: Resources.java 164530 2005-04-25 03:11:07Z niallp $
3  *
4  * Copyright 2000-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.validator;
20
21 import java.util.Locale JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25
26 import org.apache.commons.validator.Arg;
27 import org.apache.commons.validator.Field;
28 import org.apache.commons.validator.Msg;
29 import org.apache.commons.validator.Validator;
30 import org.apache.commons.validator.ValidatorAction;
31 import org.apache.commons.validator.ValidatorResources;
32 import org.apache.struts.Globals;
33 import org.apache.struts.action.ActionError;
34 import org.apache.struts.action.ActionMessage;
35 import org.apache.struts.action.ActionMessages;
36 import org.apache.struts.util.MessageResources;
37 import org.apache.struts.util.ModuleUtils;
38 import org.apache.struts.util.RequestUtils;
39 import org.apache.struts.config.ModuleConfig;
40
41 /**
42  * This class helps provides some useful methods for retrieving objects
43  * from different scopes of the application.
44  *
45  * @version $Rev: 164530 $ $Date: 2005-04-25 04:11:07 +0100 (Mon, 25 Apr 2005) $
46  * @since Struts 1.1
47  */

48 public class Resources {
49
50     /**
51      * Resources key the <code>ServletContext</code> is stored under.
52      */

53     private static String JavaDoc SERVLET_CONTEXT_PARAM = "javax.servlet.ServletContext";
54
55     /**
56      * Resources key the <code>ServletContext</code> is stored under.
57      * @deprecated This will be removed after Struts 1.2
58      */

59     public static String JavaDoc SERVLET_CONTEXT_KEY = SERVLET_CONTEXT_PARAM;
60
61     /**
62      * Resources key the <code>HttpServletRequest</code> is stored under.
63      */

64     private static String JavaDoc HTTP_SERVLET_REQUEST_PARAM =
65         "javax.servlet.http.HttpServletRequest";
66
67     /**
68      * Resources key the <code>HttpServletRequest</code> is stored under.
69      * @deprecated This will be removed after Struts 1.2
70      */

71     public static String JavaDoc HTTP_SERVLET_REQUEST_KEY = HTTP_SERVLET_REQUEST_PARAM;
72
73     /**
74      * Resources key the <code>ActionMessages</code> is stored under.
75      */

76     private static String JavaDoc ACTION_MESSAGES_PARAM =
77         "org.apache.struts.action.ActionMessages";
78
79     /**
80      * Resources key the <code>ActionErrors</code> is stored under.
81      * @deprecated This will be removed after Struts 1.2
82      */

83     public static String JavaDoc ACTION_ERRORS_KEY = ACTION_MESSAGES_PARAM;
84
85     /**
86      * Retrieve <code>ValidatorResources</code> for the current module.
87      * @param application Application Context
88      * @param request The ServletRequest
89      */

90     public static ValidatorResources getValidatorResources(
91         ServletContext JavaDoc application,
92         HttpServletRequest JavaDoc request) {
93
94         String JavaDoc prefix =
95             ModuleUtils
96                 .getInstance()
97                 .getModuleConfig(request, application)
98                 .getPrefix();
99
100         return (ValidatorResources) application.getAttribute(
101             ValidatorPlugIn.VALIDATOR_KEY + prefix);
102     }
103
104     /**
105      * Retrieve <code>MessageResources</code> for the module.
106      * @param request the servlet request
107      */

108     public static MessageResources getMessageResources(HttpServletRequest JavaDoc request) {
109         return (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);
110     }
111
112     /**
113      * Retrieve <code>MessageResources</code> for the module and bundle.
114      * @param application the servlet context
115      * @param request the servlet request
116      * @param bundle the bundle key
117      */

118     public static MessageResources getMessageResources(
119         ServletContext JavaDoc application,
120         HttpServletRequest JavaDoc request,
121         String JavaDoc bundle) {
122
123         if (bundle == null) {
124             bundle = Globals.MESSAGES_KEY;
125         }
126
127         MessageResources resources = (MessageResources)request.getAttribute(bundle);
128
129         if (resources == null) {
130             ModuleConfig moduleConfig = ModuleUtils.getInstance()
131                                          .getModuleConfig(request, application);
132             resources = (MessageResources)application.getAttribute(bundle + moduleConfig.getPrefix());
133         }
134
135         if (resources == null) {
136             resources = (MessageResources)application.getAttribute(bundle);
137         }
138
139         if (resources == null) {
140             throw new NullPointerException JavaDoc("No message resources found for bundle: " + bundle);
141         }
142
143         return resources;
144
145     }
146
147     /**
148      * Get the <code>Locale</code> of the current user.
149      * @param request servlet request
150      * @deprecated Use RequestUtils.getUserLocale() instead. This will be removed
151      * after Struts 1.2.
152      */

153     public static Locale JavaDoc getLocale(HttpServletRequest JavaDoc request) {
154         return RequestUtils.getUserLocale(request, null);
155     }
156
157     /**
158      * Gets the <code>Locale</code> sensitive value based on the key passed in.
159      * @param messages The Message resources
160      * @param locale The locale.
161      * @param key Key used to lookup the message
162      */

163     public static String JavaDoc getMessage(
164         MessageResources messages,
165         Locale JavaDoc locale,
166         String JavaDoc key) {
167         String JavaDoc message = null;
168
169         if (messages != null) {
170             message = messages.getMessage(locale, key);
171         }
172
173         return (message == null) ? "" : message;
174     }
175
176     /**
177      * Gets the <code>Locale</code> sensitive value based on the key passed in.
178      * @param request servlet request
179      * @param key the request key
180      */

181     public static String JavaDoc getMessage(HttpServletRequest JavaDoc request, String JavaDoc key) {
182         MessageResources messages = getMessageResources(request);
183
184         return getMessage(messages, RequestUtils.getUserLocale(request, null), key);
185     }
186
187     /**
188      * Gets the locale sensitive message based on the
189      * <code>ValidatorAction</code> message and the <code>Field</code>'s
190      * arg objects.
191      * @param messages The Message resources
192      * @param locale The locale
193      * @param va The Validator Action
194      * @param field The Validator Field
195      */

196     public static String JavaDoc getMessage(
197         MessageResources messages,
198         Locale JavaDoc locale,
199         ValidatorAction va,
200         Field field) {
201
202         String JavaDoc args[] = getArgs(va.getName(), messages, locale, field);
203         String JavaDoc msg =
204             field.getMsg(va.getName()) != null
205                 ? field.getMsg(va.getName())
206                 : va.getMsg();
207
208         return messages.getMessage(locale, msg, args);
209     }
210     /**
211      * Gets the <code>Locale</code> sensitive value based on the key passed in.
212      * @param application the servlet context
213      * @param request the servlet request
214      * @param defaultMessages The default Message resources
215      * @param locale The locale
216      * @param va The Validator Action
217      * @param field The Validator Field
218      */

219     public static String JavaDoc getMessage(ServletContext JavaDoc application,
220                                     HttpServletRequest JavaDoc request,
221                                     MessageResources defaultMessages,
222                                     Locale JavaDoc locale,
223                                     ValidatorAction va,
224                                     Field field) {
225
226         Msg msg = field.getMessage(va.getName());
227         if (msg != null && !msg.isResource()) {
228             return msg.getKey();
229         }
230
231         String JavaDoc msgKey = null;
232         String JavaDoc msgBundle = null;
233         MessageResources messages = defaultMessages;
234         if (msg == null) {
235            msgKey = va.getMsg();
236         } else {
237            msgKey = msg.getKey();
238            msgBundle = msg.getBundle();
239            if (msg.getBundle() != null) {
240                messages = getMessageResources(application, request, msg.getBundle());
241            }
242         }
243
244         if (msgKey == null || msgKey.length() == 0) {
245             return "??? " + va.getName() + "." + field.getProperty() + " ???";
246         }
247
248         // Get the arguments
249
Arg[] args = field.getArgs(va.getName());
250         String JavaDoc[] argValues = getArgValues(application, request, messages, locale, args);
251
252         // Return the message
253
return messages.getMessage(locale, msgKey, argValues);
254
255     }
256
257     /**
258      * Gets the <code>ActionError</code> based on the
259      * <code>ValidatorAction</code> message and the <code>Field</code>'s
260      * arg objects.
261      * @param request the servlet request
262      * @param va Validator action
263      * @param field the validator Field
264      * @deprecated Use getActionMessage() instead. This will be removed after
265      * Struts 1.2.
266      */

267     public static ActionError getActionError(
268         HttpServletRequest JavaDoc request,
269         ValidatorAction va,
270         Field field) {
271
272         String JavaDoc args[] =
273             getArgs(
274                 va.getName(),
275                 getMessageResources(request),
276                 RequestUtils.getUserLocale(request, null),
277                 field);
278
279         String JavaDoc msg =
280             field.getMsg(va.getName()) != null
281                 ? field.getMsg(va.getName())
282                 : va.getMsg();
283
284         return new ActionError(msg, args);
285     }
286     
287     /**
288      * Gets the <code>ActionMessage</code> based on the
289      * <code>ValidatorAction</code> message and the <code>Field</code>'s
290      * arg objects.
291      * @param request the servlet request
292      * @param va Validator action
293      * @param field the validator Field
294      */

295     public static ActionMessage getActionMessage(
296         HttpServletRequest JavaDoc request,
297         ValidatorAction va,
298         Field field) {
299
300         String JavaDoc args[] =
301             getArgs(
302                 va.getName(),
303                 getMessageResources(request),
304                 RequestUtils.getUserLocale(request, null),
305                 field);
306
307         String JavaDoc msg =
308             field.getMsg(va.getName()) != null
309                 ? field.getMsg(va.getName())
310                 : va.getMsg();
311
312         return new ActionMessage(msg, args);
313     }
314
315     /**
316      * Gets the <code>ActionMessage</code> based on the
317      * <code>ValidatorAction</code> message and the <code>Field</code>'s
318      * arg objects.
319      * @param validator the Validator
320      * @param request the servlet request
321      * @param va Validator action
322      * @param field the validator Field
323      */

324     public static ActionMessage getActionMessage(
325         Validator validator,
326         HttpServletRequest JavaDoc request,
327         ValidatorAction va,
328         Field field) {
329
330         Msg msg = field.getMessage(va.getName());
331         if (msg != null && !msg.isResource()) {
332             return new ActionMessage(msg.getKey(), false);
333         }
334
335         String JavaDoc msgKey = null;
336         String JavaDoc msgBundle = null;
337         if (msg == null) {
338            msgKey = va.getMsg();
339         } else {
340            msgKey = msg.getKey();
341            msgBundle = msg.getBundle();
342         }
343
344         if (msgKey == null || msgKey.length() == 0) {
345             return new ActionMessage("??? " + va.getName() + "." + field.getProperty() + " ???", false);
346         }
347
348         ServletContext JavaDoc application = (ServletContext JavaDoc)validator.getParameterValue(SERVLET_CONTEXT_PARAM);
349         MessageResources messages = getMessageResources(application, request, msgBundle);
350         Locale JavaDoc locale = RequestUtils.getUserLocale(request, null);
351
352         Arg[] args = field.getArgs(va.getName());
353         String JavaDoc[] argValues = getArgValues(application, request, messages, locale, args);
354
355         ActionMessage actionMessage = null;
356         if (msgBundle == null) {
357             actionMessage = new ActionMessage(msgKey, argValues);
358         } else {
359             String JavaDoc message = messages.getMessage(locale, msgKey, argValues);
360             actionMessage = new ActionMessage(message, false);
361         }
362         return actionMessage;
363
364     }
365
366
367     /**
368      * Gets the message arguments based on the current
369      * <code>ValidatorAction</code> and <code>Field</code>.
370      * @param actionName action name
371      * @param messages message resources
372      * @param locale the locale
373      * @param field the validator field
374      */

375     public static String JavaDoc[] getArgs(
376         String JavaDoc actionName,
377         MessageResources messages,
378         Locale JavaDoc locale,
379         Field field) {
380
381         String JavaDoc[] argMessages = new String JavaDoc[4];
382
383         Arg[] args =
384             new Arg[] {
385                 field.getArg(actionName,0),
386                 field.getArg(actionName,1),
387                 field.getArg(actionName,2),
388                 field.getArg(actionName,3)};
389
390         for (int i = 0; i < args.length; i++) {
391             if (args[i] == null) {
392                 continue;
393             }
394
395             if (args[i].isResource()) {
396                 argMessages[i] = getMessage(messages, locale, args[i].getKey());
397             } else {
398                 argMessages[i] = args[i].getKey();
399             }
400
401         }
402
403         return argMessages;
404     }
405
406     /**
407      * Gets the message arguments based on the current
408      * <code>ValidatorAction</code> and <code>Field</code>.
409      * @param application the servlet context
410      * @param request the servlet request
411      * @param defaultMessages Default message resources
412      * @param locale the locale
413      * @param args The arguments for the message
414      */

415     private static String JavaDoc[] getArgValues(
416         ServletContext JavaDoc application,
417         HttpServletRequest JavaDoc request,
418         MessageResources defaultMessages,
419         Locale JavaDoc locale,
420         Arg[] args) {
421
422         if (args == null || args.length == 0) {
423             return null;
424         }
425
426         String JavaDoc[] values = new String JavaDoc[args.length];
427         for (int i = 0; i < args.length; i++) {
428             if (args[i] != null) {
429                 if (args[i].isResource()) {
430
431                     MessageResources messages = defaultMessages;
432                     if (args[i].getBundle() != null) {
433                         messages = getMessageResources(application, request, args[i].getBundle());
434                     }
435                     values[i] = messages.getMessage(locale, args[i].getKey());
436
437                 } else {
438
439                     values[i] = args[i].getKey();
440
441                 }
442             }
443         }
444
445         return values;
446
447     }
448
449     /**
450      * Initialize the <code>Validator</code> to perform validation.
451      *
452      * @param key The key that the validation rules are under (the form elements
453      * name attribute).
454      * @param bean The bean validation is being performed on.
455      * @param application servlet context
456      * @param request The current request object.
457      * @param errors The object any errors will be stored in.
458      * @param page This in conjunction with the page property of a
459      * <code>Field<code> can control the processing of fields. If the field's
460      * page is less than or equal to this page value, it will be processed.
461      */

462     public static Validator initValidator(
463         String JavaDoc key,
464         Object JavaDoc bean,
465         ServletContext JavaDoc application,
466         HttpServletRequest JavaDoc request,
467         ActionMessages errors,
468         int page) {
469
470         ValidatorResources resources =
471             Resources.getValidatorResources(application, request);
472
473         Locale JavaDoc locale = RequestUtils.getUserLocale(request, null);
474
475         Validator validator = new Validator(resources, key);
476         validator.setUseContextClassLoader(true);
477
478         validator.setPage(page);
479
480         validator.setParameter(SERVLET_CONTEXT_PARAM, application);
481         validator.setParameter(HTTP_SERVLET_REQUEST_PARAM, request);
482         validator.setParameter(Validator.LOCALE_PARAM, locale);
483         validator.setParameter(ACTION_MESSAGES_PARAM, errors);
484         validator.setParameter(Validator.BEAN_PARAM, bean);
485
486         return validator;
487     }
488
489 }
490
Popular Tags