01: /*
02: * Copyright 2006 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.kuali.core.web.format;
17:
18: import java.util.Arrays;
19: import java.util.List;
20:
21: import org.kuali.RiceKeyConstants;
22:
23: /**
24: * This class is a formatter for little-b boolean classes, that cannot accept a null.
25: */
26: public class LittleBooleanFormatter extends Formatter {
27:
28: static final List TRUE_VALUES = Arrays.asList(new String[] { "yes",
29: "y", "true", "t", "on", "1", "enabled" });
30: static final List FALSE_VALUES = Arrays.asList(new String[] { "no",
31: "n", "false", "f", "off", "0", "disabled" });
32:
33: protected Object convertToObject(String target) {
34: if (Formatter.isEmptyValue(target))
35: return Boolean.FALSE;
36:
37: String stringValue = target.getClass().isArray() ? unwrapString(target)
38: : (String) target;
39: stringValue = stringValue.trim().toLowerCase();
40:
41: if (TRUE_VALUES.contains(stringValue))
42: return Boolean.TRUE;
43: if (FALSE_VALUES.contains(stringValue))
44: return Boolean.FALSE;
45:
46: throw new FormatException("converting",
47: RiceKeyConstants.ERROR_BOOLEAN, stringValue);
48: }
49:
50: public Object format(Object target) {
51: if (target == null)
52: return "No";
53: if (target instanceof String) {
54: return target;
55: }
56:
57: boolean isTrue = ((Boolean) target).booleanValue();
58:
59: return isTrue ? "Yes" : "No";
60: }
61:
62: protected Object getNullObjectValue() {
63: return Boolean.FALSE;
64: }
65:
66: }
|