001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.binding.convert.support;
017:
018: import org.springframework.binding.convert.ConversionContext;
019: import org.springframework.util.StringUtils;
020:
021: /**
022: * Converts a textual representation of a boolean object to a <code>Boolean</code>
023: * instance.
024: *
025: * @author Keith Donald
026: */
027: public class TextToBoolean extends AbstractConverter {
028:
029: private static final String VALUE_TRUE = "true";
030:
031: private static final String VALUE_FALSE = "false";
032:
033: private static final String VALUE_ON = "on";
034:
035: private static final String VALUE_OFF = "off";
036:
037: private static final String VALUE_YES = "yes";
038:
039: private static final String VALUE_NO = "no";
040:
041: private static final String VALUE_1 = "1";
042:
043: private static final String VALUE_0 = "0";
044:
045: private String trueString;
046:
047: private String falseString;
048:
049: /**
050: * Default constructor. No special true or false strings are considered.
051: */
052: public TextToBoolean() {
053: this (null, null);
054: }
055:
056: /**
057: * Create a text to boolean converter. Take given <i>special</i> string representations
058: * of true and false into account.
059: * @param trueString special true string to consider
060: * @param falseString special false string to consider
061: */
062: public TextToBoolean(String trueString, String falseString) {
063: this .trueString = trueString;
064: this .falseString = falseString;
065: }
066:
067: public Class[] getSourceClasses() {
068: return new Class[] { String.class };
069: }
070:
071: public Class[] getTargetClasses() {
072: return new Class[] { Boolean.class };
073: }
074:
075: protected Object doConvert(Object source, Class targetClass,
076: ConversionContext context) throws Exception {
077: String text = (String) source;
078: if (!StringUtils.hasText(text)) {
079: return null;
080: } else if (this .trueString != null
081: && text.equalsIgnoreCase(this .trueString)) {
082: return Boolean.TRUE;
083: } else if (this .falseString != null
084: && text.equalsIgnoreCase(this .falseString)) {
085: return Boolean.FALSE;
086: } else if (this .trueString == null
087: && (text.equalsIgnoreCase(VALUE_TRUE)
088: || text.equalsIgnoreCase(VALUE_ON)
089: || text.equalsIgnoreCase(VALUE_YES) || text
090: .equals(VALUE_1))) {
091: return Boolean.TRUE;
092: } else if (this .falseString == null
093: && (text.equalsIgnoreCase(VALUE_FALSE)
094: || text.equalsIgnoreCase(VALUE_OFF)
095: || text.equalsIgnoreCase(VALUE_NO) || text
096: .equals(VALUE_0))) {
097: return Boolean.FALSE;
098: } else {
099: throw new IllegalArgumentException(
100: "Invalid boolean value [" + text + "]");
101: }
102: }
103: }
|