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