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

org.opencompare.api.java.io.CSVLoader Maven / Gradle / Ivy

package org.opencompare.api.java.io;

import com.opencsv.CSVReader;
import org.opencompare.api.java.*;
import org.opencompare.api.java.util.MatrixAnalyser;
import org.opencompare.api.java.util.MatrixComparatorEqualityImpl;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gbecan on 4/2/15.
 */
public class CSVLoader implements PCMLoader {

    private PCMFactory factory;
    private char separator;
    private char quote;
    private boolean productsAsLines;
    private Map features;

    public CSVLoader(PCMFactory factory) {
        this(factory, ',', '"', true);
    }

    public CSVLoader(PCMFactory factory, char separator) {
        this(factory, separator, '"', true);
    }

    public CSVLoader(PCMFactory factory, char separator, char quote) {
        this(factory, separator, quote, true);
    }

    public CSVLoader(PCMFactory factory, boolean productsAsLines) {
        this(factory, ',', '"', productsAsLines);
    }

    public CSVLoader(PCMFactory factory, char separator, char quote, boolean productsAsLines) {
        this.factory = factory;
        this.separator = separator;
        this.quote = quote;
        this.productsAsLines = productsAsLines;
    }

    public static IOMatrix createMatrix(CSVReader reader) throws IOException {
        List csvMatrix = reader.readAll();
        IOMatrix matrix = new IOMatrix();
        for (int i = 0; i < csvMatrix.size();i++) {
            for (int j = 0; j < csvMatrix.get(i).length;j++) {
                String content = csvMatrix.get(i)[j];
                IOCell cell = new IOCell(content);
                matrix.setCell(cell, i, j, 1, 1);
            }
        }
        return matrix;
    }

    @Override
    public List load(String pcm) {
        List containers = new ArrayList<>();
        try {
            containers = load(new StringReader(pcm));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return containers;
    }

    @Override
    public List load(File file) throws IOException {
        List containers = new ArrayList<>();
        try {
            containers = load(new FileReader(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return containers;
    }

    public List load(IOMatrix matrix) {
        return new IOMatrixLoader(this.factory, this.productsAsLines).load(matrix);
    }

    public List load(Reader reader) throws IOException {
        CSVReader csvReader = new CSVReader(reader, separator, quote);
        IOMatrix matrix = createMatrix(csvReader);
        return load(matrix);
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy