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    /**
020     * {@link org.apache.commons.beanutils.Converter}
021     * implementation that converts an incoming
022     * object into a <code>java.lang.String</code> object.
023     * <p>
024     * Note that ConvertUtils really is designed to do string->object conversions,
025     * and offers very little support for object->string conversions. The
026     * ConvertUtils/ConvertUtilsBean methods only select a converter to apply
027     * based upon the target type being converted to, and generally assume that
028     * the input is a string (by calling its toString method if needed).
029     * <p>
030     * This class is therefore just a dummy converter that converts its input
031     * into a string by calling the input object's toString method and returning
032     * that value.
033     * <p>
034     * It is possible to replace this converter with something that has a big
035     * if/else statement that selects behaviour based on the real type of the
036     * object being converted (or possibly has a map of converters, and looks
037     * them up based on the class of the input object). However this is not part
038     * of the existing ConvertUtils framework.
039     *  
040     *
041     * @author Craig R. McClanahan
042     * @version $Revision: 690380 $ $Date: 2008-08-29 21:04:38 +0100 (Fri, 29 Aug 2008) $
043     * @since 1.3
044     */
045    public final class StringConverter extends AbstractConverter {
046    
047    
048        /**
049         * Construct a <b>java.lang.String</b> <i>Converter</i> that throws
050         * a <code>ConversionException</code> if an error occurs.
051         */
052        public StringConverter() {
053            super();
054        }
055    
056        /**
057         * Construct a <b>java.lang.String</b> <i>Converter</i> that returns
058         * a default value if an error occurs.
059         *
060         * @param defaultValue The default value to be returned
061         * if the value to be converted is missing or an error
062         * occurs converting the value.
063         */
064        public StringConverter(Object defaultValue) {
065            super(defaultValue);
066        }
067    
068        /**
069         * Return the default type this <code>Converter</code> handles.
070         *
071         * @return The default type this <code>Converter</code> handles.
072         * @since 1.8.0
073         */
074        protected Class getDefaultType() {
075            return String.class;
076        }
077    
078        /**
079         * Convert the specified input object into an output object of the
080         * specified type.
081         *
082         * @param type Data type to which this value should be converted.
083         * @param value The input value to be converted.
084         * @return The converted value.
085         * @throws Throwable if an error occurs converting to the specified type
086         * @since 1.8.0
087         */
088        protected Object convertToType(Class type, Object value) throws Throwable {
089            return value.toString();
090        }
091    
092    
093    }