1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.beanutils.converters;
20
21
22 import java.util.List;
23 import org.apache.commons.beanutils.ConversionException;
24
25
26 /***
27 * Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
28 * String into an array of String objects. On a conversion failure, returns
29 * a specified default value or throws a {@link ConversionException} depending
30 * on how this instance is constructed.
31 * <p>
32 * There is also some special handling where the input is of type int[].
33 * See method convert for more details.
34 *
35 * @author Craig R. McClanahan
36 * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $
37 * @since 1.4
38 * @deprecated Replaced by the new {@link ArrayConverter} implementation
39 */
40
41 public final class StringArrayConverter extends AbstractArrayConverter {
42
43
44
45
46
47 /***
48 * Create a {@link org.apache.commons.beanutils.Converter} that will throw
49 * a {@link ConversionException} if a conversion error occurs.
50 */
51 public StringArrayConverter() {
52
53 this.defaultValue = null;
54 this.useDefault = false;
55
56 }
57
58
59 /***
60 * Create a {@link org.apache.commons.beanutils.Converter} that will return
61 * the specified default value if a conversion error occurs.
62 *
63 * @param defaultValue The default value to be returned
64 */
65 public StringArrayConverter(Object defaultValue) {
66
67 this.defaultValue = defaultValue;
68 this.useDefault = true;
69
70 }
71
72
73
74
75
76 /***
77 * <p>Model object for type comparisons.</p>
78 */
79 private static final String[] MODEL = new String[0];
80
81 /***
82 * <p> Model object for int arrays.</p>
83 */
84 private static final int[] INT_MODEL = new int[0];
85
86
87
88
89
90
91 /***
92 * Convert the specified input object into an output object of the
93 * specified type.
94 * <p>
95 * If the value is already of type String[] then it is simply returned
96 * unaltered.
97 * <p>
98 * If the value is of type int[], then a String[] is returned where each
99 * element in the string array is the result of calling Integer.toString
100 * on the corresponding element of the int array. This was added as a
101 * result of bugzilla request #18297 though there is not complete
102 * agreement that this feature should have been added.
103 * <p>
104 * In all other cases, this method calls toString on the input object, then
105 * assumes the result is a comma-separated list of values. The values are
106 * split apart into the individual items and returned as the elements of an
107 * array. See class AbstractArrayConverter for the exact input formats
108 * supported.
109 *
110 * @param type is the data type to which this value should be converted.
111 * It is expected to be the class for type String[] (though this parameter
112 * is actually ignored by this method).
113 *
114 * @param value is the input value to be converted. If null then the
115 * default value is returned or an exception thrown if no default value
116 * exists.
117 * @return the converted value
118 *
119 * @exception ConversionException if conversion cannot be performed
120 * successfully, or the input is null and there is no default value set
121 * for this object.
122 */
123 public Object convert(Class type, Object value) {
124
125
126 if (value == null) {
127 if (useDefault) {
128 return (defaultValue);
129 } else {
130 throw new ConversionException("No value specified");
131 }
132 }
133
134
135 if (MODEL.getClass() == value.getClass()) {
136 return (value);
137 }
138
139
140 if (INT_MODEL.getClass() == value.getClass())
141 {
142 int[] values = (int[]) value;
143 String[] results = new String[values.length];
144 for (int i = 0; i < values.length; i++)
145 {
146 results[i] = Integer.toString(values[i]);
147 }
148
149 return (results);
150 }
151
152
153
154 try {
155 List list = parseElements(value.toString());
156 String[] results = new String[list.size()];
157 for (int i = 0; i < results.length; i++) {
158 results[i] = (String) list.get(i);
159 }
160 return (results);
161 } catch (Exception e) {
162 if (useDefault) {
163 return (defaultValue);
164 } else {
165 throw new ConversionException(value.toString(), e);
166 }
167 }
168 }
169
170 }