• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4  *******************************************************************************
5  * Copyright (C) 2012, International Business Machines Corporation and         *
6  * others. All Rights Reserved.                                                *
7  *******************************************************************************
8  */
9 package com.ibm.icu.dev.tool.currency;
10 
11 import java.io.File;
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import java.io.OutputStreamWriter;
15 import java.util.Collection;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import java.util.SortedMap;
19 import java.util.TreeSet;
20 
21 /**
22  * The tool used for ISO 4217 alpha3-numeric3 code mapping data maintenance.
23  * This code is used for synchronizing ICU alpha3-numeric3 mapping data with
24  * the data distributed by SIX Interbank Clearing.
25  */
26 public class Main {
27     private enum Command {
28         CHECK,
29         PRINT,
30         BUILD,
31     };
32 
33     private static final String RESNAME = "currencyNumericCodes";
34 
main(String... args)35     public static void main(String... args) {
36         if (args.length == 0) {
37             printUsage();
38             System.exit(1);
39         }
40 
41         Command cmd = null;
42 
43         // 1st argument must be command
44         if (args[0].equalsIgnoreCase(Command.CHECK.name())) {
45             if (args.length == 3) {
46                 cmd = Command.CHECK;
47             }
48         } else if (args[0].equalsIgnoreCase(Command.PRINT.name())) {
49             if (args.length == 3) {
50                 cmd = Command.PRINT;
51             }
52         } else if (args[0].equalsIgnoreCase(Command.BUILD.name())) {
53             if (args.length == 2) {
54                 cmd = Command.BUILD;
55             }
56         }
57 
58         if (cmd == null) {
59             printUsage();
60             System.exit(1);
61         }
62 
63         int status = 0;
64 
65         if (cmd == Command.BUILD) {
66             File outfile = new File(args[1], RESNAME + ".txt");
67             try {
68                 OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8");
69                 NumericCodeData.getDefaultInstance().writeResource(osw, RESNAME);
70             } catch (IOException e) {
71                 e.printStackTrace();
72                 status = 1;
73             }
74         } else {
75             // 2nd argument is current data xml file
76             // 3rd argument is historic data xml file
77             File currentXml = new File(args[1]);
78             File historicXml = new File(args[2]);
79             Collection<CurrencyDataEntry> currentDataEntries = null;
80             Collection<CurrencyDataEntry> historicDataEntries = null;
81 
82             try {
83                 currentDataEntries = CurrencyDataParser.parse(currentXml, false);
84                 historicDataEntries = CurrencyDataParser.parse(historicXml, true);
85             } catch (IOException e) {
86                 e.printStackTrace();
87                 status = 1;
88             }
89 
90             if (status == 0) {
91                 NumericCodeData numCodeData = NumericCodeData.createInstance(currentDataEntries);
92                 numCodeData.merge(NumericCodeData.createInstance(historicDataEntries));
93 
94                 if (cmd == Command.PRINT) {
95                     numCodeData.printArray();
96                 } else {
97                     assert(cmd == Command.CHECK);
98                     boolean isOK = checkData(numCodeData);
99                     if (isOK) {
100                         System.out.println("[OK] ICU data is synchronized with the reference data");
101                     } else {
102                         status = 1;
103                     }
104                 }
105             }
106         }
107 
108         System.exit(status);
109     }
110 
printUsage()111     private static void printUsage() {
112         System.out.println("[Usage]");
113         System.out.println("");
114         System.out.println("  1) java com.ibm.icu.dev.tool.currency.Main check <currentXML> <historicXML>");
115         System.out.println("");
116         System.out.println("  Verifies the ICU data (table in NumericCodeData) with the reference data.");
117         System.out.println("");
118         System.out.println("  Argument(s):");
119         System.out.println("    <currentXML>  - Current currencies & funds data (Table A.1) in XML format");
120         System.out.println("    <historicXML> - Historic denominations data (Table A.3) in XML format");
121         System.out.println("");
122         System.out.println("  2) java com.ibm.icu.dev.tool.currency.Main print <currentXML> <historicXML>");
123         System.out.println("");
124         System.out.println("  Prints out the alpha-numeric code mapping imported from the reference data.");
125         System.out.println("");
126         System.out.println("  Argument(s):");
127         System.out.println("    <currentXML>  - Current currencies & funds data (Table A.1) in XML format");
128         System.out.println("    <historicXML> - Historic denominations data (Table A.3) in XML format");
129         System.out.println("");
130         System.out.println("  3) java com.ibm.icu.dev.tool.currency.Main build <outtxt>");
131         System.out.println("");
132         System.out.println("  Writes out the alpha-numeric in NumericCodeData into ICU resource bundle source");
133         System.out.println("  (.txt) format.");
134         System.out.println("");
135         System.out.println("  Argument(s):");
136         System.out.println("    <outdir>      - Output directory of the ICU resource bundle source (.txt) format");
137         System.out.println("");
138         System.out.println("[Note]");
139         System.out.println("  Reference XML files are distributed by the ISO 4217 maintenance agency at");
140         System.out.println("  [http://www.currency-iso.org/iso_index/iso_tables.htm].");
141     }
142 
checkData(NumericCodeData refData)143     private static boolean checkData(NumericCodeData refData) {
144         boolean isOK = true;
145 
146         SortedMap<String, String> icuMap = NumericCodeData.getDefaultInstance().getAlphaNumericCodeMap();
147         SortedMap<String, String> refMap = refData.getAlphaNumericCodeMap();
148 
149         for (Entry<String, String> refEntry : refMap.entrySet()) {
150             String refAlpha = refEntry.getKey();
151             String refNumeric = refEntry.getValue();
152 
153             String icuNumeric = icuMap.get(refAlpha);
154             if (icuNumeric == null) {
155                 System.out.println("Missing alpha code in ICU map [" + refAlpha + "]");
156                 isOK = false;
157             } else if (!icuNumeric.equals(refNumeric)) {
158                 System.out.println("Numeric code mismatch [" + refAlpha + "] ICU=" + icuNumeric + " - Reference=" + refNumeric);
159                 isOK = false;
160             }
161         }
162 
163         Set<String> icuKeySet = icuMap.keySet();
164         Set<String> refKeySet = refMap.keySet();
165         if (!refKeySet.containsAll(icuKeySet)) {
166             isOK = false;
167             Set<String> tmp = new TreeSet<String>(icuKeySet);
168             tmp.removeAll(refKeySet);
169 
170             StringBuilder buf = new StringBuilder();
171             for (String alpha : tmp) {
172                 if (buf.length() != 0) {
173                     buf.append(", ");
174                 }
175                 buf.append(alpha);
176             }
177 
178             System.out.println("Codes not found in the reference data: " + buf);
179         }
180 
181         return isOK;
182     }
183 }
184