~ubuntu-branches/ubuntu/trusty/libstruts1.2-java/trusty-proposed

« back to all changes in this revision

Viewing changes to src/share/org/apache/struts/validator/Resources.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 15:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20041119153525-mdu08a76z4zo67xt
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.28 $
 
4
 * $Date: 2004/03/14 06:23:47 $
 
5
 *
 
6
 * Copyright 2000-2004 The Apache Software Foundation.
 
7
 * 
 
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
 
11
 * 
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
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.
 
19
 */
 
20
 
 
21
package org.apache.struts.validator;
 
22
 
 
23
import java.util.Locale;
 
24
 
 
25
import javax.servlet.ServletContext;
 
26
import javax.servlet.http.HttpServletRequest;
 
27
 
 
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;
 
40
 
 
41
/**
 
42
 * This class helps provides some useful methods for retrieving objects
 
43
 * from different scopes of the application.
 
44
 *
 
45
 * @version $Revision: 1.28 $ $Date: 2004/03/14 06:23:47 $
 
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 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 SERVLET_CONTEXT_KEY = SERVLET_CONTEXT_PARAM;
 
60
 
 
61
    /**
 
62
     * Resources key the <code>HttpServletRequest</code> is stored under.
 
63
     */
 
64
    private static String 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 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 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 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 application,
 
92
        HttpServletRequest request) {
 
93
 
 
94
        String 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 request) {
 
109
        return (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);
 
110
    }
 
111
 
 
112
    /**
 
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
 
116
     * after Struts 1.2.
 
117
     */
 
118
    public static Locale getLocale(HttpServletRequest request) {
 
119
        return RequestUtils.getUserLocale(request, null);
 
120
    }
 
121
 
 
122
    /**
 
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
 
127
     */
 
128
    public static String getMessage(
 
129
        MessageResources messages,
 
130
        Locale locale,
 
131
        String key) {
 
132
        String message = null;
 
133
 
 
134
        if (messages != null) {
 
135
            message = messages.getMessage(locale, key);
 
136
        }
 
137
 
 
138
        return (message == null) ? "" : message;
 
139
    }
 
140
 
 
141
    /**
 
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
 
145
     */
 
146
    public static String getMessage(HttpServletRequest request, String key) {
 
147
        MessageResources messages = getMessageResources(request);
 
148
 
 
149
        return getMessage(messages, RequestUtils.getUserLocale(request, null), key);
 
150
    }
 
151
 
 
152
    /**
 
153
     * Gets the locale sensitive message based on the 
 
154
     * <code>ValidatorAction</code> message and the <code>Field</code>'s 
 
155
     * arg objects.
 
156
     * @param messages  The Message resources
 
157
     * @param locale The locale
 
158
     * @param va The Validator Action
 
159
     * @param field The Validator Field
 
160
     */
 
161
    public static String getMessage(
 
162
        MessageResources messages,
 
163
        Locale locale,
 
164
        ValidatorAction va,
 
165
        Field field) {
 
166
 
 
167
        String args[] = getArgs(va.getName(), messages, locale, field);
 
168
        String msg =
 
169
            field.getMsg(va.getName()) != null
 
170
                ? field.getMsg(va.getName())
 
171
                : va.getMsg();
 
172
 
 
173
        return messages.getMessage(locale, msg, args);
 
174
    }
 
175
 
 
176
    /**
 
177
     * Gets the <code>ActionError</code> based on the 
 
178
     * <code>ValidatorAction</code> message and the <code>Field</code>'s 
 
179
     * arg objects.
 
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
 
184
     * Struts 1.2.
 
185
     */
 
186
    public static ActionError getActionError(
 
187
        HttpServletRequest request,
 
188
        ValidatorAction va,
 
189
        Field field) {
 
190
 
 
191
        String args[] =
 
192
            getArgs(
 
193
                va.getName(),
 
194
                getMessageResources(request),
 
195
                RequestUtils.getUserLocale(request, null),
 
196
                field);
 
197
 
 
198
        String msg =
 
199
            field.getMsg(va.getName()) != null
 
200
                ? field.getMsg(va.getName())
 
201
                : va.getMsg();
 
202
 
 
203
        return new ActionError(msg, args);
 
204
    }
 
205
    
 
206
    /**
 
207
     * Gets the <code>ActionMessage</code> based on the 
 
208
     * <code>ValidatorAction</code> message and the <code>Field</code>'s 
 
209
     * arg objects.
 
210
     * @param request the servlet request
 
211
     * @param va Validator action
 
212
     * @param field the validator Field
 
213
     */
 
214
    public static ActionMessage getActionMessage(
 
215
        HttpServletRequest request,
 
216
        ValidatorAction va,
 
217
        Field field) {
 
218
 
 
219
        String args[] =
 
220
            getArgs(
 
221
                va.getName(),
 
222
                getMessageResources(request),
 
223
                RequestUtils.getUserLocale(request, null),
 
224
                field);
 
225
 
 
226
        String msg =
 
227
            field.getMsg(va.getName()) != null
 
228
                ? field.getMsg(va.getName())
 
229
                : va.getMsg();
 
230
 
 
231
        return new ActionMessage(msg, args);
 
232
    }
 
233
 
 
234
    /**
 
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
 
241
     */
 
242
    public static String[] getArgs(
 
243
        String actionName,
 
244
        MessageResources messages,
 
245
        Locale locale,
 
246
        Field field) {
 
247
 
 
248
        String[] argMessages = new String[4];
 
249
 
 
250
        Arg[] args =
 
251
            new Arg[] {
 
252
                field.getArg(actionName,0),
 
253
                field.getArg(actionName,1),
 
254
                field.getArg(actionName,2),
 
255
                field.getArg(actionName,3)};
 
256
 
 
257
        for (int i = 0; i < args.length; i++) {
 
258
            if (args[i] == null) {
 
259
                continue;
 
260
            }
 
261
 
 
262
            if (args[i].isResource()) {
 
263
                argMessages[i] = getMessage(messages, locale, args[i].getKey());
 
264
            } else {
 
265
                argMessages[i] = args[i].getKey();
 
266
            }
 
267
 
 
268
        }
 
269
 
 
270
        return argMessages;
 
271
    }
 
272
 
 
273
    /**
 
274
     * Initialize the <code>Validator</code> to perform validation.
 
275
     *
 
276
     * @param key The key that the validation rules are under (the form elements 
 
277
     * name attribute).
 
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.
 
285
     */
 
286
    public static Validator initValidator(
 
287
        String key,
 
288
        Object bean,
 
289
        ServletContext application,
 
290
        HttpServletRequest request,
 
291
        ActionMessages errors,
 
292
        int page) {
 
293
 
 
294
        ValidatorResources resources =
 
295
            Resources.getValidatorResources(application, request);
 
296
 
 
297
        Locale locale = RequestUtils.getUserLocale(request, null);
 
298
 
 
299
        Validator validator = new Validator(resources, key);
 
300
        validator.setUseContextClassLoader(true);
 
301
 
 
302
        validator.setPage(page);
 
303
 
 
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);
 
309
 
 
310
        return validator;
 
311
    }
 
312
 
 
313
}