use of org.apache.commons.csv.CSVRecord in project bootique-jdbc by bootique.
the class CsvReader method loadDataSet.
TableDataSet loadDataSet(Reader dataReader) throws IOException {
try (CSVParser parser = new CSVParser(dataReader, CSVFormat.DEFAULT, 0, 0)) {
Iterator<CSVRecord> rows = parser.iterator();
if (!rows.hasNext()) {
return new TableDataSet(table, Collections.emptyList(), Collections.emptyList());
}
List<Column> header = getHeader(rows.next());
return readData(header, rows);
}
}
use of org.apache.commons.csv.CSVRecord in project onebusaway-application-modules by camsys.
the class FixedRouteParserServiceImpl method parseFixedRouteReportFile.
/**
* Parses a FixedRouteDataValidation report file into a List of
* DataValidationMode objects.
*
* @param fixedRouteReportFile name of the file containing the report
* in csv format.
* @return a List of DataValidationMode objects containing the report data.
*/
@Override
public List<DataValidationMode> parseFixedRouteReportFile(File fixedRouteReportFile) {
List<DataValidationMode> parsedModes = new ArrayList<>();
if (fixedRouteReportFile != null && fixedRouteReportFile.exists()) {
DataValidationMode currentMode = null;
try {
Reader in = new FileReader(fixedRouteReportFile);
int i = 0;
for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
if (i == 0) {
i++;
// Skip the first record, which is just the column headers
continue;
}
// When the record being parsed is for a new mode, the parseRecord
// method will add the previous mode to the parsedModes List.
currentMode = parseRecord(record, currentMode, parsedModes);
i++;
}
} catch (FileNotFoundException e) {
_log.info("Exception parsing csv file " + fixedRouteReportFile, e);
e.printStackTrace();
} catch (IOException e) {
_log.info("Exception parsing csv file " + fixedRouteReportFile, e);
e.printStackTrace();
}
// Add in the last mode processed.
parsedModes.add(currentMode);
}
return parsedModes;
}
use of org.apache.commons.csv.CSVRecord 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.CSVRecord 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.CSVRecord in project bookish by parrt.
the class DataTable method parseCSV.
public void parseCSV(String csv) {
try {
Reader in = new StringReader(csv);
CSVFormat format = CSVFormat.EXCEL.withHeader();
CSVParser parser = format.parse(in);
this.firstColIsIndex = false;
for (CSVRecord record : parser) {
if (!firstColIsIndex && Character.isAlphabetic(record.get(0).charAt(0))) {
// latch if we see alpha not number
firstColIsIndex = true;
}
List<String> row = new ArrayList<>();
for (int i = 0; i < record.size(); i++) {
String v = record.get(i);
if (!NumberUtils.isDigits(v) && NumberUtils.isCreatable(v)) {
v = String.format("%.4f", Precision.round(Double.valueOf(v), 4));
} else {
v = abbrevString(v, 25);
}
row.add(v);
}
rows.add(row);
}
Set<String> colNames = parser.getHeaderMap().keySet();
if (!firstColIsIndex) {
// remove index column name
colNames.remove("");
}
this.colNames.addAll(colNames);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations