1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.money;
17
18 import java.io.BufferedReader;
19 import java.io.FileNotFoundException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27
28
29
30
31
32
33
34
35
36 class DefaultCurrencyUnitDataProvider extends CurrencyUnitDataProvider {
37
38
39 private static final Pattern REGEX_LINE = Pattern.compile("([A-Z]{3}),(-1|[0-9]{1,3}),(-1|[0-9]),([A-Z]*)#?.*");
40
41
42
43
44
45
46 @Override
47 protected void registerCurrencies() throws Exception {
48 loadCurrenciesFromFile("/org/joda/money/MoneyData.csv", true);
49 loadCurrenciesFromFile("/org/joda/money/MoneyDataExtension.csv", false);
50 }
51
52
53
54
55
56
57
58
59 private void loadCurrenciesFromFile(String fileName, boolean isNecessary) throws Exception {
60 InputStream in = getClass().getResourceAsStream(fileName);
61 if (in == null && isNecessary) {
62 throw new FileNotFoundException("Data file " + fileName + " not found");
63 } else if (in == null && !isNecessary) {
64 return;
65 }
66 BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
67 String line;
68 while ((line = reader.readLine()) != null) {
69 Matcher matcher = REGEX_LINE.matcher(line);
70 if (matcher.matches()) {
71 List<String> countryCodes = new ArrayList<String>();
72 String codeStr = matcher.group(4);
73 String currencyCode = matcher.group(1);
74 if (codeStr.length() % 2 == 1) {
75 continue;
76 }
77 for (int i = 0; i < codeStr.length(); i += 2) {
78 countryCodes.add(codeStr.substring(i, i + 2));
79 }
80 int numericCode = Integer.parseInt(matcher.group(2));
81 int digits = Integer.parseInt(matcher.group(3));
82 registerCurrency(currencyCode, numericCode, digits, countryCodes);
83 }
84 }
85 }
86
87 }