Search in sources :

Example 46 with CSVReader

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;
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Arrays.asList(java.util.Arrays.asList)

Example 47 with CSVReader

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());
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) TranslatableException(com.serotonin.m2m2.i18n.TranslatableException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TranslatableException(com.serotonin.m2m2.i18n.TranslatableException) IOException(java.io.IOException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Example 48 with CSVReader

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 49 with CSVReader

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();
    }
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader)

Example 50 with CSVReader

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);
            }
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) IOException(java.io.IOException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) StreamReader(org.dkpro.lab.storage.StreamReader) InputStreamReader(java.io.InputStreamReader)

Aggregations

CSVReader (au.com.bytecode.opencsv.CSVReader)82 IOException (java.io.IOException)29 InputStreamReader (java.io.InputStreamReader)27 ArrayList (java.util.ArrayList)16 FileReader (java.io.FileReader)11 StringReader (java.io.StringReader)11 HashMap (java.util.HashMap)9 BufferedReader (java.io.BufferedReader)8 InputStream (java.io.InputStream)6 File (java.io.File)5 Reader (java.io.Reader)5 HttpClient (org.apache.commons.httpclient.HttpClient)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 Test (org.junit.Test)5 DBException (org.jkiss.dbeaver.DBException)4 Query (au.org.ala.spatial.util.Query)3 TransformationExample (eu.esdihumboldt.cst.test.TransformationExample)3 Date (java.util.Date)3 LinkedHashMap (java.util.LinkedHashMap)3 JSONArray (org.json.simple.JSONArray)3