Search in sources :

Example 1 with ValueType

use of android.icu.impl.locale.KeyTypeData.ValueType in project j2objc by google.

the class LocaleValidityChecker method isValidU.

/**
 * @param locale
 * @param datatype
 * @param extension
 * @param where
 * @return
 */
private boolean isValidU(ULocale locale, Datatype datatype, String extensionString, Where where) {
    String key = "";
    int typeCount = 0;
    ValueType valueType = null;
    SpecialCase specialCase = null;
    StringBuilder prefix = new StringBuilder();
    Set<String> seen = new HashSet<String>();
    StringBuilder tBuffer = datatype == Datatype.t ? new StringBuilder() : null;
    for (String subtag : SEPARATOR.split(extensionString)) {
        if (subtag.length() == 2 && (tBuffer == null || subtag.charAt(1) <= '9')) {
            // if we have accumulated a t buffer, check that first
            if (tBuffer != null) {
                // Check t buffer. Empty after 't' is ok.
                if (tBuffer.length() != 0 && !isValidLocale(tBuffer.toString(), where)) {
                    return false;
                }
                tBuffer = null;
            }
            key = KeyTypeData.toBcpKey(subtag);
            if (key == null) {
                return where.set(datatype, subtag);
            }
            if (!allowsDeprecated && KeyTypeData.isDeprecated(key)) {
                return where.set(datatype, key);
            }
            valueType = KeyTypeData.getValueType(key);
            specialCase = SpecialCase.get(key);
            typeCount = 0;
        } else if (tBuffer != null) {
            if (tBuffer.length() != 0) {
                tBuffer.append('-');
            }
            tBuffer.append(subtag);
        } else {
            ++typeCount;
            switch(valueType) {
                case single:
                    if (typeCount > 1) {
                        return where.set(datatype, key + "-" + subtag);
                    }
                    break;
                case incremental:
                    if (typeCount == 1) {
                        prefix.setLength(0);
                        prefix.append(subtag);
                    } else {
                        prefix.append('-').append(subtag);
                        subtag = prefix.toString();
                    }
                    break;
                case multiple:
                    if (typeCount == 1) {
                        seen.clear();
                    }
                    break;
            }
            switch(specialCase) {
                case anything:
                    continue;
                case codepoints:
                    try {
                        if (Integer.parseInt(subtag, 16) > 0x10FFFF) {
                            return where.set(datatype, key + "-" + subtag);
                        }
                    } catch (NumberFormatException e) {
                        return where.set(datatype, key + "-" + subtag);
                    }
                    continue;
                case reorder:
                    boolean newlyAdded = seen.add(subtag.equals("zzzz") ? "others" : subtag);
                    if (!newlyAdded || !isScriptReorder(subtag)) {
                        return where.set(datatype, key + "-" + subtag);
                    }
                    continue;
                case subdivision:
                    if (!isSubdivision(locale, subtag)) {
                        return where.set(datatype, key + "-" + subtag);
                    }
                    continue;
                case rgKey:
                    if (subtag.length() < 6 || !subtag.endsWith("zzzz")) {
                        return where.set(datatype, subtag);
                    }
                    if (!isValid(Datatype.region, subtag.substring(0, subtag.length() - 4), where)) {
                        return false;
                    }
                    continue;
            }
            // en-u-sd-usca
            // en-US-u-sd-usca
            Output<Boolean> isKnownKey = new Output<Boolean>();
            Output<Boolean> isSpecialType = new Output<Boolean>();
            String type = KeyTypeData.toBcpType(key, subtag, isKnownKey, isSpecialType);
            if (type == null) {
                return where.set(datatype, key + "-" + subtag);
            }
            if (!allowsDeprecated && KeyTypeData.isDeprecated(key, subtag)) {
                return where.set(datatype, key + "-" + subtag);
            }
        }
    }
    // Check t buffer. Empty after 't' is ok.
    if (tBuffer != null && tBuffer.length() != 0 && !isValidLocale(tBuffer.toString(), where)) {
        return false;
    }
    return true;
}
Also used : ValueType(android.icu.impl.locale.KeyTypeData.ValueType) Output(android.icu.util.Output) HashSet(java.util.HashSet)

Aggregations

ValueType (android.icu.impl.locale.KeyTypeData.ValueType)1 Output (android.icu.util.Output)1 HashSet (java.util.HashSet)1