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    
019    import org.apache.commons.convert1.ConversionException;
020    import org.apache.commons.convert1.Converter;
021    
022    
023    /**
024     * <p>Standard {@link Converter} implementation that converts an incoming
025     * String into a <code>java.lang.Class</code> object, optionally using a
026     * default value or throwing a {@link ConversionException} if a conversion
027     * error occurs.  The class will be loaded from the thread context class
028     * loader (if it exists); otherwise the class loader that loaded this class
029     * will be used.</p>
030     *
031     * @author Tomas Viberg
032     * @version $Id: ClassConverter.java 155441 2005-02-26 13:19:22Z dirkv $
033     * @since 0.1
034     */
035    
036    public final class ClassConverter implements Converter {
037    
038    
039        // ----------------------------------------------------------- Constructors
040    
041    
042        /**
043         * Create a {@link Converter} that will throw a {@link ConversionException}
044         * if a conversion error occurs.
045         */
046        public ClassConverter() {
047    
048            this.defaultValue = null;
049            this.useDefault = false;
050    
051        }
052    
053    
054        /**
055         * Create a {@link Converter} that will return the specified default value
056         * if a conversion error occurs.
057         *
058         * @param defaultValue The default value to be returned
059         */
060        public ClassConverter(Object defaultValue) {
061    
062            this.defaultValue = defaultValue;
063            this.useDefault = true;
064    
065        }
066    
067    
068        // ----------------------------------------------------- Instance Variables
069    
070    
071        /**
072         * The default value specified to our Constructor, if any.
073         */
074        private Object defaultValue = null;
075    
076    
077        /**
078         * Should we return the default value on conversion errors?
079         */
080        private boolean useDefault = true;
081    
082    
083        // --------------------------------------------------------- Public Methods
084    
085    
086        /**
087         * Convert the specified input object into an output object of the
088         * specified type.
089         *
090         * @param type Data type to which this value should be converted
091         * @param value The input value to be converted
092         *
093         * @exception ConversionException if conversion cannot be performed
094         *  successfully
095         */
096        public Object convert(Class type, Object value) {
097    
098            if (value == null) {
099                if (useDefault) {
100                    return (defaultValue);
101                } else {
102                    throw new ConversionException("No value specified");
103                }
104            }
105    
106            if (value instanceof Class) {
107                return (value);
108            }
109    
110            try {
111                ClassLoader classLoader =
112                    Thread.currentThread().getContextClassLoader();
113                if (classLoader == null) {
114                    classLoader = ClassConverter.class.getClassLoader();
115                }
116                return (classLoader.loadClass(value.toString()));
117            } catch (Exception e) {
118                if (useDefault) {
119                    return (defaultValue);
120                } else {
121                    throw new ConversionException(e);
122                }
123            }
124    
125        }
126    
127    
128    }