@Test public void testCompileValidStrings() throws Exception { List<String> lines = readLines(VALID_CODES_FILE); assertTrue(lines.size() > 0); int numValid = 0; for (String line : lines) { if (LanguageRegistryUtil.isValidLanguageTag(line)) { numValid++; } } assertEquals(numValid, lines.size()); }
public static boolean isValidLanguageTag(String tag) { if (tag == null || tag.isEmpty() || tag.indexOf("--") > -1 || !tag.matches("^[a-zA-Z0-9-]+$")) { return false; } tag = tag.toLowerCase(); if (LanguageRegistry.isGrandfatheredTag(tag)) { return true; } if (containsExtensionOrPrivateUseSubtags(tag)) { return false; } String[] subtags = tag.split("-"); if (subtags.length < 1) { return false; } String language = subtags[0]; if (!LanguageRegistry.isLanguageSubtag(language)) { return false; } if (subtags.length > 1) { String extlangFound = null; String scriptFound = null; String regionFound = null; List<String> variantWithPrefixesFound = new ArrayList<>(); List<String> variantsWithoutPrefixesFound = new ArrayList<>(); for (int i = 1; i < subtags.length; i++) { String subtag = subtags[i]; if (LanguageRegistry.isExtlangSubtag(subtag)) { if (extlangFound != null || scriptFound != null || regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } extlangFound = subtag; } else if (LanguageRegistry.isScriptSubtag(subtag)) { if (scriptFound != null || regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } scriptFound = subtag; } else if (LanguageRegistry.isRegionSubtag(subtag)) { if (regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } regionFound = subtag; } else if (LanguageRegistry.isVariantSubtag(subtag)) { if (variantWithPrefixesFound.contains(subtag) || variantsWithoutPrefixesFound.contains(subtag)) { return false; } List<String> variantPrefixes = LanguageRegistry.getVariantPrefixes(subtag); if (!variantPrefixes.isEmpty()) { if (!variantsWithoutPrefixesFound.isEmpty()) { return false; } StringBuilder prefixToCheck1Sb = new StringBuilder(language); StringBuilder prefixToCheck2Sb = new StringBuilder(language); if (extlangFound != null) { prefixToCheck1Sb.append("-").append(extlangFound); prefixToCheck2Sb.append("-").append(extlangFound); } if (scriptFound != null) { prefixToCheck1Sb.append("-").append(scriptFound); prefixToCheck2Sb.append("-").append(scriptFound); } if (regionFound != null) { prefixToCheck1Sb.append("-").append(regionFound); } for (String variantWithPrefixFound : variantWithPrefixesFound) { prefixToCheck1Sb.append("-").append(variantWithPrefixFound); prefixToCheck2Sb.append("-").append(variantWithPrefixFound); } if (!variantPrefixes.contains(prefixToCheck1Sb.toString()) && !variantPrefixes.contains(prefixToCheck2Sb.toString())) { return false; } variantWithPrefixesFound.add(subtag); } else { variantsWithoutPrefixesFound.add(subtag); } } else { return false; } } } return true; }
LanguageRegistryUtil { public static boolean isValidLanguageTag(String tag) { if (tag == null || tag.isEmpty() || tag.indexOf("--") > -1 || !tag.matches("^[a-zA-Z0-9-]+$")) { return false; } tag = tag.toLowerCase(); if (LanguageRegistry.isGrandfatheredTag(tag)) { return true; } if (containsExtensionOrPrivateUseSubtags(tag)) { return false; } String[] subtags = tag.split("-"); if (subtags.length < 1) { return false; } String language = subtags[0]; if (!LanguageRegistry.isLanguageSubtag(language)) { return false; } if (subtags.length > 1) { String extlangFound = null; String scriptFound = null; String regionFound = null; List<String> variantWithPrefixesFound = new ArrayList<>(); List<String> variantsWithoutPrefixesFound = new ArrayList<>(); for (int i = 1; i < subtags.length; i++) { String subtag = subtags[i]; if (LanguageRegistry.isExtlangSubtag(subtag)) { if (extlangFound != null || scriptFound != null || regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } extlangFound = subtag; } else if (LanguageRegistry.isScriptSubtag(subtag)) { if (scriptFound != null || regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } scriptFound = subtag; } else if (LanguageRegistry.isRegionSubtag(subtag)) { if (regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } regionFound = subtag; } else if (LanguageRegistry.isVariantSubtag(subtag)) { if (variantWithPrefixesFound.contains(subtag) || variantsWithoutPrefixesFound.contains(subtag)) { return false; } List<String> variantPrefixes = LanguageRegistry.getVariantPrefixes(subtag); if (!variantPrefixes.isEmpty()) { if (!variantsWithoutPrefixesFound.isEmpty()) { return false; } StringBuilder prefixToCheck1Sb = new StringBuilder(language); StringBuilder prefixToCheck2Sb = new StringBuilder(language); if (extlangFound != null) { prefixToCheck1Sb.append("-").append(extlangFound); prefixToCheck2Sb.append("-").append(extlangFound); } if (scriptFound != null) { prefixToCheck1Sb.append("-").append(scriptFound); prefixToCheck2Sb.append("-").append(scriptFound); } if (regionFound != null) { prefixToCheck1Sb.append("-").append(regionFound); } for (String variantWithPrefixFound : variantWithPrefixesFound) { prefixToCheck1Sb.append("-").append(variantWithPrefixFound); prefixToCheck2Sb.append("-").append(variantWithPrefixFound); } if (!variantPrefixes.contains(prefixToCheck1Sb.toString()) && !variantPrefixes.contains(prefixToCheck2Sb.toString())) { return false; } variantWithPrefixesFound.add(subtag); } else { variantsWithoutPrefixesFound.add(subtag); } } else { return false; } } } return true; } }
LanguageRegistryUtil { public static boolean isValidLanguageTag(String tag) { if (tag == null || tag.isEmpty() || tag.indexOf("--") > -1 || !tag.matches("^[a-zA-Z0-9-]+$")) { return false; } tag = tag.toLowerCase(); if (LanguageRegistry.isGrandfatheredTag(tag)) { return true; } if (containsExtensionOrPrivateUseSubtags(tag)) { return false; } String[] subtags = tag.split("-"); if (subtags.length < 1) { return false; } String language = subtags[0]; if (!LanguageRegistry.isLanguageSubtag(language)) { return false; } if (subtags.length > 1) { String extlangFound = null; String scriptFound = null; String regionFound = null; List<String> variantWithPrefixesFound = new ArrayList<>(); List<String> variantsWithoutPrefixesFound = new ArrayList<>(); for (int i = 1; i < subtags.length; i++) { String subtag = subtags[i]; if (LanguageRegistry.isExtlangSubtag(subtag)) { if (extlangFound != null || scriptFound != null || regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } extlangFound = subtag; } else if (LanguageRegistry.isScriptSubtag(subtag)) { if (scriptFound != null || regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } scriptFound = subtag; } else if (LanguageRegistry.isRegionSubtag(subtag)) { if (regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } regionFound = subtag; } else if (LanguageRegistry.isVariantSubtag(subtag)) { if (variantWithPrefixesFound.contains(subtag) || variantsWithoutPrefixesFound.contains(subtag)) { return false; } List<String> variantPrefixes = LanguageRegistry.getVariantPrefixes(subtag); if (!variantPrefixes.isEmpty()) { if (!variantsWithoutPrefixesFound.isEmpty()) { return false; } StringBuilder prefixToCheck1Sb = new StringBuilder(language); StringBuilder prefixToCheck2Sb = new StringBuilder(language); if (extlangFound != null) { prefixToCheck1Sb.append("-").append(extlangFound); prefixToCheck2Sb.append("-").append(extlangFound); } if (scriptFound != null) { prefixToCheck1Sb.append("-").append(scriptFound); prefixToCheck2Sb.append("-").append(scriptFound); } if (regionFound != null) { prefixToCheck1Sb.append("-").append(regionFound); } for (String variantWithPrefixFound : variantWithPrefixesFound) { prefixToCheck1Sb.append("-").append(variantWithPrefixFound); prefixToCheck2Sb.append("-").append(variantWithPrefixFound); } if (!variantPrefixes.contains(prefixToCheck1Sb.toString()) && !variantPrefixes.contains(prefixToCheck2Sb.toString())) { return false; } variantWithPrefixesFound.add(subtag); } else { variantsWithoutPrefixesFound.add(subtag); } } else { return false; } } } return true; } private  LanguageRegistryUtil(); }
LanguageRegistryUtil { public static boolean isValidLanguageTag(String tag) { if (tag == null || tag.isEmpty() || tag.indexOf("--") > -1 || !tag.matches("^[a-zA-Z0-9-]+$")) { return false; } tag = tag.toLowerCase(); if (LanguageRegistry.isGrandfatheredTag(tag)) { return true; } if (containsExtensionOrPrivateUseSubtags(tag)) { return false; } String[] subtags = tag.split("-"); if (subtags.length < 1) { return false; } String language = subtags[0]; if (!LanguageRegistry.isLanguageSubtag(language)) { return false; } if (subtags.length > 1) { String extlangFound = null; String scriptFound = null; String regionFound = null; List<String> variantWithPrefixesFound = new ArrayList<>(); List<String> variantsWithoutPrefixesFound = new ArrayList<>(); for (int i = 1; i < subtags.length; i++) { String subtag = subtags[i]; if (LanguageRegistry.isExtlangSubtag(subtag)) { if (extlangFound != null || scriptFound != null || regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } extlangFound = subtag; } else if (LanguageRegistry.isScriptSubtag(subtag)) { if (scriptFound != null || regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } scriptFound = subtag; } else if (LanguageRegistry.isRegionSubtag(subtag)) { if (regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } regionFound = subtag; } else if (LanguageRegistry.isVariantSubtag(subtag)) { if (variantWithPrefixesFound.contains(subtag) || variantsWithoutPrefixesFound.contains(subtag)) { return false; } List<String> variantPrefixes = LanguageRegistry.getVariantPrefixes(subtag); if (!variantPrefixes.isEmpty()) { if (!variantsWithoutPrefixesFound.isEmpty()) { return false; } StringBuilder prefixToCheck1Sb = new StringBuilder(language); StringBuilder prefixToCheck2Sb = new StringBuilder(language); if (extlangFound != null) { prefixToCheck1Sb.append("-").append(extlangFound); prefixToCheck2Sb.append("-").append(extlangFound); } if (scriptFound != null) { prefixToCheck1Sb.append("-").append(scriptFound); prefixToCheck2Sb.append("-").append(scriptFound); } if (regionFound != null) { prefixToCheck1Sb.append("-").append(regionFound); } for (String variantWithPrefixFound : variantWithPrefixesFound) { prefixToCheck1Sb.append("-").append(variantWithPrefixFound); prefixToCheck2Sb.append("-").append(variantWithPrefixFound); } if (!variantPrefixes.contains(prefixToCheck1Sb.toString()) && !variantPrefixes.contains(prefixToCheck2Sb.toString())) { return false; } variantWithPrefixesFound.add(subtag); } else { variantsWithoutPrefixesFound.add(subtag); } } else { return false; } } } return true; } private  LanguageRegistryUtil(); static void init(); static boolean isValidLanguageTag(String tag); }
LanguageRegistryUtil { public static boolean isValidLanguageTag(String tag) { if (tag == null || tag.isEmpty() || tag.indexOf("--") > -1 || !tag.matches("^[a-zA-Z0-9-]+$")) { return false; } tag = tag.toLowerCase(); if (LanguageRegistry.isGrandfatheredTag(tag)) { return true; } if (containsExtensionOrPrivateUseSubtags(tag)) { return false; } String[] subtags = tag.split("-"); if (subtags.length < 1) { return false; } String language = subtags[0]; if (!LanguageRegistry.isLanguageSubtag(language)) { return false; } if (subtags.length > 1) { String extlangFound = null; String scriptFound = null; String regionFound = null; List<String> variantWithPrefixesFound = new ArrayList<>(); List<String> variantsWithoutPrefixesFound = new ArrayList<>(); for (int i = 1; i < subtags.length; i++) { String subtag = subtags[i]; if (LanguageRegistry.isExtlangSubtag(subtag)) { if (extlangFound != null || scriptFound != null || regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } extlangFound = subtag; } else if (LanguageRegistry.isScriptSubtag(subtag)) { if (scriptFound != null || regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } scriptFound = subtag; } else if (LanguageRegistry.isRegionSubtag(subtag)) { if (regionFound != null || !variantWithPrefixesFound.isEmpty() || !variantWithPrefixesFound.isEmpty()) { return false; } regionFound = subtag; } else if (LanguageRegistry.isVariantSubtag(subtag)) { if (variantWithPrefixesFound.contains(subtag) || variantsWithoutPrefixesFound.contains(subtag)) { return false; } List<String> variantPrefixes = LanguageRegistry.getVariantPrefixes(subtag); if (!variantPrefixes.isEmpty()) { if (!variantsWithoutPrefixesFound.isEmpty()) { return false; } StringBuilder prefixToCheck1Sb = new StringBuilder(language); StringBuilder prefixToCheck2Sb = new StringBuilder(language); if (extlangFound != null) { prefixToCheck1Sb.append("-").append(extlangFound); prefixToCheck2Sb.append("-").append(extlangFound); } if (scriptFound != null) { prefixToCheck1Sb.append("-").append(scriptFound); prefixToCheck2Sb.append("-").append(scriptFound); } if (regionFound != null) { prefixToCheck1Sb.append("-").append(regionFound); } for (String variantWithPrefixFound : variantWithPrefixesFound) { prefixToCheck1Sb.append("-").append(variantWithPrefixFound); prefixToCheck2Sb.append("-").append(variantWithPrefixFound); } if (!variantPrefixes.contains(prefixToCheck1Sb.toString()) && !variantPrefixes.contains(prefixToCheck2Sb.toString())) { return false; } variantWithPrefixesFound.add(subtag); } else { variantsWithoutPrefixesFound.add(subtag); } } else { return false; } } } return true; } private  LanguageRegistryUtil(); static void init(); static boolean isValidLanguageTag(String tag);  }