001 /* 002 * Copyright 2003-2004 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.commons.convert1.string; 017 018 import java.io.File; 019 020 import org.apache.commons.convert1.ConversionException; 021 import org.apache.commons.convert1.Converter; 022 023 /** 024 * <p>Standard {@link Converter} implementation that converts an incoming 025 * String into a <code>java.io.FileL</code> object, optionally using a 026 * default value or throwing a {@link ConversionException} if a conversion 027 * error occurs.</p> 028 * 029 * @author James Strachan 030 * @version $Id: FileConverter.java 155441 2005-02-26 13:19:22Z dirkv $ 031 * @since 0.1 032 */ 033 public final class FileConverter implements Converter { 034 035 // ----------------------------------------------------- Instance Variables 036 037 038 /** 039 * The default value specified to our Constructor, if any. 040 */ 041 private Object defaultValue = null; 042 043 044 /** 045 * Should we return the default value on conversion errors? 046 */ 047 private boolean useDefault = true; 048 049 050 // ----------------------------------------------------------- Constructors 051 052 053 /** 054 * Create a {@link Converter} that will throw a {@link ConversionException} 055 * if a conversion error occurs. 056 */ 057 public FileConverter() { 058 059 this.defaultValue = null; 060 this.useDefault = false; 061 062 } 063 064 065 /** 066 * Create a {@link Converter} that will return the specified default value 067 * if a conversion error occurs. 068 * 069 * @param defaultValue The default value to be returned 070 */ 071 public FileConverter(Object defaultValue) { 072 073 this.defaultValue = defaultValue; 074 this.useDefault = true; 075 076 } 077 078 079 080 // --------------------------------------------------------- Public Methods 081 082 083 /** 084 * Convert the specified input object into an output object of the 085 * specified type. 086 * 087 * @param type Data type to which this value should be converted 088 * @param value The input value to be converted 089 * 090 * @exception ConversionException if conversion cannot be performed 091 * successfully 092 */ 093 public Object convert(Class type, Object value) { 094 095 if (value == null) { 096 if (useDefault) { 097 return (defaultValue); 098 } else { 099 throw new ConversionException("No value specified"); 100 } 101 } 102 103 if (value instanceof File) { 104 return (value); 105 } 106 107 return new File(value.toString()); 108 } 109 }