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

br.com.tecsinapse.dataio.util.ExporterUtil Maven / Gradle / Ivy

There is a newer version: 3.1.8
Show 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.util;

import static br.com.tecsinapse.dataio.type.SeparatorType.SEMICOLON;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import br.com.tecsinapse.dataio.Table;
import br.com.tecsinapse.dataio.exceptions.ExporterException;
import br.com.tecsinapse.dataio.exceptions.ExporterNotImplementedException;
import br.com.tecsinapse.dataio.txt.FieldTxt;
import br.com.tecsinapse.dataio.txt.FileTxt;
import br.com.tecsinapse.dataio.type.FileType;

public final class ExporterUtil {
    private ExporterUtil() {

    }

    public static File writeDataToFile(List table, FileType fileType, String filename) throws IOException {
        return writeDataToFile(table, fileType, filename, "UTF-8");
    }

    public static File writeDataToFile(List
table, FileType fileType, String filename, String charset) throws IOException { return writeDataToFile(table, fileType, filename, charset, SEMICOLON.getSeparator()); } public static File writeDataToFile(List
table, FileType fileType, String filename, String charset, String separator) throws IOException { File file = createFile(filename); try (FileOutputStream fos = new FileOutputStream(file)) { writeData(table, fileType, fos, charset, separator); } return file; } public static void writeData(List
table, FileType fileType, OutputStream outputStream, String charset, String separator) throws IOException { BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); if (fileType == FileType.XLSX || fileType == FileType.XLSM) { writeXlsx(table, bufferedOutputStream); bufferedOutputStream.flush(); return; } if (fileType == FileType.SXLSX) { writeSxlsx(table, bufferedOutputStream); bufferedOutputStream.flush(); return; } if (fileType == FileType.XLS) { writeXls(table, bufferedOutputStream); bufferedOutputStream.flush(); return; } if (fileType == FileType.CSV || fileType == FileType.TXT) { writeCsv(table, bufferedOutputStream, charset, separator); bufferedOutputStream.flush(); return; } throw new ExporterNotImplementedException(String.format("File type not supported. %s", fileType)); } public static void writeFileTxtToOutput(FileTxt file, String chartsetName, OutputStream out) throws IOException { try (OutputStreamWriter writer = new OutputStreamWriter(out, chartsetName)) { for (List lines : file.getFields()) { StringBuilder line = new StringBuilder(); for (Iterator it = lines.iterator(); it.hasNext(); ) { FieldTxt field = it.next(); line.append(field.getValue()); if (it.hasNext() || file.isEndsWithSeparator()) { line.append(field.getSeparator()); } } line.append("\r\n"); writer.write(line.toString()); } writer.flush(); } } private static void writeSxlsx(List
tables, OutputStream outputStream) throws IOException { SXSSFWorkbook wb = new SXSSFWorkbook(new XSSFWorkbook(), 1000); writeSpreadsheet(wb, tables, outputStream); } private static void writeXlsx(List
tables, OutputStream outputStream) throws IOException { writeSpreadsheet(new XSSFWorkbook(), tables, outputStream); } private static void writeXls(List
tables, OutputStream outputStream) throws IOException { writeSpreadsheet(new HSSFWorkbook(), tables, outputStream); } private static void writeSpreadsheet(Workbook workbook, List
tables, OutputStream outputStream) throws IOException { WorkbookUtil.newWorkbookUtil().toWorkBook(workbook, tables).write(outputStream); } private static void writeCsv(List
tables, OutputStream outputStream, String charset, String separator) throws IOException { if (tables == null || tables.isEmpty()) { throw new ExporterException("Invalid Table."); } CsvUtil.write(tables.get(0).toStringMatrix(), outputStream, charset, separator); } private static File createFile(String fileName) throws IOException { File originalFile = new File(fileName); if (originalFile.isAbsolute()) { return originalFile; } Path tempDir = Files.createTempDirectory("data-io-tmp"); File newFile = new File(tempDir.toFile(), fileName); if (!newFile.getParentFile().exists()) { newFile.mkdirs(); } return newFile; } public static File getCsvFile(Table t, String fileName, String charsetName, char separator) throws IOException { return getCsvFile(t, fileName, charsetName, String.valueOf(separator)); } public static File getCsvFile(Table t, String fileName, String charsetName, String separator) throws IOException { return writeDataToFile(Arrays.asList(t), FileType.CSV, fileName, charsetName, separator); } public static File getCsvFile(Table t, File f, String charsetName, char separator) throws IOException { return getCsvFile(t, f, charsetName, String.valueOf(separator)); } public static File getCsvFile(Table t, File f, String charsetName, String separator) throws IOException { try (BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(f))) { writeData(Arrays.asList(t), FileType.CSV, fos, charsetName, separator); } return f; } public static File getXlsFile(Table t, String file) throws IOException { return getMoreThanOneSheetXlsFile(Arrays.asList(t), file); } public static File getMoreThanOneSheetXlsFile(List
t, String file) throws IOException { return writeDataToFile(t, FileType.XLS, file); } public static File getXlsxFile(Table t, String file) throws IOException { return getMoreThanOneSheetXlsxFile(Arrays.asList(t), file); } public static File getSxlsxFile(Table t, String file) throws IOException { return getMoreThanOneSheetSxlsxFile(Arrays.asList(t), file); } public static File getMoreThanOneSheetXlsxFile(List
t, String file) throws IOException { return writeDataToFile(t, FileType.XLSX, file); } public static File getMoreThanOneSheetSxlsxFile(List
t, String file) throws IOException { return writeDataToFile(t, FileType.SXLSX, file); } public static void writeCsvToOutput(Table t, String chartsetName, OutputStream out) throws IOException { writeData(Arrays.asList(t), FileType.CSV, out, chartsetName, SEMICOLON.getSeparator()); } }