Search in sources :

Example 6 with ICUResourceBundle

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

the class CompactDecimalDataCache method load.

private static DataBundle load(ULocale ulocale) throws MissingResourceException {
    DataBundle dataBundle = DataBundle.createEmpty();
    String nsName = NumberingSystem.getInstance(ulocale).getName();
    ICUResourceBundle r = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, ulocale);
    CompactDecimalDataSink sink = new CompactDecimalDataSink(dataBundle, ulocale);
    sink.isFallback = false;
    // First load the number elements data from nsName if nsName is not Latin.
    if (!nsName.equals(LATIN_NUMBERING_SYSTEM)) {
        sink.isLatin = false;
        try {
            r.getAllItemsWithFallback(NUMBER_ELEMENTS + "/" + nsName, sink);
        } catch (MissingResourceException e) {
        // Silently ignore and use Latin
        }
        // Set the "isFallback" flag for when we read Latin
        sink.isFallback = true;
    }
    // Now load Latin, which will fill in things that were left out from above.
    sink.isLatin = true;
    r.getAllItemsWithFallback(NUMBER_ELEMENTS + "/" + LATIN_NUMBERING_SYSTEM, sink);
    // If longData is empty, default it to be equal to shortData
    if (dataBundle.longData.isEmpty()) {
        dataBundle.longData = dataBundle.shortData;
    }
    // Check for "other" variants in each of the three data classes
    checkForOtherVariants(dataBundle.longData, ulocale, LONG_STYLE);
    checkForOtherVariants(dataBundle.shortData, ulocale, SHORT_STYLE);
    checkForOtherVariants(dataBundle.shortCurrencyData, ulocale, SHORT_CURRENCY_STYLE);
    // Resolve missing elements
    fillInMissing(dataBundle.longData);
    fillInMissing(dataBundle.shortData);
    fillInMissing(dataBundle.shortCurrencyData);
    // Return the data bundle
    return dataBundle;
}
Also used : MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 7 with ICUResourceBundle

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

the class CollationMiscTest method hasCollationElements.

private static final boolean hasCollationElements(Locale locale) {
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, locale);
    if (rb != null) {
        try {
            String collkey = rb.getStringWithFallback("collations/default");
            ICUResourceBundle elements = rb.getWithFallback("collations/" + collkey);
            if (elements != null) {
                return true;
            }
        } catch (Exception e) {
        }
    }
    return false;
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 8 with ICUResourceBundle

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

the class CollationMiscTest method TestLocaleRuleBasedCollators.

@Test
public void TestLocaleRuleBasedCollators() {
    if (TestFmwk.getExhaustiveness() < 5) {
        // not serious enough to run this
        return;
    }
    Locale[] locale = Collator.getAvailableLocales();
    String prevrule = null;
    for (int i = 0; i < locale.length; i++) {
        Locale l = locale[i];
        try {
            ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, l);
            String collkey = rb.getStringWithFallback("collations/default");
            ICUResourceBundle elements = rb.getWithFallback("collations/" + collkey);
            if (elements == null) {
                continue;
            }
            String rule = null;
            /*
                  Object[][] colldata = (Object[][])elements;
                  // %%CollationBin
                  if (colldata[0][1] instanceof byte[]){
                  rule = (String)colldata[1][1];
                  }
                  else {
                  rule = (String)colldata[0][1];
                  }
                */
            rule = elements.getString("Sequence");
            RuleBasedCollator col1 = (RuleBasedCollator) Collator.getInstance(l);
            if (!rule.equals(col1.getRules())) {
                errln("Rules should be the same in the RuleBasedCollator and Locale");
            }
            if (rule != null && rule.length() > 0 && !rule.equals(prevrule)) {
                RuleBasedCollator col2 = new RuleBasedCollator(rule);
                if (!col1.equals(col2)) {
                    errln("Error creating RuleBasedCollator from " + "locale rules for " + l.toString());
                }
            }
            prevrule = rule;
        } catch (Exception e) {
            warnln("Error retrieving resource bundle for testing: " + e.toString());
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) RuleBasedCollator(android.icu.text.RuleBasedCollator) ICUResourceBundle(android.icu.impl.ICUResourceBundle) Test(org.junit.Test)

Example 9 with ICUResourceBundle

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

the class ICUResourceBundleTest method TestCLDRStyleAliases.

@Test
public void TestCLDRStyleAliases() {
    String result = null;
    String expected = null;
    String[] expects = new String[] { "", "a41", "a12", "a03", "ar4" };
    logln("Testing CLDR style aliases......\n");
    UResourceBundle rb = UResourceBundle.getBundleInstance("android/icu/dev/data/testdata", "te_IN_REVISED", testLoader);
    ICUResourceBundle alias = (ICUResourceBundle) rb.get("a");
    for (int i = 1; i < 5; i++) {
        String resource = "a" + i;
        UResourceBundle a = (alias).getWithFallback(resource);
        result = a.getString();
        if (result.equals(expected)) {
            errln("CLDR style aliases failed resource with name " + resource + "resource, exp " + expects[i] + " , got " + result);
        }
    }
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) ICUResourceBundle(android.icu.impl.ICUResourceBundle) Test(org.junit.Test)

Example 10 with ICUResourceBundle

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

the class ICUResourceBundleTest method TestUResourceBundleCoverage.

@Test
public void TestUResourceBundleCoverage() {
    Locale locale = null;
    ULocale ulocale = null;
    String baseName = null;
    UResourceBundle rb1, rb2, rb3, rb4, rb5, rb6, rb7;
    rb1 = UResourceBundle.getBundleInstance(ulocale);
    rb2 = UResourceBundle.getBundleInstance(baseName);
    rb3 = UResourceBundle.getBundleInstance(baseName, ulocale);
    rb4 = UResourceBundle.getBundleInstance(baseName, locale);
    rb5 = UResourceBundle.getBundleInstance(baseName, ulocale, testLoader);
    rb6 = UResourceBundle.getBundleInstance(baseName, locale, testLoader);
    try {
        rb7 = UResourceBundle.getBundleInstance("bogus", Locale.getDefault(), testLoader);
        errln("Should have thrown exception with bogus baseName.");
    } catch (java.util.MissingResourceException ex) {
    }
    if (rb1 == null || rb2 == null || rb3 == null || rb4 == null || rb5 == null || rb6 == null) {
        errln("Error getting resource bundle.");
    }
    rb7 = UResourceBundle.getBundleInstance("android.icu.dev.data.resources.TestDataElements", Locale.getDefault(), testLoader);
    try {
        rb1.getBinary();
        errln("getBinary() call should have thrown UResourceTypeMismatchException.");
    } catch (UResourceTypeMismatchException ex) {
    }
    try {
        rb1.getStringArray();
        errln("getStringArray() call should have thrown UResourceTypeMismatchException.");
    } catch (UResourceTypeMismatchException ex) {
    }
    try {
        byte[] ba = { 0x00 };
        rb1.getBinary(ba);
        errln("getBinary(byte[]) call should have thrown UResourceTypeMismatchException.");
    } catch (UResourceTypeMismatchException ex) {
    }
    try {
        rb1.getInt();
        errln("getInt() call should have thrown UResourceTypeMismatchException.");
    } catch (UResourceTypeMismatchException ex) {
    }
    try {
        rb1.getIntVector();
        errln("getIntVector() call should have thrown UResourceTypeMismatchException.");
    } catch (UResourceTypeMismatchException ex) {
    }
    try {
        rb1.getUInt();
        errln("getUInt() call should have thrown UResourceTypeMismatchException.");
    } catch (UResourceTypeMismatchException ex) {
    }
    if (rb1.getVersion() != null) {
        errln("getVersion() call should have returned null.");
    }
    if (rb7.getType() != UResourceBundle.NONE) {
        errln("getType() call should have returned NONE.");
    }
    if (rb7.getKey() != null) {
        errln("getKey() call should have returned null.");
    }
    if (((ICUResourceBundle) rb1).findTopLevel(0) == null) {
        errln("Error calling findTopLevel().");
    }
    if (ICUResourceBundle.getFullLocaleNameSet() == null) {
        errln("Error calling getFullLocaleNameSet().");
    }
    UResourceBundleIterator itr = rb1.getIterator();
    while (itr.hasNext()) {
        itr.next();
    }
    try {
        itr.next();
        errln("NoSuchElementException exception should have been thrown.");
    } catch (NoSuchElementException ex) {
    }
    try {
        itr.nextString();
        errln("NoSuchElementException exception should have been thrown.");
    } catch (NoSuchElementException ex) {
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) UResourceBundleIterator(android.icu.util.UResourceBundleIterator) ULocale(android.icu.util.ULocale) MissingResourceException(java.util.MissingResourceException) UResourceTypeMismatchException(android.icu.util.UResourceTypeMismatchException) NoSuchElementException(java.util.NoSuchElementException) 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