View Javadoc

1   /*
2    *  Copyright 2009-2013 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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   * Provider for available currencies using a file.
29   * <p>
30   * This reads currencies from two files.
31   * Firstly it reads the mandatory resource named {@code /org/joda/money/MoneyData.csv}.
32   * Then it reads the optional resource named {@code /org/joda/money/MoneyDataExtension.csv}.
33   * Both will be read as the first found on the classpath.
34   * The second file may replace entries in the first file.
35   */
36  class DefaultCurrencyUnitDataProvider extends CurrencyUnitDataProvider {
37  
38      /** Regex format for the csv line. */
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       * Registers all the currencies known by this provider.
43       * 
44       * @throws Exception if an error occurs
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       * Loads Currencies from a file
54       *  
55       * @param fileName  the file to load, not null
56       * @param isNecessary  whether or not the file is necessary
57       * @throws Exception if a necessary file is not found
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; // no extension file found, no problem. just 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;  // invalid line
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  }