FileDocCategorySizeDatePackage
LocaleBean.javaAPI DocExample12996Thu Jun 28 16:14:16 BST 2001com.ora.jsp.beans.locale

LocaleBean

public class LocaleBean extends Object implements Serializable
This class contains a number of methods for dealing with localized content. It's used by the com.ora.jsp.tags.generic localization tag handler classes, but can also be used stand-alone.
author
Hans Bergsten, Gefion software
version
1.0

Fields Summary
private Locale
locale
private Locale[]
requestLocales
private ResourceBundle
bundle
private String
supportedLangs
private String
bundleName
private String
language
private NumberFormat
numberFormat
private DateFormat
dateFormat
private String
charset
private Hashtable
parameters
Constructors Summary
Methods Summary
public java.util.DategetDate(java.lang.String date)
Returns the specified date String converted to a Date, parsed as defined by the currently selected locale.

        if (dateFormat == null) {
            dateFormat = DateFormat.getDateInstance(dateFormat.FULL, getLocale());
        }
        return dateFormat.parse(date);
    
public java.lang.StringgetDateString(java.util.Date date)
Returns the specified Date converted to a String, formatted as defined by the currently selected locale.

        if (dateFormat == null) {
            dateFormat = DateFormat.getDateInstance(dateFormat.FULL, getLocale());
        }
        return dateFormat.format(date);
    
private java.lang.StringgetDecodedValue(java.lang.String value)
This method returns a new String created from the specified String, encoded as 8859_1 converted to the currently set charset.

        if (charset == null) {
            return value;
        }
        return new String(value.getBytes("8859_1"), charset);
    
private java.util.LocalegetDefaultLocale()
Returns a Locale for the default language.

        StringTokenizer st = new StringTokenizer(supportedLangs, ",");
        String defaultLang = st.nextToken().trim();
        return toLocale(defaultLang);
    
public doublegetDouble(java.lang.String number)
Returns the specified number String converted to a double, parsed as defined by the currently selected locale.

        return getNumber(number).doubleValue();
    
public floatgetFloat(java.lang.String number)
Returns the specified number String converted to a float, parsed as defined by the currently selected locale.

        return getNumber(number).floatValue();
    
public intgetInt(java.lang.String number)
Returns the specified number String converted to a int, parsed as defined by the currently selected locale.

        return getNumber(number).intValue();
    
public java.lang.StringgetLanguage()
Returns the language code for the currently selected locale.

        if (language == null) {
            language = toLanguage(getLocale());
        }
        return language;
    
public java.util.LocalegetLocale()
Returns a Locale. The Locale is constructed based on the language property, if set. If not, the Locale determined based on the Accept-Language header (the requestLocales property) is returned, if set. If the Locale found this way matches one of the supported languages, it's returned. Otherwise the Locale for first language in the list of supported languages is returned.

        if (locale == null) {
            if (language != null && isSupportedLang(language)) {
                locale = toLocale(language);
            }
            else if (requestLocales != null) {
                locale = getSupportedLocale(requestLocales);
            }
            if (locale == null) {
                locale = getDefaultLocale();
            }
        }
        return locale;
    
public longgetLong(java.lang.String number)
Returns the specified number String converted to a long, parsed as defined by the currently selected locale.

        return getNumber(number).longValue();
    
private java.lang.NumbergetNumber(java.lang.String number)
Returns the specified String as a Number, parsed based on the currently selected locale.

        if (numberFormat == null) {
            numberFormat = NumberFormat.getNumberInstance(getLocale());
        }
        return numberFormat.parse(number);
    
public java.lang.StringgetNumberString(double number)
Returns the specified number converted to a String, formatted as defined by the currently selected locale.

        if (numberFormat == null) {
            numberFormat = NumberFormat.getNumberInstance(getLocale());
        }
        return numberFormat.format(number);
    
public java.lang.StringgetPageName(java.lang.String basePageName)
Returns a version of the specified page name with a language/country suffix for the currently selected locale.

        StringBuffer pageName = new StringBuffer();
        Locale locale = getLocale();
        if (locale.equals(getDefaultLocale())) {
            pageName.append(basePageName);
        }
        else {
            String suffix = getLocale().toString();
            int extPos = basePageName.indexOf(".");
            if (extPos != -1) {
                pageName.append(basePageName.substring(0, extPos)).
                    append("_").append(suffix).
                    append(basePageName.substring(extPos));
            }
            else {
                pageName.append(basePageName).
                    append("_").append(suffix);
            }
        }
        return pageName.toString();
    
public java.lang.StringgetParameter(java.lang.String parameter)
Returns the first all value for the specified parameter, parsed using the currently specified charset.

        String firstValue = null;
        String[] values = getParameterValues(parameter);
        if (values != null && values.length > 0) {
            firstValue = values[0];
        }
        return firstValue;
    
public java.util.EnumerationgetParameterNames()
Returns an Enumeration of all parameter names.

        if (parameters == null) {
            parameters = new Hashtable();
        }
        return parameters.keys();
    
public java.lang.String[]getParameterValues(java.lang.String parameter)
Returns an array of all values for the specified parameter, parsed using the currently specified charset.

        String[] values = null;
        if (parameters != null) {
            String[] encodedValues = (String[]) parameters.get(parameter);
            if (encodedValues != null) {
                values = new String[encodedValues.length];
                for (int i = 0; i < encodedValues.length; i++) {
                    values[i] = getDecodedValue(encodedValues[i]);
                }
            }
        }
        return values;
    
private java.util.LocalegetSupportedLocale(java.util.Locale[] locales)
Returns the first Locale that matches a supported language, or the default Locale if no match.

        Locale locale = getDefaultLocale();
        for (int i = 0; i < locales.length; i++) {
            if (isSupportedLang(toLanguage(locales[i]))) {
                locale = locales[i];
                break;
            }
        }
        return locale;
    
public java.lang.StringgetText(java.lang.String resourceName)
Returns the text resource for the specified key, with the best match for the currently selected locale.

        if (bundle == null) {
            bundle = ResourceBundle.getBundle(bundleName, getLocale());
        }
        return bundle.getString(resourceName);
    
private booleanisEqual(java.util.Locale[] arr1, java.util.Locale[] arr2)
Returns true if the the specifed Locale arrays contains exactly the same locale information.

        boolean isEqual = true;
        if (arr1.length != arr2.length) {
            isEqual = false;
        }
        else {
            for (int i = 1; i < arr1.length; i++) {
                if (!arr1[i].equals(arr2[i])) {
                    isEqual = false;
                    break;
                }
            }
        }
        return isEqual;
    
private booleanisSupportedLang(java.lang.String language)
Returns true if the specified languge is a supported language.

        return supportedLangs.indexOf(language) != -1;
    
private voidresetBundle()
Resets the ResourceBundle.

        bundle = null;
    
private voidresetLocale()
Resets all locale dependent variables.

        locale = null;
        numberFormat = null;
        dateFormat = null;
        bundle = null;
    
public voidsetBundleName(java.lang.String bundleName)
Sets the bundle name.

Resets the current ResourceBundle so that a new will be created with the new bundle name the next time the ResourceBundle is retrieved.

        if (this.bundleName != null && !bundleName.equals(this.bundleName)) {
            resetBundle();
        }
        this.bundleName = bundleName;
    
public voidsetCharset(java.lang.String charset)
Sets the charset used to parse request parameters.

        this.charset = charset;
    
public voidsetLanguage(java.lang.String language)
Sets the user selected language/country code.

Resets the currently selected locale if the new language is different from the current. This is done so that all possible locale sources will be evaluated again the next time the locale property is retrieved.

        if (this.language != null && !language.equals(this.language)) {
            resetLocale();
        }
        this.language = language;
    
public voidsetParameters(java.util.Hashtable parameters)
Sets the parameter list.

        this.parameters = parameters;
    
public voidsetRequestLocales(java.util.Locale[] locales)
Sets the set of locales received with the request, and resets the currently selected locale if the new set is different from the current set. This is done so that all possible locale sources will be evaluated again the next time the locale property is retrieved.

        if (requestLocales != null && !isEqual(requestLocales, locales)) {
            resetLocale();
        }
        requestLocales = locales;
    
public voidsetSupportedLangs(java.lang.String supportedLangs)
Sets the set of supported languages, provided as a comma separated list of country/language codes. This is a mandatory property.

Resets the currently selected locale if the new set is different from the current set. This is done so that all possible locale sources will be evaluated again the next time the locale property is retrieved.

        if (this.supportedLangs != null && 
            !this.supportedLangs.equals(supportedLangs)) {
            resetLocale();
        }
        this.supportedLangs = supportedLangs;
    
private java.lang.StringtoLanguage(java.util.Locale locale)
Returns the language/country code for the specified Locale.

        String language = locale.getLanguage();
        String country = locale.getCountry();
        if (country != null && country.length() > 0) {
            language = language + "-" + country;
        }
        return language;
    
private java.util.LocaletoLocale(java.lang.String language)
Returns a Locale for the specifed language/country code.

        Locale locale = null;
        int countrySeparator = language.indexOf("-");
        if (countrySeparator != -1) {
            locale = new Locale(language.substring(0, countrySeparator),
                language.substring(countrySeparator + 1));
        }
        else {
            locale = new Locale(language, "");
        }
        return locale;