1 package org.unicode.cldr.tool;
2 
3 import java.util.LinkedHashMap;
4 import java.util.Map;
5 import java.util.Map.Entry;
6 
7 import org.unicode.cldr.util.CLDRPaths;
8 import org.unicode.cldr.util.XMLFileReader;
9 
10 public class ReadXMB {
11 
12     /*
13      * Two cases:
14      * <!-- //ldml/characters/ellipsis[@type="final"] -->
15      * <msg id='8185172660664561036' desc='Supply the elipsis pattern for when the final part of a string is omitted.
16      * Note: before translating, be sure to read http://cldr.org/translation/characters.'
17      * ><ph name='FIRST_PART_OF_TEXT'><ex>very long na</ex>{0}</ph>…</msg>
18      * <!-- English original: {0}… -->
19      * and
20      * <!-- //ldml/characters/exemplarCharacters[@type="currencySymbol"] -->
21      * <msg id='684343635911473473' desc='Supply the characters used in your language for the "currencySymbol" category.
22      * Note: before translating, be sure to read http://cldr.org/translation/exemplars.'
23      * >[a b c č d e f g h i j k l ł m n o º p q r s t u v w x y z]</msg>
24      */
load(String directory, String file)25     public static Map<String, String> load(String directory, String file) {
26         final CasingHandler simpleHandler = new CasingHandler();
27         XMLFileReader xfr = new XMLFileReader().setHandler(simpleHandler);
28         xfr.read(directory + "/" + file, XMLFileReader.CONTENT_HANDLER | XMLFileReader.ERROR_HANDLER
29             | XMLFileReader.LEXICAL_HANDLER, true);
30         simpleHandler.flush();
31         return simpleHandler.info;
32     }
33 
34     private static class CasingHandler extends XMLFileReader.SimpleHandler {
35         public Map<String, String> info = new LinkedHashMap<>();
36         String path;
37         String id;
38         String value;
39 
40         @Override
handlePathValue(String pathx, String value)41         public void handlePathValue(String pathx, String value) {
42             // System.out.println("*PATH:\t" + pathx + "\t\t" + value);
43             int pos = pathx.indexOf("[@id=\"");
44             int posEnd = pathx.indexOf("\"]", pos + 6);
45             id = pathx.substring(pos + 6, posEnd);
46             if (id == null) {
47                 System.out.println("PATH:\t" + pathx + "\t\t" + value);
48             }
49             this.value = value;
50         }
51 
52         @Override
handleComment(String pathx, String comment)53         public void handleComment(String pathx, String comment) {
54             // System.out.println("*COMMENT:\t" + path + "\t\t" + comment);
55             comment = comment.trim();
56             if (comment.startsWith("//ldml")) {
57                 flush();
58                 path = comment;
59             } else if (comment.startsWith("English original:")) {
60                 value = comment.substring(17).trim();
61             } else {
62                 System.out.println("COMMENT:\t" + pathx + "\t\t" + comment);
63             }
64         }
65 
flush()66         private void flush() {
67             // System.out.println(id + "\t" + value + "\t" + path);
68             if (path != null) {
69                 info.put(path, value);
70             }
71             id = value = path = null;
72         }
73     }
74 
main(String[] args)75     public static void main(String[] args) {
76         Map<String, String> info = load(CLDRPaths.BASE_DIRECTORY + "tools/java/org/unicode/cldr/unittest/data/xmb/",
77             "en.xml");
78         System.out.println("============");
79         for (Entry<String, String> entry : info.entrySet()) {
80             System.out.println(entry.getValue() + "\t" + entry.getKey());
81         }
82     }
83 }