Search in sources :

Example 36 with ICUResourceBundle

use of android.icu.impl.ICUResourceBundle in project j2objc by google.

the class LocaleData method getExemplarSet.

/**
 * Returns the set of exemplar characters for a locale.
 *
 * @param options   Bitmask for options to apply to the exemplar pattern.
 *                  Specify zero to retrieve the exemplar set as it is
 *                  defined in the locale data.  Specify
 *                  UnicodeSet.CASE to retrieve a case-folded exemplar
 *                  set.  See {@link UnicodeSet#applyPattern(String,
 *                  int)} for a complete list of valid options.  The
 *                  IGNORE_SPACE bit is always set, regardless of the
 *                  value of 'options'.
 * @param extype    The type of exemplar set to be retrieved,
 *                  ES_STANDARD, ES_INDEX, ES_AUXILIARY, or ES_PUNCTUATION
 * @return          The set of exemplar characters for the given locale.
 *                  If there is nothing available for the locale,
 *                  then null is returned if {@link #getNoSubstitute()} is true, otherwise the
 *                  root value is returned (which may be UnicodeSet.EMPTY).
 * @exception       RuntimeException if the extype is invalid.
 */
public UnicodeSet getExemplarSet(int options, int extype) {
    String[] exemplarSetTypes = { "ExemplarCharacters", "AuxExemplarCharacters", "ExemplarCharactersIndex", "ExemplarCharactersCurrency", "ExemplarCharactersPunctuation" };
    if (extype == ES_CURRENCY) {
        // currency symbol exemplar is no longer available
        return noSubstitute ? null : UnicodeSet.EMPTY;
    }
    try {
        // will throw an out-of-bounds exception
        final String aKey = exemplarSetTypes[extype];
        ICUResourceBundle stringBundle = (ICUResourceBundle) bundle.get(aKey);
        if (noSubstitute && !bundle.isRoot() && stringBundle.isRoot()) {
            return null;
        }
        String unicodeSetPattern = stringBundle.getString();
        return new UnicodeSet(unicodeSetPattern, UnicodeSet.IGNORE_SPACE | options);
    } catch (ArrayIndexOutOfBoundsException aiooe) {
        throw new IllegalArgumentException(aiooe);
    } catch (Exception ex) {
        return noSubstitute ? null : UnicodeSet.EMPTY;
    }
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle) UnicodeSet(android.icu.text.UnicodeSet) MissingResourceException(java.util.MissingResourceException)

Example 37 with ICUResourceBundle

use of android.icu.impl.ICUResourceBundle in project j2objc by google.

the class ICUResourceBundleTest method TestPreventFallback.

@Test
public void TestPreventFallback() {
    String noFallbackResource = "string_in_te_no_te_IN_fallback";
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance("android/icu/dev/data/testdata", "te_IN_NE", testLoader);
    try {
        rb.getStringWithFallback(noFallbackResource);
        fail("Expected MissingResourceException.");
    } catch (MissingResourceException e) {
    // Expected
    }
    rb.getStringWithFallback("string_only_in_te");
    rb = (ICUResourceBundle) UResourceBundle.getBundleInstance("android/icu/dev/data/testdata", "te", testLoader);
    rb.getStringWithFallback(noFallbackResource);
}
Also used : MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) Test(org.junit.Test)

Example 38 with ICUResourceBundle

use of android.icu.impl.ICUResourceBundle in project j2objc by google.

the class ICUResourceBundleTest method TestAliases.

@Test
public void TestAliases() {
    String simpleAlias = "Open";
    UResourceBundle rb = UResourceBundle.getBundleInstance("android/icu/dev/data/testdata", "testaliases", testLoader);
    if (rb == null) {
        warnln("could not load testaliases data");
        return;
    }
    UResourceBundle sub = rb.get("simplealias");
    String s1 = sub.getString("simplealias");
    if (s1.equals(simpleAlias)) {
        logln("Alias mechanism works for simplealias");
    } else {
        errln("Did not get the expected output for simplealias");
    }
    {
        try {
            rb = UResourceBundle.getBundleInstance("android/icu/dev/data/testdata", "testaliases", testLoader);
            sub = rb.get("nonexisting");
            errln("Did not get the expected exception for nonexisting");
        } catch (MissingResourceException ex) {
            logln("Alias mechanism works for nonexisting alias");
        }
    }
    {
        rb = UResourceBundle.getBundleInstance("android/icu/dev/data/testdata", "testaliases", testLoader);
        sub = rb.get("referencingalias");
        s1 = sub.getString();
        if (s1.equals("H:mm:ss")) {
            logln("Alias mechanism works for referencingalias");
        } else {
            errln("Did not get the expected output for referencingalias");
        }
    }
    {
        UResourceBundle rb1 = UResourceBundle.getBundleInstance("android/icu/dev/data/testdata", "testaliases", testLoader);
        if (rb1 != rb) {
            errln("Caching of the resource bundle failed");
        } else {
            logln("Caching of resource bundle passed");
        }
        sub = rb1.get("testGetStringByKeyAliasing");
        s1 = sub.get("KeyAlias0PST").getString();
        if (s1.equals("America/Los_Angeles")) {
            logln("Alias mechanism works for KeyAlias0PST");
        } else {
            errln("Did not get the expected output for KeyAlias0PST");
        }
        s1 = sub.getString("KeyAlias1PacificStandardTime");
        if (s1.equals("Pacific Standard Time")) {
            logln("Alias mechanism works for KeyAlias1PacificStandardTime");
        } else {
            errln("Did not get the expected output for KeyAlias1PacificStandardTime");
        }
        s1 = sub.getString("KeyAlias2PDT");
        if (s1.equals("PDT")) {
            logln("Alias mechanism works for KeyAlias2PDT");
        } else {
            errln("Did not get the expected output for KeyAlias2PDT");
        }
        s1 = sub.getString("KeyAlias3LosAngeles");
        if (s1.equals("Los Angeles")) {
            logln("Alias mechanism works for KeyAlias3LosAngeles. Got: " + s1);
        } else {
            errln("Did not get the expected output for KeyAlias3LosAngeles. Got: " + s1);
        }
    }
    {
        sub = rb.get("testGetStringByIndexAliasing");
        s1 = sub.getString(0);
        if (s1.equals("America/Los_Angeles")) {
            logln("Alias mechanism works for testGetStringByIndexAliasing/0. Got: " + s1);
        } else {
            errln("Did not get the expected output for testGetStringByIndexAliasing/0. Got: " + s1);
        }
        s1 = sub.getString(1);
        if (s1.equals("Pacific Standard Time")) {
            logln("Alias mechanism works for testGetStringByIndexAliasing/1");
        } else {
            errln("Did not get the expected output for testGetStringByIndexAliasing/1");
        }
        s1 = sub.getString(2);
        if (s1.equals("PDT")) {
            logln("Alias mechanism works for testGetStringByIndexAliasing/2");
        } else {
            errln("Did not get the expected output for testGetStringByIndexAliasing/2");
        }
        s1 = sub.getString(3);
        if (s1.equals("Los Angeles")) {
            logln("Alias mechanism works for testGetStringByIndexAliasing/3. Got: " + s1);
        } else {
            errln("Did not get the expected output for testGetStringByIndexAliasing/3. Got: " + s1);
        }
    }
    // Note: Following test cases are no longer working because collation data is now in the collation module
    // {
    // sub = rb.get("testAliasToTree" );
    // 
    // ByteBuffer buf = sub.get("standard").get("%%CollationBin").getBinary();
    // if(buf==null){
    // errln("Did not get the expected output for %%CollationBin");
    // }
    // }
    // 
    // rb = (UResourceBundle) UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_COLLATION_BASE_NAME,"zh_TW");
    // UResourceBundle b = (UResourceBundle) rb.getObject("collations");
    // if(b != null){
    // if(b.get(0).getKey().equals( "default")){
    // logln("Alias mechanism works");
    // }else{
    // errln("Alias mechanism failed for zh_TW collations");
    // }
    // }else{
    // errln("Did not get the expected object for collations");
    // }
    // Test case for #7996
    {
        UResourceBundle bundle = UResourceBundle.getBundleInstance("android/icu/dev/data/testdata", "te", testLoader);
        UResourceBundle table = bundle.get("tableT7996");
        try {
            String s = table.getString("a7996");
            logln("Alias in nested table referring one in sh worked - " + s);
        } catch (MissingResourceException e) {
            errln("Alias in nested table referring one in sh failed");
        }
        try {
            String s = ((ICUResourceBundle) table).getStringWithFallback("b7996");
            logln("Alias with /LOCALE/ in nested table in root referring back to another key in the current locale bundle worked - " + s);
        } catch (MissingResourceException e) {
            errln("Alias with /LOCALE/ in nested table in root referring back to another key in the current locale bundle failed");
        }
    }
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) MissingResourceException(java.util.MissingResourceException) Test(org.junit.Test)

Example 39 with ICUResourceBundle

use of android.icu.impl.ICUResourceBundle in project j2objc by google.

the class ICUResourceBundleTest method TestJB4102.

@Test
public void TestJB4102() {
    try {
        ICUResourceBundle root = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "root");
        ICUResourceBundle t = null;
        // }
        try {
            t = root.getWithFallback("calendar/islamic-civil/eras/abbreviated/0/mikimaus/pera");
            errln("Second resource does not exist. How did it get here?\n");
        } catch (MissingResourceException ex) {
            logln("Got the expected exception");
        }
        if (t != null) {
            errln("t is not null!");
        }
    } catch (MissingResourceException e) {
        warnln("Could not load the locale data: " + e.getMessage());
    }
}
Also used : MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) Test(org.junit.Test)

Example 40 with ICUResourceBundle

use of android.icu.impl.ICUResourceBundle in project j2objc by google.

the class ICUResourceBundleTest method TestAliasFallback.

@Test
public void TestAliasFallback() {
    try {
        ULocale loc = new ULocale("en_US");
        ICUResourceBundle b = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, loc);
        ICUResourceBundle b1 = b.getWithFallback("calendar/hebrew/monthNames/format/abbreviated");
        if (b1 != null) {
            logln("loaded data for abbreviated month names: " + b1.getKey());
        }
    } catch (MissingResourceException ex) {
        warnln("Failed to load data for abbreviated month names");
    }
}
Also used : ULocale(android.icu.util.ULocale) MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) Test(org.junit.Test)

Aggregations

ICUResourceBundle (android.icu.impl.ICUResourceBundle)42 MissingResourceException (java.util.MissingResourceException)21 ULocale (android.icu.util.ULocale)13 Test (org.junit.Test)11 UResourceBundle (android.icu.util.UResourceBundle)8 HashMap (java.util.HashMap)4 Locale (java.util.Locale)4 Map (java.util.Map)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Calendar (android.icu.util.Calendar)2 HebrewCalendar (android.icu.util.HebrewCalendar)2 UResourceBundleIterator (android.icu.util.UResourceBundleIterator)2 ArrayList (java.util.ArrayList)2 Enumeration (java.util.Enumeration)2 TreeMap (java.util.TreeMap)2 ChineseDateFormat (android.icu.text.ChineseDateFormat)1 ChineseDateFormatSymbols (android.icu.text.ChineseDateFormatSymbols)1 DateFormat (android.icu.text.DateFormat)1 DateFormatSymbols (android.icu.text.DateFormatSymbols)1