|
|
/*
** Java currency library package.
** Copyright (c) 1999 by Bruno Antunes
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/
package org.gjt.currency;
import java.text.*;
import java.util.*;
import java.io.*;
import org.gjt.currency.*;
/**
* Associates default <code>NumberFormat</code> instances with currency's.
* <br>
* The defaults <code>NumberFormat</code> are read from a properties file
* defaultcurrencylocales.properties, that has the follwing entrys, for
* each currency<br>
* currency.locale.<International Currency Symbol>=<value of Locale.toString()>
*
* @see NumberFormat
*
* @author Bruno Antunes
* @version 1.0 30/10/1999
*/
public class CurrenyFormatManager {
/**
* Associates a international currency symbol with a NumberFormat for
* the currency
*/
private static Hashtable symbolToCurrency = new Hashtable();
private CurrenyFormatManager() {
}
/**
* The registered NumberFormat for the specified currency
*
* @return the NumberFormat for the specified Currency
*/
public static NumberFormat getCurrencyFormat(Currency currency) {
return (NumberFormat) symbolToCurrency.get(
currency.getInternacionalCurrencySymbol());
}
/**
* Registers a NumberFormat instance for a currency
*
* @param currency the currency to associate the format
* @param nf the NumberFormat of the currency
*/
public static void setCurrencyFormat(Currency currency, NumberFormat nf) {
symbolToCurrency.put(currency.getInternacionalCurrencySymbol(),nf);
}
static {
// read the locales to get the default NumberFormat and register the
// instances as default NumberFormat renders for the currencys
try {
Properties p = PropertiesHelper.loadProperties(CurrenyFormatManager.class,
"defaultcurrencylocales.properties");
Locale []locales = NumberFormat.getAvailableLocales();
Locale locale;
NumberFormat currencyFormat;
DecimalFormat df;
DecimalFormatSymbols dfs;
String symbol;
String localeString;
String symbolKey;
String aux;
boolean registerNewSymbols;
// TOSEE: This approach takes to long. Maybe is not a good idea to
// transverse all the NumberFormat.getAvailableLocales()
for (int i=0; i < locales.length; i++) {
currencyFormat = NumberFormat.getCurrencyInstance(locales[i]);
if (currencyFormat instanceof DecimalFormat) {
symbol = ((DecimalFormat)currencyFormat).
getDecimalFormatSymbols().
getInternationalCurrencySymbol();
if (!symbolToCurrency.containsKey(symbol)) {
// We dont have a map, so add one
symbolKey = "currency.locale." + symbol;
localeString = p.getProperty(symbolKey,null);
if (localeString != null) {
// there is an entry in the properties, so lets use
// this locale to get the cutrency instance of
// NumberFormat to use as the formatter
locale = LocaleUtil.parseLocale(localeString);
currencyFormat = NumberFormat.getCurrencyInstance(
locale);
df = (DecimalFormat) currencyFormat;
dfs = df.getDecimalFormatSymbols();
// if there is specified, an groupingSeparator or
// monetaryDecimalSeparator keys, than the dfs
// symbols are used as a base, and will use the new
// currency values specified in the NumberFormat
registerNewSymbols = false;
aux = p.getProperty(symbolKey + ".groupingSeparator",
null);
if ((aux != null) && (aux.length() > 0)) {
registerNewSymbols = true;
dfs.setGroupingSeparator(aux.charAt(0));
}
aux = p.getProperty(symbolKey +
".monetaryDecimalSeparator", null);
if ((aux != null) && (aux.length() > 0)) {
registerNewSymbols = true;
dfs.setMonetaryDecimalSeparator(aux.charAt(0));
}
if (registerNewSymbols) {
df.setDecimalFormatSymbols(dfs);
}
}
// TOSEE: Is it a good idea to store all currencys, even
// if they are not mapped int he properties file?
symbolToCurrency.put(symbol,currencyFormat);
}
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
throw new RuntimeException("Unable do read default properties: " +
ioe.toString());
}
}
}
|