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 | 1 | class DefaultCurrencyUnitDataProvider extends CurrencyUnitDataProvider { |
37 | |
|
38 | |
|
39 | 1 | 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 | 1 | loadCurrenciesFromFile("/org/joda/money/MoneyData.csv", true); |
49 | 1 | loadCurrenciesFromFile("/org/joda/money/MoneyDataExtension.csv", false); |
50 | 1 | } |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
private void loadCurrenciesFromFile(String fileName, boolean isNecessary) throws Exception { |
60 | 2 | InputStream in = getClass().getResourceAsStream(fileName); |
61 | 2 | if (in == null && isNecessary) { |
62 | 0 | throw new FileNotFoundException("Data file " + fileName + " not found"); |
63 | 2 | } else if (in == null && !isNecessary) { |
64 | 0 | return; |
65 | |
} |
66 | 2 | BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); |
67 | |
String line; |
68 | 185 | while ((line = reader.readLine()) != null) { |
69 | 183 | Matcher matcher = REGEX_LINE.matcher(line); |
70 | 183 | if (matcher.matches()) { |
71 | 172 | List<String> countryCodes = new ArrayList<String>(); |
72 | 172 | String codeStr = matcher.group(4); |
73 | 172 | String currencyCode = matcher.group(1); |
74 | 172 | if (codeStr.length() % 2 == 1) { |
75 | 0 | continue; |
76 | |
} |
77 | 428 | for (int i = 0; i < codeStr.length(); i += 2) { |
78 | 256 | countryCodes.add(codeStr.substring(i, i + 2)); |
79 | |
} |
80 | 172 | int numericCode = Integer.parseInt(matcher.group(2)); |
81 | 172 | int digits = Integer.parseInt(matcher.group(3)); |
82 | 172 | registerCurrency(currencyCode, numericCode, digits, countryCodes); |
83 | |
} |
84 | 183 | } |
85 | 2 | } |
86 | |
|
87 | |
} |