Search in sources :

Example 26 with UResourceBundle

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

the class NumberingSystem method lookupInstanceByName.

private static NumberingSystem lookupInstanceByName(String name) {
    int radix;
    boolean isAlgorithmic;
    String description;
    try {
        UResourceBundle numberingSystemsInfo = UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "numberingSystems");
        UResourceBundle nsCurrent = numberingSystemsInfo.get("numberingSystems");
        UResourceBundle nsTop = nsCurrent.get(name);
        description = nsTop.getString("desc");
        UResourceBundle nsRadixBundle = nsTop.get("radix");
        UResourceBundle nsAlgBundle = nsTop.get("algorithmic");
        radix = nsRadixBundle.getInt();
        int algorithmic = nsAlgBundle.getInt();
        isAlgorithmic = (algorithmic == 1);
    } catch (MissingResourceException ex) {
        return null;
    }
    return getInstance(name, radix, isAlgorithmic, description);
}
Also used : UResourceBundle(android.icu.util.UResourceBundle) ICUResourceBundle(android.icu.impl.ICUResourceBundle) MissingResourceException(java.util.MissingResourceException)

Example 27 with UResourceBundle

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

the class CollationLoader method loadTailoring.

public static CollationTailoring loadTailoring(ULocale locale, Output<ULocale> outValidLocale) {
    // Java porting note: ICU4J getWithFallback/getStringWithFallback currently does not
    // work well when alias table is involved in a resource path, unless full path is specified.
    // For now, collation resources does not contain such data, so the code below should work fine.
    CollationTailoring root = CollationRoot.getRoot();
    String localeName = locale.getName();
    if (localeName.length() == 0 || localeName.equals("root")) {
        outValidLocale.value = ULocale.ROOT;
        return root;
    }
    UResourceBundle bundle = null;
    try {
        bundle = ICUResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, locale, ICUResourceBundle.OpenType.LOCALE_ROOT);
    } catch (MissingResourceException e) {
        outValidLocale.value = ULocale.ROOT;
        return root;
    }
    ULocale validLocale = bundle.getULocale();
    // Normalize the root locale. See
    // http://bugs.icu-project.org/trac/ticket/10715
    String validLocaleName = validLocale.getName();
    if (validLocaleName.length() == 0 || validLocaleName.equals("root")) {
        validLocale = ULocale.ROOT;
    }
    outValidLocale.value = validLocale;
    // There are zero or more tailorings in the collations table.
    UResourceBundle collations;
    try {
        collations = bundle.get("collations");
        if (collations == null) {
            return root;
        }
    } catch (MissingResourceException ignored) {
        return root;
    }
    // Fetch the collation type from the locale ID and the default type from the data.
    String type = locale.getKeywordValue("collation");
    String defaultType = "standard";
    String defT = ((ICUResourceBundle) collations).findStringWithFallback("default");
    if (defT != null) {
        defaultType = defT;
    }
    if (type == null || type.equals("default")) {
        type = defaultType;
    } else {
        type = ASCII.toLowerCase(type);
    }
    // Load the collations/type tailoring, with type fallback.
    // Java porting note: typeFallback is used for setting U_USING_DEFAULT_WARNING in
    // ICU4C, but not used by ICU4J
    // boolean typeFallback = false;
    UResourceBundle data = findWithFallback(collations, type);
    if (data == null && type.length() > 6 && type.startsWith("search")) {
        // fall back from something like "searchjl" to "search"
        // typeFallback = true;
        type = "search";
        data = findWithFallback(collations, type);
    }
    if (data == null && !type.equals(defaultType)) {
        // fall back to the default type
        // typeFallback = true;
        type = defaultType;
        data = findWithFallback(collations, type);
    }
    if (data == null && !type.equals("standard")) {
        // fall back to the "standard" type
        // typeFallback = true;
        type = "standard";
        data = findWithFallback(collations, type);
    }
    if (data == null) {
        return root;
    }
    // Is this the same as the root collator? If so, then use that instead.
    ULocale actualLocale = data.getULocale();
    // http://bugs.icu-project.org/trac/ticket/10715 ICUResourceBundle(root).getULocale() != ULocale.ROOT
    // Therefore not just if (actualLocale.equals(ULocale.ROOT) && type.equals("standard")) {
    String actualLocaleName = actualLocale.getName();
    if (actualLocaleName.length() == 0 || actualLocaleName.equals("root")) {
        actualLocale = ULocale.ROOT;
        if (type.equals("standard")) {
            return root;
        }
    }
    CollationTailoring t = new CollationTailoring(root.settings);
    t.actualLocale = actualLocale;
    // deserialize
    UResourceBundle binary = data.get("%%CollationBin");
    ByteBuffer inBytes = binary.getBinary();
    try {
        CollationDataReader.read(root, inBytes, t);
    } catch (IOException e) {
        throw new ICUUncheckedIOException("Failed to load collation tailoring data for locale:" + actualLocale + " type:" + type, e);
    }
    // Try to fetch the optional rules string.
    try {
        t.setRulesResource(data.get("Sequence"));
    } catch (MissingResourceException ignored) {
    }
    // For the valid locale, suppress the default type.
    if (!type.equals(defaultType)) {
        outValidLocale.value = validLocale.setKeywordValue("collation", type);
    }
    // For the actual locale "zh" we need to suppress pinyin instead.
    if (!actualLocale.equals(validLocale)) {
        // Opening a bundle for the actual locale should always succeed.
        UResourceBundle actualBundle = UResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, actualLocale);
        defT = ((ICUResourceBundle) actualBundle).findStringWithFallback("collations/default");
        if (defT != null) {
            defaultType = defT;
        }
    }
    if (!type.equals(defaultType)) {
        t.actualLocale = t.actualLocale.setKeywordValue("collation", type);
    }
    return t;
}
Also used : UResourceBundle(android.icu.util.UResourceBundle) ICUResourceBundle(android.icu.impl.ICUResourceBundle) ULocale(android.icu.util.ULocale) MissingResourceException(java.util.MissingResourceException) ICUResourceBundle(android.icu.impl.ICUResourceBundle) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) ICUUncheckedIOException(android.icu.util.ICUUncheckedIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 28 with UResourceBundle

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

the class CollationLoader method loadRules.

static String loadRules(ULocale locale, String collationType) {
    UResourceBundle bundle = UResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, locale);
    UResourceBundle data = ((ICUResourceBundle) bundle).getWithFallback("collations/" + ASCII.toLowerCase(collationType));
    String rules = data.getString("Sequence");
    return rules;
}
Also used : UResourceBundle(android.icu.util.UResourceBundle) ICUResourceBundle(android.icu.impl.ICUResourceBundle) ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 29 with UResourceBundle

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

the class CollationLoader method loadRootRules.

private static void loadRootRules() {
    if (rootRules != null) {
        return;
    }
    synchronized (CollationLoader.class) {
        if (rootRules == null) {
            UResourceBundle rootBundle = UResourceBundle.getBundleInstance(ICUData.ICU_COLLATION_BASE_NAME, ULocale.ROOT);
            rootRules = rootBundle.getString("UCARules");
        }
    }
}
Also used : UResourceBundle(android.icu.util.UResourceBundle) ICUResourceBundle(android.icu.impl.ICUResourceBundle)

Example 30 with UResourceBundle

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

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