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 * Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
028 * String into an array of String objects. On a conversion failure, returns
029 * a specified default value or throws a {@link ConversionException} depending
030 * on how this instance is constructed.
031 * <p>
032 * There is also some special handling where the input is of type int[].
033 * See method convert for more details.
034 *
035 * @author Craig R. McClanahan
036 * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $
037 * @since 1.4
038 * @deprecated Replaced by the new {@link ArrayConverter} implementation
039 */
040
041 public final class StringArrayConverter extends AbstractArrayConverter {
042
043
044 // ----------------------------------------------------------- Constructors
045
046
047 /**
048 * Create a {@link org.apache.commons.beanutils.Converter} that will throw
049 * a {@link ConversionException} if a conversion error occurs.
050 */
051 public StringArrayConverter() {
052
053 this.defaultValue = null;
054 this.useDefault = false;
055
056 }
057
058
059 /**
060 * Create a {@link org.apache.commons.beanutils.Converter} that will return
061 * the specified default value if a conversion error occurs.
062 *
063 * @param defaultValue The default value to be returned
064 */
065 public StringArrayConverter(Object defaultValue) {
066
067 this.defaultValue = defaultValue;
068 this.useDefault = true;
069
070 }
071
072
073 // ------------------------------------------------------- Static Variables
074
075
076 /**
077 * <p>Model object for type comparisons.</p>
078 */
079 private static final String[] MODEL = new String[0];
080
081 /**
082 * <p> Model object for int arrays.</p>
083 */
084 private static final int[] INT_MODEL = new int[0];
085
086
087
088 // --------------------------------------------------------- Public Methods
089
090
091 /**
092 * Convert the specified input object into an output object of the
093 * specified type.
094 * <p>
095 * If the value is already of type String[] then it is simply returned
096 * unaltered.
097 * <p>
098 * If the value is of type int[], then a String[] is returned where each
099 * 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 // Deal with a null value
126 if (value == null) {
127 if (useDefault) {
128 return (defaultValue);
129 } else {
130 throw new ConversionException("No value specified");
131 }
132 }
133
134 // Deal with the no-conversion-needed case
135 if (MODEL.getClass() == value.getClass()) {
136 return (value);
137 }
138
139 // Deal with the input value as an int array
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 // Parse the input value as a String into elements
153 // and convert to the appropriate type
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 }