5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * The contents of this file are subject to the terms
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * of the Common Development and Distribution License
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * (the License). You may not use this file except in
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * compliance with the License.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * You can obtain a copy of the License at
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * See the License for the specific language governing
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * permission and limitations under the License.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * When distributing Covered Code, include this CDDL
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Header Notice in each file and include the License file
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * If applicable, add the following below the CDDL Header,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * with the fields enclosed by brackets [] replaced by
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * your own identifying information:
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * $Id: Locale.java,v 1.7 2009/07/07 17:32:02 bina Exp $
be1785efc04c1f3f74f021992ca53155807dba2dDavid Luna * Portions Copyrighted 2014-2015 ForgeRock AS.
8f8adeb1540d89588dba4c26d00a6ed6887cf6deKohei Tamura * Portions Copyrighted 2014 Nomura Research Institute, Ltd.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport com.sun.identity.shared.configuration.SystemPropertiesManager;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This class <code>Locale.java</code> is a utility that provides
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * functionality for applications and services to internationalize their
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @supported.all.api
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static final int LOCALE_STRING_MAX_LEN = 5;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster protected static final String USER_PROPERTIES = "amUser";
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster protected static final String DATE_SYNTAX = "dateSyntax";
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static final String normalizedDateString = "yyyy/MM/dd HH:mm:ss";
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static final SimpleDateFormat normalizedDateFormat;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * The list of characters that are not encoded have been determined by
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * referencing O'Reilly's "HTML: The Definitive Guide" (page 164).
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // Intialize static variables
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * encoding a space to a + is done in the
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * encode() method
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String loc = SystemPropertiesManager.get(Constants.AM_LOCALE, "en_US");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster normalizedDateFormat = new SimpleDateFormat(normalizedDateString);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster System.out.println(":" + Locale.getLocale(args[0]) + ":");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster System.out.println(":" + Locale.getLocale(args[0]).getCountry() + ":");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the locale object for the specified localized string format.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param stringformat
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * String representation of the locale. Examples:
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * <code>en_US, en_UK, ja_JP</code>.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the <code>java.util.locale</code> object.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static java.util.Locale getLocale(String stringformat) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster java.util.Locale locale = java.util.Locale.getDefault();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster tk = new StringTokenizer(stringformat,UNDERSCORE);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster locale = new java.util.Locale(lang, country, variant);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns locale from accept-language header HTTP accept language header
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * can have more than one language in the header, we honor the first
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * language as locale
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param langstr
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Value from Accept-Language header of HTTP
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return locale string in this format <code>en_US, fr</code>
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getLocaleStringFromAcceptLangHeader(String langstr) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Accept Language Syntax Accept-Language = "Accept-Language" ":" 1#(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * language-range [ ";" "q" "=" qvalue ] ) language-range = ( ( 1*8ALPHA
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * *("-" 1*8ALPHA ) ) | "*" ) For more info Read RFC 2616 Examples:
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Accept-Language: da, en-gb;q=0.8, en;q=0.7 Accept-Language: en-gb, en
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Accept-Language: ja Accept-Language: zh-cn Accept-Language: *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * We will use first language as locale. We will not process any
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * further.Netscape,IE will give mostly one language as Accept-Language
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * header. Max length of string is 5 lang-> 2chars , country -> two
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * chars and separator is -
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster while (Character.isWhitespace(lstr[leadSpace]))
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster boolean isCountry = false;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // "*" can be a valid accept-lang but does
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // give idea about locale, return null and force the caller to
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // default locale
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster lstr[i] = '_'; // We will follow Java mechanism en_US
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } else if (ch == ';' || ch == ',') {// Language separators used
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // by accept-lang
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return new String(lstr, leadSpace, i - leadSpace);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets locale from accept-language header HTTP accept language header can
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * have more than one language in the header, we honor the first language as
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param langStr
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Value from Accept-Language header of HTTP
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return locale string in this format <code>en_US, fr</code>.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static java.util.Locale getLocaleObjFromAcceptLangHeader(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String lstr = getLocaleStringFromAcceptLangHeader(langStr);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the resource bundle corresponding to the specified locale and the
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * localized property file name.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param bundle
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Localized property file name.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param stringformat
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * String representation of the locale.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return <code>java.util.ResourceBundle</code> object.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static ResourceBundle getResourceBundle(String bundle,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return ResourceBundle.getBundle(bundle, getLocale(stringformat));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster protected static ResourceBundle getResourceBundle(String bundle) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the default install resource bundle for the default locale
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param bundle
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Localized property file name
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the install resource bundle object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static ResourceBundle getInstallResourceBundle(String bundle) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String loc = SystemPropertiesManager.get(Constants.AM_LOCALE, "en_US");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return ResourceBundle.getBundle(bundle, getLocale(loc));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the default locale
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the default Locale object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static java.util.Locale getDefaultLocale() {
be1785efc04c1f3f74f021992ca53155807dba2dDavid Luna String loc = SystemPropertiesManager.get(Constants.AM_LOCALE, "en_US");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Formats messages using <code>MessageFormat</code> Class.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param formatStr
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * string format template.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param obj1
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * object to be added to the template.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return formatted message.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String formatMessage(String formatStr, Object obj1) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Formats to format messages using <code>MessageFormat</code> Class.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * given params to format them with
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param formatStr
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * string format template.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param objs
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * objects to be added to the template.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return formatted message.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String formatMessage(String formatStr, Object[] objs) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns the Date object from the date string in <code>ISO-8601</code>
8d3140b524c0e28c0a49dc7c7d481123ef3cfe11Chris Lee * format. OpenAM stores date in <code>ISO-8601</code> format
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * <code>yyyy/MM/yy hh:mm</code>
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param dateString
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * in the format <code>2002/12/31 23:59</code>.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return Date object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Date parseNormalizedDateString(String dateString) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Date date = normalizedDateFormat.parse(dateString, pos);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.error("Locale.parseNormalizedDateString: "
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster + "Unable to parse date string");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("Locale.parseNormalizedDateString(" + dateString
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets Date object from date string with specified locale.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param dateString
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * date string
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param locale
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Locale object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param dateSyntax
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * syntax of the date string.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return Date object returned if <code>dateString</code> matches the
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * <code> dateSyntax</code>. If the syntax or date string is
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * empty, or the string does not match the syntax, null will be
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Date parseDateString(String dateString,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("Local.parseDateString(date, locale, syntax)");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("locale = " + locale.toString());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((dateString == null) || (dateString.length() < 1)
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster || (dateSyntax == null) || (dateSyntax.length() < 1)) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster SimpleDateFormat sdf = new SimpleDateFormat(dateSyntax);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.warning("Locale.parseDateString: unable to parse the date.");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets Date object from date string with specified locale. Syntax of date
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * string is defined in amUser_<locale> properties file.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param dateString
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * date string
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param locale
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Locale object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return Date object. null will be returned if error happens
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Date parseDateString(String dateString,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ResourceBundle rb = AMResourceBundleCache.getInstance().getResBundle(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.error("Locale.parseDateString: Unable to get resource "
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.error("Locale.parseDateString: Unable to get " + DATE_SYNTAX
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return parseDateString(dateString, locale, dateSyntax);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Converts the Date object into <code>ISO-8601</code> format
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * <code>yyyy/MM/dd HH:mm</code> like <code>2002/12/23 20:40</code>.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param date
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * to be normalized.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return date in <code>ISO8601</code> format
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * <code>2002/12/31 11:59</code>.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getNormalizedDateString(Date date) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets date string from date with specified locale.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param date
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Date object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param locale
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Locale object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return date string. null will be returned if error happens
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getDateString(Date date, java.util.Locale locale) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ResourceBundle rb = AMResourceBundleCache.getInstance().getResBundle(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.error("Locale.getDateString: Unable to get resource "
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.error("Locale.getDateString: Unable to get " + DATE_SYNTAX
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("Locale.getDateString: dateSyntax = " + dateSyntax);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster SimpleDateFormat sdf = new SimpleDateFormat(dateSyntax);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Converts date string from source locale to destination locale
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param srcDateString
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * source date string
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param srcLocale
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * source Locale object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param dstLocale
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * destination Locale object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return converted date string. null will be returned if error happens
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String convertDateString(String srcDateString,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster java.util.Locale srcLocale, java.util.Locale dstLocale) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Date date = parseDateString(srcDateString, srcLocale);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the localized string for the specified key formatted as per passed
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * parameters.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * resource bundle.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param resource
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * the specified key.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param params
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * formatting done as per these parameters.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the localized string representation formatted as per passed
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * parameters.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getString(ResourceBundle rb, String resource,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return MessageFormat.format(rb.getString(resource), params);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("missing resource: " + resource);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the localized string for the specified key from the specified
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Resource or from the specified default resource formatted as per provided
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * parameters.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * resource bundle.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param resource
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * the specified key.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param defaultRb
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Default resource bundle.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param params
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * formatting done as per these parameters.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the localized string representation formatted as per passed
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * parameters.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getString(ResourceBundle rb, String resource,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return MessageFormat.format(rb.getString(resource), params);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("missing resource: " + resource);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("fall back to default resource bundle");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return MessageFormat.format(defaultRb.getString(resource),
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("missing resource in default resource bundle:"
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the localized string for the specified key
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * resource bundle.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param resource
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * the specified key.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param debug
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * the debug instance to which the debug messages need to be
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the localized string representation
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getString(ResourceBundle rb, String resource,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("missing resource: " + resource);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the localized string for the specified key from the specified
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Resource or from the specified default resource
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * resource bundle.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param resource
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * the specified key.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param debug
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * the debug instance to which the debug messages need to be
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param defaultRb
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Default resource bundle.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the localized string representation
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getString(ResourceBundle rb, String resource,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("missing resource: " + resource);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("fall back to default resource bundle");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("missing resource in default resource bundle:"
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the localized string for the specified key.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * resource bundle.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param resource
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * the specified key.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the localized string representation
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getString(ResourceBundle rb, String resource) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("missing resource: " + resource);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the localized string for the specified key from the specified
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Resource or from the specified default resource.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * resource bundle.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param resource
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * the specified key.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param defaultRb
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Default resource bundle.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the localized string representation
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getString(ResourceBundle rb, String resource,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("missing resource: " + resource);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("fall back to default resource bundle");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.message("missing resource in default resource bundle:"
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This method is replacement function for <code>URLEncoder</code>
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Function URL encoder function converts input string into
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * <code>URLEncoded</code> byte stream after converting Unicode string
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * into bytes using native encoding. The <code>URLEncoder</code> does not
8d3140b524c0e28c0a49dc7c7d481123ef3cfe11Chris Lee * work for OpenAM if default encoding is not
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * <code>UTF-8</code>, hence this method was written.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param input
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * the input string.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param enc
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * the encoding format.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the encoded string.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @throws UnsupportedEncodingException
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String URLEncodeField(String input, String enc)
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster StringBuffer result = new StringBuffer(inputLen * 4); // approx size
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (c == ' ') {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster char ch = Character.forDigit((c >> 4) & 0xF, 16);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This method is replacement function for <code>URLEncoder<code> Function
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * URL encoder function converts input string into <code>URLencoded</code>
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * byte stream after converting Unicode string into bytes using native
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * encoding. The <code>URLEncoder</code> does not work for Sun Java System
8d3140b524c0e28c0a49dc7c7d481123ef3cfe11Chris Lee * OpenAM if default encoding is not <code>UTF-8</code>, hence this
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * method was written.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param input the input string
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param enc the encoding format
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param debug the debug instance to which debug messages need to
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * be printed
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the encoded string
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String URLEncodeField(String input, String enc, Debug debug) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.error("Locale.URLEncodeField: Unsupported Encoding "
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster StringBuffer result = new StringBuffer(inputLen * 4); // approx size
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (c == ' ') {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster char ch = Character.forDigit((c >> 4) & 0xF, 16);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster static public String URLDecodeField(String strIn, Debug debug) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Translate the individual field values in the encoding value Do not use
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * getBytes instead convert unicode into bytes by casting. Using getBytes
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * results in conversion into platform encoding. It appears to work file in
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * C locale because default encoding is 8859-1 but fails in japanese locale.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param strIn the inputString @param charset character encoding of
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * inputString @param debug the debug instance to which debug messages need
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * to be printed.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return the decoded string
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster static public String URLDecodeField(String strIn, String charset,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster while (i < len) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster byte b = (byte) carr[i];
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster switch (b) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster b = (byte) carr[i];
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster "invalid hex char");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster b = (byte) carr[i];
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster "invalid hex char");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (charset == null || charset.length() == 0) {