Search in sources :

Example 36 with UResourceBundle

use of android.icu.util.UResourceBundle in project j2objc by google.

the class PluralRulesLoader method getRulesForRulesId.

/**
 * Gets the rule from the rulesId. If there is no rule for this rulesId,
 * return null.
 */
public PluralRules getRulesForRulesId(String rulesId) {
    // synchronize on the map.  release the lock temporarily while we build the rules.
    PluralRules rules = null;
    // Separate boolean because stored rules can be null.
    boolean hasRules;
    synchronized (rulesIdToRules) {
        hasRules = rulesIdToRules.containsKey(rulesId);
        if (hasRules) {
            // can be null
            rules = rulesIdToRules.get(rulesId);
        }
    }
    if (!hasRules) {
        try {
            UResourceBundle pluralb = getPluralBundle();
            UResourceBundle rulesb = pluralb.get("rules");
            UResourceBundle setb = rulesb.get(rulesId);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < setb.getSize(); ++i) {
                UResourceBundle b = setb.get(i);
                if (i > 0) {
                    sb.append("; ");
                }
                sb.append(b.getKey());
                sb.append(": ");
                sb.append(b.getString());
            }
            rules = PluralRules.parseDescription(sb.toString());
        } catch (ParseException e) {
        } catch (MissingResourceException e) {
        }
        synchronized (rulesIdToRules) {
            if (rulesIdToRules.containsKey(rulesId)) {
                rules = rulesIdToRules.get(rulesId);
            } else {
                // can be null
                rulesIdToRules.put(rulesId, rules);
            }
        }
    }
    return rules;
}
Also used : UResourceBundle(android.icu.util.UResourceBundle) PluralRules(android.icu.text.PluralRules) MissingResourceException(java.util.MissingResourceException) ParseException(java.text.ParseException)

Example 37 with UResourceBundle

use of android.icu.util.UResourceBundle in project j2objc by google.

the class DateFormatSymbols method initializeData.

/**
 * Initializes format symbols for the locale and calendar type
 * @param desiredLocale The locale whose symbols are desired.
 * @param b Resource bundle provided externally
 * @param calendarType  The calendar type being used
 * @deprecated This API is ICU internal only.
 * @hide draft / provisional / internal are hidden on Android
 */
@Deprecated
protected // This API was accidentally marked as @stable ICU 3.0 formerly.
void initializeData(ULocale desiredLocale, ICUResourceBundle b, String calendarType) {
    // Create a CalendarSink to load this data and a resource bundle
    CalendarDataSink calendarSink = new CalendarDataSink();
    if (b == null) {
        b = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, desiredLocale);
    }
    // Iterate over the resource bundle data following the fallbacks through different calendar types
    while (calendarType != null) {
        // Enumerate this calendar type. If the calendar is not found fallback to gregorian.
        ICUResourceBundle dataForType = b.findWithFallback("calendar/" + calendarType);
        if (dataForType == null) {
            if (!"gregorian".equals(calendarType)) {
                calendarType = "gregorian";
                calendarSink.visitAllResources();
                continue;
            }
            throw new MissingResourceException("The 'gregorian' calendar type wasn't found for the locale: " + desiredLocale.getBaseName(), getClass().getName(), "gregorian");
        }
        calendarSink.preEnumerate(calendarType);
        dataForType.getAllItemsWithFallback("", calendarSink);
        // Stop loading when gregorian was loaded
        if (calendarType.equals("gregorian")) {
            break;
        }
        // Get the next calendar type to process from the sink
        calendarType = calendarSink.nextCalendarType;
        // Gregorian is always the last fallback
        if (calendarType == null) {
            calendarType = "gregorian";
            calendarSink.visitAllResources();
        }
    }
    Map<String, String[]> arrays = calendarSink.arrays;
    Map<String, Map<String, String>> maps = calendarSink.maps;
    eras = arrays.get("eras/abbreviated");
    eraNames = arrays.get("eras/wide");
    narrowEras = arrays.get("eras/narrow");
    months = arrays.get("monthNames/format/wide");
    shortMonths = arrays.get("monthNames/format/abbreviated");
    narrowMonths = arrays.get("monthNames/format/narrow");
    standaloneMonths = arrays.get("monthNames/stand-alone/wide");
    standaloneShortMonths = arrays.get("monthNames/stand-alone/abbreviated");
    standaloneNarrowMonths = arrays.get("monthNames/stand-alone/narrow");
    String[] lWeekdays = arrays.get("dayNames/format/wide");
    weekdays = new String[8];
    // 1-based
    weekdays[0] = "";
    System.arraycopy(lWeekdays, 0, weekdays, 1, lWeekdays.length);
    String[] aWeekdays = arrays.get("dayNames/format/abbreviated");
    shortWeekdays = new String[8];
    // 1-based
    shortWeekdays[0] = "";
    System.arraycopy(aWeekdays, 0, shortWeekdays, 1, aWeekdays.length);
    String[] sWeekdays = arrays.get("dayNames/format/short");
    shorterWeekdays = new String[8];
    // 1-based
    shorterWeekdays[0] = "";
    System.arraycopy(sWeekdays, 0, shorterWeekdays, 1, sWeekdays.length);
    String[] nWeekdays = arrays.get("dayNames/format/narrow");
    if (nWeekdays == null) {
        nWeekdays = arrays.get("dayNames/stand-alone/narrow");
        if (nWeekdays == null) {
            nWeekdays = arrays.get("dayNames/format/abbreviated");
            if (nWeekdays == null) {
                throw new MissingResourceException("Resource not found", getClass().getName(), "dayNames/format/abbreviated");
            }
        }
    }
    narrowWeekdays = new String[8];
    // 1-based
    narrowWeekdays[0] = "";
    System.arraycopy(nWeekdays, 0, narrowWeekdays, 1, nWeekdays.length);
    String[] swWeekdays = null;
    swWeekdays = arrays.get("dayNames/stand-alone/wide");
    standaloneWeekdays = new String[8];
    // 1-based
    standaloneWeekdays[0] = "";
    System.arraycopy(swWeekdays, 0, standaloneWeekdays, 1, swWeekdays.length);
    String[] saWeekdays = null;
    saWeekdays = arrays.get("dayNames/stand-alone/abbreviated");
    standaloneShortWeekdays = new String[8];
    // 1-based
    standaloneShortWeekdays[0] = "";
    System.arraycopy(saWeekdays, 0, standaloneShortWeekdays, 1, saWeekdays.length);
    String[] ssWeekdays = null;
    ssWeekdays = arrays.get("dayNames/stand-alone/short");
    standaloneShorterWeekdays = new String[8];
    // 1-based
    standaloneShorterWeekdays[0] = "";
    System.arraycopy(ssWeekdays, 0, standaloneShorterWeekdays, 1, ssWeekdays.length);
    String[] snWeekdays = null;
    snWeekdays = arrays.get("dayNames/stand-alone/narrow");
    standaloneNarrowWeekdays = new String[8];
    // 1-based
    standaloneNarrowWeekdays[0] = "";
    System.arraycopy(snWeekdays, 0, standaloneNarrowWeekdays, 1, snWeekdays.length);
    ampms = arrays.get("AmPmMarkers");
    ampmsNarrow = arrays.get("AmPmMarkersNarrow");
    quarters = arrays.get("quarters/format/wide");
    shortQuarters = arrays.get("quarters/format/abbreviated");
    standaloneQuarters = arrays.get("quarters/stand-alone/wide");
    standaloneShortQuarters = arrays.get("quarters/stand-alone/abbreviated");
    abbreviatedDayPeriods = loadDayPeriodStrings(maps.get("dayPeriod/format/abbreviated"));
    wideDayPeriods = loadDayPeriodStrings(maps.get("dayPeriod/format/wide"));
    narrowDayPeriods = loadDayPeriodStrings(maps.get("dayPeriod/format/narrow"));
    standaloneAbbreviatedDayPeriods = loadDayPeriodStrings(maps.get("dayPeriod/stand-alone/abbreviated"));
    standaloneWideDayPeriods = loadDayPeriodStrings(maps.get("dayPeriod/stand-alone/wide"));
    standaloneNarrowDayPeriods = loadDayPeriodStrings(maps.get("dayPeriod/stand-alone/narrow"));
    for (int i = 0; i < DT_MONTH_PATTERN_COUNT; i++) {
        String monthPatternPath = LEAP_MONTH_PATTERNS_PATHS[i];
        if (monthPatternPath != null) {
            Map<String, String> monthPatternMap = maps.get(monthPatternPath);
            if (monthPatternMap != null) {
                String leapMonthPattern = monthPatternMap.get("leap");
                if (leapMonthPattern != null) {
                    if (leapMonthPatterns == null) {
                        leapMonthPatterns = new String[DT_MONTH_PATTERN_COUNT];
                    }
                    leapMonthPatterns[i] = leapMonthPattern;
                }
            }
        }
    }
    shortYearNames = arrays.get("cyclicNameSets/years/format/abbreviated");
    shortZodiacNames = arrays.get("cyclicNameSets/zodiacs/format/abbreviated");
    requestedLocale = desiredLocale;
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, desiredLocale);
    localPatternChars = patternChars;
    // TODO: obtain correct actual/valid locale later
    ULocale uloc = rb.getULocale();
    setLocale(uloc, uloc);
    capitalization = new HashMap<CapitalizationContextUsage, boolean[]>();
    boolean[] noTransforms = new boolean[2];
    noTransforms[0] = false;
    noTransforms[1] = false;
    CapitalizationContextUsage[] allUsages = CapitalizationContextUsage.values();
    for (CapitalizationContextUsage usage : allUsages) {
        capitalization.put(usage, noTransforms);
    }
    UResourceBundle contextTransformsBundle = null;
    try {
        contextTransformsBundle = rb.getWithFallback("contextTransforms");
    } catch (MissingResourceException e) {
        // probably redundant
        contextTransformsBundle = null;
    }
    if (contextTransformsBundle != null) {
        UResourceBundleIterator ctIterator = contextTransformsBundle.getIterator();
        while (ctIterator.hasNext()) {
            UResourceBundle contextTransformUsage = ctIterator.next();
            int[] intVector = contextTransformUsage.getIntVector();
            if (intVector.length >= 2) {
                String usageKey = contextTransformUsage.getKey();
                CapitalizationContextUsage usage = contextUsageTypeMap.get(usageKey);
                if (usage != null) {
                    boolean[] transforms = new boolean[2];
                    transforms[0] = (intVector[0] != 0);
                    transforms[1] = (intVector[1] != 0);
                    capitalization.put(usage, transforms);
                }
            }
        }
    }
    NumberingSystem ns = NumberingSystem.getInstance(desiredLocale);
    // Latin is default.
    String nsName = ns == null ? "latn" : ns.getName();
    String tsPath = "NumberElements/" + nsName + "/symbols/timeSeparator";
    try {
        setTimeSeparatorString(rb.getStringWithFallback(tsPath));
    } catch (MissingResourceException e) {
        setTimeSeparatorString(DEFAULT_TIME_SEPARATOR);
    }
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) UResourceBundleIterator(android.icu.util.UResourceBundleIterator) ULocale(android.icu.util.ULocale) MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 38 with UResourceBundle

use of android.icu.util.UResourceBundle in project j2objc by google.

the class ResourceModule method getFromTable.

static UResourceBundle getFromTable(UResourceBundle res, String key, int[] expResTypes) throws DataModuleFormatError {
    assert_is(res != null && key != null && res.getType() == UResourceBundle.TABLE);
    UResourceBundle t = res.get(key);
    assert_not(t == null);
    int type = t.getType();
    Arrays.sort(expResTypes);
    if (Arrays.binarySearch(expResTypes, type) >= 0) {
        return t;
    } else {
        throw new DataModuleFormatError(new UResourceTypeMismatchException("Actual type " + t.getType() + " != expected types " + Arrays.toString(expResTypes) + "."));
    }
}
Also used : UResourceBundle(android.icu.util.UResourceBundle) ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceTypeMismatchException(android.icu.util.UResourceTypeMismatchException)

Example 39 with UResourceBundle

use of android.icu.util.UResourceBundle in project j2objc by google.

the class ICUResourceBundleTest method TestTable32.

@Test
public void TestTable32() {
    TestCase[] arr = new TestCase[] { new TestCase("ooooooooooooooooo", 0), new TestCase("oooooooooooooooo1", 1), new TestCase("ooooooooooooooo1o", 2), new TestCase("oo11ooo1ooo11111o", 25150), new TestCase("oo11ooo1ooo111111", 25151), new TestCase("o1111111111111111", 65535), new TestCase("1oooooooooooooooo", 65536), new TestCase("1ooooooo11o11ooo1", 65969), new TestCase("1ooooooo11o11oo1o", 65970), new TestCase("1ooooooo111oo1111", 65999) };
    UResourceBundle bundle = null;
    try {
        bundle = UResourceBundle.getBundleInstance("android/icu/dev/data/testdata", "testtable32", testLoader);
    } catch (MissingResourceException ex) {
        warnln("could not load resource data: " + ex.getMessage());
        return;
    }
    if (bundle.getType() != UResourceBundle.TABLE) {
        errln("Could not get the correct type for bundle testtable32");
    }
    int size = bundle.getSize();
    if (size != 66000) {
        errln("Could not get the correct size for bundle testtable32");
    }
    int number = -1;
    // get the items by index
    for (int i = 0; i < size; i++) {
        UResourceBundle item = bundle.get(i);
        String key = item.getKey();
        int parsedNumber = parseTable32Key(key);
        switch(item.getType()) {
            case UResourceBundle.STRING:
                String value = item.getString();
                number = UTF16.charAt(value, 0);
                break;
            case UResourceBundle.INT:
                number = item.getInt();
                break;
            default:
                errln("Got unexpected resource type in testtable32");
        }
        if (number != parsedNumber) {
            errln("Did not get expected value in testtypes32 for key" + key + ". Expected: " + parsedNumber + " Got:" + number);
        }
    }
    // search for some items by key
    for (int i = 0; i < arr.length; i++) {
        UResourceBundle item = bundle.get(arr[i].key);
        switch(item.getType()) {
            case UResourceBundle.STRING:
                String value = item.getString();
                number = UTF16.charAt(value, 0);
                break;
            case UResourceBundle.INT:
                number = item.getInt();
                break;
            default:
                errln("Got unexpected resource type in testtable32");
        }
        if (number != arr[i].value) {
            errln("Did not get expected value in testtypes32 for key" + arr[i].key + ". Expected: " + arr[i].value + " Got:" + number);
        }
    }
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) MissingResourceException(java.util.MissingResourceException) Test(org.junit.Test)

Example 40 with UResourceBundle

use of android.icu.util.UResourceBundle in project j2objc by google.

the class ICUResourceBundleTest method TestNorwegian.

@Test
public void TestNorwegian() {
    try {
        UResourceBundle rb = UResourceBundle.getBundleInstance(ICUData.ICU_REGION_BASE_NAME, "no_NO_NY");
        UResourceBundle sub = rb.get("Countries");
        String s1 = sub.getString("NO");
        if (s1.equals("Noreg")) {
            logln("got expected output ");
        } else {
            errln("did not get the expected result");
        }
    } catch (IllegalArgumentException ex) {
        errln("Caught an unexpected expected");
    }
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) Test(org.junit.Test)

Aggregations

UResourceBundle (android.icu.util.UResourceBundle)46 ICUResourceBundle (android.icu.impl.ICUResourceBundle)26 MissingResourceException (java.util.MissingResourceException)24 Test (org.junit.Test)17 ULocale (android.icu.util.ULocale)8 UResourceBundleIterator (android.icu.util.UResourceBundleIterator)6 UResourceTypeMismatchException (android.icu.util.UResourceTypeMismatchException)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 SimpleTimeZone (android.icu.util.SimpleTimeZone)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 EnumSet (java.util.EnumSet)2 Locale (java.util.Locale)2 Map (java.util.Map)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 JavaTimeZone (android.icu.impl.JavaTimeZone)1