8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: I18n.java,v 1.3 2008/06/25 05:41:41 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Portions Copyrighted [2011] [ForgeRock AS]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.iplanet.services.util;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.UnsupportedEncodingException;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.text.DateFormat;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.text.MessageFormat;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Date;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.ResourceBundle;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.StringTokenizer;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.TimeZone;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The <code>I18n</code> class provides methods for applications and services
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to internationalize their messages.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <p>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * In order for <code>I18n</code> to internationalize messages, it needs to
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * determine the resource bundle name, i.e., properties file name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>I18n</code> supports two techniques by which applications and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * services can specify their I18N resource bundle name. The recommendation is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to specify them during service (or application) registration via <b> SMS</b>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * using a XML file (see <code>com.iplanet.services.ServiceManager
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * </code> and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * service registration DTD). The XML file could specify the resource bundle
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * name (the attribute defined in the DTD is <code>i18nFileName</code>) and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * optionally URL of the jar file which contains the property file (the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * attribute defined in the DTD is <code>resourceBundleURL</code>). If URL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * for the jar file is not specified it is assumed that the resource bundle is
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * in the <code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * CLASSPATH</code>. Using this technique it is possible to
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * customize resource bundle name and URL of the jar file by using SMS APIs,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * commands (CLI) or GUI. The solution makes internationalization of messages
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * very dynamic and highly customizable.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <p>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>I18n</code> class be instantiated by calling the static
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>getInstance(String serviceName)</code> method. The parameter
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>serviceName</code> specifies the name of the service as mentioned in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the XML file at the time of service registration.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <p>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Alternatively services and application can instantiate <code>I18n</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * object by specifying the resource bundle name (i.e., properties file name).
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Using this technique it is not possible to customize at runtime either the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * resource bundle name or the URL of the jar file that contains the properties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * file name. It is assumed that the properties file is present in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>CLASSPATH</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class I18n {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* ASCII ISO */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static final String ASCII_CHARSET = "ISO-8859-1";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* Static varibale that holds all the I18n objects */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Map i18nMap = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // private static SSOToken userSSOToken = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* Instance variable */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private boolean initialized = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String serviceName = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String i18nFile = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private ClassLoader ucl = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map resourceBundles = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This constructor takes the name of the component as an argument and it
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * should match with name of the resource bundle
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected I18n(String serviceName) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.serviceName = serviceName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private void initialize() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (initialized)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // %%% Hack to get around cyclic dependency on I18n
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // and other components that call I18n
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster i18nFile = serviceName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initialized = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Method to get an instance of I18n object that has been either previously
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * created or to obtain a new instance if it does'nt exist
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param serviceName
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * name of the service for which messages must be
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * internationalized
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return I18n object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static I18n getInstance(String serviceName) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (serviceName == null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster I18n i18nobj = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized (i18nMap) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((i18nobj = (I18n) i18nMap.get(serviceName)) == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster i18nobj = new I18n(serviceName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster i18nMap.put(serviceName, i18nobj);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (i18nobj);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Method to obtain Locale object given its string representation
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param stringformat
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Locale in a string format
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return Locale object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static java.util.Locale getLocale(String stringformat) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (stringformat == null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return java.util.Locale.getDefault();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringTokenizer tk = new StringTokenizer(stringformat, "_");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String lang = "";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String country = "";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String variant = "";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (tk.hasMoreTokens())
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster lang = tk.nextToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (tk.hasMoreTokens())
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster country = tk.nextToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (tk.hasMoreTokens())
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster variant = tk.nextToken();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (new java.util.Locale(lang, country, variant));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the resource file name associated with the service
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return Returns the the ResourceBundle name associated with the service
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getResBundleName() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initialize();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return i18nFile;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* Gets the resource bundle */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private synchronized ResourceBundle getResourceBundle(String stringformat) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceBundle bundle = (ResourceBundle) resourceBundles
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .get(stringformat);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (bundle == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (ucl != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bundle = ResourceBundle.getBundle(i18nFile,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getLocale(stringformat), ucl);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster bundle = ResourceBundle.getBundle(i18nFile,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster getLocale(stringformat));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (initialized)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster resourceBundles.put(stringformat, bundle);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (bundle);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* Get the default locale stored in config */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String getDefaultLocale() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String loc = "en_US";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /* %%% Get the default locale stored in config - to be implemented */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (loc);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Method to obtain internationalized message from the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * resource bundle given the key and locale.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param key
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * key string in the properties file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param locale
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * locale in a string format
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return returns internationalized message for the specified key
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getString(String key, String locale) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initialize();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (key == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceBundle bundle = getResourceBundle(locale);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (bundle.getString(key));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Method to obtain internationalized message from the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * resource bundle given the key.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param key
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Key string in the properties file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return Returns value to the specified key
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getString(String key) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initialize();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (key == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResourceBundle bundle = getResourceBundle(getDefaultLocale());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (bundle.getString(key));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Method to obtain internationalized message from the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * resource bundle given the key, locale and parameters.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param key
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * key string in the properties file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param locale
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * locale in a string format
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param params
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * parameters to be applied to the message
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return returns internationalized message for the specified key
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getString(String key, String locale, Object[] params) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initialize();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (key == null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (MessageFormat.format(getString(key, locale), params));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Method to obtain internationalized message from the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * resource bundle given the key and parameters.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param key
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Key string in the properties file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param params
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * parameters to be applied to the message
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return Returns value to the specified key
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @supported.api
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getString(String key, Object[] params) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster initialize();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (key == null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (null);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (MessageFormat.format(getString(key), params));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Decodes the string into specified charset
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param s
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * string to be decoded
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param charset
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * character set in which the string to be decoded
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return Returns the decoded string
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String decodeCharset(String s, String charset) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (s == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster byte buf[] = s.getBytes(ASCII_CHARSET);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (new String(buf, 0, buf.length, charset));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (UnsupportedEncodingException uee) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return s;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Checks whether the string is ascii or not
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param s
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * string to be checked
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return true if the string is ascii, otherwise false
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static boolean isAscii(String s) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (s == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!s.equals(new String(s.getBytes(ASCII_CHARSET), ASCII_CHARSET)))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (java.io.UnsupportedEncodingException uee) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String format(MessageFormat mf, Object o) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = mf.format(new Object[] { o }, new StringBuffer(), null)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .toString();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return msg;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Formats the objects into specified message format.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param pattern
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * pattern for which the message to be formatted
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param j
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Object to be formatted & substituted
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param l
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * locale in a string format
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return Returns the formatted message
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String format(String pattern, Long j, String l) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster MessageFormat mf = new MessageFormat("");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mf.setLocale(getLocale(l));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mf.applyPattern(pattern);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = format(mf, j);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return msg;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Formats the objects into specified message format.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param pattern
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * pattern for which the message to be formatted
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param i
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Integer to be formatted & substituted
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param l
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * locale in a string format
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return Returns the formatted message
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String format(String pattern, Integer i, String l) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster MessageFormat mf = new MessageFormat("");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mf.setLocale(getLocale(l));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mf.applyPattern(pattern);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String msg = format(mf, i);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return msg;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Formats the objects into specified message format
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param pattern
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * pattern for which the message to be formatted
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param d
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * date
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param tz
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Timezone
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param l
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * locale in a string format
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return Returns the formatted message
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static String format(String pattern, Date d, TimeZone tz, String l) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster MessageFormat mf = new MessageFormat("");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mf.setLocale(getLocale(l));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster mf.applyPattern(pattern);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ((DateFormat) mf.getFormats()[0]).setTimeZone(tz);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster DateFormat df1 = ((DateFormat) mf.getFormats()[0]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (df1 != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster df1.setTimeZone(tz);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster DateFormat df2 = ((DateFormat) mf.getFormats()[1]);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (df2 != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster df2.setTimeZone(tz);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return format(mf, d);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}