From bff99dbeeb05260c1c70c4ac79dbeba4474255d4 Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 5 Nov 2003 00:07:53 +0000 Subject: [PATCH] Ok, lets pull out the currency shit into it owns files where it belongs. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1287 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/util/CurrencyUtil.java | 82 +++++++++++++++++++ .../java/com/samskivert/util/StringUtil.java | 22 +---- .../com/samskivert/velocity/CurrencyTool.java | 68 +++++++++++++++ .../velocity/DispatcherServlet.java | 9 +- .../com/samskivert/velocity/StringTool.java | 19 +---- 5 files changed, 160 insertions(+), 40 deletions(-) create mode 100644 projects/samskivert/src/java/com/samskivert/util/CurrencyUtil.java create mode 100644 projects/samskivert/src/java/com/samskivert/velocity/CurrencyTool.java diff --git a/projects/samskivert/src/java/com/samskivert/util/CurrencyUtil.java b/projects/samskivert/src/java/com/samskivert/util/CurrencyUtil.java new file mode 100644 index 00000000..ad9f81b9 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/util/CurrencyUtil.java @@ -0,0 +1,82 @@ +// +// $Id: CurrencyUtil.java,v 1.1 2003/11/05 00:07:53 eric Exp $ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.util; + +import java.text.NumberFormat; +import java.util.Locale; + +/** + * Currency related utility functions. + */ +public class CurrencyUtil +{ + /** + * Converts a number representing pennies to a currency display string + * using the supplied local. + */ + public static String currencyPennies (double value, Locale locale) + { + return currency(value / 100.0, locale); + } + + /** + * Converts a number representing dollars to a currency display string + * using the supplied locale. + */ + public static String currency (double value, Locale locale) + { + NumberFormat numberFormatter = + NumberFormat.getCurrencyInstance(locale); + + return currency(value, numberFormatter); + } + + /** + * Converts a number representing pennies to a currency display string + * using the default local. + */ + public static String currencyPennies (double value) + { + return currency(value / 100.0, _defaultFormatter); + } + + /** + * Converts a number representing dollars to a currency display string + * using the default locale. + */ + public static String currency (double value) + { + return currency(value, _defaultFormatter); + } + + /** + * Converts a number representing dollars to a currency display + * string using the supplied number format. + */ + protected static String currency (double value, NumberFormat nformat) + { + return nformat.format(value); + } + + /** A number format for the default local. */ + protected static NumberFormat _defaultFormatter = + NumberFormat.getCurrencyInstance(); +} diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index 068bf46c..bd6b2a7f 100644 --- a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java @@ -1,5 +1,5 @@ // -// $Id: StringUtil.java,v 1.61 2003/10/29 19:42:40 ray Exp $ +// $Id: StringUtil.java,v 1.62 2003/11/05 00:07:53 eric Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -194,22 +194,6 @@ public class StringUtil return false; } - /** - * Converts a number representing dollars to a currency display string. - */ - public static String currency (double value) - { - return _curFormatter.format(value); - } - - /** - * Converts a number representing pennies to a currency display string. - */ - public static String currencyPennies (double value) - { - return currency(value / 100.0); - } - /** * Formats a floating point value with useful default rules; * ie. always display a digit to the left of the decimal and display @@ -1151,8 +1135,4 @@ public class StringUtil _ffmt.setMinimumFractionDigits(1); _ffmt.setMaximumFractionDigits(2); } - - /** Used to easily format numbers as currency. Bling Bling. */ - protected static NumberFormat _curFormatter = - NumberFormat.getCurrencyInstance(); } diff --git a/projects/samskivert/src/java/com/samskivert/velocity/CurrencyTool.java b/projects/samskivert/src/java/com/samskivert/velocity/CurrencyTool.java new file mode 100644 index 00000000..885dc178 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/velocity/CurrencyTool.java @@ -0,0 +1,68 @@ +// +// $Id: CurrencyTool.java,v 1.1 2003/11/05 00:07:53 eric Exp $ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.velocity; + +import javax.servlet.http.HttpServletRequest; + +import com.samskivert.util.CurrencyUtil; + +/** + * Provides handy currency functions for use in velocity. + */ +public class CurrencyTool +{ + /** + * Creates a new CurrencyTool which will used the supplied request to + * look up the locale with which to do currency formatting in. + */ + public CurrencyTool (HttpServletRequest req) + { + _req = req; + } + + /** + * Converts a number representing dollars to a currency display string. + */ + public String currency (double value) + { + return CurrencyUtil.currency(value, _req.getLocale()); + } + + /** + * Converts a number representing pennies to a currency display string. + */ + public String currencyPennies (double value) + { + return CurrencyUtil.currencyPennies(value, _req.getLocale()); + } + + /** + * Velocity currently doesn't support floats, so we have to provide + * our own support to convert pennies to a dollar amount. + */ + public String penniesToDollars (int pennies) + { + return "" + (pennies / 100.0); + } + + /** The servlet request we are providing currency functionality for. */ + protected HttpServletRequest _req; +} diff --git a/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java b/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java index ec2bcca8..18628b41 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/DispatcherServlet.java @@ -1,5 +1,5 @@ // -// $Id: DispatcherServlet.java,v 1.21 2003/09/25 18:06:29 eric Exp $ +// $Id: DispatcherServlet.java,v 1.22 2003/11/05 00:07:53 eric Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -292,6 +292,10 @@ public class DispatcherServlet extends VelocityServlet DataTool datatool = new DataTool(); ictx.put(DATATOOL_KEY, datatool); + // create a curreny tool set up to use the correct locale + CurrencyTool ctool = new CurrencyTool(req); + ictx.put(CURRENCYTOOL_KEY, ctool); + // resolve the appropriate logic class for this URI and // execute it if it exists String path = req.getServletPath(); @@ -451,6 +455,9 @@ public class DispatcherServlet extends VelocityServlet /** The key used to store the data tool in the context. */ protected static final String DATATOOL_KEY = "data"; + /** The key used to store the currency tool in the context. */ + protected static final String CURRENCYTOOL_KEY = "cash"; + /** The servlet parameter key specifying the application class. */ protected static final String APP_CLASS_KEY = "app_class"; diff --git a/projects/samskivert/src/java/com/samskivert/velocity/StringTool.java b/projects/samskivert/src/java/com/samskivert/velocity/StringTool.java index 178035c0..4b455f5a 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/StringTool.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/StringTool.java @@ -1,5 +1,5 @@ // -// $Id: StringTool.java,v 1.12 2003/10/23 16:09:34 eric Exp $ +// $Id: StringTool.java,v 1.13 2003/11/05 00:07:53 eric Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -53,23 +53,6 @@ public class StringTool { return String.valueOf(value); } - - /** - * Converts a number representing dollars to a currency display string. - */ - public static String currency (double value) - { - return StringUtil.currency(value); - } - - /** - * Converts a number representing pennies to a currency display string. - */ - public static String currencyPennies (double value) - { - return StringUtil.currencyPennies(value); - } - /** * Adds <p> tags between each pair of consecutive newlines. */