Commit 2e352f15 authored by Matija Obreza's avatar Matija Obreza
Browse files

Added support to specify CSV separator, quote and escape characters (fixes #15)

parent abce00d4
......@@ -34,9 +34,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Level;
......@@ -50,6 +47,9 @@ import org.genesys2.gringlobal.taxonomy.model.SpeciesRow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
/**
* Validates your CSV!
*/
......@@ -76,6 +76,9 @@ public class GGTC {
private InMemoryTaxonomyDatabase taxonomyDatabase = new CachingInMemoryTaxonomyDatabase();
private TaxonomyChecker taxonomyChecker = new TaxonomyChecker();
private char separatorChar = ',';
private char quoteChar = 0;
private char escapeChar = 0;
public static void main(String[] args) {
GGTC ggtc = new GGTC();
......@@ -90,12 +93,26 @@ public class GGTC {
System.exit(-1);
}
for (String arg : args) {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("-v".equals(arg)) {
// Increase logging level
org.apache.log4j.Logger logger = LogManager.getLogger("org.genesys2");
logger.setLevel(nextLevel(logger.getLevel()));
LOG.warn("Increasing loglevel to {}", logger.getLevel());
} else if ("-csv".equals(arg)) {
char[] csv = args[++i].toCharArray();
LOG.warn("Setting CSV chars to {}", csv);
for (int j = 0; j < csv.length; j++) {
if (j == 0)
ggtc.separatorChar = csv[0];
if (j == 1)
ggtc.quoteChar = csv[1];
if (j == 2)
ggtc.escapeChar = csv[2];
}
} else {
// Files
if (sourceName == null) {
......@@ -171,6 +188,7 @@ public class GGTC {
System.out.println("<outputFile> File name or - to write CSV to STDOUT");
System.out.println("-v Increase log level.");
System.out.println("-v Increase log level.");
System.out.println("-csv ',\"\\' Set input CSV separator, quote and escape chars");
System.out.println("\nThe program writes log messages to STDERR.");
}
......@@ -200,7 +218,7 @@ public class GGTC {
taxonomyChecker.setTaxonomyDatabase(taxonomyDatabase);
LOG.info("Running the validation against {}", taxonomyDatabase);
try (CSVReader reader = new CSVReader(new InputStreamReader(inputStream, "UTF-8"), ',', (char) 0, (char) 0, 0, false, true, false)) {
try (CSVReader reader = new CSVReader(new InputStreamReader(inputStream, "UTF-8"), this.separatorChar, this.quoteChar, this.escapeChar, 0, false, true, false)) {
String[] headers = reader.readNext();
LOG.debug("Input CSV headers: {}", Arrays.toString(headers));
......@@ -293,7 +311,7 @@ public class GGTC {
if (outputHeaders.indexOf(HEADER_GENUS_CHECK) >= 0) {
String genus = sourceLine[sourceMapping.get(HEADER_GENUS)];
String genus_check = taxonomyChecker.suggestGenus(genus, 5).stream().reduce(null, (prev, suggestion) -> prev == null ? suggestion : prev + ";" + suggestion);
if (LOG.isTraceEnabled())
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment