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

sirius.web.data.LineBasedProcessor Maven / Gradle / Ivy

There is a newer version: 22.2.3
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.web.data;

import sirius.kernel.health.Exceptions;

import java.io.InputStream;

/**
 * Processes line based input files like MS Excel or CSV.
 */
public interface LineBasedProcessor {

    /**
     * Generates an appropriate LineBasedProcessor based on the file extension of the given file.
     *
     * @param name  the name of the file to process
     * @param input an input stream containing the data to import
     * @return an appropriate processor for the given file
     * @throws sirius.kernel.health.HandledException if no processor can handle the given file
     */
    static LineBasedProcessor create(String name, InputStream input) {
        if (name.toLowerCase().endsWith("xls")) {
            return new XLSProcessor(input, false);
        }
        if (name.toLowerCase().endsWith("xlsx")) {
            return new XLSProcessor(input, true);
        }
        if (name.toLowerCase().endsWith("csv")) {
            return new CSVProcessor(input);
        }
        throw Exceptions.createHandled().withSystemErrorMessage("Cannot process files of type: %s", name).handle();
    }

    /**
     * Starts processing and sends each line to the given rowProcessor.
     *
     * @param rowProcessor the processor which handles each row of the file
     * @throws Exception in case an error occurred while processing.
     */
    void run(RowProcessor rowProcessor) throws Exception;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy