use of org.apache.commons.csv.CSVFormat in project solr-cmd-utils by tblsoft.
the class BaseBlacklistFilter method init.
@Override
public void init() {
topicValues = new HashMap<String, HashSet<String>>();
InputStream in = null;
try {
String filename = getProperty("filename", null);
String absoluteFilename = IOUtils.getAbsoluteFile(getBaseDir(), filename);
in = IOUtils.getInputStream(absoluteFilename);
java.io.Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8.name());
CSVFormat format = CSVFormat.RFC4180.withHeader().withDelimiter(',');
CSVParser parser = format.parse(reader);
Iterator<CSVRecord> csvIterator = parser.iterator();
while (csvIterator.hasNext()) {
CSVRecord record = csvIterator.next();
String topic = record.get(0);
String value = record.get(1);
if (!topicValues.containsKey(topic)) {
topicValues.put(topic, new HashSet<String>());
}
topicValues.get(topic).add(value);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
}
}
}
fieldTopic = getProperty("fieldTopic", null);
fieldValue = getProperty("fieldValue", null);
super.init();
}
use of org.apache.commons.csv.CSVFormat in project solr-cmd-utils by tblsoft.
the class CSVWriter method document.
@Override
public void document(Document document) {
if (firstDocument) {
try {
if (headers == null) {
headers = getFieldNames(document);
}
// PrintWriter out = new PrintWriter(absoluteFilename);
OutputStream out = IOUtils.getOutputStream(absoluteFilename);
CSVFormat format = CSVFormat.RFC4180;
if (withHeaders) {
format = format.withDelimiter(delimiter.charAt(0)).withHeader(headers);
} else {
format = format.withDelimiter(delimiter.charAt(0));
}
Writer out1 = new BufferedWriter(new OutputStreamWriter(out));
printer = new CSVPrinter(out1, format);
firstDocument = false;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
try {
List<List<String>> csvRows = new ArrayList<List<String>>();
List<String> csvList = new ArrayList<String>();
for (String headerField : headers) {
Field field = document.getField(headerField);
String value = null;
if (field != null && field.getValues() != null) {
value = Joiner.on(multiValueSeperator).skipNulls().join(field.getValues());
}
csvList.add(value);
}
csvRows.add(csvList);
printer.printRecords(csvRows);
} catch (IOException e) {
throw new RuntimeException(e);
}
super.document(document);
}
use of org.apache.commons.csv.CSVFormat in project LogHub by fbacchella.
the class FileMap method mapFromCsv.
private Map<Object, Object> mapFromCsv() {
try (Reader in = new FileReader(mappingFile)) {
CSVFormat format;
switch(csvFormat.toUpperCase()) {
case "EXCEL":
format = CSVFormat.EXCEL;
break;
case "RFC4180":
format = CSVFormat.RFC4180;
break;
case "TDF":
format = CSVFormat.TDF;
break;
case "DEFAULT":
format = CSVFormat.DEFAULT;
break;
default:
logger.error("Unknown CSV format name");
return null;
}
boolean withHeaders;
if (key != null && value != null) {
format = format.withFirstRecordAsHeader();
withHeaders = true;
} else if (keyColumn > 0 && valueColumn > 0) {
format = format.withSkipHeaderRecord(false);
withHeaders = false;
} else {
logger.error("Neither column name or number defined");
return null;
}
Iterable<CSVRecord> records = format.parse(in);
Map<Object, Object> mapping = new HashMap<>();
for (CSVRecord record : records) {
if (withHeaders) {
mapping.put(record.get(key), record.get(value));
} else {
mapping.put(record.get(keyColumn), record.get(valueColumn));
}
}
return mapping;
} catch (IOException e) {
logger.error("Can't read mapping file {}", mappingFile);
return null;
}
}
use of org.apache.commons.csv.CSVFormat in project structr by structr.
the class FromCsvFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 5)) {
try {
final List<Map<String, String>> objects = new LinkedList<>();
final String source = sources[0].toString();
String delimiter = ";";
String quoteChar = "\"";
String recordSeparator = "\n";
boolean customColumnNamesSupplied = false;
switch(sources.length) {
case 5:
customColumnNamesSupplied = (sources[4] instanceof Collection);
case 4:
recordSeparator = (String) sources[3];
case 3:
quoteChar = (String) sources[2];
case 2:
delimiter = (String) sources[1];
break;
}
CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0));
if (customColumnNamesSupplied) {
format = format.withHeader(((Collection<String>) sources[4]).toArray(new String[] {})).withSkipHeaderRecord(false);
} else {
format = format.withHeader().withSkipHeaderRecord(true);
}
format = format.withQuote(quoteChar.charAt(0));
format = format.withRecordSeparator(recordSeparator);
format = format.withIgnoreEmptyLines(true);
format = format.withIgnoreSurroundingSpaces(true);
format = format.withQuoteMode(QuoteMode.ALL);
CSVParser parser = new CSVParser(new StringReader(source), format);
for (final CSVRecord record : parser.getRecords()) {
objects.add(record.toMap());
}
return objects;
} catch (Throwable t) {
logException(t, "{}: Exception for parameter: {}", new Object[] { getName(), getParametersAsString(sources) });
}
return "";
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return usage(ctx.isJavaScriptContext());
}
use of org.apache.commons.csv.CSVFormat in project structr by structr.
the class GetCsvHeadersFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) {
if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 4)) {
try {
final String source = sources[0].toString();
String delimiter = ";";
String quoteChar = "\"";
String recordSeparator = "\n";
switch(sources.length) {
case 4:
recordSeparator = (String) sources[3];
case 3:
quoteChar = (String) sources[2];
case 2:
delimiter = (String) sources[1];
break;
}
CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0)).withHeader();
if (quoteChar.length() > 0) {
format = format.withQuote(quoteChar.charAt(0));
} else {
format = format.withQuote(null);
}
format = format.withRecordSeparator(recordSeparator);
format = format.withIgnoreEmptyLines(true);
format = format.withIgnoreSurroundingSpaces(true);
format = format.withQuoteMode(QuoteMode.ALL);
try (final CSVParser parser = new CSVParser(new StringReader(source), format)) {
return parser.getHeaderMap().keySet();
}
} catch (Throwable t) {
logException(t, "{}: Exception for parameter: {}", new Object[] { getName(), getParametersAsString(sources) });
}
return "";
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return usage(ctx.isJavaScriptContext());
}
Aggregations