001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.validator.routines;
018    
019    import java.text.DecimalFormat;
020    import java.text.Format;
021    
022    /**
023     * <p><b>Currency Validation</b> and Conversion routines (<code>java.math.BigDecimal</code>).</p>
024     * 
025     * <p>This is one implementation of a currency validator that has the following features:</p>
026     *    <ul>
027     *       <li>It is <i>lenient</i> about the the presence of the <i>currency symbol</i></li>
028     *       <li>It converts the currency to a <code>java.math.BigDecimal</code></li>
029     *    </ul>
030     * 
031     * <p>However any of the <i>number</i> validators can be used for <i>currency</i> validation.
032     *    For example, if you wanted a <i>currency</i> validator that converts to a
033     *    <code>java.lang.Integer</code> then you can simply instantiate an
034     *    <code>IntegerValidator</code> with the appropriate <i>format type</i>:</p>
035     *    
036     *    <p><code>... = new IntegerValidator(false, IntegerValidator.CURRENCY_FORMAT);</code></p>
037     *
038     * <p>Pick the appropriate validator, depending on the type (e.g Float, Double, Integer, Long etc)
039     *    you want the currency converted to. One thing to note - only the CurrencyValidator
040     *    implements <i>lenient</i> behaviour regarding the currency symbol.</p>
041     * 
042     * @version $Revision: 493905 $ $Date: 2007-01-08 03:11:38 +0100 (Mo, 08. Jan 2007) $
043     * @since Validator 1.3.0
044     */
045    public class CurrencyValidator extends BigDecimalValidator {
046    
047        private static final CurrencyValidator VALIDATOR = new CurrencyValidator();
048    
049        /** DecimalFormat's currency symbol */
050        private static final char CURRENCY_SYMBOL = '\u00A4';
051    
052        /**
053         * Return a singleton instance of this validator.
054         * @return A singleton instance of the CurrencyValidator.
055         */
056        public static BigDecimalValidator getInstance() {
057            return VALIDATOR;
058        }
059    
060        /**
061         * Construct a <i>strict</i> instance.
062         */
063        public CurrencyValidator() {
064            this(true, true);
065        }
066    
067        /**
068         * Construct an instance with the specified strict setting.
069         * 
070         * @param strict <code>true</code> if strict 
071         *        <code>Format</code> parsing should be used.
072         * @param allowFractions <code>true</code> if fractions are
073         *        allowed or <code>false</code> if integers only.
074         */
075        public CurrencyValidator(boolean strict, boolean allowFractions) {
076            super(strict, CURRENCY_FORMAT, allowFractions);
077        }
078    
079        /**
080         * <p>Parse the value with the specified <code>Format</code>.</p>
081         * 
082         * <p>This implementation is lenient whether the currency symbol
083         *    is present or not. The default <code>NumberFormat</code>
084         *    behaviour is for the parsing to "fail" if the currency
085         *    symbol is missing. This method re-parses with a format
086         *    without the currency symbol if it fails initially.</p>
087         * 
088         * @param value The value to be parsed.
089         * @param formatter The Format to parse the value with.
090         * @return The parsed value if valid or <code>null</code> if invalid.
091         */
092        protected Object parse(String value, Format formatter) {
093    
094            // Initial parse of the value
095            Object parsedValue = super.parse(value, formatter);
096            if (parsedValue != null || !(formatter instanceof DecimalFormat)) {
097                return parsedValue;
098            }
099    
100            // Re-parse using a pattern without the currency symbol
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    }