Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/struts/validator/Resources.java


1   /*
2    * $Id: Resources.java 54929 2004-10-16 16:38:42Z germuska $ 
3    *
4    * Copyright 2000-2004 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;
22  
23  import javax.servlet.ServletContext;
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.apache.commons.validator.Arg;
27  import org.apache.commons.validator.Field;
28  import org.apache.commons.validator.Validator;
29  import org.apache.commons.validator.ValidatorAction;
30  import org.apache.commons.validator.ValidatorResources;
31  import org.apache.struts.Globals;
32  import org.apache.struts.action.ActionError;
33  import org.apache.struts.action.ActionMessage;
34  import org.apache.struts.action.ActionMessages;
35  import org.apache.struts.util.MessageResources;
36  import org.apache.struts.util.ModuleUtils;
37  import org.apache.struts.util.RequestUtils;
38  
39  /**
40   * This class helps provides some useful methods for retrieving objects
41   * from different scopes of the application.
42   *
43   * @version $Rev: 54929 $ $Date: 2004-10-16 09:38:42 -0700 (Sat, 16 Oct 2004) $
44   * @since Struts 1.1
45   */
46  public class Resources {
47  
48      /**
49       * Resources key the <code>ServletContext</code> is stored under.
50       */
51      private static String SERVLET_CONTEXT_PARAM = "javax.servlet.ServletContext";
52  
53      /**
54       * Resources key the <code>ServletContext</code> is stored under.
55       * @deprecated This will be removed after Struts 1.2
56       */
57      public static String SERVLET_CONTEXT_KEY = SERVLET_CONTEXT_PARAM;
58  
59      /**
60       * Resources key the <code>HttpServletRequest</code> is stored under.
61       */
62      private static String HTTP_SERVLET_REQUEST_PARAM =
63          "javax.servlet.http.HttpServletRequest";
64  
65      /**
66       * Resources key the <code>HttpServletRequest</code> is stored under.
67       * @deprecated This will be removed after Struts 1.2
68       */
69      public static String HTTP_SERVLET_REQUEST_KEY = HTTP_SERVLET_REQUEST_PARAM;
70  
71      /**
72       * Resources key the <code>ActionMessages</code> is stored under.
73       */
74      private static String ACTION_MESSAGES_PARAM =
75          "org.apache.struts.action.ActionMessages";
76  
77      /**
78       * Resources key the <code>ActionErrors</code> is stored under.
79       * @deprecated This will be removed after Struts 1.2
80       */
81      public static String ACTION_ERRORS_KEY = ACTION_MESSAGES_PARAM;
82  
83      /**
84       * Retrieve <code>ValidatorResources</code> for the current module.
85       * @param application Application Context
86       * @param request The ServletRequest
87       */
88      public static ValidatorResources getValidatorResources(
89          ServletContext application,
90          HttpServletRequest request) {
91  
92          String prefix =
93              ModuleUtils
94                  .getInstance()
95                  .getModuleConfig(request, application)
96                  .getPrefix();
97  
98          return (ValidatorResources) application.getAttribute(
99              ValidatorPlugIn.VALIDATOR_KEY + prefix);
100     }
101 
102     /**
103      * Retrieve <code>MessageResources</code> for the module.
104      * @param request the servlet request
105      */
106     public static MessageResources getMessageResources(HttpServletRequest request) {
107         return (MessageResources) request.getAttribute(Globals.MESSAGES_KEY);
108     }
109 
110     /**
111      * Get the <code>Locale</code> of the current user.
112      * @param request servlet request
113      * @deprecated Use RequestUtils.getUserLocale() instead.  This will be removed
114      * after Struts 1.2.
115      */
116     public static Locale getLocale(HttpServletRequest request) {
117         return RequestUtils.getUserLocale(request, null);
118     }
119 
120     /**
121      * Gets the <code>Locale</code> sensitive value based on the key passed in.
122      * @param messages The Message resources
123      * @param locale The locale.
124      * @param key Key used to lookup the message
125      */
126     public static String getMessage(
127         MessageResources messages,
128         Locale locale,
129         String key) {
130         String message = null;
131 
132         if (messages != null) {
133             message = messages.getMessage(locale, key);
134         }
135 
136         return (message == null) ? "" : message;
137     }
138 
139     /**
140      * Gets the <code>Locale</code> sensitive value based on the key passed in.
141      * @param request servlet request
142      * @param key the request key
143      */
144     public static String getMessage(HttpServletRequest request, String key) {
145         MessageResources messages = getMessageResources(request);
146 
147         return getMessage(messages, RequestUtils.getUserLocale(request, null), key);
148     }
149 
150     /**
151      * Gets the locale sensitive message based on the 
152      * <code>ValidatorAction</code> message and the <code>Field</code>'s 
153      * arg objects.
154      * @param messages  The Message resources
155      * @param locale The locale
156      * @param va The Validator Action
157      * @param field The Validator Field
158      */
159     public static String getMessage(
160         MessageResources messages,
161         Locale locale,
162         ValidatorAction va,
163         Field field) {
164 
165         String args[] = getArgs(va.getName(), messages, locale, field);
166         String msg =
167             field.getMsg(va.getName()) != null
168                 ? field.getMsg(va.getName())
169                 : va.getMsg();
170 
171         return messages.getMessage(locale, msg, args);
172     }
173 
174     /**
175      * Gets the <code>ActionError</code> based on the 
176      * <code>ValidatorAction</code> message and the <code>Field</code>'s 
177      * arg objects.
178      * @param request the servlet request
179      * @param va Validator action
180      * @param field the validator Field
181      * @deprecated Use getActionMessage() instead.  This will be removed after
182      * Struts 1.2.
183      */
184     public static ActionError getActionError(
185         HttpServletRequest request,
186         ValidatorAction va,
187         Field field) {
188 
189         String args[] =
190             getArgs(
191                 va.getName(),
192                 getMessageResources(request),
193                 RequestUtils.getUserLocale(request, null),
194                 field);
195 
196         String msg =
197             field.getMsg(va.getName()) != null
198                 ? field.getMsg(va.getName())
199                 : va.getMsg();
200 
201         return new ActionError(msg, args);
202     }
203     
204     /**
205      * Gets the <code>ActionMessage</code> based on the 
206      * <code>ValidatorAction</code> message and the <code>Field</code>'s 
207      * arg objects.
208      * @param request the servlet request
209      * @param va Validator action
210      * @param field the validator Field
211      */
212     public static ActionMessage getActionMessage(
213         HttpServletRequest request,
214         ValidatorAction va,
215         Field field) {
216 
217         String args[] =
218             getArgs(
219                 va.getName(),
220                 getMessageResources(request),
221                 RequestUtils.getUserLocale(request, null),
222                 field);
223 
224         String msg =
225             field.getMsg(va.getName()) != null
226                 ? field.getMsg(va.getName())
227                 : va.getMsg();
228 
229         return new ActionMessage(msg, args);
230     }
231 
232     /**
233      * Gets the message arguments based on the current 
234      * <code>ValidatorAction</code> and <code>Field</code>.
235      * @param actionName action name
236      * @param messages message resources
237      * @param locale the locale
238      * @param field the validator field
239      */
240     public static String[] getArgs(
241         String actionName,
242         MessageResources messages,
243         Locale locale,
244         Field field) {
245 
246         String[] argMessages = new String[4];
247 
248         Arg[] args =
249             new Arg[] {
250                 field.getArg(actionName,0),
251                 field.getArg(actionName,1),
252                 field.getArg(actionName,2),
253                 field.getArg(actionName,3)};
254 
255         for (int i = 0; i < args.length; i++) {
256             if (args[i] == null) {
257                 continue;
258             }
259 
260             if (args[i].isResource()) {
261                 argMessages[i] = getMessage(messages, locale, args[i].getKey());
262             } else {
263                 argMessages[i] = args[i].getKey();
264             }
265 
266         }
267 
268         return argMessages;
269     }
270 
271     /**
272      * Initialize the <code>Validator</code> to perform validation.
273      *
274      * @param key The key that the validation rules are under (the form elements 
275      * name attribute).
276      * @param bean The bean validation is being performed on.
277      * @param application servlet context
278      * @param request The current request object.
279      * @param errors The object any errors will be stored in.
280      * @param page This in conjunction with  the page property of a 
281      * <code>Field<code> can control the processing of fields.  If the field's 
282      * page is less than or equal to this page value, it will be processed.
283      */
284     public static Validator initValidator(
285         String key,
286         Object bean,
287         ServletContext application,
288         HttpServletRequest request,
289         ActionMessages errors,
290         int page) {
291 
292         ValidatorResources resources =
293             Resources.getValidatorResources(application, request);
294 
295         Locale locale = RequestUtils.getUserLocale(request, null);
296 
297         Validator validator = new Validator(resources, key);
298         validator.setUseContextClassLoader(true);
299 
300         validator.setPage(page);
301 
302         validator.setParameter(SERVLET_CONTEXT_PARAM, application);
303         validator.setParameter(HTTP_SERVLET_REQUEST_PARAM, request);
304         validator.setParameter(Validator.LOCALE_PARAM, locale);
305         validator.setParameter(ACTION_MESSAGES_PARAM, errors);
306         validator.setParameter(Validator.BEAN_PARAM, bean);
307 
308         return validator;
309     }
310 
311 }