📄 messageresources.java
字号:
/*
* $Header: /sfroot/cvs/esimple/src/core/org/apache/struts/util/MessageResources.java,v 1.1.1.1 2004/09/08 06:38:42 lava Exp $
* $Revision: 1.1.1.1 $
* $Date: 2004/09/08 06:38:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.struts.util;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* General purpose abstract class that describes an API for retrieving
* Locale-sensitive messages from underlying resource locations of an
* unspecified design, and optionally utilizing the <code>MessageFormat</code>
* class to produce internationalized messages with parametric replacement.
* <p>
* Calls to <code>getMessage()</code> variants without a <code>Locale</code>
* argument are presumed to be requesting a message string in the default
* <code>Locale</code> for this JVM.
* <p>
* Calls to <code>getMessage()</code> with an unknown key, or an unknown
* <code>Locale</code> will return <code>null</code> if the
* <code>returnNull</code> property is set to <code>true</code>. Otherwise,
* a suitable error message will be returned instead.
* <p>
* <strong>IMPLEMENTATION NOTE</strong> - Classes that extend this class
* must be Serializable so that instances may be used in distributable
* application server environments.
*
* @author Craig R. McClanahan
* @version $Revision: 1.1.1.1 $ $Date: 2004/09/08 06:38:42 $
*/
public abstract class MessageResources implements Serializable {
// ------------------------------------------------------------- Properties
/**
* Commons Logging instance.
*/
protected static Log log = LogFactory.getLog(MessageResources.class);
/**
* The configuration parameter used to initialize this MessageResources.
*/
protected String config = null;
/**
* The configuration parameter used to initialize this MessageResources.
* @return parameter used to initialize this MessageResources
*/
public String getConfig() {
return (this.config);
}
/**
* The default Locale for our environment.
*/
protected Locale defaultLocale = Locale.getDefault();
/**
* The <code>MessageResourcesFactory</code> that created this instance.
*/
protected MessageResourcesFactory factory = null;
/**
* The <code>MessageResourcesFactory</code> that created this instance.
* @return <code>MessageResourcesFactory</code> that created instance
*/
public MessageResourcesFactory getFactory() {
return (this.factory);
}
/**
* The set of previously created MessageFormat objects, keyed by the
* key computed in <code>messageKey()</code>.
*/
protected HashMap formats = new HashMap();
/**
* Indicate is a <code>null</code> is returned instead of an error message string
* when an unknown Locale or key is requested.
*/
protected boolean returnNull = false;
/**
* Indicates that a <code>null</code> is returned instead of an error message string
* if an unknown Locale or key is requested.
* @return true if null is returned if unknown key or locale is requested
*/
public boolean getReturnNull() {
return (this.returnNull);
}
/**
* Indicates that a <code>null</code> is returned instead of an error message string
* if an unknown Locale or key is requested.
* @param returnNull true Indicates that a <code>null</code> is returned
* if an unknown Locale or key is requested.
*/
public void setReturnNull(boolean returnNull) {
this.returnNull = returnNull;
}
// ----------------------------------------------------------- Constructors
/**
* Construct a new MessageResources according to the specified parameters.
*
* @param factory The MessageResourcesFactory that created us
* @param config The configuration parameter for this MessageResources
*/
public MessageResources(MessageResourcesFactory factory, String config) {
this(factory, config, false);
}
/**
* Construct a new MessageResources according to the specified parameters.
*
* @param factory The MessageResourcesFactory that created us
* @param config The configuration parameter for this MessageResources
* @param returnNull The returnNull property we should initialize with
*/
public MessageResources(MessageResourcesFactory factory, String config,
boolean returnNull) {
super();
this.factory = factory;
this.config = config;
this.returnNull = returnNull;
}
// --------------------------------------------------------- Public Methods
/**
* Returns a text message for the specified key, for the default Locale.
*
* @param key The message key to look up
*/
public String getMessage(String key) {
return (getMessage((Locale) null, key));
}
/**
* Returns a text message after parametric replacement of the specified
* parameter placeholders.
*
* @param key The message key to look up
* @param args An array of replacement parameters for placeholders
*/
public String getMessage(String key, Object args[]) {
return (getMessage((Locale) null, key, args));
}
/**
* Returns a text message after parametric replacement of the specified
* parameter placeholders.
*
* @param key The message key to look up
* @param arg0 The replacement for placeholder {0} in the message
*/
public String getMessage(String key, Object arg0) {
return (getMessage((Locale) null, key, arg0));
}
/**
* Returns a text message after parametric replacement of the specified
* parameter placeholders.
*
* @param key The message key to look up
* @param arg0 The replacement for placeholder {0} in the message
* @param arg1 The replacement for placeholder {1} in the message
*/
public String getMessage(String key, Object arg0, Object arg1) {
return (getMessage((Locale) null, key, arg0, arg1));
}
/**
* Returns a text message after parametric replacement of the specified
* parameter placeholders.
*
* @param key The message key to look up
* @param arg0 The replacement for placeholder {0} in the message
* @param arg1 The replacement for placeholder {1} in the message
* @param arg2 The replacement for placeholder {2} in the message
*/
public String getMessage(String key, Object arg0, Object arg1,
Object arg2) {
return (getMessage((Locale) null, key, arg0, arg1, arg2));
}
/**
* Returns a text message after parametric replacement of the specified
* parameter placeholders.
*
* @param key The message key to look up
* @param arg0 The replacement for placeholder {0} in the message
* @param arg1 The replacement for placeholder {1} in the message
* @param arg2 The replacement for placeholder {2} in the message
* @param arg3 The replacement for placeholder {3} in the message
*/
public String getMessage(String key, Object arg0, Object arg1,
Object arg2, Object arg3) {
return (getMessage((Locale) null, key, arg0, arg1, arg2, arg3));
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -