Coverage Report - org.apache.commons.convert1.string.FileConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
FileConverter
0%
0/17
0%
0/6
3
 
 1  
 /*
 2  
  *  Copyright 2003-2004 The Apache Software Foundation
 3  
  *
 4  
  *  Licensed under the Apache License, Version 2.0 (the "License");
 5  
  *  you may not use this file except in compliance with the License.
 6  
  *  You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  *  Unless required by applicable law or agreed to in writing, software
 11  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 12  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  *  See the License for the specific language governing permissions and
 14  
  *  limitations under the License.
 15  
  */
 16  
 package org.apache.commons.convert1.string;
 17  
 
 18  
 import java.io.File;
 19  
 
 20  
 import org.apache.commons.convert1.ConversionException;
 21  
 import org.apache.commons.convert1.Converter;
 22  
 
 23  
 /**
 24  
  * <p>Standard {@link Converter} implementation that converts an incoming
 25  
  * String into a <code>java.io.FileL</code> object, optionally using a
 26  
  * default value or throwing a {@link ConversionException} if a conversion
 27  
  * error occurs.</p>
 28  
  *
 29  
  * @author James Strachan
 30  
  * @version $Id: FileConverter.java 155441 2005-02-26 13:19:22Z dirkv $
 31  
  * @since 0.1
 32  
  */
 33  
 public final class FileConverter implements Converter {
 34  
 
 35  
     // ----------------------------------------------------- Instance Variables
 36  
 
 37  
 
 38  
     /**
 39  
      * The default value specified to our Constructor, if any.
 40  
      */
 41  0
     private Object defaultValue = null;
 42  
 
 43  
 
 44  
     /**
 45  
      * Should we return the default value on conversion errors?
 46  
      */
 47  0
     private boolean useDefault = true;
 48  
 
 49  
 
 50  
     // ----------------------------------------------------------- Constructors
 51  
 
 52  
 
 53  
     /**
 54  
      * Create a {@link Converter} that will throw a {@link ConversionException}
 55  
      * if a conversion error occurs.
 56  
      */
 57  0
     public FileConverter() {
 58  
 
 59  0
         this.defaultValue = null;
 60  0
         this.useDefault = false;
 61  
 
 62  0
     }
 63  
 
 64  
 
 65  
     /**
 66  
      * Create a {@link Converter} that will return the specified default value
 67  
      * if a conversion error occurs.
 68  
      *
 69  
      * @param defaultValue The default value to be returned
 70  
      */
 71  0
     public FileConverter(Object defaultValue) {
 72  
 
 73  0
         this.defaultValue = defaultValue;
 74  0
         this.useDefault = true;
 75  
 
 76  0
     }
 77  
 
 78  
 
 79  
 
 80  
     // --------------------------------------------------------- Public Methods
 81  
 
 82  
 
 83  
     /**
 84  
      * Convert the specified input object into an output object of the
 85  
      * specified type.
 86  
      *
 87  
      * @param type Data type to which this value should be converted
 88  
      * @param value The input value to be converted
 89  
      *
 90  
      * @exception ConversionException if conversion cannot be performed
 91  
      *  successfully
 92  
      */
 93  
     public Object convert(Class type, Object value) {
 94  
 
 95  0
         if (value == null) {
 96  0
             if (useDefault) {
 97  0
                 return (defaultValue);
 98  
             } else {
 99  0
                 throw new ConversionException("No value specified");
 100  
             }
 101  
         }
 102  
 
 103  0
         if (value instanceof File) {
 104  0
             return (value);
 105  
         }
 106  
 
 107  0
         return new File(value.toString());
 108  
     }
 109  
 }