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);
}
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;
}
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;
}
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");
}
}
}
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);
}
Aggregations