001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 019 package org.apache.commons.beanutils.converters; 020 021 022 import java.util.List; 023 import org.apache.commons.beanutils.ConversionException; 024 025 026 /** 027 * <p>Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming 028 * String into a primitive array of boolean. On a conversion failure, returns 029 * a specified default value or throws a {@link ConversionException} depending 030 * on how this instance is constructed.</p> 031 * 032 * <p>By default, the values to be converted are expected to be those 033 * recognised by a default instance of BooleanConverter. A customised 034 * BooleanConverter can be provided in order to recognise alternative values 035 * as true/false. </p> 036 * 037 * @author Craig R. McClanahan 038 * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $ 039 * @since 1.4 040 * @deprecated Replaced by the new {@link ArrayConverter} implementation 041 */ 042 043 public final class BooleanArrayConverter extends AbstractArrayConverter { 044 045 046 // ----------------------------------------------------------- Constructors 047 048 049 /** 050 * Create a {@link org.apache.commons.beanutils.Converter} that will throw 051 * a {@link ConversionException} if a conversion error occurs. 052 * 053 * <p>Conversion of strings to boolean values will be done via a default 054 * instance of class BooleanConverter.</p> 055 */ 056 public BooleanArrayConverter() { 057 058 super(); 059 this.booleanConverter = DEFAULT_CONVERTER; 060 061 } 062 063 064 /** 065 * Create a {@link org.apache.commons.beanutils.Converter} that will return 066 * the specified default value if a conversion error occurs. 067 * 068 * <p>Conversion of strings to boolean values will be done via a default 069 * instance of class BooleanConverter.</p> 070 * 071 * @param defaultValue The default value to be returned 072 */ 073 public BooleanArrayConverter(Object defaultValue) { 074 075 super(defaultValue); 076 this.booleanConverter = DEFAULT_CONVERTER; 077 078 } 079 080 081 /** 082 * Create a {@link org.apache.commons.beanutils.Converter} that will return 083 * the specified default value if a conversion error occurs. 084 * 085 * <p>Conversion of strings to boolean values will be done via the 086 * specified converter.</p> 087 * 088 * @param converter is the converter object that will be used to 089 * convert each input string-value into a boolean. 090 * 091 * @param defaultValue is the default value to be returned by method 092 * convert if conversion fails; null is a valid default value. See the 093 * documentation for method "convert" for more information. 094 * The value BooleanArrayConverter.NO_DEFAULT may be passed here to 095 * specify that an exception should be thrown on conversion failure. 096 * 097 */ 098 public BooleanArrayConverter(BooleanConverter converter, Object defaultValue) { 099 100 super(defaultValue); 101 this.booleanConverter = converter; 102 103 } 104 105 // ------------------------------------------------------- Static Variables 106 107 /** 108 * Type which this class converts its input to. This value can be 109 * used as a parameter to the ConvertUtils.register method. 110 */ 111 public static final Class MODEL = new boolean[0].getClass(); 112 113 /** 114 * The converter that all instances of this class will use to 115 * do individual string->boolean conversions, unless overridden 116 * in the constructor. 117 */ 118 private static final BooleanConverter DEFAULT_CONVERTER 119 = new BooleanConverter(); 120 121 // ---------------------------------------------------- Instance Variables 122 123 /** 124 * This object is used to perform the conversion of individual strings 125 * into Boolean/boolean values. 126 */ 127 protected final BooleanConverter booleanConverter; 128 129 // --------------------------------------------------------- Public Methods 130 131 132 /** 133 * Convert the specified input object into an output object of type 134 * array-of-boolean. 135 * 136 * <p>If the input value is null, then the default value specified in the 137 * constructor is returned. If no such value was provided, then a 138 * ConversionException is thrown instead.</p> 139 * 140 * <p>If the input value is of type String[] then the returned array shall 141 * be of the same size as this array, with a true or false value in each 142 * array element depending on the result of applying method 143 * BooleanConverter.convert to each string.</p> 144 * 145 * <p>For all other types of value, the object's toString method is 146 * expected to return a string containing a comma-separated list of 147 * values, eg "true, false, true". See the documentation for 148 * {@link AbstractArrayConverter#parseElements} for more information on 149 * the exact formats supported.</p> 150 * 151 * <p>If the result of value.toString() cannot be split into separate 152 * words, then the default value is also returned (or an exception thrown). 153 * </p> 154 * 155 * <p>If any of the elements in the value array (or the elements resulting 156 * from splitting up value.toString) are not recognised by the 157 * BooleanConverter associated with this object, then what happens depends 158 * on whether that BooleanConverter has a default value or not: if it does, 159 * then that unrecognised element is converted into the BooleanConverter's 160 * default value. If the BooleanConverter does <i>not</i> have a default 161 * value, then the default value for this object is returned as the 162 * <i>complete</i> conversion result (not just for the element), or an 163 * exception is thrown if this object has no default value defined.</p> 164 * 165 * @param type is the type to which this value should be converted. In the 166 * case of this BooleanArrayConverter class, this value is ignored. 167 * 168 * @param value is the input value to be converted. 169 * 170 * @return an object of type boolean[], or the default value if there was 171 * any sort of error during conversion and the constructor 172 * was provided with a default value. 173 * 174 * @exception ConversionException if conversion cannot be performed 175 * successfully and the constructor was not provided with a default 176 * value to return on conversion failure. 177 * 178 * @exception NullPointerException if value is an array, and any of the 179 * array elements are null. 180 */ 181 public Object convert(Class type, Object value) { 182 183 // Deal with a null value 184 if (value == null) { 185 if (useDefault) { 186 return (defaultValue); 187 } else { 188 throw new ConversionException("No value specified"); 189 } 190 } 191 192 // Deal with the no-conversion-needed case 193 if (MODEL == value.getClass()) { 194 return (value); 195 } 196 197 // Deal with input value as a String array 198 // 199 // TODO: use if (value.getClass().isArray() instead... 200 // this requires casting to Object[], then using values[i].toString() 201 if (strings.getClass() == value.getClass()) { 202 try { 203 String[] values = (String[]) value; 204 boolean[] results = new boolean[values.length]; 205 for (int i = 0; i < values.length; i++) { 206 String stringValue = values[i]; 207 Object result = booleanConverter.convert(Boolean.class, stringValue); 208 results[i] = ((Boolean) result).booleanValue(); 209 } 210 return (results); 211 } catch (Exception e) { 212 if (useDefault) { 213 return (defaultValue); 214 } else { 215 throw new ConversionException(value.toString(), e); 216 } 217 } 218 } 219 220 // We only get here if the input value is not of type String[]. 221 // In this case, we assume value.toString() returns a comma-separated 222 // sequence of values; see method AbstractArrayConverter.parseElements 223 // for more information. 224 try { 225 List list = parseElements(value.toString()); 226 boolean[] results = new boolean[list.size()]; 227 for (int i = 0; i < results.length; i++) { 228 String stringValue = (String) list.get(i); 229 Object result = booleanConverter.convert(Boolean.class, stringValue); 230 results[i] = ((Boolean) result).booleanValue(); 231 } 232 return (results); 233 } catch (Exception e) { 234 if (useDefault) { 235 return (defaultValue); 236 } else { 237 throw new ConversionException(value.toString(), e); 238 } 239 } 240 241 } 242 243 244 }