001    package org.apache.fulcrum.intake.validator;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.text.ParseException;
023    
024    import java.util.Map;
025    
026    import org.apache.commons.lang.StringUtils;
027    
028    /**
029     * Validator for boolean field types.<br><br>
030     *
031     * Values are validated by attemting to match the value to
032     * a list of strings for true and false values.  The string
033     * values are compared without reguard to case.<br>
034     *
035     * Valid values for Boolean.TRUE:
036     * <ul>
037     * <li>TRUE</li>
038     * <li>T</li>
039     * <li>YES</li>
040     * <li>Y</li>
041     * <li>1</li>
042     * <li>ON</li>
043     * </ul>
044     * Valid values for Boolean.FALSE:
045     * <ul>
046     * <li>FALSE</li>
047     * <li>F</li>
048     * <li>NO</li>
049     * <li>N</li>
050     * <li>0</li>
051     * <li>OFF</li>
052     * </ul>
053     *
054     * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
055     * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
056     * @author <a href="mailto:jh@byteaction.de">J&uuml;rgen Hoffmann</a>
057     * @version $Id: BooleanValidator.java 671324 2008-06-24 20:01:41Z tv $
058     */
059    public class BooleanValidator
060            extends DefaultValidator
061    {
062        /** String values which would evaluate to Boolean.TRUE */
063        private static String[] trueValues = {"TRUE","T","YES","Y","1","ON"};
064    
065        /** String values which would evaluate to Boolean.FALSE */
066        private static String[] falseValues = {"FALSE","F","NO","N","0","OFF"};
067    
068        /**
069         * Default Constructor
070         */
071        public BooleanValidator()
072        {
073        }
074    
075        /**
076         * Constructor to use when initialising Object
077         *
078         * @param paramMap
079         * @throws InvalidMaskException
080         */
081        public BooleanValidator(Map paramMap)
082                throws InvalidMaskException
083        {
084            super(paramMap);
085        }
086    
087        /**
088         * Determine whether a testValue meets the criteria specified
089         * in the constraints defined for this validator
090         *
091         * @param testValue a <code>String</code> to be tested
092         * @exception ValidationException containing an error message if the
093         * testValue did not pass the validation tests.
094         */
095        public void assertValidity(String testValue)
096                throws ValidationException
097        {
098            super.assertValidity(testValue);
099    
100            if (required || StringUtils.isNotEmpty(testValue))
101            {
102                try
103                {
104                    parse(testValue);
105                }
106                catch (ParseException e)
107                {
108                    throw new ValidationException(e.getMessage());
109                }
110            }
111        }
112    
113        /**
114         * Parses a srting value into a Boolean object.
115         *
116         * @param stringValue the value to parse
117         * @return a <code>Boolean</a> object
118         */
119        public Boolean parse(String stringValue)
120                throws ParseException
121        {
122            Boolean result = null;
123    
124            for (int cnt = 0;
125                 cnt < Math.max(trueValues.length, falseValues.length); cnt++)
126            {
127                // Short-cut evaluation or bust!
128                if ((cnt < trueValues.length) &&
129                        stringValue.equalsIgnoreCase(trueValues[cnt]))
130                {
131                    result = Boolean.TRUE;
132                    break;
133                }
134    
135                if ((cnt < falseValues.length) &&
136                        stringValue.equalsIgnoreCase(falseValues[cnt]))
137                {
138                    result = Boolean.FALSE;
139                    break;
140                }
141            }
142    
143            if (result == null)
144            {
145                throw new ParseException(stringValue +
146                        " could not be converted to a Boolean", 0);
147            }
148            return result;
149        }
150    }