Locale.java revision 8f8adeb1540d89588dba4c26d00a6ed6887cf6de
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at opensso/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* $Id: Locale.java,v 1.7 2009/07/07 17:32:02 bina Exp $
*
*/
/*
* Portions Copyrighted 2014 ForgeRock AS.
* Portions Copyrighted 2014 Nomura Research Institute, Ltd.
*/
/**
* This class <code>Locale.java</code> is a utility that provides
* functionality for applications and services to internationalize their
* messages.
* @supported.all.api
*/
public class Locale {
static BitSet dontEncode;
private static final int LOCALE_STRING_MAX_LEN = 5;
private static final SimpleDateFormat normalizedDateFormat;
/*
* The list of characters that are not encoded have been determined by
* referencing O'Reilly's "HTML: The Definitive Guide" (page 164).
*/
static {
// Intialize static variables
int i;
for (i = 'a'; i <= 'z'; i++) {
dontEncode.set(i);
}
for (i = 'A'; i <= 'Z'; i++) {
dontEncode.set(i);
}
for (i = '0'; i <= '9'; i++) {
dontEncode.set(i);
}
* encoding a space to a + is done in the
* encode() method
*/
}
}
/**
* Gets the locale object for the specified localized string format.
*
* @param stringformat
* String representation of the locale. Examples:
* <code>en_US, en_UK, ja_JP</code>.
* @return the <code>java.util.locale</code> object.
*/
if (stringformat == null) {
return locale;
}
} else {
}
if (tk.hasMoreTokens()) {
}
if (tk.hasMoreTokens()) {
}
if (tk.hasMoreTokens()) {
}
}
return locale;
}
/**
* Returns locale from accept-language header HTTP accept language header
* can have more than one language in the header, we honor the first
* language as locale
*
* @param langstr
* Value from Accept-Language header of HTTP
* @return locale string in this format <code>en_US, fr</code>
*/
return null;
int leadSpace = 0;
/*
* Accept Language Syntax Accept-Language = "Accept-Language" ":" 1#(
* language-range [ ";" "q" "=" qvalue ] ) language-range = ( ( 1*8ALPHA
* *("-" 1*8ALPHA ) ) | "*" ) For more info Read RFC 2616 Examples:
* Accept-Language: da, en-gb;q=0.8, en;q=0.7 Accept-Language: en-gb, en
* Accept-Language: ja Accept-Language: zh-cn Accept-Language: *
*
* We will use first language as locale. We will not process any
* further.Netscape,IE will give mostly one language as Accept-Language
* header. Max length of string is 5 lang-> 2chars , country -> two
* chars and separator is -
*/
try {
leadSpace++;
boolean isCountry = false;
if (ch == '*')
return null;
// "*" can be a valid accept-lang but does
// give idea about locale, return null and force the caller to
// use
// default locale
if (ch == '-') {
isCountry = true;
// by accept-lang
} else if (isCountry) {
}
}
} catch (IndexOutOfBoundsException ex) {
return null;
}
}
/**
* Gets locale from accept-language header HTTP accept language header can
* have more than one language in the header, we honor the first language as
* locale
*
* @param langStr
* Value from Accept-Language header of HTTP
* @return locale string in this format <code>en_US, fr</code>.
*/
return null;
}
/**
* Gets the resource bundle corresponding to the specified locale and the
* localized property file name.
*
* @param bundle
* Localized property file name.
* @param stringformat
* String representation of the locale.
*
* @return <code>java.util.ResourceBundle</code> object.
*
*/
}
return getInstallResourceBundle(bundle);
}
/**
* Gets the default install resource bundle for the default locale
*
* @param bundle
* Localized property file name
* @return the install resource bundle object
*/
}
/**
* Gets the default locale
*
* @return the default Locale object
*/
return defaultLocale;
}
/**
* Formats messages using <code>MessageFormat</code> Class.
*
* @param formatStr
* string format template.
* @param obj1
* object to be added to the template.
* @return formatted message.
*/
}
/**
* Formats to format messages using <code>MessageFormat</code> Class.
* given params to format them with
*
* @param formatStr
* string format template.
* @param objs
* objects to be added to the template.
* @return formatted message.
*/
}
/**
* Returns the Date object from the date string in <code>ISO-8601</code>
* format. OpenSSO stores date in <code>ISO-8601</code> format
*
* @param dateString
* in the format <code>2002/12/31 23:59</code>.
* @return Date object
*/
if (dateString == null)
return null;
+ "Unable to parse date string");
}
if (debug.messageEnabled()) {
+ ")=" + date);
}
return date;
}
/**
* Gets Date object from date string with specified locale.
*
* @param dateString
* date string
* @param locale
* Locale object
* @param dateSyntax
* syntax of the date string.
*
* @return Date object returned if <code>dateString</code> matches the
* <code> dateSyntax</code>. If the syntax or date string is
* empty, or the string does not match the syntax, null will be
* returned.
*/
if (debug.messageEnabled()) {
}
return null;
}
sdf.setLenient(false);
}
return date;
}
/**
* Gets Date object from date string with specified locale. Syntax of date
* string is defined in amUser_<locale> properties file.
*
* @param dateString
* date string
* @param locale
* Locale object
*
* @return Date object. null will be returned if error happens
*/
+ "bundle. Locale = " + locale);
return null;
}
try {
+ ". Locale " + locale);
return null;
}
}
/**
* Converts the Date object into <code>ISO-8601</code> format
*
* @param date
* to be normalized.
* @return date in <code>ISO8601</code> format
* <code>2002/12/31 11:59</code>.
*/
return null;
}
/**
* Gets date string from date with specified locale.
*
* @param date
* Date object
* @param locale
* Locale object
*
* @return date string. null will be returned if error happens
*/
return null;
}
+ "bundle. Locale = " + locale);
return null;
}
try {
+ ". Locale " + locale);
return null;
}
if (debug.messageEnabled()) {
}
}
/**
* Converts date string from source locale to destination locale
*
* @param srcDateString
* source date string
* @param srcLocale
* source Locale object
* @param dstLocale
* destination Locale object
*
* @return converted date string. null will be returned if error happens
*/
}
/**
* Gets the localized string for the specified key formatted as per passed
* parameters.
*
* @param rb
* resource bundle.
* @param resource
* the specified key.
* @param params
* formatting done as per these parameters.
*
* @return the localized string representation formatted as per passed
* parameters.
*/
try {
if (debug.messageEnabled()) {
}
}
return resource;
}
/**
* Gets the localized string for the specified key from the specified
* Resource or from the specified default resource formatted as per provided
* parameters.
*
* @param rb
* resource bundle.
* @param resource
* the specified key.
* @param defaultRb
* Default resource bundle.
* @param params
* formatting done as per these parameters.
*
* @return the localized string representation formatted as per passed
* parameters.
*
*/
try {
try {
if (debug.messageEnabled()) {
}
params);
if (debug.messageEnabled()) {
+ resource);
}
}
}
return resource;
}
/**
* Gets the localized string for the specified key
*
* @param rb
* resource bundle.
* @param resource
* the specified key.
* @param debug
* the debug instance to which the debug messages need to be
* printed.
*
* @return the localized string representation
*/
try {
if (debug.messageEnabled()) {
}
}
return resource;
}
/**
* Gets the localized string for the specified key from the specified
* Resource or from the specified default resource
*
* @param rb
* resource bundle.
* @param resource
* the specified key.
* @param debug
* the debug instance to which the debug messages need to be
* printed.
* @param defaultRb
* Default resource bundle.
*
* @return the localized string representation
*/
try {
try {
if (debug.messageEnabled()) {
}
if (debug.messageEnabled()) {
+ resource);
}
}
}
return resource;
}
/**
* Gets the localized string for the specified key.
*
* @param rb
* resource bundle.
* @param resource
* the specified key.
* @return the localized string representation
*/
try {
if (debug.messageEnabled()) {
}
}
return resource;
}
/**
* Gets the localized string for the specified key from the specified
* Resource or from the specified default resource.
*
* @param rb
* resource bundle.
* @param resource
* the specified key.
* @param defaultRb
* Default resource bundle.
* @return the localized string representation
*/
try {
try {
if (debug.messageEnabled()) {
}
if (debug.messageEnabled()) {
+ resource);
}
}
}
return resource;
}
/**
* This method is replacement function for <code>URLEncoder</code>
* Function URL encoder function converts input string into
* <code>URLEncoded</code> byte stream after converting Unicode string
* into bytes using native encoding. The <code>URLEncoder</code> does not
* work for OpenSSO if default encoding is not
* <code>UTF-8</code>, hence this method was written.
*
* @param input
* the input string.
* @param enc
* the encoding format.
* @return the encoded string.
* @throws UnsupportedEncodingException
*/
throws UnsupportedEncodingException {
int c = byteOut[i] & 0xff;
if (dontEncode.get(c)) {
if (c == ' ') {
c = '+';
}
} else {
}
}
}
}
}
/**
* This method is replacement function for <code>URLEncoder<code> Function
* URL encoder function converts input string into <code>URLencoded</code>
* byte stream after converting Unicode string into bytes using native
* encoding. The <code>URLEncoder</code> does not work for Sun Java System
* OpenSSO if default encoding is not <code>UTF-8</code>, hence this
* method was written.
*
* @param input the input string
* @param enc the encoding format
* @param debug the debug instance to which debug messages need to
* be printed
*
* @return the encoded string
*/
byte[] byteOut;
try {
} catch (UnsupportedEncodingException ex) {
}
return input;
}
int c = byteOut[i] & 0xff;
if (dontEncode.get(c)) {
if (c == ' ') {
c = '+';
}
} else {
}
}
}
}
}
}
/*
* Translate the individual field values in the encoding value Do not use
* getBytes instead convert unicode into bytes by casting. Using getBytes
* results in conversion into platform encoding. It appears to work file in
* C locale because default encoding is 8859-1 but fails in japanese locale.
*
* @param strIn the inputString @param charset character encoding of
* inputString @param debug the debug instance to which debug messages need
* to be printed.
*
* @return the decoded string
*/
return strIn;
}
try {
int i = 0;
int offset = 0;
while (i < len) {
byte b = (byte) carr[i];
switch (b) {
case '%':
int val = 0;
if (i + 2 < len) {
i++;
b = (byte) carr[i];
if ('a' <= b && b <= 'f') {
b -= caseDiff;
}
if ('A' <= b && b <= 'F') {
} else if ('0' <= b && b <= '9') {
} else {
throw new IllegalArgumentException(
"invalid hex char");
}
i++;
b = (byte) carr[i];
if ('a' <= b && b <= 'f') {
b -= caseDiff;
}
if ('A' <= b && b <= 'F') {
} else if ('0' <= b && b <= '9') {
val += b - '0';
} else {
throw new IllegalArgumentException(
"invalid hex char");
}
i++;
} else {
}
break;
default:
break;
}
}
} else {
}
}
return strOut;
}
}