• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         public ValuePathStatus.MissingOK transform(String source) {
28             return ValuePathStatus.MissingOK.valueOf(source);
29         }
30     };
31 
32     static final RegexLookup<ValuePathStatus.MissingOK> missingOk = new RegexLookup<ValuePathStatus.MissingOK>()
33         .setPatternTransform(RegexLookup.RegexFinderTransformPath)
34         .setValueTransform(MISSING_STATUS_TRANSFORM)
35         .loadFromFile(ValuePathStatus.class, "data/paths/missingOk.txt");
36 
countZeros(String otherValue)37     static int countZeros(String otherValue) {
38         int result = 0;
39         for (int i = 0; i < otherValue.length(); ++i) {
40             if (otherValue.charAt(i) == '0') {
41                 ++result;
42             }
43         }
44         return result;
45     }
46 
isMissingOk(CLDRFile sourceFile, String path, boolean latin, boolean aliased)47     public static boolean isMissingOk(CLDRFile sourceFile, String path, boolean latin, boolean aliased) {
48         Output<String[]> arguments = new Output<String[]>();
49         List<String> failures = null;
50 //        if (path.startsWith("//ldml/characters/parseLenients")) {
51 //            int debug = 0;
52 //            failures = new ArrayList<>();
53 //        }
54         ValuePathStatus.MissingOK value = missingOk.get(path, null, arguments, null, failures);
55         if (value == null) {
56             return false;
57         }
58         switch (value) {
59         case ok:
60             return true;
61         case latin:
62             return latin;
63         case alias:
64             return aliased;
65         case compact:
66             // special processing for compact numbers
67             if (path.contains("[@count=\"other\"]")) {
68                 return false; // the 'other' class always counts as missing
69             }
70             String otherPath = "//ldml/numbers/decimalFormats[@numberSystem=\"" + arguments.value[1]
71                 + "\"]/decimalFormatLength[@type=\"" + arguments.value[2]
72                 + "\"]/decimalFormat[@type=\"standard\"]/pattern[@type=\"" + arguments.value[3]
73                 + "\"][@count=\"other\"]";
74             String otherValue = sourceFile.getWinningValue(otherPath);
75             if (otherValue == null) {
76                 return false; // something's wrong, bail
77             }
78             int digits = countZeros(otherValue);
79             if (digits > 4) { // we can only handle to 4 digits
80                 return false;
81             }
82             // if there are no possible Count values for this many digits, then it is ok to be missing.
83             Count c = Count.valueOf(arguments.value[4]);
84             SupplementalDataInfo supplementalDataInfo2 = CLDRConfig.getInstance().getSupplementalDataInfo();
85             // SupplementalDataInfo.getInstance(sourceFile.getSupplementalDirectory());
86             PluralInfo plurals = supplementalDataInfo2.getPlurals(sourceFile.getLocaleID());
87             return plurals == null || !plurals.hasSamples(c, digits); // ok if no samples
88         // TODO: handle fractions
89         default:
90             throw new IllegalArgumentException();
91         }
92     }
93 
94 }
95