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    
018    
019    package org.apache.commons.beanutils.converters;
020    
021    
022    import java.util.List;
023    import org.apache.commons.beanutils.ConversionException;
024    
025    
026    /**
027     * <p>Standard {@link org.apache.commons.beanutils.Converter} implementation that converts an incoming
028     * String into a primitive array of char.  On a conversion failure, returns
029     * a specified default value or throws a {@link ConversionException} depending
030     * on how this instance is constructed.</p>
031     *
032     * @author Craig R. McClanahan
033     * @version $Revision: 556229 $ $Date: 2007-07-14 07:11:19 +0100 (Sat, 14 Jul 2007) $
034     * @since 1.4
035     * @deprecated Replaced by the new {@link ArrayConverter} implementation
036     */
037    
038    public final class CharacterArrayConverter extends AbstractArrayConverter {
039    
040    
041        // ----------------------------------------------------------- Constructors
042    
043    
044        /**
045         * Create a {@link org.apache.commons.beanutils.Converter} that will throw
046         * a {@link ConversionException} if a conversion error occurs.
047         */
048        public CharacterArrayConverter() {
049    
050            this.defaultValue = null;
051            this.useDefault = false;
052    
053        }
054    
055    
056        /**
057         * Create a {@link org.apache.commons.beanutils.Converter} that will return
058         * the specified default value if a conversion error occurs.
059         *
060         * @param defaultValue The default value to be returned
061         */
062        public CharacterArrayConverter(Object defaultValue) {
063    
064            this.defaultValue = defaultValue;
065            this.useDefault = true;
066    
067        }
068    
069    
070        // ------------------------------------------------------- Static Variables
071    
072    
073        /**
074         * <p>Model object for type comparisons.</p>
075         */
076        private static final char[] MODEL = new char[0];
077    
078    
079        // --------------------------------------------------------- Public Methods
080    
081    
082        /**
083         * Convert the specified input object into an output object of the
084         * specified type.
085         *
086         * @param type Data type to which this value should be converted
087         * @param value The input value to be converted
088         * @return the converted value
089         *
090         * @exception ConversionException if conversion cannot be performed
091         *  successfully
092         */
093        public Object convert(Class type, Object value) {
094    
095            // Deal with a null value
096            if (value == null) {
097                if (useDefault) {
098                    return (defaultValue);
099                } else {
100                    throw new ConversionException("No value specified");
101                }
102            }
103    
104            // Deal with the no-conversion-needed case
105            if (MODEL.getClass() == value.getClass()) {
106                return (value);
107            }
108    
109            // Deal with input value as a String array
110            if (strings.getClass() == value.getClass()) {
111                try {
112                    String[] values = (String[]) value;
113                    char[] results = new char[values.length];
114                    for (int i = 0; i < values.length; i++) {
115                        results[i] = values[i].charAt(0);
116                    }
117                    return (results);
118                } catch (Exception e) {
119                    if (useDefault) {
120                        return (defaultValue);
121                    } else {
122                        throw new ConversionException(value.toString(), e);
123                    }
124                }
125            }
126    
127            // Parse the input value as a String into elements
128            // and convert to the appropriate type
129            try {
130                List list = parseElements(value.toString());
131                char[] results = new char[list.size()];
132                for (int i = 0; i < results.length; i++) {
133                    results[i] = ((String) list.get(i)).charAt(0);
134                }
135                return (results);
136            } catch (Exception e) {
137                if (useDefault) {
138                    return (defaultValue);
139                } else {
140                    throw new ConversionException(value.toString(), e);
141                }
142            }
143    
144        }
145    
146    
147    }