1 package org.unicode.cldr.util; 2 3 import java.util.List; 4 5 import org.unicode.cldr.util.CLDRFile.WinningChoice; 6 import org.unicode.cldr.util.SupplementalDataInfo.PluralInfo; 7 import org.unicode.cldr.util.SupplementalDataInfo.PluralInfo.Count; 8 9 import com.ibm.icu.text.Transform; 10 import com.ibm.icu.text.UnicodeSet; 11 import com.ibm.icu.util.Output; 12 13 public class ValuePathStatus { 14 15 public enum MissingOK { 16 ok, latin, alias, compact 17 } 18 19 public static final UnicodeSet LATIN = new UnicodeSet("[:sc=Latn:]").freeze(); 20 isLatinScriptLocale(CLDRFile sourceFile)21 public static boolean isLatinScriptLocale(CLDRFile sourceFile) { 22 UnicodeSet main = sourceFile.getExemplarSet("", WinningChoice.WINNING); 23 return LATIN.containsSome(main); 24 } 25 26 public static Transform<String, ValuePathStatus.MissingOK> MISSING_STATUS_TRANSFORM = new Transform<String, ValuePathStatus.MissingOK>() { 27 @Override 28 public ValuePathStatus.MissingOK transform(String source) { 29 return ValuePathStatus.MissingOK.valueOf(source); 30 } 31 }; 32 33 static final RegexLookup<ValuePathStatus.MissingOK> missingOk = new RegexLookup<ValuePathStatus.MissingOK>() 34 .setPatternTransform(RegexLookup.RegexFinderTransformPath) 35 .setValueTransform(MISSING_STATUS_TRANSFORM) 36 .loadFromFile(ValuePathStatus.class, "data/paths/missingOk.txt"); 37 countZeros(String otherValue)38 static int countZeros(String otherValue) { 39 int result = 0; 40 for (int i = 0; i < otherValue.length(); ++i) { 41 if (otherValue.charAt(i) == '0') { 42 ++result; 43 } 44 } 45 return result; 46 } 47 isMissingOk(CLDRFile sourceFile, String path, boolean latin, boolean aliased)48 public static boolean isMissingOk(CLDRFile sourceFile, String path, boolean latin, boolean aliased) { 49 Output<String[]> arguments = new Output<>(); 50 List<String> failures = null; 51 // if (path.startsWith("//ldml/characters/parseLenients")) { 52 // int debug = 0; 53 // failures = new ArrayList<>(); 54 // } 55 ValuePathStatus.MissingOK value = missingOk.get(path, null, arguments, null, failures); 56 if (value == null) { 57 return false; 58 } 59 switch (value) { 60 case ok: 61 return true; 62 case latin: 63 return latin; 64 case alias: 65 return aliased; 66 case compact: 67 // special processing for compact numbers 68 if (path.contains("[@count=\"other\"]")) { 69 return false; // the 'other' class always counts as missing 70 } 71 String otherPath = "//ldml/numbers/decimalFormats[@numberSystem=\"" + arguments.value[1] 72 + "\"]/decimalFormatLength[@type=\"" + arguments.value[2] 73 + "\"]/decimalFormat[@type=\"standard\"]/pattern[@type=\"" + arguments.value[3] 74 + "\"][@count=\"other\"]"; 75 String otherValue = sourceFile.getWinningValue(otherPath); 76 if (otherValue == null) { 77 return false; // something's wrong, bail 78 } 79 int digits = countZeros(otherValue); 80 if (digits > 4) { // we can only handle to 4 digits 81 return false; 82 } 83 // if there are no possible Count values for this many digits, then it is ok to be missing. 84 Count c = Count.valueOf(arguments.value[4]); 85 SupplementalDataInfo supplementalDataInfo2 = CLDRConfig.getInstance().getSupplementalDataInfo(); 86 // SupplementalDataInfo.getInstance(sourceFile.getSupplementalDirectory()); 87 PluralInfo plurals = supplementalDataInfo2.getPlurals(sourceFile.getLocaleID()); 88 return plurals == null || !plurals.hasSamples(c, digits); // ok if no samples 89 // TODO: handle fractions 90 default: 91 throw new IllegalArgumentException(); 92 } 93 } 94 95 } 96