2
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/validator/Resources.java,v 1.28 2004/03/14 06:23:47 sraeburn Exp $
4
* $Date: 2004/03/14 06:23:47 $
6
* Copyright 2000-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.struts.validator;
23
import java.util.Locale;
25
import javax.servlet.ServletContext;
26
import javax.servlet.http.HttpServletRequest;
28
import org.apache.commons.validator.Arg;
29
import org.apache.commons.validator.Field;
30
import org.apache.commons.validator.Validator;
31
import org.apache.commons.validator.ValidatorAction;
32
import org.apache.commons.validator.ValidatorResources;
33
import org.apache.struts.Globals;
34
import org.apache.struts.action.ActionError;
35
import org.apache.struts.action.ActionMessage;
36
import org.apache.struts.action.ActionMessages;
37
import org.apache.struts.util.MessageResources;
38
import org.apache.struts.util.ModuleUtils;
39
import org.apache.struts.util.RequestUtils;
42
* This class helps provides some useful methods for retrieving objects
43
* from different scopes of the application.
45
* @version $Revision: 1.28 $ $Date: 2004/03/14 06:23:47 $
48
public class Resources {
51
* Resources key the <code>ServletContext</code> is stored under.
53
private static String SERVLET_CONTEXT_PARAM = "javax.servlet.ServletContext";
56
* Resources key the <code>ServletContext</code> is stored under.
57
* @deprecated This will be removed after Struts 1.2
59
public static String SERVLET_CONTEXT_KEY = SERVLET_CONTEXT_PARAM;
62
* Resources key the <code>HttpServletRequest</code> is stored under.
64
private static String HTTP_SERVLET_REQUEST_PARAM =
65
"javax.servlet.http.HttpServletRequest";
68
* Resources key the <code>HttpServletRequest</code> is stored under.
69
* @deprecated This will be removed after Struts 1.2
71
public static String HTTP_SERVLET_REQUEST_KEY = HTTP_SERVLET_REQUEST_PARAM;
74
* Resources key the <code>ActionMessages</code> is stored under.
76
private static String ACTION_MESSAGES_PARAM =
77
"org.apache.struts.action.ActionMessages";
80
* Resources key the <code>ActionErrors</code> is stored under.
81
* @deprecated This will be removed after Struts 1.2
83
public static String ACTION_ERRORS_KEY = ACTION_MESSAGES_PARAM;
86
* Retrieve <code>ValidatorResources</code> for the current module.
87
* @param application Application Context
88
* @param request The ServletRequest
90
public static ValidatorResources getValidatorResources(
91
ServletContext application,
92
HttpServletRequest request) {
97
.getModuleConfig(request, application)
100
return (ValidatorResources) application.getAttribute(
101
ValidatorPlugIn.VALIDATOR_KEY + prefix);
105
* Retrieve <code>MessageResources</code> for the module.
106
* @param request the servlet request
108
public static MessageResources getMessageResources(HttpServletRequest request) {
109
return (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);
113
* Get the <code>Locale</code> of the current user.
114
* @param request servlet request
115
* @deprecated Use RequestUtils.getUserLocale() instead. This will be removed
118
public static Locale getLocale(HttpServletRequest request) {
119
return RequestUtils.getUserLocale(request, null);
123
* Gets the <code>Locale</code> sensitive value based on the key passed in.
124
* @param messages The Message resources
125
* @param locale The locale.
126
* @param key Key used to lookup the message
128
public static String getMessage(
129
MessageResources messages,
132
String message = null;
134
if (messages != null) {
135
message = messages.getMessage(locale, key);
138
return (message == null) ? "" : message;
142
* Gets the <code>Locale</code> sensitive value based on the key passed in.
143
* @param request servlet request
144
* @param key the request key
146
public static String getMessage(HttpServletRequest request, String key) {
147
MessageResources messages = getMessageResources(request);
149
return getMessage(messages, RequestUtils.getUserLocale(request, null), key);
153
* Gets the locale sensitive message based on the
154
* <code>ValidatorAction</code> message and the <code>Field</code>'s
156
* @param messages The Message resources
157
* @param locale The locale
158
* @param va The Validator Action
159
* @param field The Validator Field
161
public static String getMessage(
162
MessageResources messages,
167
String args[] = getArgs(va.getName(), messages, locale, field);
169
field.getMsg(va.getName()) != null
170
? field.getMsg(va.getName())
173
return messages.getMessage(locale, msg, args);
177
* Gets the <code>ActionError</code> based on the
178
* <code>ValidatorAction</code> message and the <code>Field</code>'s
180
* @param request the servlet request
181
* @param va Validator action
182
* @param field the validator Field
183
* @deprecated Use getActionMessage() instead. This will be removed after
186
public static ActionError getActionError(
187
HttpServletRequest request,
194
getMessageResources(request),
195
RequestUtils.getUserLocale(request, null),
199
field.getMsg(va.getName()) != null
200
? field.getMsg(va.getName())
203
return new ActionError(msg, args);
207
* Gets the <code>ActionMessage</code> based on the
208
* <code>ValidatorAction</code> message and the <code>Field</code>'s
210
* @param request the servlet request
211
* @param va Validator action
212
* @param field the validator Field
214
public static ActionMessage getActionMessage(
215
HttpServletRequest request,
222
getMessageResources(request),
223
RequestUtils.getUserLocale(request, null),
227
field.getMsg(va.getName()) != null
228
? field.getMsg(va.getName())
231
return new ActionMessage(msg, args);
235
* Gets the message arguments based on the current
236
* <code>ValidatorAction</code> and <code>Field</code>.
237
* @param actionName action name
238
* @param messages message resources
239
* @param locale the locale
240
* @param field the validator field
242
public static String[] getArgs(
244
MessageResources messages,
248
String[] argMessages = new String[4];
252
field.getArg(actionName,0),
253
field.getArg(actionName,1),
254
field.getArg(actionName,2),
255
field.getArg(actionName,3)};
257
for (int i = 0; i < args.length; i++) {
258
if (args[i] == null) {
262
if (args[i].isResource()) {
263
argMessages[i] = getMessage(messages, locale, args[i].getKey());
265
argMessages[i] = args[i].getKey();
274
* Initialize the <code>Validator</code> to perform validation.
276
* @param key The key that the validation rules are under (the form elements
278
* @param bean The bean validation is being performed on.
279
* @param application servlet context
280
* @param request The current request object.
281
* @param errors The object any errors will be stored in.
282
* @param page This in conjunction with the page property of a
283
* <code>Field<code> can control the processing of fields. If the field's
284
* page is less than or equal to this page value, it will be processed.
286
public static Validator initValidator(
289
ServletContext application,
290
HttpServletRequest request,
291
ActionMessages errors,
294
ValidatorResources resources =
295
Resources.getValidatorResources(application, request);
297
Locale locale = RequestUtils.getUserLocale(request, null);
299
Validator validator = new Validator(resources, key);
300
validator.setUseContextClassLoader(true);
302
validator.setPage(page);
304
validator.setParameter(SERVLET_CONTEXT_PARAM, application);
305
validator.setParameter(HTTP_SERVLET_REQUEST_PARAM, request);
306
validator.setParameter(Validator.LOCALE_PARAM, locale);
307
validator.setParameter(ACTION_MESSAGES_PARAM, errors);
308
validator.setParameter(Validator.BEAN_PARAM, bean);