Example usage for org.apache.commons.csv CSVParser getRecords

List of usage examples for org.apache.commons.csv CSVParser getRecords

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVParser getRecords.

Prototype

public List<CSVRecord> getRecords() throws IOException 

Source Link

Document

Parses the CSV input according to the given format and returns the content as a list of CSVRecord CSVRecords .

Usage

From source file:org.shareok.data.documentProcessor.CsvHandler.java

/**
 * Reads out the data in an excel file and stores data in a hashmap
 * <p>Also sets the total record number and file heading</p>
 * //from  www  .  j a  va 2 s  .c  o m
 * @throws Exception
 */
@Override
public void readData() {
    FileReader fileReader = null;
    CSVParser csvFileParser = null;
    String[] headMapping = null;
    //CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING);

    try {
        //initialize FileReader object
        fileReader = new FileReader(fileName);

        //initialize CSVParser object
        if (null == csvFormat) {
            csvFormat = CSVFormat.DEFAULT;
        }
        csvFileParser = new CSVParser(fileReader, csvFormat);

        //Get a list of CSV file records
        List csvRecords = csvFileParser.getRecords();

        int size = csvRecords.size();

        setRecordCount(size);

        data = new HashMap();

        //Read the CSV file records starting from the second record to skip the header
        for (int i = 0; i < size; i++) {
            CSVRecord record = (CSVRecord) csvRecords.get(i);
            if (null != record) {
                if (i == 0) {
                    List headMappingList = new ArrayList();
                    Iterator it = record.iterator();
                    while (it.hasNext()) {
                        String value = (String) it.next();
                        headMappingList.add(value);
                    }
                    headMapping = new String[headMappingList.size()];
                    headMapping = (String[]) headMappingList.toArray(headMapping);
                    setFileHeadMapping(headMapping);
                } else {
                    for (int j = 0; j < fileHeadMapping.length; j++) {
                        String colName = fileHeadMapping[j].trim();
                        String key = colName + "-" + i;
                        data.put(key, record.get(j));
                    }
                }
            }
        }

    } catch (Exception e) {
        System.out.println("Error in CsvFileReader !!!");
        e.printStackTrace();
    } finally {
        try {
            fileReader.close();
            csvFileParser.close();
        } catch (IOException e) {
            System.out.println("Error while closing fileReader/csvFileParser !!!");
            e.printStackTrace();
        }
    }
}

From source file:org.structr.csv.FromCsvFunction.java

@Override
public Object apply(ActionContext ctx, final GraphObject entity, final Object[] sources) {

    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 4)) {

        try {//from   ww  w.ja va2s  . c o m

            final List<Map<String, String>> objects = new LinkedList<>();
            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();
            format = format.withQuote(quoteChar.charAt(0));
            format = format.withRecordSeparator(recordSeparator);
            format = format.withIgnoreEmptyLines(true);
            format = format.withIgnoreSurroundingSpaces(true);
            format = format.withSkipHeaderRecord(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, "{0}: Exception for parameter: {1}",
                    new Object[] { getName(), getParametersAsString(sources) });

        }

        return "";

    } else {

        logParameterError(entity, sources, ctx.isJavaScriptContext());

    }

    return usage(ctx.isJavaScriptContext());
}

From source file:org.structr.function.FromCsvFunction.java

@Override
public Object apply(ActionContext ctx, final GraphObject entity, final Object[] sources) {

    if (sources != null && sources.length > 0) {

        if (sources[0] != null) {

            try {

                final List<Map<String, String>> objects = new LinkedList<>();
                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;
                }/*from   ww w.j ava2s  . c  o m*/

                CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0)).withHeader();
                format = format.withQuote(quoteChar.charAt(0));
                format = format.withRecordSeparator(recordSeparator);
                format = format.withIgnoreEmptyLines(true);
                format = format.withIgnoreSurroundingSpaces(true);
                format = format.withSkipHeaderRecord(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) {
                t.printStackTrace();
            }
        }

        return "";
    }

    return usage(ctx.isJavaScriptContext());
}

From source file:org.thegalactic.context.io.ContextSerializerCsv.java

/**
 * Read a context from a csv file./*from  w  w w.  j a  v a 2s  .  com*/
 *
 * The following format is respected:
 *
 * The first line contains the attribute names, the other lines contains the
 * observations identifier followed by boolean values
 *
 * ~~~
 * "",a,b,c,d,e
 * 1,1,0,1,0,0
 * 2,1,1,0,0,0
 * 3,0,1,0,1,1
 * 4,0,0,1,0,1
 * ~~~
 *
 * If the first attribute is the empty string, the first column corresponds
 * to the individual identifiers. In the other case, the individual
 * identifiers will be generated by successive integers.
 *
 * ~~~
 * a,b,c,d,e
 * 1,0,1,0,0
 * 1,1,0,0,0
 * 0,1,0,1,1
 * 0,0,1,0,1
 * ~~~
 *
 * @param context a context to read
 * @param file    a file
 *
 * @throws IOException When an IOException occurs
 */
public void read(Context context, BufferedReader file) throws IOException {
    // Parse the file
    CSVParser parser = CSVFormat.RFC4180.parse(file);

    // Get the records and record size
    List<CSVRecord> records = parser.getRecords();
    int length = records.size();

    // Verify length
    if (length == 0) {
        throw new IOException("CSV cannot be empty");
    }

    // Get the attributes and the attribute size
    CSVRecord attributes = records.get(0);
    int size = attributes.size();

    // Detect invalid attribute size
    if (size == 1 && attributes.get(0).equals("")) {
        throw new IOException("Attribute size cannot be 0");
    }

    // Index of the first attribute
    int first = 0;
    if (attributes.get(0).equals("")) {
        first = 1;
    }

    // Get the attributes
    for (int i = first; i < size; i++) {
        String attribute = attributes.get(i);

        // Detect duplicated attribute
        if (!context.addToAttributes(attribute)) {
            throw new IOException("Duplicated attribute");
        }

        // Detect empty attribute
        if ("".equals(attribute)) {
            throw new IOException("Empty attribute");
        }
    }

    // Get the data
    for (int j = 1; j < length; j++) {
        // Get the current record
        CSVRecord record = records.get(j);

        // Detect incorrect size
        if (record.size() != size) {
            throw new IOException("Line does not have the correct number of attributes");
        }

        // Get the observation identifier
        String identifier;
        if (first == 1) {
            identifier = record.get(0);
        } else {
            identifier = String.valueOf(j);
        }

        // Detect duplicated identifier
        if (!context.addToObservations(identifier)) {
            throw new IOException("Duplicated identifier");
        }

        // Add the extent/intent for the current identifier and current attribute
        for (int i = first; i < size; i++) {
            if (record.get(i).equals("1")) {
                context.addExtentIntent(identifier, attributes.get(i));
            }
        }
    }

    // Close the parser
    parser.close();
    context.setBitSets();
}

From source file:poe.trade.assist.Main.java

private List<CSVRecord> loadCSVRaw() {
    List<CSVRecord> records = null;
    try {/*from w  ww . j  av a  2 s.  c  om*/
        String searchFileFromTextField = StringUtils.trimToEmpty(searchFileTextField.getText());
        if (searchFileFromTextField.isEmpty()
                || searchFileFromTextField.equalsIgnoreCase(LOCAL_SEARCH_FILE_NAME)) {
            File file = getSearchFile();
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                CSVParser csvParser = CSVFormat.RFC4180.withHeader().parse(br);
                records = csvParser.getRecords();
            }
        } else {
            String url = searchFileFromTextField;
            if (url.contains("google") && url.contains("/edit")) {
                // handle google spreadsheet url that is not an export url
                // https://docs.google.com/spreadsheets/d/1V8r0mIn5njpmVYwFWpqnptAMI6udrIaqhCby1i79UGw/edit#gid=0
                url = StringUtils.substringBeforeLast(url, "/edit");
                url += "/export?gid=0&format=csv";
            }
            try (BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream()))) {
                CSVParser csvParser = CSVFormat.RFC4180.withHeader().parse(br);
                records = csvParser.getRecords();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        Dialogs.showError(e);
    }
    return records;
}

From source file:test.com.xceptance.xlt.common.actions.LWSimpleURLTest.java

/**
 * Create records from string//from   w w w .  j av  a 2s  .  c o  m
 */
private List<CSVRecord> createRecords(final String... records) throws IOException {
    final StringBuilder fullRecord = new StringBuilder();

    for (final String record : records) {
        fullRecord.append(record.replace("{url}", testUrl));
        fullRecord.append("\n");
    }

    final CSVFormat csvFormat = CSVFormat.RFC4180.toBuilder().withIgnoreEmptyLines(true).withCommentStart('#')
            .withHeader().build();
    final CSVParser parser = new CSVParser(fullRecord.toString(), csvFormat);

    return parser.getRecords();
}

From source file:test.com.xceptance.xlt.common.util.AbstractCSVBasedURLAction_Test.java

/**
 * Create records from string/* w w w.  j  ava2 s . c  o  m*/
 */
protected List<CSVRecord> createRecords(final String... records) throws IOException {
    final StringBuilder fullRecord = new StringBuilder();

    for (final String record : records) {
        fullRecord.append(record);
        fullRecord.append("\n");
    }

    final CSVFormat csvFormat = CSVFormat.RFC4180.toBuilder().withIgnoreEmptyLines(true).withCommentStart('#')
            .withHeader().build();
    final CSVParser parser = new CSVParser(fullRecord.toString(), csvFormat);

    return parser.getRecords();
}

From source file:test.com.xceptance.xlt.common.util.CSVBasedURLActionTest.java

/**
 * Create records from string/*w w  w .  j  a v  a  2 s  .c  o  m*/
 */
private List<CSVRecord> createRecords(final String... records) throws IOException {
    final StringBuilder fullRecord = new StringBuilder();

    for (final String record : records) {
        fullRecord.append(record);
        fullRecord.append("\n");
    }

    final CSVFormat csvFormat = CSVFormat.RFC4180.toBuilder().withIgnoreEmptyLines(true).withCommentStart('#')
            .withHeader().build();
    final CSVParser parser = new CSVParser(fullRecord.toString(), csvFormat);

    return parser.getRecords();
}

From source file:trainer.userinput.TrainingFileDB.java

public static UserInputTrainingRecord parseLine(String line) throws IOException {
    CSVParser lineParser = CSVParser.parse(line, TrainingFileDB.getCSVFormat());
    List<CSVRecord> csvRecords = lineParser.getRecords();
    UserInputTrainingRecord retVal = null;
    for (CSVRecord record : csvRecords) {
        retVal = new UserInputTrainingRecord(record.get(0), record.get(1));
    }//  w w w  .  j  a v a2  s. c o m
    return retVal;
}

From source file:umich.ms.batmass.filesupport.files.types.mzrt.model.MzrtFile.java

public void load() throws DataLoadingException {

    int[] counts = new int[3]; // [0] - \r\n, [1] - \n, [2] - \r
    final String[] separators = { "\r\n", "\n", "\r" };
    // detecting line separator
    try (InputStreamReader isr = new InputStreamReader(
            new BufferedInputStream(new FileInputStream(file.toFile())), Charsets.UTF_8)) {
        int c;//from   w ww.  j ava  2  s  .c  o  m
        int encountered = 0;
        boolean isPrevR = false;
        int cutoff = 50;
        readLoop: while ((c = isr.read()) != -1) {
            char ch = (char) c;
            switch (ch) {
            case '\r':
                if (++counts[2] > cutoff) {
                    break readLoop;
                }
                isPrevR = true;
                break;
            case '\n':
                if (isPrevR) {
                    counts[2]--;
                    if (++counts[0] > cutoff) {
                        break readLoop;
                    }
                } else {
                    if (++counts[1] > cutoff) {
                        break readLoop;
                    }
                }
                isPrevR = false;
                break;
            default:
                isPrevR = false;
            }
        }
    } catch (IOException ex) {
        throw new DataLoadingException("Could not detect line separator", ex);
    }

    List<Integer> idxMax = new ArrayList<>();
    for (int i = 0; i < counts.length; i++) {
        if (idxMax.isEmpty()) {
            idxMax.add(i);
        } else if (counts[i] > counts[idxMax.get(0)]) {
            idxMax.clear();
            idxMax.add(i);
        } else if (counts[i] == counts[idxMax.get(0)]) {
            idxMax.add(i);
        }
    }

    String recordSeparator;
    if (idxMax.size() > 1) {
        if (idxMax.contains(0)) {
            recordSeparator = separators[0];
        } else if (idxMax.contains(1)) {
            recordSeparator = separators[1];
        } else {
            recordSeparator = separators[idxMax.get(0)];
        }
    } else {
        recordSeparator = separators[idxMax.get(0)];
    }

    // detecting delimiter
    char delimiter;
    try (BufferedReader br = new BufferedReader(new FileReader(file.toFile()))) {
        List<String> lines = new ArrayList<>();
        String line;
        int numTestLines = 10;
        while ((line = br.readLine()) != null) {
            if (!line.isEmpty()) {
                lines.add(line);
                if (lines.size() >= numTestLines)
                    break;
            }
        }

        delimiter = guessDelimiter(lines);
    } catch (IOException ex) {
        throw new DataLoadingException("Could not detect delimiter character", ex);
    }

    try (BufferedReader br = new BufferedReader(new FileReader(file.toFile()))) {
        CSVFormat fmt = CSVFormat.newFormat(delimiter);
        fmt = fmt.withHeader().withIgnoreEmptyLines(true).withTrim(true).withIgnoreHeaderCase(true)
                .withQuoteMode(QuoteMode.NON_NUMERIC).withRecordSeparator(recordSeparator).withQuote('"');

        CSVParser parser = fmt.parse(br);

        records = parser.getRecords();
        header = parser.getHeaderMap();

        String[] colNames = { HEAD_MZLO, HEAD_MZHI, HEAD_RTLO, HEAD_RTHI };
        for (int i = 0; i < colNames.length; i++) {
            Integer index = header.get(colNames[i]);
            if (index == null)
                throw new DataLoadingException(String.format("Missing header column [%s]", colNames[i]));
            indexesMzRtColorOpacity[i] = index;
        }
        Integer indexColor = header.get(HEAD_COLOR);
        if (indexColor != null && indexColor >= 0)
            indexesMzRtColorOpacity[4] = indexColor;
        Integer indexOpacity = header.get(HEAD_OPACITY);
        if (indexOpacity != null && indexOpacity >= 0)
            indexesMzRtColorOpacity[5] = indexOpacity;

    } catch (IOException ex) {
        throw new DataLoadingException(ex);
    }
}