com.sun.jsft.util.MessageUtil Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2011, 2018 Oracle and/or its affiliates. All rights reserved.
* Portions Copyright (c) 2011 Ken Paulsen
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.jsft.util;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
import com.sun.jsft.util.ResourceBundleManager;
/**
* This class gets ResourceBundle messages and formats them.
*
* Created March 29, 2011
* @author Ken Paulsen ([email protected])
*/
public class MessageUtil extends Object {
/**
* This class should not be instantiated directly.
*/
private MessageUtil() {
}
/**
* Use this to get an instance of this class.
*/
public static MessageUtil getInstance() {
return _instance;
}
/**
* This method returns a formatted String from the requested
* ResourceBundle
.
*
* @param baseName The ResourceBundle
name.
* @param key The ResourceBundle
key.
*/
public String getMessage(String baseName, String key) {
return getMessage(baseName, key, null);
}
/**
* This method returns a formatted String from the requested
* ResourceBundle
.
*
* @param baseName The ResourceBundle
name.
* @param key The ResourceBundle
key.
* @param args The substitution values (may be null).
*/
public String getMessage(String baseName, String key, Object args[]) {
return getMessage(null, baseName, key, args);
}
/**
* This method returns a formatted String from the requested
* ResourceBundle
.
*
* @param locale The desired Locale
(may be null).
* @param baseName The ResourceBundle
name.
* @param key The ResourceBundle
key.
* @param args The substitution values (may be null).
*/
public String getMessage(Locale locale, String baseName, String key, Object args[]) {
if (key == null) {
return null;
}
if (baseName == null) {
throw new RuntimeException(
"'baseName' is null for key '" + key + "'!");
}
FacesContext ctx = FacesContext.getCurrentInstance();
if (locale == null) {
locale = Util.getLocale(ctx);
}
// Get the ResourceBundle
ResourceBundle bundle =
ResourceBundleManager.getInstance(ctx).getBundle(baseName, locale);
if (bundle == null) {
// FIXME: Log a warning
return key;
}
String message = null;
try {
message = bundle.getString(key);
} catch (MissingResourceException ex) {
// Key not found!
// FIXME: Log a warning
}
if (message == null) {
// No message found?
return key;
}
return getFormattedMessage(message, args);
}
/**
* Format message using given arguments.
*
* @param message The string used as a pattern for inserting arguments.
* @param args The arguments to be inserted into the string.
*/
public static String getFormattedMessage(String message, Object args[]) {
// Sanity Check
if ((message == null) || (args == null) || (args.length == 0)) {
return message;
}
String result = null;
MessageFormat mf = new MessageFormat(message);
result = mf.format(args);
return (result != null) ? result : message;
}
/**
* Singleton. This one is OK to share across VMs (no state).
*/
private static final MessageUtil _instance = new MessageUtil();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy