1 /* 2 * Copyright 2004 Jonathan M. Lehr 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * 15 * MODIFIED BY THE KUALI FOUNDATION 16 */ 17 // begin Kuali Foundation modification 18 package org.kuali.rice.kns.web.format; 19 // end Kuali Foundation modification 20 21 import java.util.Arrays; 22 import java.util.List; 23 24 import org.kuali.rice.kns.util.RiceKeyConstants; 25 26 /** 27 * begin Kuali Foundation modification 28 * This class is used to format boolean values. 29 * end Kuali Foundation modification 30 * @author Kuali Rice Team (rice.collab@kuali.org) 31 */ 32 public class BooleanFormatter extends Formatter { 33 // begin Kuali Foundation modification 34 // deleted line: public static final String BOOLEAN_ERROR_KEY = "error.boolean"; 35 private static final long serialVersionUID = -4109390572922205211L; 36 37 // deleted line: static final String CONVERT_MSG = "Unable to create Boolean object from "; 38 // end Kuali Foundation modification 39 40 // begin Kuali Foundation modification 41 // "y" and "t" added to TRUE_VALUES, "n" and "f" added to FALSE_VALUES 42 static final List TRUE_VALUES = Arrays.asList(new String[] { "yes", "y", "true", "t", "on", "1", "enabled" }); 43 static final List FALSE_VALUES = Arrays.asList(new String[] { "no", "n", "false", "f", "off", "0", "disabled" }); 44 // end Kuali Foundation modification 45 46 /* begin Kuali Foundation modification 47 deleted following method */ 48 // /** 49 // * Returns the error key for this Formatter. 50 // * 51 // * @see Formatter#getErrorKey() 52 // */ 53 // public String getErrorKey() { 54 // return BOOLEAN_ERROR_KEY; 55 // } 56 // end Kuali Foundation modification 57 58 protected Object convertToObject(String target) { 59 if (Formatter.isEmptyValue(target)) 60 return null; 61 62 String stringValue = target.getClass().isArray() ? unwrapString(target) : (String) target; 63 stringValue = stringValue.trim().toLowerCase(); 64 65 if (TRUE_VALUES.contains(stringValue)) 66 return Boolean.TRUE; 67 if (FALSE_VALUES.contains(stringValue)) 68 return Boolean.FALSE; 69 70 // begin Kuali Foundation modification 71 // was: throw new FormatException(CONVERT_MSG + stringValue); 72 throw new FormatException("converting", RiceKeyConstants.ERROR_BOOLEAN, stringValue); 73 // end Kuali Foundation modification 74 } 75 76 public Object format(Object target) { 77 if (target == null) 78 return null; 79 // begin Kuali Foundation modification 80 if (target instanceof String) { 81 return target; 82 } 83 // end Kuali Foundation modification 84 85 boolean isTrue = ((Boolean) target).booleanValue(); 86 87 return isTrue ? "Yes" : "No"; 88 } 89 }