xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/json/LdmlConfigFileReader.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.json;
2 
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.TreeMap;
9 import org.unicode.cldr.json.Ldml2JsonConverter.JSONSection;
10 import org.unicode.cldr.json.Ldml2JsonConverter.RunType;
11 import org.unicode.cldr.util.FileProcessor;
12 import org.unicode.cldr.util.PatternCache;
13 
14 /** Reader for the JSON_config.txt type files */
15 public class LdmlConfigFileReader {
16     final List<JSONSection> sections = new ArrayList<JSONSection>();
17     /** Map of package to description */
18     final Map<String, String> packages = new TreeMap<>();
19 
20     final Map<String, String> dependencies = new HashMap<>();
21 
getSections()22     public List<JSONSection> getSections() {
23         return sections;
24     }
25 
getPackages()26     public Set<String> getPackages() {
27         return packages.keySet();
28     }
29 
getPackageDescriptions()30     public Map<String, String> getPackageDescriptions() {
31         return packages;
32     }
33 
getDependencies()34     public Map<String, String> getDependencies() {
35         return dependencies;
36     }
37 
read(final String configFile, final RunType type)38     public void read(final String configFile, final RunType type) {
39         FileProcessor myReader =
40                 new FileProcessor() {
41                     @Override
42                     protected boolean handleLine(int lineCount, String line) {
43                         String[] lineParts = line.trim().split("\\s*;\\s*");
44                         String key,
45                                 value,
46                                 section = null,
47                                 path = null,
48                                 packageName = null,
49                                 dependency = null;
50                         String packageDesc = "A CLDR package with no packageDesc description.";
51                         boolean hasSection = false;
52                         boolean hasPath = false;
53                         boolean hasPackage = false;
54                         boolean hasDependency = false;
55                         for (String linePart : lineParts) {
56                             int pos = linePart.indexOf('=');
57                             if (pos < 0) {
58                                 throw new IllegalArgumentException();
59                             }
60                             key = linePart.substring(0, pos);
61                             value = linePart.substring(pos + 1);
62                             if (key.equals("section")) {
63                                 hasSection = true;
64                                 section = value;
65                             } else if (key.equals("path")) {
66                                 hasPath = true;
67                                 path = value;
68                             } else if (key.equals("package")) {
69                                 hasPackage = true;
70                                 packageName = value;
71                             } else if (key.equals("packageDesc")) {
72                                 packageDesc = value;
73                             } else if (key.equals("dependency")) {
74                                 hasDependency = true;
75                                 dependency = value;
76                             }
77                         }
78                         if (hasSection && hasPath) {
79                             JSONSection j = new JSONSection();
80                             j.section = section;
81                             j.pattern = PatternCache.get(path);
82                             if (hasPackage) {
83                                 j.packageName = packageName;
84                             }
85                             sections.add(j);
86                         }
87                         if (hasDependency && hasPackage) {
88                             dependencies.put(packageName, dependency);
89                         }
90                         if (hasPackage) {
91                             packages.putIfAbsent(packageName, packageDesc);
92                         }
93                         return true;
94                     }
95                 };
96 
97         if (configFile != null) {
98             myReader.process(configFile);
99         } else {
100             switch (type) {
101                 case main:
102                     myReader.process(Ldml2JsonConverter.class, "JSON_config.txt");
103                     break;
104                 case supplemental:
105                     myReader.process(Ldml2JsonConverter.class, "JSON_config_supplemental.txt");
106                     break;
107                 case segments:
108                     myReader.process(Ldml2JsonConverter.class, "JSON_config_segments.txt");
109                     break;
110                 case rbnf:
111                     myReader.process(Ldml2JsonConverter.class, "JSON_config_rbnf.txt");
112                     break;
113                 default:
114                     myReader.process(
115                             Ldml2JsonConverter.class, "JSON_config_" + type.name() + ".txt");
116             }
117         }
118 
119         // Add a section at the end of the list that will match anything not already matched.
120         JSONSection j = new JSONSection();
121         j.section = "other";
122         j.pattern = PatternCache.get(".*");
123         sections.add(j);
124     }
125 }
126