1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.validator.routines;
18
19 import java.text.DecimalFormat;
20 import java.text.Format;
21
22 /***
23 * <p><b>Currency Validation</b> and Conversion routines (<code>java.math.BigDecimal</code>).</p>
24 *
25 * <p>This is one implementation of a currency validator that has the following features:</p>
26 * <ul>
27 * <li>It is <i>lenient</i> about the the presence of the <i>currency symbol</i></li>
28 * <li>It converts the currency to a <code>java.math.BigDecimal</code></li>
29 * </ul>
30 *
31 * <p>However any of the <i>number</i> validators can be used for <i>currency</i> validation.
32 * For example, if you wanted a <i>currency</i> validator that converts to a
33 * <code>java.lang.Integer</code> then you can simply instantiate an
34 * <code>IntegerValidator</code> with the appropriate <i>format type</i>:</p>
35 *
36 * <p><code>... = new IntegerValidator(false, IntegerValidator.CURRENCY_FORMAT);</code></p>
37 *
38 * <p>Pick the appropriate validator, depending on the type (e.g Float, Double, Integer, Long etc)
39 * you want the currency converted to. One thing to note - only the CurrencyValidator
40 * implements <i>lenient</i> behaviour regarding the currency symbol.</p>
41 *
42 * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
43 * @since Validator 1.3.0
44 */
45 public class CurrencyValidator extends BigDecimalValidator {
46
47 private static final CurrencyValidator VALIDATOR = new CurrencyValidator();
48
49 /*** DecimalFormat's currency symbol */
50 private static final char CURRENCY_SYMBOL = '\u00A4';
51
52 /***
53 * Return a singleton instance of this validator.
54 * @return A singleton instance of the CurrencyValidator.
55 */
56 public static BigDecimalValidator getInstance() {
57 return VALIDATOR;
58 }
59
60 /***
61 * Construct a <i>strict</i> instance.
62 */
63 public CurrencyValidator() {
64 this(true, true);
65 }
66
67 /***
68 * Construct an instance with the specified strict setting.
69 *
70 * @param strict <code>true</code> if strict
71 * <code>Format</code> parsing should be used.
72 * @param allowFractions <code>true</code> if fractions are
73 * allowed or <code>false</code> if integers only.
74 */
75 public CurrencyValidator(boolean strict, boolean allowFractions) {
76 super(strict, CURRENCY_FORMAT, allowFractions);
77 }
78
79 /***
80 * <p>Parse the value with the specified <code>Format</code>.</p>
81 *
82 * <p>This implementation is lenient whether the currency symbol
83 * is present or not. The default <code>NumberFormat</code>
84 * behaviour is for the parsing to "fail" if the currency
85 * symbol is missing. This method re-parses with a format
86 * without the currency symbol if it fails initially.</p>
87 *
88 * @param value The value to be parsed.
89 * @param formatter The Format to parse the value with.
90 * @return The parsed value if valid or <code>null</code> if invalid.
91 */
92 protected Object parse(String value, Format formatter) {
93
94
95 Object parsedValue = super.parse(value, formatter);
96 if (parsedValue != null || !(formatter instanceof DecimalFormat)) {
97 return parsedValue;
98 }
99
100
101 DecimalFormat decimalFormat = (DecimalFormat)formatter;
102 String pattern = decimalFormat.toPattern();
103 if (pattern.indexOf(CURRENCY_SYMBOL) >= 0) {
104 StringBuffer buffer = new StringBuffer(pattern.length());
105 for (int i = 0; i < pattern.length(); i++) {
106 if (pattern.charAt(i) != CURRENCY_SYMBOL) {
107 buffer.append(pattern.charAt(i));
108 }
109 }
110 decimalFormat.applyPattern(buffer.toString());
111 parsedValue = super.parse(value, decimalFormat);
112 }
113 return parsedValue;
114 }
115 }