Home | History | Annotate | Download | only in tool
      1 package org.unicode.cldr.tool;
      2 
      3 import java.util.LinkedHashMap;
      4 import java.util.Map;
      5 import java.util.Map.Entry;
      6 import java.util.Set;
      7 import java.util.TreeMap;
      8 import java.util.TreeSet;
      9 
     10 import org.unicode.cldr.util.CLDRFile;
     11 import org.unicode.cldr.util.CLDRPaths;
     12 import org.unicode.cldr.util.Counter;
     13 import org.unicode.cldr.util.Factory;
     14 import org.unicode.cldr.util.LanguageTagParser;
     15 import org.unicode.cldr.util.LocaleIDParser;
     16 import org.unicode.cldr.util.PrettyPath;
     17 
     18 import com.ibm.icu.dev.util.CollectionUtilities;
     19 import com.ibm.icu.impl.Relation;
     20 
     21 public class ShowChildren {
     22 
     23     public static void main(String[] args) {
     24         System.out.println("Arguments: " + CollectionUtilities.join(args, " "));
     25 
     26         long startTime = System.currentTimeMillis();
     27 
     28         Factory cldrFactory = Factory.make(CLDRPaths.MAIN_DIRECTORY, ".*");
     29         Set<String> locales = new TreeSet<String>(cldrFactory.getAvailable());
     30 
     31         Relation<String, String> parent2children = Relation.of(new TreeMap<String, Set<String>>(), TreeSet.class);
     32         LanguageTagParser ltp = new LanguageTagParser();
     33         for (String locale : locales) {
     34             String parent = getParentWithoutRegion(ltp, locale);
     35             if (!parent.equals(locale)) {
     36                 parent2children.put(parent, locale);
     37             }
     38         }
     39         PrettyPath prettyPath = new PrettyPath();
     40 
     41         final CLDRFile english = ToolConfig.getToolInstance().getEnglish();
     42         Counter<String> deviations = new Counter<String>();
     43 
     44         for (Entry<String, Set<String>> entry : parent2children.keyValuesSet()) {
     45             Map<String, Relation<String, String>> path2value2locales = new TreeMap<String, Relation<String, String>>();
     46             String parent = entry.getKey();
     47             String parentName = english.getName(parent);
     48             CLDRFile parentFile = (CLDRFile) cldrFactory.make(parent, true);
     49 
     50             Set<String> children = entry.getValue();
     51             for (String child : children) {
     52                 CLDRFile file = (CLDRFile) cldrFactory.make(child, false);
     53                 for (String path : file) {
     54                     if (path.startsWith("//ldml/identity")
     55                         || path.endsWith("/alias")
     56                         || path.endsWith("/commonlyUsed")) {
     57                         continue;
     58                     }
     59                     Relation<String, String> value2locales = path2value2locales.get(path);
     60                     if (value2locales == null) {
     61                         path2value2locales.put(path,
     62                             value2locales = Relation.of(new LinkedHashMap<String, Set<String>>(), TreeSet.class));
     63                     }
     64                     String parentValue = parentFile.getStringValue(path);
     65                     if (parentValue == null) {
     66                         parentValue = "*MISSING*";
     67                     }
     68                     String childValue = file.getStringValue(path);
     69                     if (parentValue.equals(childValue)) {
     70                         continue;
     71                     }
     72                     value2locales.put(parentValue, parent);
     73                     value2locales.put(childValue, child);
     74                 }
     75             }
     76             if (path2value2locales.size() == 0) {
     77                 continue;
     78             }
     79             for (Entry<String, Relation<String, String>> datum : path2value2locales.entrySet()) {
     80                 String path = datum.getKey();
     81                 String ppath = prettyPath.getPrettyPath(path, false);
     82                 Relation<String, String> value2locales = datum.getValue();
     83                 for (Entry<String, Set<String>> valueAndLocales : value2locales.keyValuesSet()) {
     84                     System.out.println(parentName + "\t" + parent + "\t" + valueAndLocales.getKey() + "\t"
     85                         + valueAndLocales.getValue() + "\t" + ppath);
     86                 }
     87                 System.out.println();
     88             }
     89             deviations.add(parent, path2value2locales.size());
     90         }
     91         for (String locale : deviations.getKeysetSortedByKey()) {
     92             String parentName = english.getName(locale);
     93             System.out.println(parentName + "\t" + locale + "\t" + deviations.get(locale));
     94         }
     95 
     96         System.out
     97             .println("Done -- Elapsed time: " + ((System.currentTimeMillis() - startTime) / 60000.0) + " minutes");
     98     }
     99 
    100     private static String getParentWithoutRegion(LanguageTagParser ltp, String locale) {
    101         while (true) {
    102             if (ltp.set(locale).getRegion().isEmpty()) {
    103                 return locale;
    104             }
    105             locale = LocaleIDParser.getParent(locale);
    106         }
    107     }
    108 }