001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.coerce;
016
017 import org.apache.hivemind.HiveMind;
018
019 /**
020 * Converts a String to a Boolean. Strings that do contain non-whitespace are considered true. As a
021 * special case, the string "false" evaluates to false (this allow boolean component parameters to
022 * be bound to "false" instead of "ognl:false" and still operate as expected).
023 *
024 * @author Howard M. Lewis Ship
025 * @since 4.0
026 */
027 public final class StringToBooleanConverter implements TypeConverter
028 {
029
030 public Object convertValue(Object value)
031 {
032 String s = (String) value;
033
034 boolean b = HiveMind.isNonBlank(s) && !s.equals("false");
035
036 // JDK 1.4 has a Boolean.valueOf(), but we're trying for 1.3 compatibility
037
038 return b ? Boolean.TRUE : Boolean.FALSE;
039 }
040
041 }