📄 resources.java.svn-base
字号:
/* * $Id$ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */package org.apache.struts.validator;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.commons.validator.Arg;import org.apache.commons.validator.Field;import org.apache.commons.validator.Msg;import org.apache.commons.validator.Validator;import org.apache.commons.validator.ValidatorAction;import org.apache.commons.validator.ValidatorResources;import org.apache.commons.validator.Var;import org.apache.struts.Globals;import org.apache.struts.action.ActionMessage;import org.apache.struts.action.ActionMessages;import org.apache.struts.config.ModuleConfig;import org.apache.struts.util.MessageResources;import org.apache.struts.util.ModuleUtils;import org.apache.struts.util.RequestUtils;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import java.util.Locale;/** * This class helps provides some useful methods for retrieving objects from * different scopes of the application. * * @version $Rev$ $Date: 2005-09-16 23:34:41 -0400 (Fri, 16 Sep 2005) * $ * @since Struts 1.1 */public class Resources { /** * The message resources for this package. */ private static MessageResources sysmsgs = MessageResources.getMessageResources( "org.apache.struts.validator.LocalStrings"); /** * <p>Commons Logging instance.</p> */ private static Log log = LogFactory.getLog(Resources.class); /** * Resources key the <code>ServletContext</code> is stored under. */ private static String SERVLET_CONTEXT_PARAM = "javax.servlet.ServletContext"; /** * Resources key the <code>HttpServletRequest</code> is stored under. */ private static String HTTP_SERVLET_REQUEST_PARAM = "javax.servlet.http.HttpServletRequest"; /** * Resources key the <code>ActionMessages</code> is stored under. */ private static String ACTION_MESSAGES_PARAM = "org.apache.struts.action.ActionMessages"; /** * Retrieve <code>ValidatorResources</code> for the current module. * * @param application Application Context * @param request The ServletRequest */ public static ValidatorResources getValidatorResources( ServletContext application, HttpServletRequest request) { String prefix = ModuleUtils.getInstance().getModuleConfig(request, application) .getPrefix(); return (ValidatorResources) application.getAttribute(ValidatorPlugIn.VALIDATOR_KEY + prefix); } /** * Retrieve <code>MessageResources</code> for the module. * * @param request the servlet request */ public static MessageResources getMessageResources( HttpServletRequest request) { return (MessageResources) request.getAttribute(Globals.MESSAGES_KEY); } /** * Retrieve <code>MessageResources</code> for the module and bundle. * * @param application the servlet context * @param request the servlet request * @param bundle the bundle key */ public static MessageResources getMessageResources( ServletContext application, HttpServletRequest request, String bundle) { if (bundle == null) { bundle = Globals.MESSAGES_KEY; } MessageResources resources = (MessageResources) request.getAttribute(bundle); if (resources == null) { ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, application); resources = (MessageResources) application.getAttribute(bundle + moduleConfig.getPrefix()); } if (resources == null) { resources = (MessageResources) application.getAttribute(bundle); } if (resources == null) { throw new NullPointerException( "No message resources found for bundle: " + bundle); } return resources; } /** * Get the value of a variable. * * @param varName The variable name * @param field the validator Field * @param validator The Validator * @param request the servlet request * @param required Whether the variable is mandatory * @return The variable's value */ public static String getVarValue(String varName, Field field, Validator validator, HttpServletRequest request, boolean required) { Var var = field.getVar(varName); if (var == null) { String msg = sysmsgs.getMessage("variable.missing", varName); if (required) { throw new IllegalArgumentException(msg); } if (log.isDebugEnabled()) { log.debug(field.getProperty() + ": " + msg); } return null; } ServletContext application = (ServletContext) validator.getParameterValue(SERVLET_CONTEXT_PARAM); return getVarValue(var, application, request, required); } /** * Get the value of a variable. * * @param var the validator variable * @param application The ServletContext * @param request the servlet request * @param required Whether the variable is mandatory * @return The variables values */ public static String getVarValue(Var var, ServletContext application, HttpServletRequest request, boolean required) { String varName = var.getName(); String varValue = var.getValue(); // Non-resource variable if (!var.isResource()) { return varValue; } // Get the message resources String bundle = var.getBundle(); MessageResources messages = getMessageResources(application, request, bundle); // Retrieve variable's value from message resources Locale locale = RequestUtils.getUserLocale(request, null); String value = messages.getMessage(locale, varValue, null); // Not found in message resources if ((value == null) && required) { throw new IllegalArgumentException(sysmsgs.getMessage( "variable.resource.notfound", varName, varValue, bundle)); } if (log.isDebugEnabled()) { log.debug("Var=[" + varName + "], " + "bundle=[" + bundle + "], " + "key=[" + varValue + "], " + "value=[" + value + "]"); } return value; } /** * Gets the <code>Locale</code> sensitive value based on the key passed * in. * * @param messages The Message resources * @param locale The locale. * @param key Key used to lookup the message */ public static String getMessage(MessageResources messages, Locale locale, String key) { String message = null; if (messages != null) { message = messages.getMessage(locale, key); } return (message == null) ? "" : message; } /** * Gets the <code>Locale</code> sensitive value based on the key passed * in. * * @param request servlet request * @param key the request key */ public static String getMessage(HttpServletRequest request, String key) { MessageResources messages = getMessageResources(request); return getMessage(messages, RequestUtils.getUserLocale(request, null), key); } /** * Gets the locale sensitive message based on the <code>ValidatorAction</code> * message and the <code>Field</code>'s arg objects. * * @param messages The Message resources * @param locale The locale
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -