FileDocCategorySizeDatePackage
LocaleHelpers.javaAPI DocphoneME MR2 API (J2ME)9950Wed May 02 18:00:46 BST 2007com.sun.j2me.global

LocaleHelpers

public class LocaleHelpers extends Object
This class provides general helper methods related with locale manipulation.

Fields Summary
Constructors Summary
Methods Summary
public static int[]byteArrayToIntArray(byte[] source)
Creates an array of int values from a given byte array. Every four sequential one-byte values are packed into one int value.

Caution: initial array's length must be a multiple of 4.

param
source original array of bytes
return
new array of ints

        int[] output = new int[source.length >> 2];
        for (int i = 0, j = 0; i < source.length; i += 4, ++j) {
            output[j] = ((source[i] & 0xff) << 24) | 
                    ((source[i + 1] & 0xff) << 16) | 
                    ((source[i + 2] & 0xff) << 8) | 
                    source[i + 3] & 0xff;
        }
        return output;
    
public static short[]byteArrayToShortArray(byte[] source)
Creates an array of short values from a given byte array. Every two sequential one-byte values are packed into one short value.

Caution: initial array must have even length.

param
source original array of bytes
return
new array of shorts

        short[] output = new short[source.length >> 1];
        for (int i = 0, j = 0; i < source.length; i += 2, ++j) {
            output[j] = (short)(((source[i] & 0xff) << 8) | 
                    source[i + 1] & 0xff);
        }
        return output;
    
private static java.lang.StringextractElement(java.lang.String input)

        String trimmed = input.trim();
        int length = trimmed.length();
        
        if ((length >= 2) && (trimmed.charAt(0) == '"") && 
                (trimmed.charAt(length - 1) == '"")) {
            return trimmed.substring(1, length - 1);
        }
        
        return trimmed;
    
public static java.lang.StringgetParentLocale(java.lang.String locale)
Returns parent for the given locale. Parent locale is obtained by removing the last component of locale code, which is in form: <language>-<country>-<variant>. Parent locale for <language> is empty string.

param
locale the locale to obtain parent of.
return
parent locale or null if locale doesn't have parent (if locale is already null or empty string.


        if (locale == null) {
            return null;
        }
        int len = locale.length();
        if (len == 0) {
            return null;
        }

        if (len > 6) {
            return locale.substring(0, 5);
        }
        if (len > 3) {
            return locale.substring(0, 2);
        }
        return "";
    
public static intindexInSupportedLocales(java.lang.String locale, java.lang.String[] locales)
Get an index of the given locale in array of locales returned from any of getSupportedLocales methods (e.g. {@link javax.microedition.global.ResourceManager#getSupportedLocales}).

param
locale the locale to search for
param
locales array of locales to search in
return
locale index in the given array, or -1 if the array doesn't contain the specified locale


        if (locale == null || locales == null || locales.length == 0) {
            return -1;
        }

        for (int i = 0; i < locales.length; i++) {
            if (locale.equals(locales[i])) {
                return i;
            }
        }

        return -1;
    
public static booleanisValidLocale(java.lang.String locale)
Checks whether given locale is valid or not according to JSR 238 specification, i.e. is in form of <language>-<country>-<variant>. Restrictions apply to length and validity of characters of language part, country part and separators. No checking of variant is performed.

param
locale locale code
return
true if locale is valid, false otherwise

        if (locale == null) {
            // Null locale is acceptable according to MIDP 2.0.
            return true;
        }

        int len = locale.length();
	if (len == 0) {
	    return true;
	}
        if (len < 2) {
            // Language part, if present, must be 2 characters long.
            return false;
        }

        char ch = locale.charAt(0);
        if (ch < 'a" || ch > 'z") {
            return false;
        }
        ch = locale.charAt(1);
        if (ch < 'a" || ch > 'z") {
            return false;
        }

        if (len > 2) {
            if (len < 5 || len == 6) {
                // Country part, if present, must be 2 characters long.
                return false;
            }

            ch = locale.charAt(2);
            if (ch != '-" && ch != '_") {
                return false;
            }
            ch = locale.charAt(3);
            if (ch < 'A" || ch > 'Z") {
                return false;
            }
            ch = locale.charAt(4);
            if (ch < 'A" || ch > 'Z") {
                return false;
            }

            if (len > 6) {
                ch = locale.charAt(5);
                if (ch != '-" && ch != '_") {
                    return false;
                }
            }
        }

        return true;
    
public static java.lang.StringnormalizeLocale(java.lang.String locale)
Replaces characters (expected dashes or underscores) on appropriate positions of locale code by dashes. The resulting locale name is likely to conform with MIDP 2.0 specification.

param
locale locale code
return
corrected locale code, or null in case of invalid format of given locale

        if (locale == null) {
            return null;
        }

        int len = locale.length();
        if (len > 3) {
            locale = locale.substring(0, 2) + '-" + locale.substring(3);
            if (len > 6) {
                locale = locale.substring(0, 5) + '-" + locale.substring(6);
            }
        }

        return locale;
    
public static java.lang.String[]splitString(java.lang.String input, java.lang.String separator, int limit)
Splits the given string into parts according to the given separator. It stops parsing the string after the first limit parts are extracted. If limit equals -1 all parts are extracted.

param
input the input string
param
separator the separator
param
limit the maximum number of parts or -1 if no maximum is given
return
the extracted parts as an array of strings

        int index = input.indexOf(separator);
        int count = 1;
        
        // get the count of elements
        while (index >= 0) {
            ++count;
            index = input.indexOf(separator, index + 1);
        }
        
        if ((limit != -1) && (count > limit)) {
            count = limit;
        }
        
        // create array for elements
        String[] elements = new String[count];
        splitString(elements, input, separator, limit);
        
        return elements;
    
public static intsplitString(java.lang.String[] elements, java.lang.String input, java.lang.String separator, int limit)
Splits the given string into parts according to the given separator. It stops parsing the string after the first limit parts are extracted. If limit equals -1 all parts are extracted.

param
elements an array of strings where to put the results to
param
input the input string
param
separator the separator
param
limit the maximum number of parts or -1 if no maximum is given
return
the number of extracted parts

        int lastIndex = -1;
        int index = input.indexOf(separator);
        int count = 0;

        if ((limit == -1) || (limit > elements.length)) {
            limit = elements.length;
        }
        
        if (limit == 0) {
            return 0;
        }
        
        while (index >= 0) {
            elements[count++] = extractElement(input.substring(lastIndex + 1,
                    index));
            if (count == limit) {
                return count;
            }
            lastIndex = index;
            index = input.indexOf(separator, lastIndex + 1);
        }

        elements[count++] = extractElement(input.substring(lastIndex + 1,
                input.length()));
        
        return count;