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

io.aubay.fase.maven.plugin.data.spi.CSVDataTransformer Maven / Gradle / Ivy

There is a newer version: 0.3.7
Show newest version
package io.aubay.fase.maven.plugin.data.spi;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

/*-
 * #%L
 * fase-maven-plugin Maven Plugin
 * %%
 * Copyright (C) 2018 - 2019 Aubay
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.opencsv.CSVIterator;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;

import io.aubay.fase.maven.plugin.FaseMojo;
import io.aubay.fase.maven.plugin.FaseRow;
import io.aubay.fase.maven.plugin.FaseTable.ColumnName;

public class CSVDataTransformer extends AbstractDataTransformer {

	private static final Logger LOG = LogManager.getLogger();

	public CSVDataTransformer() {
		super();
		super.setId("CSV");
	}

	@Override
	public boolean supportsFormat(String dataFileFormat) {
		return "CSV".equals(dataFileFormat);
	}

	@Override
	protected boolean validateFaseRow(FaseRow row) {
		File dataFile = new File(FaseMojo.getBasedir(), row.getValue(ColumnName.DATA_FILE));
		return validateFile(dataFile) && validateFormat(dataFile, row.getValue(ColumnName.DATA_FILE_TRANSFORMER_PARAMS));
	}

	@Override
	public List> transformRow(FaseRow row) {
		// no need to use validateFaseRow. It has been done by AbstractDataTransformer
		List> rows = new ArrayList<>();

		File dataFile = new File(FaseMojo.getBasedir(), row.getValue(ColumnName.DATA_FILE));

		try (FileReader fr = new FileReader(dataFile); CSVReader reader = getCSVReader(fr, row.getValue(ColumnName.DATA_FILE_TRANSFORMER_PARAMS))) {
			CSVIterator iterator = new CSVIterator(reader);

			String[] lineValues = null;
			while (iterator.hasNext()) {
				lineValues = iterator.next();
				rows.add(Arrays.asList(lineValues));
			}
		} catch (IOException e) {
			LOG.error("Parser error caused by IOException : dataFile='{}' ", dataFile.getAbsolutePath(), e);
		}

		return rows;
	}

	private boolean validateFile(File file) {
		if (!file.exists()) {
			LOG.error("file={} DOES NOT EXIST", file.getAbsolutePath());
			return false;
		}

		if (!file.canRead()) {
			LOG.error("file={} CANNOT BE READ", file.getAbsolutePath());
			return false;
		}
		return true;
	}

	private boolean validateFormat(File file, String dataFileTransformer) {
		boolean valid = false;
		try (FileReader fr = new FileReader(file); CSVReader reader = getCSVReader(fr, dataFileTransformer)) {
			CSVIterator iterator = new CSVIterator(reader);
			valid = iterator.hasNext();
		} catch (IOException e) {
			LOG.error("Parser error caused by IOException : dataFile='{}' ", file.getAbsolutePath(), e);
		}
		return valid;
	}

	private CSVReader getCSVReader(FileReader fr, String dataFileTransformerParams) {

		final CSVParser parser = new CSVParserBuilder()
				.withSeparator(this.getCSVSeparator(dataFileTransformerParams))
				.build();

		return new CSVReaderBuilder(fr)
				.withCSVParser(parser)
				.build();
		// return new CSVReader(fr);
	}
	
	private char getCSVSeparator(String dataFileTransformerParams) {
		if(dataFileTransformerParams==null) {
			return ',';
		} else {
			return dataFileTransformerParams.trim().charAt(0);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy