All Downloads are FREE. Search and download functionalities are using the official Maven repository.

au.com.bytecode.opencsv.bean.CsvToBean Maven / Gradle / Ivy

There is a newer version: 2.3
Show newest version
package au.com.bytecode.opencsv.bean;

/**
 Copyright 2007 Kyle Miller.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import au.com.bytecode.opencsv.CSVReader;

public class CsvToBean {
	Map , PropertyEditor> editorMap = null;
    public CsvToBean() {
    }

    public List parse(MappingStrategy mapper, Reader reader) {

    	return parse(mapper, new CSVReader(reader));
    }
    
    public List parse(MappingStrategy mapper, CSVReader csv) {
        try {
            mapper.captureHeader(csv);
            String[] line;
            List list = new ArrayList();
            while(null != (line = csv.readNext())) {
                T obj = processLine(mapper, line);
                list.add(obj); // TODO: (Kyle) null check object
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException("Error parsing CSV!", e);
        }
    }

    protected T processLine(MappingStrategy mapper, String[] line) throws IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException {
    	T bean = mapper.createBean();
        for(int col = 0; col < line.length; col++) {
            String value = line[col];
            PropertyDescriptor prop = mapper.findDescriptor(col);
            if (null != prop) {
                Object obj = convertValue(value, prop);
                prop.getWriteMethod().invoke(bean, new Object[] {obj});
            }
        }
        return bean;
    }

    protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException, IllegalAccessException {
        PropertyEditor editor = getPropertyEditor(prop);
        Object obj = value;
        if (null != editor) {
            editor.setAsText(value.trim());
            obj = editor.getValue();
        }
        return obj;
    }
    
    private PropertyEditor getPropertyEditorValue(Class cls)
    {
       if (editorMap == null)
       {
          editorMap = new HashMap, PropertyEditor>();
       }
       
       PropertyEditor editor = editorMap.get(cls);
       
       if (editor == null)
       {
          editor = PropertyEditorManager.findEditor(cls);
          addEditorToMap(cls, editor);
       }
       
       return editor;
    }

   private void addEditorToMap(Class cls, PropertyEditor editor)
   {
      if (editor != null)
       {   
          editorMap.put(cls, editor);
       }
   }


    /*
     * Attempt to find custom property editor on descriptor first, else try the propery editor manager.
     */
    protected PropertyEditor getPropertyEditor(PropertyDescriptor desc) throws InstantiationException, IllegalAccessException {
        Class cls = desc.getPropertyEditorClass();
        if (null != cls) return (PropertyEditor) cls.newInstance();
        return getPropertyEditorValue(desc.getPropertyType());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy