1 16 17 18 package org.apache.commons.beanutils.converters; 19 20 21 import java.util.List ; 22 import org.apache.commons.beanutils.ConversionException; 23 import org.apache.commons.beanutils.Converter; 24 25 26 36 37 public final class BooleanArrayConverter extends AbstractArrayConverter { 38 39 40 42 43 47 public BooleanArrayConverter() { 48 49 this.defaultValue = null; 50 this.useDefault = false; 51 52 } 53 54 55 61 public BooleanArrayConverter(Object defaultValue) { 62 63 this.defaultValue = defaultValue; 64 this.useDefault = true; 65 66 } 67 68 69 71 72 75 private static boolean model[] = new boolean[0]; 76 77 78 80 81 91 public Object convert(Class type, Object value) { 92 93 if (value == null) { 95 if (useDefault) { 96 return (defaultValue); 97 } else { 98 throw new ConversionException("No value specified"); 99 } 100 } 101 102 if (model.getClass() == value.getClass()) { 104 return (value); 105 } 106 107 if (strings.getClass() == value.getClass()) { 109 try { 110 String values[] = (String []) value; 111 boolean results[] = new boolean[values.length]; 112 for (int i = 0; i < values.length; i++) { 113 String stringValue = values[i]; 114 if (stringValue.equalsIgnoreCase("yes") || 115 stringValue.equalsIgnoreCase("y") || 116 stringValue.equalsIgnoreCase("true") || 117 stringValue.equalsIgnoreCase("on") || 118 stringValue.equalsIgnoreCase("1")) { 119 results[i] = true; 120 } else if (stringValue.equalsIgnoreCase("no") || 121 stringValue.equalsIgnoreCase("n") || 122 stringValue.equalsIgnoreCase("false") || 123 stringValue.equalsIgnoreCase("off") || 124 stringValue.equalsIgnoreCase("0")) { 125 results[i] = false; 126 } else { 127 if (useDefault) { 128 return (defaultValue); 129 } else { 130 throw new ConversionException(value.toString()); 131 } 132 } 133 } 134 return (results); 135 } catch (Exception e) { 136 if (useDefault) { 137 return (defaultValue); 138 } else { 139 throw new ConversionException(value.toString(), e); 140 } 141 } 142 } 143 144 try { 147 List list = parseElements(value.toString()); 148 boolean results[] = new boolean[list.size()]; 149 for (int i = 0; i < results.length; i++) { 150 String stringValue = (String ) list.get(i); 151 if (stringValue.equalsIgnoreCase("yes") || 152 stringValue.equalsIgnoreCase("y") || 153 stringValue.equalsIgnoreCase("true") || 154 stringValue.equalsIgnoreCase("on") || 155 stringValue.equalsIgnoreCase("1")) { 156 results[i] = true; 157 } else if (stringValue.equalsIgnoreCase("no") || 158 stringValue.equalsIgnoreCase("n") || 159 stringValue.equalsIgnoreCase("false") || 160 stringValue.equalsIgnoreCase("off") || 161 stringValue.equalsIgnoreCase("0")) { 162 results[i] = false; 163 } else { 164 if (useDefault) { 165 return (defaultValue); 166 } else { 167 throw new ConversionException(value.toString()); 168 } 169 } 170 } 171 return (results); 172 } catch (Exception e) { 173 if (useDefault) { 174 return (defaultValue); 175 } else { 176 throw new ConversionException(value.toString(), e); 177 } 178 } 179 180 } 181 182 183 } 184 | Popular Tags |