1 /*
2 * The SmartWeb Framework
3 * Copyright (C) 2004-2006
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * For further informations on the SmartWeb Framework please visit
20 *
21 * http://smartweb.sourceforge.net
22 */
23 package net.smartlab.web.bean;
24
25 import java.util.Locale;
26
27 /**
28 * TODO documentation
29 *
30 * @author rlogiacco
31 */
32 public class BooleanConverter extends Converter {
33
34 private boolean value;
35
36
37 /**
38 * TODO documentation
39 *
40 * @param value
41 */
42 public BooleanConverter(boolean value) {
43 this.value = value;
44 }
45
46 /**
47 * @see net.smartlab.web.bean.Converter#convert(java.lang.Class,
48 * java.lang.Object, java.util.Locale)
49 */
50 public Object convert(Class type, Object value, Locale locale) throws ConversionException {
51 if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
52 if (value == null || value.toString().trim().length() == 0) {
53 value = "off";
54 }
55 if (value.toString().equalsIgnoreCase("on") || value.toString().equalsIgnoreCase("y")
56 || value.toString().equalsIgnoreCase("yes") || value.toString().equalsIgnoreCase("1")
57 || value.toString().equalsIgnoreCase("true")) {
58 return Boolean.TRUE;
59 } else if (value.toString().equalsIgnoreCase("off") || value.toString().equalsIgnoreCase("n")
60 || value.toString().equalsIgnoreCase("no") || value.toString().equalsIgnoreCase("0")
61 || value.toString().equalsIgnoreCase("false")) {
62 return Boolean.FALSE;
63 } else {
64 throw new ConversionException("Unable to convert to boolean `" + value + "`");
65 }
66 } else {
67 return value.toString();
68 }
69 }
70
71 /**
72 * TODO documentation
73 *
74 * @return
75 */
76 protected boolean hasDefault() {
77 return true;
78 }
79
80 /**
81 * TODO documentation
82 *
83 * @return
84 */
85 protected boolean getDefault() {
86 return value;
87 }
88 }