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    package org.apache.commons.beanutils.converters;
018    
019    import java.io.File;
020    
021    /**
022     * {@link org.apache.commons.beanutils.Converter} implementaion that handles conversion
023     * to and from <b>java.io.File</b> objects.
024     * <p>
025     * Can be configured to either return a <i>default value</i> or throw a
026     * <code>ConversionException</code> if a conversion error occurs.
027     *
028     * @author James Strachan
029     * @version $Revision: 690380 $ $Date: 2008-08-29 21:04:38 +0100 (Fri, 29 Aug 2008) $
030     * @since 1.6
031     */
032    public final class FileConverter extends AbstractConverter {
033    
034        /**
035         * Construct a <b>java.io.File</b> <i>Converter</i> that throws
036         * a <code>ConversionException</code> if an error occurs.
037         */
038        public FileConverter() {
039            super();
040        }
041    
042        /**
043         * Construct a <b>java.io.File</b> <i>Converter</i> that returns
044         * a default value if an error occurs.
045         *
046         * @param defaultValue The default value to be returned
047         * if the value to be converted is missing or an error
048         * occurs converting the value.
049         */
050        public FileConverter(Object defaultValue) {
051            super(defaultValue);
052        }
053    
054        /**
055         * Return the default type this <code>Converter</code> handles.
056         *
057         * @return The default type this <code>Converter</code> handles.
058         * @since 1.8.0
059         */
060        protected Class getDefaultType() {
061            return File.class;
062        }
063    
064        /**
065         * <p>Convert the input object into a java.io.File.</p>
066         *
067         * @param type Data type to which this value should be converted.
068         * @param value The input value to be converted.
069         * @return The converted value.
070         * @throws Throwable if an error occurs converting to the specified type
071         * @since 1.8.0
072         */
073        protected Object convertToType(Class type, Object value) throws Throwable {
074            return new File(value.toString());
075        }
076    }