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: package org.apache.commons.beanutils.converters;
019:
020: import java.util.List;
021: import org.apache.commons.beanutils.ConversionException;
022:
023: /**
024: * Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
025: * String into an array of String objects. On a conversion failure, returns
026: * a specified default value or throws a {@link ConversionException} depending
027: * on how this instance is constructed.
028: * <p>
029: * There is also some special handling where the input is of type int[].
030: * See method convert for more details.
031: *
032: * @author Craig R. McClanahan
033: * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $
034: * @since 1.4
035: * @deprecated Replaced by the new {@link ArrayConverter} implementation
036: */
037:
038: public final class StringArrayConverter extends AbstractArrayConverter {
039:
040: // ----------------------------------------------------------- Constructors
041:
042: /**
043: * Create a {@link org.apache.commons.beanutils.Converter} that will throw
044: * a {@link ConversionException} if a conversion error occurs.
045: */
046: public StringArrayConverter() {
047:
048: this .defaultValue = null;
049: this .useDefault = false;
050:
051: }
052:
053: /**
054: * Create a {@link org.apache.commons.beanutils.Converter} that will return
055: * the specified default value if a conversion error occurs.
056: *
057: * @param defaultValue The default value to be returned
058: */
059: public StringArrayConverter(Object defaultValue) {
060:
061: this .defaultValue = defaultValue;
062: this .useDefault = true;
063:
064: }
065:
066: // ------------------------------------------------------- Static Variables
067:
068: /**
069: * <p>Model object for type comparisons.</p>
070: */
071: private static final String[] MODEL = new String[0];
072:
073: /**
074: * <p> Model object for int arrays.</p>
075: */
076: private static final int[] INT_MODEL = new int[0];
077:
078: // --------------------------------------------------------- Public Methods
079:
080: /**
081: * Convert the specified input object into an output object of the
082: * specified type.
083: * <p>
084: * If the value is already of type String[] then it is simply returned
085: * unaltered.
086: * <p>
087: * If the value is of type int[], then a String[] is returned where each
088: * element in the string array is the result of calling Integer.toString
089: * on the corresponding element of the int array. This was added as a
090: * result of bugzilla request #18297 though there is not complete
091: * agreement that this feature should have been added.
092: * <p>
093: * In all other cases, this method calls toString on the input object, then
094: * assumes the result is a comma-separated list of values. The values are
095: * split apart into the individual items and returned as the elements of an
096: * array. See class AbstractArrayConverter for the exact input formats
097: * supported.
098: *
099: * @param type is the data type to which this value should be converted.
100: * It is expected to be the class for type String[] (though this parameter
101: * is actually ignored by this method).
102: *
103: * @param value is the input value to be converted. If null then the
104: * default value is returned or an exception thrown if no default value
105: * exists.
106: * @return the converted value
107: *
108: * @exception ConversionException if conversion cannot be performed
109: * successfully, or the input is null and there is no default value set
110: * for this object.
111: */
112: public Object convert(Class type, Object value) {
113:
114: // Deal with a null value
115: if (value == null) {
116: if (useDefault) {
117: return (defaultValue);
118: } else {
119: throw new ConversionException("No value specified");
120: }
121: }
122:
123: // Deal with the no-conversion-needed case
124: if (MODEL.getClass() == value.getClass()) {
125: return (value);
126: }
127:
128: // Deal with the input value as an int array
129: if (INT_MODEL.getClass() == value.getClass()) {
130: int[] values = (int[]) value;
131: String[] results = new String[values.length];
132: for (int i = 0; i < values.length; i++) {
133: results[i] = Integer.toString(values[i]);
134: }
135:
136: return (results);
137: }
138:
139: // Parse the input value as a String into elements
140: // and convert to the appropriate type
141: try {
142: List list = parseElements(value.toString());
143: String[] results = new String[list.size()];
144: for (int i = 0; i < results.length; i++) {
145: results[i] = (String) list.get(i);
146: }
147: return (results);
148: } catch (Exception e) {
149: if (useDefault) {
150: return (defaultValue);
151: } else {
152: throw new ConversionException(value.toString(), e);
153: }
154: }
155: }
156:
157: }
|