KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > convert > support > TextToBoolean


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.springframework.binding.convert.support;
17
18 import org.springframework.binding.convert.ConversionContext;
19 import org.springframework.util.StringUtils;
20
21 /**
22  * Converts a textual representation of a class object to a <code>Class</code>
23  * instance.
24  * @author Keith Donald
25  */

26 public class TextToBoolean extends AbstractConverter {
27
28     private static final String JavaDoc VALUE_TRUE = "true";
29
30     private static final String JavaDoc VALUE_FALSE = "false";
31
32     private static final String JavaDoc VALUE_ON = "on";
33
34     private static final String JavaDoc VALUE_OFF = "off";
35
36     private static final String JavaDoc VALUE_YES = "yes";
37
38     private static final String JavaDoc VALUE_NO = "no";
39
40     private static final String JavaDoc VALUE_1 = "1";
41
42     private static final String JavaDoc VALUE_0 = "0";
43
44     private String JavaDoc trueString;
45
46     private String JavaDoc falseString;
47
48     public TextToBoolean() {
49
50     }
51
52     public TextToBoolean(String JavaDoc trueString, String JavaDoc falseString) {
53         this.trueString = trueString;
54         this.falseString = falseString;
55     }
56
57     public Class JavaDoc[] getSourceClasses() {
58         return new Class JavaDoc[] { String JavaDoc.class };
59     }
60
61     public Class JavaDoc[] getTargetClasses() {
62         return new Class JavaDoc[] { Boolean JavaDoc.class };
63     }
64
65     protected Object JavaDoc doConvert(Object JavaDoc source, Class JavaDoc targetClass, ConversionContext context) throws Exception JavaDoc {
66         String JavaDoc text = (String JavaDoc)source;
67         if (!StringUtils.hasText(text)) {
68             return null;
69         }
70         else if (this.trueString != null && text.equalsIgnoreCase(this.trueString)) {
71             return Boolean.TRUE;
72         }
73         else if (this.falseString != null && text.equalsIgnoreCase(this.falseString)) {
74             return Boolean.FALSE;
75         }
76         else if (this.trueString == null
77                 && (text.equalsIgnoreCase(VALUE_TRUE) || text.equalsIgnoreCase(VALUE_ON)
78                         || text.equalsIgnoreCase(VALUE_YES) || text.equals(VALUE_1))) {
79             return Boolean.TRUE;
80         }
81         else if (this.falseString == null
82                 && (text.equalsIgnoreCase(VALUE_FALSE) || text.equalsIgnoreCase(VALUE_OFF)
83                         || text.equalsIgnoreCase(VALUE_NO) || text.equals(VALUE_0))) {
84             return Boolean.FALSE;
85         }
86         else {
87             throw new IllegalArgumentException JavaDoc("Invalid boolean value [" + text + "]");
88         }
89     }
90 }
Popular Tags