1 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.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 48 public class Resources { 49 50 53 private static String SERVLET_CONTEXT_PARAM = "javax.servlet.ServletContext"; 54 55 59 public static String SERVLET_CONTEXT_KEY = SERVLET_CONTEXT_PARAM; 60 61 64 private static String HTTP_SERVLET_REQUEST_PARAM = 65 "javax.servlet.http.HttpServletRequest"; 66 67 71 public static String HTTP_SERVLET_REQUEST_KEY = HTTP_SERVLET_REQUEST_PARAM; 72 73 76 private static String ACTION_MESSAGES_PARAM = 77 "org.apache.struts.action.ActionMessages"; 78 79 83 public static String ACTION_ERRORS_KEY = ACTION_MESSAGES_PARAM; 84 85 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 108 public static MessageResources getMessageResources(HttpServletRequest request) { 109 return (MessageResources) request.getAttribute(Globals.MESSAGES_KEY); 110 } 111 112 118 public static MessageResources getMessageResources( 119 ServletContext application, 120 HttpServletRequest request, 121 String 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 ("No message resources found for bundle: " + bundle); 141 } 142 143 return resources; 144 145 } 146 147 153 public static Locale getLocale(HttpServletRequest request) { 154 return RequestUtils.getUserLocale(request, null); 155 } 156 157 163 public static String getMessage( 164 MessageResources messages, 165 Locale locale, 166 String key) { 167 String message = null; 168 169 if (messages != null) { 170 message = messages.getMessage(locale, key); 171 } 172 173 return (message == null) ? "" : message; 174 } 175 176 181 public static String getMessage(HttpServletRequest request, String key) { 182 MessageResources messages = getMessageResources(request); 183 184 return getMessage(messages, RequestUtils.getUserLocale(request, null), key); 185 } 186 187 196 public static String getMessage( 197 MessageResources messages, 198 Locale locale, 199 ValidatorAction va, 200 Field field) { 201 202 String args[] = getArgs(va.getName(), messages, locale, field); 203 String 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 219 public static String getMessage(ServletContext application, 220 HttpServletRequest request, 221 MessageResources defaultMessages, 222 Locale 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 msgKey = null; 232 String 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 Arg[] args = field.getArgs(va.getName()); 250 String [] argValues = getArgValues(application, request, messages, locale, args); 251 252 return messages.getMessage(locale, msgKey, argValues); 254 255 } 256 257 267 public static ActionError getActionError( 268 HttpServletRequest request, 269 ValidatorAction va, 270 Field field) { 271 272 String args[] = 273 getArgs( 274 va.getName(), 275 getMessageResources(request), 276 RequestUtils.getUserLocale(request, null), 277 field); 278 279 String 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 295 public static ActionMessage getActionMessage( 296 HttpServletRequest request, 297 ValidatorAction va, 298 Field field) { 299 300 String args[] = 301 getArgs( 302 va.getName(), 303 getMessageResources(request), 304 RequestUtils.getUserLocale(request, null), 305 field); 306 307 String 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 324 public static ActionMessage getActionMessage( 325 Validator validator, 326 HttpServletRequest 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 msgKey = null; 336 String 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 application = (ServletContext )validator.getParameterValue(SERVLET_CONTEXT_PARAM); 349 MessageResources messages = getMessageResources(application, request, msgBundle); 350 Locale locale = RequestUtils.getUserLocale(request, null); 351 352 Arg[] args = field.getArgs(va.getName()); 353 String [] 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 message = messages.getMessage(locale, msgKey, argValues); 360 actionMessage = new ActionMessage(message, false); 361 } 362 return actionMessage; 363 364 } 365 366 367 375 public static String [] getArgs( 376 String actionName, 377 MessageResources messages, 378 Locale locale, 379 Field field) { 380 381 String [] argMessages = new String [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 415 private static String [] getArgValues( 416 ServletContext application, 417 HttpServletRequest request, 418 MessageResources defaultMessages, 419 Locale locale, 420 Arg[] args) { 421 422 if (args == null || args.length == 0) { 423 return null; 424 } 425 426 String [] values = new String [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 462 public static Validator initValidator( 463 String key, 464 Object bean, 465 ServletContext application, 466 HttpServletRequest request, 467 ActionMessages errors, 468 int page) { 469 470 ValidatorResources resources = 471 Resources.getValidatorResources(application, request); 472 473 Locale 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 |