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

org.supercsv.ext.io.CsvAnnotationBeanWriter Maven / Gradle / Ivy

Go to download

CSVのJavaライブラリであるSuperCSVに、アノテーション機能を追加したライブラリです。

There is a newer version: 2.3
Show newest version
package org.supercsv.ext.io;

import java.io.IOException;
import java.io.Writer;
import java.util.Collection;

import org.supercsv.ext.builder.CsvAnnotationBeanParser;
import org.supercsv.ext.builder.CsvBeanMapping;
import org.supercsv.prefs.CsvPreference;


/**
 *
 * @version 1.2
 * @author T.TSUCHIE
 *
 */
public class CsvAnnotationBeanWriter extends ValidatableCsvBeanWriter {
    
    protected CsvAnnotationBeanParser beanParser = new CsvAnnotationBeanParser();
    
    protected final CsvBeanMapping beanMapping;
    
    protected final BeanMappingCache mappingCache;
    
    public CsvAnnotationBeanWriter(final Class clazz, final Writer writer, final CsvPreference preferences) {
        this(clazz, writer, preferences, false);
    }
    
    public CsvAnnotationBeanWriter(final Class clazz, final Writer writer, final CsvPreference preferences,
            final boolean ignoreValidationProcessor, final CsvAnnotationBeanParser beanParser) { 
        super(writer, preferences);
        this.beanParser = beanParser;
        this.beanMapping = beanParser.parse(clazz, ignoreValidationProcessor);
        this.mappingCache = new BeanMappingCache(beanMapping);
    }
    
    public CsvAnnotationBeanWriter(final Class clazz, Writer writer, final CsvPreference preferences, final boolean ignoreValidationProcessor) { 
        super(writer, preferences);
        this.beanMapping = beanParser.parse(clazz, ignoreValidationProcessor);
        this.mappingCache = new BeanMappingCache(beanMapping);
    }
    
    public CsvAnnotationBeanWriter(final CsvBeanMapping beanMapping, final Writer writer, final CsvPreference preferences) {
        super(writer, preferences);
        this.beanMapping = beanMapping;
        this.mappingCache = new BeanMappingCache(this.beanMapping);
    }
    
    public boolean hasHeader() {
        return getBeanMapping().isHeader();
    }
    
    public void writeHeader() throws IOException {
        writeHeader(getMappingCache().getHeader());
    }
    
    public void write(final T source) throws IOException {
        write(source, getMappingCache().getNameMapping(), getMappingCache().getOutputCellProcessors());
    }
    
    public void writeAll(final Collection collection) throws IOException {
        for(T item : collection) {
            write(item, getMappingCache().getNameMapping(), getMappingCache().getOutputCellProcessors());
        }
    }
    
    public CsvBeanMapping getBeanMapping() {
        return beanMapping;
    }
    
    public BeanMappingCache getMappingCache() {
        return mappingCache;
    }
    
    @Override
    public String[] getDefinedHeader() {
        return hasHeader() ? mappingCache.getHeader() : null;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy