Search in sources :

Example 1 with UResourceBundleIterator

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

the class ICUResourceBundle method createULocaleList.

private static final ULocale[] createULocaleList(String baseName, ClassLoader root) {
    // the canned list is a subset of all the available .res files, the idea
    // is we don't export them
    // all. gotta be a better way to do this, since to add a locale you have
    // to update this list,
    // and it's embedded in our binary resources.
    ICUResourceBundle bundle = (ICUResourceBundle) UResourceBundle.instantiateBundle(baseName, ICU_RESOURCE_INDEX, root, true);
    bundle = (ICUResourceBundle) bundle.get(INSTALLED_LOCALES);
    int length = bundle.getSize();
    int i = 0;
    ULocale[] locales = new ULocale[length];
    UResourceBundleIterator iter = bundle.getIterator();
    iter.reset();
    while (iter.hasNext()) {
        String locstr = iter.next().getKey();
        if (locstr.equals("root")) {
            locales[i++] = ULocale.ROOT;
        } else {
            locales[i++] = new ULocale(locstr);
        }
    }
    bundle = null;
    return locales;
}
Also used : UResourceBundleIterator(android.icu.util.UResourceBundleIterator) ULocale(android.icu.util.ULocale)

Example 2 with UResourceBundleIterator

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

the class ICUResourceBundle method addLocaleIDsFromIndexBundle.

// Same as createULocaleList() but catches the MissingResourceException
// and returns the data in a different form.
private static final void addLocaleIDsFromIndexBundle(String baseName, ClassLoader root, Set<String> locales) {
    ICUResourceBundle bundle;
    try {
        bundle = (ICUResourceBundle) UResourceBundle.instantiateBundle(baseName, ICU_RESOURCE_INDEX, root, true);
        bundle = (ICUResourceBundle) bundle.get(INSTALLED_LOCALES);
    } catch (MissingResourceException e) {
        if (DEBUG) {
            System.out.println("couldn't find " + baseName + '/' + ICU_RESOURCE_INDEX + ".res");
            Thread.dumpStack();
        }
        return;
    }
    UResourceBundleIterator iter = bundle.getIterator();
    iter.reset();
    while (iter.hasNext()) {
        String locstr = iter.next().getKey();
        locales.add(locstr);
    }
}
Also used : UResourceBundleIterator(android.icu.util.UResourceBundleIterator) MissingResourceException(java.util.MissingResourceException)

Example 3 with UResourceBundleIterator

use of android.icu.util.UResourceBundleIterator 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)

Example 4 with UResourceBundleIterator

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

the class KeyTypeData method getTypeInfo.

/**
 * Reads:
 *typeInfo{
 *    deprecated{
 *        co{
 *            direct{"true"}
 *        }
 *        tz{
 *            camtr{"true"}
 *        }
 *    }
 *}
 */
private static void getTypeInfo(UResourceBundle typeInfoRes) {
    Map<String, Set<String>> _deprecatedKeyTypes = new LinkedHashMap<String, Set<String>>();
    for (UResourceBundleIterator keyInfoIt = typeInfoRes.getIterator(); keyInfoIt.hasNext(); ) {
        UResourceBundle keyInfoEntry = keyInfoIt.next();
        String key = keyInfoEntry.getKey();
        TypeInfoType typeInfo = TypeInfoType.valueOf(key);
        for (UResourceBundleIterator keyInfoIt2 = keyInfoEntry.getIterator(); keyInfoIt2.hasNext(); ) {
            UResourceBundle keyInfoEntry2 = keyInfoIt2.next();
            String key2 = keyInfoEntry2.getKey();
            Set<String> _deprecatedTypes = new LinkedHashSet<String>();
            for (UResourceBundleIterator keyInfoIt3 = keyInfoEntry2.getIterator(); keyInfoIt3.hasNext(); ) {
                UResourceBundle keyInfoEntry3 = keyInfoIt3.next();
                String key3 = keyInfoEntry3.getKey();
                switch(// allow for expansion
                typeInfo) {
                    case deprecated:
                        _deprecatedTypes.add(key3);
                        break;
                }
            }
            _deprecatedKeyTypes.put(key2, Collections.unmodifiableSet(_deprecatedTypes));
        }
    }
    DEPRECATED_KEY_TYPES = Collections.unmodifiableMap(_deprecatedKeyTypes);
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) LinkedHashSet(java.util.LinkedHashSet) UResourceBundleIterator(android.icu.util.UResourceBundleIterator) Set(java.util.Set) HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) LinkedHashSet(java.util.LinkedHashSet) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with UResourceBundleIterator

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

the class KeyTypeData method initFromResourceBundle.

private static void initFromResourceBundle() {
    UResourceBundle keyTypeDataRes = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "keyTypeData", ICUResourceBundle.ICU_DATA_CLASS_LOADER);
    getKeyInfo(keyTypeDataRes.get("keyInfo"));
    getTypeInfo(keyTypeDataRes.get("typeInfo"));
    UResourceBundle keyMapRes = keyTypeDataRes.get("keyMap");
    UResourceBundle typeMapRes = keyTypeDataRes.get("typeMap");
    // alias data is optional
    UResourceBundle typeAliasRes = null;
    UResourceBundle bcpTypeAliasRes = null;
    try {
        typeAliasRes = keyTypeDataRes.get("typeAlias");
    } catch (MissingResourceException e) {
    // fall through
    }
    try {
        bcpTypeAliasRes = keyTypeDataRes.get("bcpTypeAlias");
    } catch (MissingResourceException e) {
    // fall through
    }
    // iterate through keyMap resource
    UResourceBundleIterator keyMapItr = keyMapRes.getIterator();
    Map<String, Set<String>> _Bcp47Keys = new LinkedHashMap<String, Set<String>>();
    while (keyMapItr.hasNext()) {
        UResourceBundle keyMapEntry = keyMapItr.next();
        String legacyKeyId = keyMapEntry.getKey();
        String bcpKeyId = keyMapEntry.getString();
        boolean hasSameKey = false;
        if (bcpKeyId.length() == 0) {
            // Empty value indicates that BCP key is same with the legacy key.
            bcpKeyId = legacyKeyId;
            hasSameKey = true;
        }
        final LinkedHashSet<String> _bcp47Types = new LinkedHashSet<String>();
        _Bcp47Keys.put(bcpKeyId, Collections.unmodifiableSet(_bcp47Types));
        boolean isTZ = legacyKeyId.equals("timezone");
        // reverse type alias map
        Map<String, Set<String>> typeAliasMap = null;
        if (typeAliasRes != null) {
            UResourceBundle typeAliasResByKey = null;
            try {
                typeAliasResByKey = typeAliasRes.get(legacyKeyId);
            } catch (MissingResourceException e) {
            // fall through
            }
            if (typeAliasResByKey != null) {
                typeAliasMap = new HashMap<String, Set<String>>();
                UResourceBundleIterator typeAliasResItr = typeAliasResByKey.getIterator();
                while (typeAliasResItr.hasNext()) {
                    UResourceBundle typeAliasDataEntry = typeAliasResItr.next();
                    String from = typeAliasDataEntry.getKey();
                    String to = typeAliasDataEntry.getString();
                    if (isTZ) {
                        from = from.replace(':', '/');
                    }
                    Set<String> aliasSet = typeAliasMap.get(to);
                    if (aliasSet == null) {
                        aliasSet = new HashSet<String>();
                        typeAliasMap.put(to, aliasSet);
                    }
                    aliasSet.add(from);
                }
            }
        }
        // reverse bcp type alias map
        Map<String, Set<String>> bcpTypeAliasMap = null;
        if (bcpTypeAliasRes != null) {
            UResourceBundle bcpTypeAliasResByKey = null;
            try {
                bcpTypeAliasResByKey = bcpTypeAliasRes.get(bcpKeyId);
            } catch (MissingResourceException e) {
            // fall through
            }
            if (bcpTypeAliasResByKey != null) {
                bcpTypeAliasMap = new HashMap<String, Set<String>>();
                UResourceBundleIterator bcpTypeAliasResItr = bcpTypeAliasResByKey.getIterator();
                while (bcpTypeAliasResItr.hasNext()) {
                    UResourceBundle bcpTypeAliasDataEntry = bcpTypeAliasResItr.next();
                    String from = bcpTypeAliasDataEntry.getKey();
                    String to = bcpTypeAliasDataEntry.getString();
                    Set<String> aliasSet = bcpTypeAliasMap.get(to);
                    if (aliasSet == null) {
                        aliasSet = new HashSet<String>();
                        bcpTypeAliasMap.put(to, aliasSet);
                    }
                    aliasSet.add(from);
                }
            }
        }
        Map<String, Type> typeDataMap = new HashMap<String, Type>();
        EnumSet<SpecialType> specialTypeSet = null;
        // look up type map for the key, and walk through the mapping data
        UResourceBundle typeMapResByKey = null;
        try {
            typeMapResByKey = typeMapRes.get(legacyKeyId);
        } catch (MissingResourceException e) {
            // type map for each key must exist
            assert false;
        }
        if (typeMapResByKey != null) {
            UResourceBundleIterator typeMapResByKeyItr = typeMapResByKey.getIterator();
            while (typeMapResByKeyItr.hasNext()) {
                UResourceBundle typeMapEntry = typeMapResByKeyItr.next();
                String legacyTypeId = typeMapEntry.getKey();
                String bcpTypeId = typeMapEntry.getString();
                // special types
                final char first = legacyTypeId.charAt(0);
                final boolean isSpecialType = '9' < first && first < 'a' && bcpTypeId.length() == 0;
                if (isSpecialType) {
                    if (specialTypeSet == null) {
                        specialTypeSet = EnumSet.noneOf(SpecialType.class);
                    }
                    specialTypeSet.add(SpecialType.valueOf(legacyTypeId));
                    _bcp47Types.add(legacyTypeId);
                    continue;
                }
                if (isTZ) {
                    // a timezone key uses a colon instead of a slash in the resource.
                    // e.g. America:Los_Angeles
                    legacyTypeId = legacyTypeId.replace(':', '/');
                }
                boolean hasSameType = false;
                if (bcpTypeId.length() == 0) {
                    // Empty value indicates that BCP type is same with the legacy type.
                    bcpTypeId = legacyTypeId;
                    hasSameType = true;
                }
                _bcp47Types.add(bcpTypeId);
                // Note: legacy type value should never be
                // equivalent to bcp type value of a different
                // type under the same key. So we use a single
                // map for lookup.
                Type t = new Type(legacyTypeId, bcpTypeId);
                typeDataMap.put(AsciiUtil.toLowerString(legacyTypeId), t);
                if (!hasSameType) {
                    typeDataMap.put(AsciiUtil.toLowerString(bcpTypeId), t);
                }
                // Also put aliases in the map
                if (typeAliasMap != null) {
                    Set<String> typeAliasSet = typeAliasMap.get(legacyTypeId);
                    if (typeAliasSet != null) {
                        for (String alias : typeAliasSet) {
                            typeDataMap.put(AsciiUtil.toLowerString(alias), t);
                        }
                    }
                }
                if (bcpTypeAliasMap != null) {
                    Set<String> bcpTypeAliasSet = bcpTypeAliasMap.get(bcpTypeId);
                    if (bcpTypeAliasSet != null) {
                        for (String alias : bcpTypeAliasSet) {
                            typeDataMap.put(AsciiUtil.toLowerString(alias), t);
                        }
                    }
                }
            }
        }
        KeyData keyData = new KeyData(legacyKeyId, bcpKeyId, typeDataMap, specialTypeSet);
        KEYMAP.put(AsciiUtil.toLowerString(legacyKeyId), keyData);
        if (!hasSameKey) {
            KEYMAP.put(AsciiUtil.toLowerString(bcpKeyId), keyData);
        }
    }
    BCP47_KEYS = Collections.unmodifiableMap(_Bcp47Keys);
}
Also used : ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) LinkedHashSet(java.util.LinkedHashSet) UResourceBundleIterator(android.icu.util.UResourceBundleIterator) Set(java.util.Set) HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MissingResourceException(java.util.MissingResourceException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

UResourceBundleIterator (android.icu.util.UResourceBundleIterator)8 ICUResourceBundle (android.icu.impl.ICUResourceBundle)6 UResourceBundle (android.icu.util.UResourceBundle)6 MissingResourceException (java.util.MissingResourceException)4 ULocale (android.icu.util.ULocale)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 EnumSet (java.util.EnumSet)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 UResourceTypeMismatchException (android.icu.util.UResourceTypeMismatchException)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 TreeMap (java.util.TreeMap)1 Test (org.junit.Test)1