1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 * @version $Id$
36 * @since 1.4
37 * @deprecated Replaced by the new {@link ArrayConverter} implementation
38 */
39
40 @Deprecated
41 public final class StringArrayConverter extends AbstractArrayConverter {
42
43
44 // ----------------------------------------------------------- Constructors
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(final Object defaultValue) {
66
67 this.defaultValue = defaultValue;
68 this.useDefault = true;
69
70 }
71
72
73 // ------------------------------------------------------- Static Variables
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 // --------------------------------------------------------- Public Methods
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 * @throws 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 @Override
124 public Object convert(final Class type, final Object value) {
125
126 // Deal with a null value
127 if (value == null) {
128 if (useDefault) {
129 return (defaultValue);
130 } else {
131 throw new ConversionException("No value specified");
132 }
133 }
134
135 // Deal with the no-conversion-needed case
136 if (MODEL.getClass() == value.getClass()) {
137 return (value);
138 }
139
140 // Deal with the input value as an int array
141 if (INT_MODEL.getClass() == value.getClass())
142 {
143 final int[] values = (int[]) value;
144 final String[] results = new String[values.length];
145 for (int i = 0; i < values.length; i++)
146 {
147 results[i] = Integer.toString(values[i]);
148 }
149
150 return (results);
151 }
152
153 // Parse the input value as a String into elements
154 // and convert to the appropriate type
155 try {
156 final List list = parseElements(value.toString());
157 final String[] results = new String[list.size()];
158 for (int i = 0; i < results.length; i++) {
159 results[i] = (String) list.get(i);
160 }
161 return (results);
162 } catch (final Exception e) {
163 if (useDefault) {
164 return (defaultValue);
165 } else {
166 throw new ConversionException(value.toString(), e);
167 }
168 }
169 }
170
171 }