Permalink
Browse files

Complete change to allow second currency file

  • Loading branch information...
1 parent 6e3d154 commit 225190ff0745d903ca1df61d23e0086da2b8e2c5 @jodastephen jodastephen committed Jan 30, 2013
@@ -8,6 +8,11 @@
<body>
<!-- types are add, fix, remove, update -->
+ <release version="0.7" date="2013-01-30" description="Version 0.7.1">
+ <action dev="timmolter" type="add">
+ Allow a second currency data file to enhance those in the base file [9,13]
+ </action>
+ </release>
<release version="0.7" date="2013-01-03" description="Version 0.7">
<action dev="scolebourne" type="fix">
Fix printing of grouped negative numbers [10]
@@ -27,58 +27,60 @@
/**
* Provider for available currencies using a file.
* <p>
- * This reads the first resource named {@code /org/joda/money/MoneyData.csv} on the classpath.
+ * This reads currencies from two files.
+ * Firstly it reads the mandatory resource named {@code /org/joda/money/MoneyData.csv}.
+ * Then it reads the optional resource named {@code /org/joda/money/MoneyDataExtension.csv}.
+ * Both will be read as the first found on the classpath.
*/
class DefaultCurrencyUnitDataProvider extends CurrencyUnitDataProvider {
/** Regex format for the csv line. */
private static final Pattern REGEX_LINE = Pattern.compile("([A-Z]{3}),(-1|[0-9]{1,3}),(-1|0|1|2|3),([A-Z]*)#?.*");
-
/**
* Registers all the currencies known by this provider.
*
* @throws Exception if an error occurs
*/
@Override
protected void registerCurrencies() throws Exception {
- loadCurrenciesFromFile("/org/joda/money/MoneyData.csv", true);
- loadCurrenciesFromFile("/org/joda/money/MoneyDataExtension.csv", false);
+ loadCurrenciesFromFile("/org/joda/money/MoneyData.csv", true);
+ loadCurrenciesFromFile("/org/joda/money/MoneyDataExtension.csv", false);
}
/**
* Loads Currencies from a file
*
- * @param fileName the file to load
- * @param isNecessary whether or not the file is necessary
- * @throws Exception if a necessary file isn't found
+ * @param fileName the file to load, not null
+ * @param isNecessary whether or not the file is necessary
+ * @throws Exception if a necessary file is not found
*/
- private void loadCurrenciesFromFile(String fileName, boolean isNecessary) throws Exception {
- InputStream in = getClass().getResourceAsStream(fileName);
- if (in == null && isNecessary) {
- throw new FileNotFoundException("Data file " + fileName + " not found");
- }else if (in == null && !isNecessary) {
- return; // no extension file found, no problem. just return
- }
- BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
- String line;
- while ((line = reader.readLine()) != null) {
- Matcher matcher = REGEX_LINE.matcher(line);
- if (matcher.matches()) {
- List<String> countryCodes = new ArrayList<String>();
- String codeStr = matcher.group(4);
- String currencyCode = matcher.group(1);
- if (codeStr.length() % 2 == 1) {
- continue; // invalid line
- }
- for (int i = 0; i < codeStr.length(); i += 2) {
- countryCodes.add(codeStr.substring(i, i + 2));
- }
- int numericCode = Integer.parseInt(matcher.group(2));
- int digits = Integer.parseInt(matcher.group(3));
- registerCurrency(currencyCode, numericCode, digits, countryCodes);
- }
- }
+ private void loadCurrenciesFromFile(String fileName, boolean isNecessary) throws Exception {
+ InputStream in = getClass().getResourceAsStream(fileName);
+ if (in == null && isNecessary) {
+ throw new FileNotFoundException("Data file " + fileName + " not found");
+ } else if (in == null && !isNecessary) {
+ return; // no extension file found, no problem. just return
+ }
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
+ String line;
+ while ((line = reader.readLine()) != null) {
+ Matcher matcher = REGEX_LINE.matcher(line);
+ if (matcher.matches()) {
+ List<String> countryCodes = new ArrayList<String>();
+ String codeStr = matcher.group(4);
+ String currencyCode = matcher.group(1);
+ if (codeStr.length() % 2 == 1) {
+ continue; // invalid line
+ }
+ for (int i = 0; i < codeStr.length(); i += 2) {
+ countryCodes.add(codeStr.substring(i, i + 2));
+ }
+ int numericCode = Integer.parseInt(matcher.group(2));
+ int digits = Integer.parseInt(matcher.group(3));
+ registerCurrency(currencyCode, numericCode, digits, countryCodes);
+ }
+ }
}
}
@@ -51,10 +51,10 @@ The formatting code is a good example of this.
<section name="Currency information">
<p>
Joda-Money includes its own currency class as the implementation in the JDK is too restrictive.
-The data for the Joda-Money class is provided by a single configuration file.
+The data for the Joda-Money class is provided by two configuration files.
</p>
<p>
-The file <code>MoneyData.csv</code> holds a list of all the valid currencies.
+The file <code>org/joda/money/MoneyData.csv</code> holds a basic list of valid currencies.
The columns are as follows:
<ul>
<li><a href="http://en.wikipedia.org/wiki/ISO_4217">ISO-4217</a> currency code - three letters, mandatory</li>
@@ -68,6 +68,12 @@ adjacent to one another and without separators</li>
The library will load the first version of this file it finds in the classpath based on the
class loader that Joda-Money is loaded in.
</p>
+<p>
+The file <code>org/joda/money/MoneyDataExtension.csv</code> holds any user extensions to the currency list.
+It has the same file format as above.
+This file is optional and not supplied by default.
+Again, the first instance of the file on the classpath will be loaded.
+</p>
</section>

0 comments on commit 225190f

Please sign in to comment.