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