001    /*
002     *  Copyright 2003-2004 The Apache Software Foundation
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package org.apache.commons.convert1.string;
017    
018    
019    import org.apache.commons.convert1.ConversionException;
020    import org.apache.commons.convert1.Converter;
021    
022    
023    /**
024     * <p>Standard {@link Converter} implementation that converts an incoming
025     * String into a <code>java.lang.Boolean</code> object, optionally using a
026     * default value or throwing a {@link ConversionException} if a conversion
027     * error occurs.</p>
028     *
029     * @author Craig R. McClanahan
030     * @version $Id: BooleanConverter.java 155441 2005-02-26 13:19:22Z dirkv $
031     * @since 0.1
032     */
033    
034    public final class BooleanConverter implements Converter {
035    
036    
037        // ----------------------------------------------------------- Constructors
038    
039    
040        /**
041         * Create a {@link Converter} that will throw a {@link ConversionException}
042         * if a conversion error occurs.
043         */
044        public BooleanConverter() {
045    
046            this.defaultValue = null;
047            this.useDefault = false;
048    
049        }
050    
051    
052        /**
053         * Create a {@link Converter} that will return the specified default value
054         * if a conversion error occurs.
055         *
056         * @param defaultValue The default value to be returned
057         */
058        public BooleanConverter(Object defaultValue) {
059    
060            this.defaultValue = defaultValue;
061            this.useDefault = true;
062    
063        }
064    
065    
066        // ----------------------------------------------------- Instance Variables
067    
068    
069        /**
070         * The default value specified to our Constructor, if any.
071         */
072        private Object defaultValue = null;
073    
074    
075        /**
076         * Should we return the default value on conversion errors?
077         */
078        private boolean useDefault = true;
079    
080    
081        // --------------------------------------------------------- Public Methods
082    
083    
084        /**
085         * Convert the specified input object into an output object of the
086         * specified type.
087         *
088         * @param type Data type to which this value should be converted
089         * @param value The input value to be converted
090         *
091         * @exception ConversionException if conversion cannot be performed
092         *  successfully
093         */
094        public Object convert(Class type, Object value) {
095    
096            if (value == null) {
097                if (useDefault) {
098                    return (defaultValue);
099                } else {
100                    throw new ConversionException("No value specified");
101                }
102            }
103    
104            if (value instanceof Boolean) {
105                return (value);
106            }
107    
108            try {
109                String stringValue = value.toString();
110                if (stringValue.equalsIgnoreCase("yes") ||
111                    stringValue.equalsIgnoreCase("y") ||
112                    stringValue.equalsIgnoreCase("true") ||
113                    stringValue.equalsIgnoreCase("on") ||
114                    stringValue.equalsIgnoreCase("1")) {
115                    return (Boolean.TRUE);
116                } else if (stringValue.equalsIgnoreCase("no") ||
117                           stringValue.equalsIgnoreCase("n") ||
118                           stringValue.equalsIgnoreCase("false") ||
119                           stringValue.equalsIgnoreCase("off") ||
120                           stringValue.equalsIgnoreCase("0")) {
121                    return (Boolean.FALSE);
122                } else if (useDefault) {
123                    return (defaultValue);
124                } else {
125                    throw new ConversionException(stringValue);
126                }
127            } catch (ClassCastException e) {
128                if (useDefault) {
129                    return (defaultValue);
130                } else {
131                    throw new ConversionException(e);
132                }
133            }
134    
135        }
136    
137    
138    }