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

br.com.tecsinapse.dataio.importer.Importer Maven / Gradle / Ivy

The newest version!
/*
 * Tecsinapse Data Input and Output
 *
 * License: GNU Lesser General Public License (LGPL), version 3 or later
 * See the LICENSE file in the root directory or .
 */
package br.com.tecsinapse.dataio.importer;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;

import com.google.common.base.Strings;

import br.com.tecsinapse.dataio.converter.group.Default;
import br.com.tecsinapse.dataio.importer.parser.CsvParser;
import br.com.tecsinapse.dataio.importer.parser.SpreadsheetParser;
import br.com.tecsinapse.dataio.type.FileType;

public class Importer implements Closeable {

    public static final int DEFAULT_START_ROW = 1;
    private final Class group;
    private Class clazz;
    private File file;
    private InputStream inputStream;
    private String filename;
    private Charset charset;
    private Parser parser;
    private int headersRows = DEFAULT_START_ROW;
    private boolean isLastSheet;

    public Importer(Class clazz, Charset charset, File file) {
        this(clazz, charset, Default.class);
        this.file = file;
    }

    public Importer(Class clazz, Charset charset, InputStream inputStream, String filename) {
        this(clazz, inputStream, filename);
        this.charset = charset;
    }

    public Importer(Class clazz, File file) {
        this(clazz, file, Default.class);
    }

    public Importer(Class clazz, File file, Class group) {
        this(clazz, group);
        this.file = file;
    }

    public Importer(Class clazz, InputStream inputStream, String filename) {
        this(clazz, inputStream, filename, Default.class);
    }

    public Importer(Class clazz, InputStream inputStream, String filename, Class group) {
        this(clazz, inputStream, filename, false, group);
    }

    public Importer(Class clazz, InputStream inputStream, String filename, boolean isLastSheet) {
        this(clazz, inputStream, filename, isLastSheet, Default.class);
    }

    public Importer(Class clazz, InputStream inputStream, String filename, boolean isLastSheet, Class group) {
        this(clazz, inputStream, filename, DEFAULT_START_ROW, isLastSheet, group);
    }

    public Importer(Class clazz, InputStream inputStream, String filename, int afterLine, boolean isLastSheet) {
        this(clazz, inputStream, filename, afterLine, isLastSheet, Default.class);
    }

    public Importer(Class clazz, InputStream inputStream, String filename, int afterLine, boolean isLastSheet, Class group) {
        this(clazz, group);
        this.inputStream = inputStream;
        this.filename = filename;
        this.headersRows = afterLine;
        this.isLastSheet = isLastSheet;
    }

    private Importer(Class clazz, Class group) {
        this(clazz, Charset.defaultCharset(), group);
    }

    private Importer(Class clazz, Charset charset, Class group) {
        this.clazz = clazz;
        this.charset = charset;
        this.group = group;
    }

    private void beforeParser() throws IOException {
        doBeforeParser();
    }

    private void doBeforeParser() throws IOException {
        FileType fileType = getFileType();
        if (fileType == FileType.XLSX || fileType == FileType.XLS) {
            if (file != null) {
                parser = new SpreadsheetParser<>(clazz, file);
                parser.setGroup(group);
                parser.setHeadersRows(headersRows);
                parser.setLastsheet(isLastSheet);
                return;
            }
            if (inputStream != null) {
                parser = new SpreadsheetParser<>(clazz, inputStream, fileType);
                parser.setGroup(group);
                parser.setHeadersRows(headersRows);
                parser.setLastsheet(isLastSheet);
                return;
            }
        }
        if (file != null) {
            parser = new CsvParser<>(clazz, file, charset, headersRows, group);
            return;
        }
        if (inputStream != null) {
            parser = new CsvParser<>(clazz, inputStream, charset, headersRows, group);
        }
    }

    public FileType getFileType() {
        if (this.file == null && Strings.isNullOrEmpty(this.filename)) {
            throw new IllegalStateException("File is null and filename is null");
        }
        String name = this.filename;
        if (file != null) {
            name = file.getName();
        }
        return FileType.getFileType(name);
    }

    public void setAfterLine(int afterLine) {
        this.headersRows = afterLine;
    }

    public void setCharset(Charset charset) {
        this.charset = charset;
    }

    public List parse() throws Exception {
        beforeParser();
        return parser.parse();
    }

    public int getNumberOfSheets() {
        return parser.getNumberOfSheets();
    }

    @Override
    public void close() throws IOException {
        parser.close();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy