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

apoc.load.util.LoadCsvConfig Maven / Gradle / Ivy

There is a newer version: 5.25.1
Show newest version
package apoc.load.util;

import apoc.load.Mapping;
import apoc.util.CompressionConfig;
import apoc.util.Util;

import java.util.*;

import static apoc.util.Util.parseCharFromConfig;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;

public class LoadCsvConfig extends CompressionConfig {

    public static final char DEFAULT_ARRAY_SEP = ';';
    public static final char DEFAULT_SEP = ',';
    public static final char DEFAULT_QUOTE_CHAR = '"';
    // this is the same value as ICSVParser.DEFAULT_ESCAPE_CHARACTER
    public static final char DEFAULT_ESCAPE_CHAR = '\\';

    private final boolean ignoreErrors;
    private char separator;
    private char arraySep;
    private char quoteChar;
    private char escapeChar;
    private long skip;
    private boolean hasHeader;
    private long limit;

    private boolean failOnError;
    private boolean ignoreQuotations;

    private EnumSet results;

    private List ignore;
    private List nullValues;
    private Map> mapping;
    private Map mappings;

    public LoadCsvConfig(Map config) {
        super(config);
        if (config == null) {
            config = Collections.emptyMap();
        }
        ignoreErrors = Util.toBoolean(config.getOrDefault("ignoreErrors", false));
        separator = parseCharFromConfig(config, "sep", DEFAULT_SEP);
        arraySep = parseCharFromConfig(config, "arraySep", DEFAULT_ARRAY_SEP);
        quoteChar = parseCharFromConfig(config,"quoteChar", DEFAULT_QUOTE_CHAR);
        escapeChar = parseCharFromConfig(config,"escapeChar", DEFAULT_ESCAPE_CHAR);
        long skip = (long) config.getOrDefault("skip", 0L);
        this.skip = skip > -1 ? skip : 0L;
        hasHeader = (boolean) config.getOrDefault("header", true);
        limit = (long) config.getOrDefault("limit", Long.MAX_VALUE);
        failOnError = (boolean) config.getOrDefault("failOnError", true);
        ignoreQuotations = (boolean) config.getOrDefault("ignoreQuotations", false);

        results = EnumSet.noneOf(Results.class);
        List resultList = (List) config.getOrDefault("results", asList("map","list"));
        for (String result : resultList) {
            results.add(Results.valueOf(result));
        }

        ignore = (List) config.getOrDefault("ignore", emptyList());
        nullValues = (List) config.getOrDefault("nullValues", emptyList());
        mapping = (Map>) config.getOrDefault("mapping", Collections.emptyMap());
        mappings = createMapping(mapping, arraySep, ignore);
    }

    private Map createMapping(Map> mapping, char arraySep, List ignore) {
        if (mapping.isEmpty()) return Collections.emptyMap();
        HashMap result = new HashMap<>(mapping.size());
        for (Map.Entry> entry : mapping.entrySet()) {
            String name = entry.getKey();
            result.put(name, new Mapping(name, entry.getValue(), arraySep, ignore.contains(name)));
        }
        return result;
    }

    public char getSeparator() {
        return separator;
    }

    public long getSkip() {
        return skip;
    }

    public boolean isHasHeader() {
        return hasHeader;
    }

    public long getLimit() {
        return limit;
    }

    public boolean isFailOnError() {
        return failOnError;
    }

    public EnumSet getResults() {
        return results;
    }

    public List getIgnore() {
        return ignore;
    }

    public List getNullValues() {
        return nullValues;
    }

    public Map getMappings() {
        return mappings;
    }

    public char getQuoteChar() {
        return quoteChar;
    }

    public char getEscapeChar() {
        return escapeChar;
    }

    public boolean getIgnoreErrors() {
        return ignoreErrors;
    }

    public boolean isIgnoreQuotations() {
        return ignoreQuotations;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy