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 StringArrayConverter extends AbstractArrayConverter { 38 39 40 42 43 47 public StringArrayConverter() { 48 49 this.defaultValue = null; 50 this.useDefault = false; 51 52 } 53 54 55 61 public StringArrayConverter(Object defaultValue) { 62 63 this.defaultValue = defaultValue; 64 this.useDefault = true; 65 66 } 67 68 69 71 72 75 private static String model[] = new String [0]; 76 77 80 private static int ints[] = new int[0]; 81 82 83 84 86 87 97 public Object convert(Class type, Object value) { 98 99 if (value == null) { 101 if (useDefault) { 102 return (defaultValue); 103 } else { 104 throw new ConversionException("No value specified"); 105 } 106 } 107 108 if (model.getClass() == value.getClass()) { 110 return (value); 111 } 112 113 if (ints.getClass() == value.getClass()) 115 { 116 int[] values = (int[]) value; 117 String [] results = new String [values.length]; 118 for (int i = 0; i < values.length; i++) 119 { 120 results[i] = Integer.toString(values[i]); 121 } 122 123 return (results); 124 } 125 126 try { 129 List list = parseElements(value.toString()); 130 String results[] = new String [list.size()]; 131 for (int i = 0; i < results.length; i++) { 132 results[i] = (String ) list.get(i); 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 145 146 } 147 | Popular Tags |