1 package org.unicode.cldr.util; 2 3 import com.google.common.base.Splitter; 4 import com.google.common.collect.ImmutableMap; 5 import com.google.common.collect.Maps; 6 import com.ibm.icu.text.UnicodeSet; 7 import java.io.BufferedReader; 8 import java.io.File; 9 import java.io.IOException; 10 import java.util.ArrayList; 11 import java.util.Iterator; 12 import java.util.List; 13 import java.util.Map; 14 15 public class ScriptToExemplars { 16 public static final String FILE_PATH = "data/locales/scriptToExemplars.txt"; 17 getExemplars(String script)18 public static UnicodeSet getExemplars(String script) { 19 return ScriptToExemplarsLoader.SINGLETON.getExemplars(script); 20 } 21 22 /** return the comment block from the original file */ getCommentBlock()23 private static String getCommentBlock() { 24 return String.join("\n", ScriptToExemplarsLoader.SINGLETON.comments) + '\n'; 25 } 26 27 private static class ScriptToExemplarsLoader { 28 private static final ScriptToExemplarsLoader SINGLETON = new ScriptToExemplarsLoader(); 29 private Map<String, UnicodeSet> data; 30 private String[] comments; 31 getExemplars(String script)32 private UnicodeSet getExemplars(String script) { 33 UnicodeSet result = data.get(script); 34 return result == null ? UnicodeSet.EMPTY : result; 35 } 36 37 { 38 Map<String, UnicodeSet> _data = Maps.newTreeMap(); 39 List<String> _comments = new ArrayList<String>(); try(BufferedReader reader = FileReaders.openFile(ScriptToExemplars.class, FILE_PATH))40 try (BufferedReader reader = FileReaders.openFile(ScriptToExemplars.class, FILE_PATH)) { 41 Iterable<String> rlsi = 42 With.toIterable(new FileReaders.ReadLineSimpleIterator(reader)); 43 for (String line : rlsi) { 44 if (line.isBlank()) { 45 continue; 46 } else if (line.startsWith("#")) { 47 _comments.add(line.trim()); 48 continue; 49 } 50 Iterator<String> parts = Splitter.on(';').trimResults().split(line).iterator(); 51 String script = parts.next(); 52 int size = Integer.parseInt(parts.next()); 53 UnicodeSet uset = new UnicodeSet(parts.next()).freeze(); 54 _data.put(script, uset); 55 } 56 } catch (IOException e) { 57 throw new RuntimeException(e); 58 } 59 data = ImmutableMap.copyOf(_data); 60 comments = _comments.toArray(new String[_comments.size()]); 61 } 62 } 63 64 /** Called by LikelySubtagsTest.testGetResolvedScriptVsExemplars */ write(Map<String, UnicodeSet> expected)65 public static void write(Map<String, UnicodeSet> expected) { 66 final File file = new File(CLDRPaths.UTIL_SRC_DATA_DIR, FILE_PATH); 67 try (TempPrintWriter out = new TempPrintWriter(file)) { 68 // copy all comment lines in the file 69 out.println(getCommentBlock()); 70 // copy all updated sets 71 for (Map.Entry<String, UnicodeSet> entry : expected.entrySet()) { 72 String script = entry.getKey(); 73 UnicodeSet flattened = entry.getValue(); 74 if (!flattened.isEmpty()) { 75 out.println( 76 script 77 + " ;\t" 78 + flattened.size() 79 + " ;\t" 80 + flattened.toPattern(false)); 81 } 82 } 83 System.err.println( 84 "Wrote: " 85 + file.getAbsolutePath() 86 + "\n Please check it carefully and commit it if needed."); 87 } 88 } 89 } 90