001package au.com.bytecode.opencsv.bean;
002
003/**
004 Copyright 2007 Kyle Miller.
005
006 Licensed under the Apache License, Version 2.0 (the "License");
007 you may not use this file except in compliance with the License.
008 You may obtain a copy of the License at
009
010 http://www.apache.org/licenses/LICENSE-2.0
011
012 Unless required by applicable law or agreed to in writing, software
013 distributed under the License is distributed on an "AS IS" BASIS,
014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 See the License for the specific language governing permissions and
016 limitations under the License.
017 */
018
019import java.beans.IntrospectionException;
020import java.beans.PropertyDescriptor;
021import java.beans.PropertyEditor;
022import java.beans.PropertyEditorManager;
023import java.io.Reader;
024import java.lang.reflect.InvocationTargetException;
025import java.util.ArrayList;
026import java.util.List;
027
028import au.com.bytecode.opencsv.CSVReader;
029
030public class CsvToBean {
031
032    public CsvToBean() {
033    }
034
035    public List parse(MappingStrategy mapper, Reader reader) {
036        try {
037            CSVReader csv = new CSVReader(reader);
038            mapper.captureHeader(csv);
039            String[] line;
040            List list = new ArrayList();
041            while(null != (line = csv.readNext())) {
042                Object obj = processLine(mapper, line);
043                list.add(obj); // TODO: (Kyle) null check object
044            }
045            return list;
046        } catch (Exception e) {
047            throw new RuntimeException("Error parsing CSV!", e);
048        }
049    }
050
051    protected Object processLine(MappingStrategy mapper, String[] line) throws IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException {
052        Object bean = mapper.createBean();
053        for(int col = 0; col < line.length; col++) {
054            String value = line[col];
055            PropertyDescriptor prop = mapper.findDescriptor(col);
056            if (null != prop) {
057                Object obj = convertValue(value, prop);
058                prop.getWriteMethod().invoke(bean, new Object[] {obj});
059            }
060        }
061        return bean;
062    }
063
064    protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException, IllegalAccessException {
065        PropertyEditor editor = getPropertyEditor(prop);
066        Object obj = value;
067        if (null != editor) {
068            editor.setAsText(value);
069            obj = editor.getValue();
070        }
071        return obj;
072    }
073
074    /*
075     * Attempt to find custom property editor on descriptor first, else try the propery editor manager.
076     */
077    protected PropertyEditor getPropertyEditor(PropertyDescriptor desc) throws InstantiationException, IllegalAccessException {
078        Class cls = desc.getPropertyEditorClass();
079        if (null != cls) return (PropertyEditor) cls.newInstance();
080        return PropertyEditorManager.findEditor(desc.getPropertyType());
081    }
082
083}