Coverage Report - org.apache.commons.beanutils.converters.StringArrayConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
StringArrayConverter
45%
14/31
35%
5/14
6
 
 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  
  * @author Craig R. McClanahan
 36  
  * @version $Revision: 556229 $ $Date: 2007-07-14 02:11:19 -0400 (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  
     // ----------------------------------------------------------- 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  1
     public StringArrayConverter() {
 52  
 
 53  1
         this.defaultValue = null;
 54  1
         this.useDefault = false;
 55  
 
 56  1
     }
 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  0
     public StringArrayConverter(Object defaultValue) {
 66  
 
 67  0
         this.defaultValue = defaultValue;
 68  0
         this.useDefault = true;
 69  
 
 70  0
     }
 71  
 
 72  
 
 73  
     // ------------------------------------------------------- Static Variables
 74  
 
 75  
 
 76  
     /**
 77  
      * <p>Model object for type comparisons.</p>
 78  
      */
 79  1
     private static final String[] MODEL = new String[0];
 80  
     
 81  
     /**
 82  
      * <p> Model object for int arrays.</p>
 83  
      */
 84  1
     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  
      * @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  1
         if (value == null) {
 127  0
             if (useDefault) {
 128  0
                 return (defaultValue);
 129  
             } else {
 130  0
                 throw new ConversionException("No value specified");
 131  
             }
 132  
         }
 133  
 
 134  
         // Deal with the no-conversion-needed case
 135  1
         if (MODEL.getClass() == value.getClass()) {
 136  0
             return (value);
 137  
         }
 138  
 
 139  
         // Deal with the input value as an int array
 140  1
         if (INT_MODEL.getClass() == value.getClass())
 141  
         {
 142  1
             int[] values = (int[]) value;
 143  1
             String[] results = new String[values.length];
 144  6
             for (int i = 0; i < values.length; i++)
 145  
             {
 146  5
                 results[i] = Integer.toString(values[i]);
 147  
             }
 148  
 
 149  1
             return (results);
 150  
         }
 151  
 
 152  
         // Parse the input value as a String into elements
 153  
         // and convert to the appropriate type
 154  
         try {
 155  0
             List list = parseElements(value.toString());
 156  0
             String[] results = new String[list.size()];
 157  0
             for (int i = 0; i < results.length; i++) {
 158  0
                 results[i] = (String) list.get(i);
 159  
             }
 160  0
             return (results);
 161  0
         } catch (Exception e) {
 162  0
             if (useDefault) {
 163  0
                 return (defaultValue);
 164  
             } else {
 165  0
                 throw new ConversionException(value.toString(), e);
 166  
             }
 167  
         }
 168  
     }
 169  
 
 170  
 }