001 /*
002 * Copyright 2004 Jonathan M. Lehr
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
005 * You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software
010 * distributed under the License is distributed on an "AS IS" BASIS,
011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 * See the License for the specific language governing permissions and
013 * limitations under the License.
014 *
015 * MODIFIED BY THE KUALI FOUNDATION
016 */
017 // begin Kuali Foundation modification
018 package org.kuali.rice.kns.web.format;
019 // end Kuali Foundation modification
020
021 import java.util.Arrays;
022 import java.util.List;
023
024 import org.kuali.rice.kns.util.RiceKeyConstants;
025
026 /**
027 * begin Kuali Foundation modification
028 * This class is used to format boolean values.
029 * end Kuali Foundation modification
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032 public class BooleanFormatter extends Formatter {
033 // begin Kuali Foundation modification
034 // deleted line: public static final String BOOLEAN_ERROR_KEY = "error.boolean";
035 private static final long serialVersionUID = -4109390572922205211L;
036
037 // deleted line: static final String CONVERT_MSG = "Unable to create Boolean object from ";
038 // end Kuali Foundation modification
039
040 // begin Kuali Foundation modification
041 // "y" and "t" added to TRUE_VALUES, "n" and "f" added to FALSE_VALUES
042 static final List TRUE_VALUES = Arrays.asList(new String[] { "yes", "y", "true", "t", "on", "1", "enabled" });
043 static final List FALSE_VALUES = Arrays.asList(new String[] { "no", "n", "false", "f", "off", "0", "disabled" });
044 // end Kuali Foundation modification
045
046 /* begin Kuali Foundation modification
047 deleted following method */
048 // /**
049 // * Returns the error key for this Formatter.
050 // *
051 // * @see Formatter#getErrorKey()
052 // */
053 // public String getErrorKey() {
054 // return BOOLEAN_ERROR_KEY;
055 // }
056 // end Kuali Foundation modification
057
058 protected Object convertToObject(String target) {
059 if (Formatter.isEmptyValue(target))
060 return null;
061
062 String stringValue = target.getClass().isArray() ? unwrapString(target) : (String) target;
063 stringValue = stringValue.trim().toLowerCase();
064
065 if (TRUE_VALUES.contains(stringValue))
066 return Boolean.TRUE;
067 if (FALSE_VALUES.contains(stringValue))
068 return Boolean.FALSE;
069
070 // begin Kuali Foundation modification
071 // was: throw new FormatException(CONVERT_MSG + stringValue);
072 throw new FormatException("converting", RiceKeyConstants.ERROR_BOOLEAN, stringValue);
073 // end Kuali Foundation modification
074 }
075
076 public Object format(Object target) {
077 if (target == null)
078 return null;
079 // begin Kuali Foundation modification
080 if (target instanceof String) {
081 return target;
082 }
083 // end Kuali Foundation modification
084
085 boolean isTrue = ((Boolean) target).booleanValue();
086
087 return isTrue ? "Yes" : "No";
088 }
089 }