use of au.com.bytecode.opencsv.CSVReader in project molgenis by molgenis.
the class OmimRepository method getEntitiesByGeneSymbol.
private Map<String, List<Entity>> getEntitiesByGeneSymbol() {
if (entitiesByGeneSymbol == null) {
Map<String, List<List<String>>> omimEntriesByGeneSymbol = new HashMap<>();
entitiesByGeneSymbol = new LinkedHashMap<>();
try (CSVReader csvReader = new CSVReader(new InputStreamReader(new FileInputStream(file), forName("UTF-8")), SEPARATOR, DEFAULT_QUOTE_CHARACTER, 1)) {
String[] values = csvReader.readNext();
while (values != null) {
addLineToMap(omimEntriesByGeneSymbol, values);
values = csvReader.readNext();
}
for (String geneSymbol : omimEntriesByGeneSymbol.keySet()) {
addEntityToGeneEntityList(omimEntriesByGeneSymbol, geneSymbol);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return entitiesByGeneSymbol;
}
use of au.com.bytecode.opencsv.CSVReader in project ma-modules-public by infiniteautomation.
the class DataImportController method parseFile.
/* (non-Javadoc)
* @see com.serotonin.m2m2.web.mvc.controller.FileUploadController#parseFile(java.io.InputStream, java.util.Map, com.serotonin.m2m2.i18n.Translations, javax.servlet.http.HttpServletRequest)
*/
@Override
protected void parseFile(InputStream input, Map<String, Object> model, Translations translations, HttpServletRequest request) {
// Setup for error tracking
List<String> errorMessages = new ArrayList<String>();
model.put("errorMessages", errorMessages);
CSVReader csvReader = new CSVReader(new InputStreamReader(input));
try {
importCsv(csvReader, model, translations, errorMessages);
} catch (Exception e) {
if (e instanceof TranslatableException)
errorMessages.add(((TranslatableException) e).getTranslatableMessage().translate(translations));
else
errorMessages.add(e.getMessage());
} finally {
model.put("hasImportErrors", errorMessages.size() > 0);
try {
csvReader.close();
} catch (IOException e) {
errorMessages.add(e.getMessage());
}
}
}
use of au.com.bytecode.opencsv.CSVReader in project cu-kfs by CU-CommunityApps.
the class CsvBatchInputFileTypeBase method validateCSVFileInput.
/**
* Validates the CSV file content against the CSVEnum items as header
* 1. content header must match CSVEnum order/value
* 2. each data row should have same size as the header
*
* @param expectedHeaderList expected CSV header String list
* @param fileContents contents to validate
*
* @throws IOException
*/
private void validateCSVFileInput(final List<String> expectedHeaderList, InputStream fileContents) throws IOException {
// use csv reader to parse the csv content
CSVReader csvReader = new CSVReader(new InputStreamReader(fileContents));
List<String> inputHeaderList = Arrays.asList(csvReader.readNext());
String errorMessage = null;
// validate
if (!CollectionUtils.isEqualCollection(expectedHeaderList, inputHeaderList)) {
errorMessage = "CSV Batch Input File contains incorrect number of headers";
// collection has same elements, now check the exact content orders by looking at the toString comparisons
} else if (!expectedHeaderList.equals(inputHeaderList)) {
errorMessage = "CSV Batch Input File headers are different";
} else {
// check the content size as well if headers are validated
int line = 1;
List<String> inputDataList = null;
while ((inputDataList = Arrays.asList(csvReader.readNext())) != null && errorMessage != null) {
// if the data list size does not match header list (its missing data)
if (inputDataList.size() != expectedHeaderList.size()) {
errorMessage = "line " + line + " layout does not match the header";
}
line++;
}
}
if (errorMessage != null) {
LOG.error(errorMessage);
throw new RuntimeException(errorMessage);
}
}
use of au.com.bytecode.opencsv.CSVReader in project incubator-sdap-mudrod by apache.
the class DataGenerator method parseFile.
/**
* Parses the original user-specified CSV file, storing the contents for future calculations
* and formatting.
*/
public static void parseFile() {
String[][] dataArr = null;
try {
String sourceDir = mySourceDir;
if (// Case where multiple files have to be processed
isMultFiles) {
// Iterate over files in directory
File directory = new File(sourceDir);
File[] directoryListing = directory.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
CSVReader csvReader = new CSVReader(new FileReader(child));
List<String[]> list = csvReader.readAll();
// Store into 2D array by transforming array list to normal array
dataArr = new String[list.size()][];
dataArr = list.toArray(dataArr);
calculateVec(dataArr);
csvReader.close();
}
// Store the header
storeHead(dataArr);
}
} else // Process only one file
{
File file = new File(sourceDir);
if (file != null) {
CSVReader csvReader = new CSVReader(new FileReader(file));
List<String[]> list = csvReader.readAll();
// Store into 2D array by transforming array list to normal array
dataArr = new String[list.size()][];
dataArr = list.toArray(dataArr);
// Store the header
storeHead(dataArr);
calculateVec(dataArr);
csvReader.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
use of au.com.bytecode.opencsv.CSVReader in project dkpro-tc by dkpro.
the class TcFlexTable method getCsvReader.
public StreamReader getCsvReader() {
return new StreamReader() {
@Override
public void read(InputStream aStream) throws IOException {
try {
CSVReader reader = new CSVReader(new InputStreamReader(aStream, "UTF-8"));
String[] headers = reader.readNext();
Method converter = TcFlexTable.this.dataClass.getMethod("valueOf", String.class);
String[] data;
while ((data = reader.readNext()) != null) {
Map<String, V> row = new LinkedHashMap<String, V>();
for (int i = 1; i < headers.length; i++) {
@SuppressWarnings("unchecked") V value = (V) converter.invoke(null, data[i]);
row.put(headers[i], value);
}
addRow(data[0], row);
}
reader.close();
} catch (IOException e) {
throw e;
} catch (NoSuchMethodException e) {
throw new IOException("Data class " + TcFlexTable.this.dataClass.getName() + " does not have a " + "public static Object valueOf(String) method - unable unmarshall the " + "data.");
} catch (Exception e) {
throw new IOException(e);
}
}
};
}
Aggregations