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

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

Introduction

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

Prototype

public CSVParser(final Reader reader, final CSVFormat format) throws IOException 

Source Link

Document

Customized CSV parser using the given CSVFormat

If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .

Usage

From source file:org.softinica.maven.jmeter.report.parser.SimpleCSVParser.java

@Override
public Input parseInput(InputDefinition definition) {
    CSVParser parser = null;/*from www.j a va 2  s. c  om*/
    List<String> headers = new LinkedList<String>();
    Input input = new Input();
    try {
        Reader reader = new InputStreamReader(new FileInputStream(definition.getInputFile()));
        parser = new CSVParser(reader, CSVFormat.DEFAULT);
        Iterator<CSVRecord> it = parser.iterator();
        if (it.hasNext()) {
            CSVRecord header = it.next();
            for (String value : header) {
                headers.add(value);
            }
            while (it.hasNext()) {
                Sample sample = new Sample();
                CSVRecord record = it.next();
                for (int i = 0; i < record.size(); i++) {
                    sample.put(headers.get(i), record.get(i));
                }
                input.getSamples().add(sample);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        Utils.close(parser);
    }
    return input;
}

From source file:org.sonar.server.source.index.SourceLineResultSetIterator.java

@Override
protected SourceFile read(ResultSet rs) throws SQLException {
    String projectUuid = rs.getString(1);
    String fileUuid = rs.getString(2);
    Long updatedAt = SqlUtil.getLong(rs, 3);
    if (updatedAt == null) {
        updatedAt = System.currentTimeMillis();
    }/*from  w  w  w  .jav a 2s.  co m*/
    Date updatedDate = new Date(updatedAt);
    SourceFile result = new SourceFile(fileUuid, updatedAt);

    Reader csv = rs.getCharacterStream(4);
    if (csv == null) {
        return result;
    }

    int line = 1;
    CSVParser csvParser = null;
    try {
        csvParser = new CSVParser(csv, CSVFormat.DEFAULT);

        for (CSVRecord csvRecord : csvParser) {
            SourceLineDoc doc = new SourceLineDoc(Maps.<String, Object>newHashMap());

            doc.setProjectUuid(projectUuid);
            doc.setFileUuid(fileUuid);
            doc.setLine(line);
            doc.setUpdateDate(updatedDate);
            doc.setScmRevision(csvRecord.get(0));
            doc.setScmAuthor(csvRecord.get(1));
            doc.setScmDate(DateUtils.parseDateTimeQuietly(csvRecord.get(2)));
            // UT
            doc.setUtLineHits(parseIntegerFromRecord(csvRecord.get(3)));
            doc.setUtConditions(parseIntegerFromRecord(csvRecord.get(4)));
            doc.setUtCoveredConditions(parseIntegerFromRecord(csvRecord.get(5)));
            // IT
            doc.setItLineHits(parseIntegerFromRecord(csvRecord.get(6)));
            doc.setItConditions(parseIntegerFromRecord(csvRecord.get(7)));
            doc.setItCoveredConditions(parseIntegerFromRecord(csvRecord.get(8)));
            // OVERALL
            doc.setOverallLineHits(parseIntegerFromRecord(csvRecord.get(9)));
            doc.setOverallConditions(parseIntegerFromRecord(csvRecord.get(10)));
            doc.setOverallCoveredConditions(parseIntegerFromRecord(csvRecord.get(11)));
            doc.setHighlighting(csvRecord.get(12));
            doc.setSymbols(csvRecord.get(13));

            doc.setDuplications(parseDuplications(csvRecord.get(14)));
            doc.setSource(csvRecord.get(csvRecord.size() - 1));

            result.addLine(doc);

            line++;
        }
    } catch (IOException ioError) {
        throw new IllegalStateException(
                "Impossible to open stream for file_sources.data with file_uuid " + fileUuid, ioError);
    } catch (ArrayIndexOutOfBoundsException lineError) {
        throw new IllegalStateException(
                String.format("Impossible to parse source line data, stuck at line %d", line), lineError);
    } finally {
        IOUtils.closeQuietly(csv);
        IOUtils.closeQuietly(csvParser);
    }

    return result;
}

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  w  w  w  .ja va2s  . co 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.csv.GetCsvHeadersFunction.java

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

    try {// w  w  w .  j a v  a 2 s.  c om

        assertArrayHasMinLengthAndMaxLengthAndAllElementsNotNull(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 "";

    } catch (IllegalArgumentException e) {

        logParameterError(caller, sources, e.getMessage(), 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 w  w w.j av  a 2 s  .  co  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.thingsboard.server.service.install.cql.CassandraDbHelper.java

public static void appendToEndOfLine(Path targetDumpFile, String toAppend) throws Exception {
    Path tmp = Files.createTempFile(null, null);
    try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(targetDumpFile), CSV_DUMP_FORMAT)) {
        try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(tmp), CSV_DUMP_FORMAT)) {
            csvParser.forEach(record -> {
                List<String> newRecord = new ArrayList<>();
                record.forEach(val -> newRecord.add(val));
                newRecord.add(toAppend);
                try {
                    csvPrinter.printRecord(newRecord);
                } catch (IOException e) {
                    throw new RuntimeException("Error appending to EOL", e);
                }/*from  www  .  j a v a 2 s. c om*/
            });
        }
    }
    Files.move(tmp, targetDumpFile, StandardCopyOption.REPLACE_EXISTING);
}

From source file:org.thingsboard.server.service.install.cql.CassandraDbHelper.java

public static void loadCf(KeyspaceMetadata ks, Session session, String cfName, String[] columns,
        Path sourceFile, boolean parseHeader) throws Exception {
    TableMetadata tableMetadata = ks.getTable(cfName);
    PreparedStatement prepared = session.prepare(createInsertStatement(cfName, columns));
    CSVFormat csvFormat = CSV_DUMP_FORMAT;
    if (parseHeader) {
        csvFormat = csvFormat.withFirstRecordAsHeader();
    } else {//from   w w w  .  j  av  a  2  s .  com
        csvFormat = CSV_DUMP_FORMAT.withHeader(columns);
    }
    try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(sourceFile), csvFormat)) {
        csvParser.forEach(record -> {
            BoundStatement boundStatement = prepared.bind();
            for (String column : columns) {
                setColumnValue(tableMetadata, column, record, boundStatement);
            }
            session.execute(boundStatement);
        });
    }
}

From source file:org.thingsboard.server.service.install.DatabaseHelper.java

public static void upgradeTo40_assignDashboards(Path dashboardsDump, DashboardService dashboardService,
        boolean sql) throws Exception {
    JavaType assignedCustomersType = objectMapper.getTypeFactory().constructCollectionType(HashSet.class,
            ShortCustomerInfo.class);
    try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(dashboardsDump),
            CSV_DUMP_FORMAT.withFirstRecordAsHeader())) {
        csvParser.forEach(record -> {
            String customerIdString = record.get(CUSTOMER_ID);
            String assignedCustomersString = record.get(ASSIGNED_CUSTOMERS);
            DashboardId dashboardId = new DashboardId(toUUID(record.get(ID), sql));
            List<CustomerId> customerIds = new ArrayList<>();
            if (!StringUtils.isEmpty(assignedCustomersString)) {
                try {
                    Set<ShortCustomerInfo> assignedCustomers = objectMapper.readValue(assignedCustomersString,
                            assignedCustomersType);
                    assignedCustomers.forEach((customerInfo) -> {
                        CustomerId customerId = customerInfo.getCustomerId();
                        if (!customerId.isNullUid()) {
                            customerIds.add(customerId);
                        }//from  w  ww  .j  a  va 2s .c  o  m
                    });
                } catch (IOException e) {
                    log.error("Unable to parse assigned customers field", e);
                }
            }
            if (!StringUtils.isEmpty(customerIdString)) {
                CustomerId customerId = new CustomerId(toUUID(customerIdString, sql));
                if (!customerId.isNullUid()) {
                    customerIds.add(customerId);
                }
            }
            for (CustomerId customerId : customerIds) {
                dashboardService.assignDashboardToCustomer(new TenantId(EntityId.NULL_UUID), dashboardId,
                        customerId);
            }
        });
    }
}

From source file:org.thingsboard.server.service.install.sql.SqlDbHelper.java

public static void loadTable(Connection conn, String tableName, String[] columns, Path sourceFile,
        boolean parseHeader) throws Exception {
    CSVFormat csvFormat = CSV_DUMP_FORMAT;
    if (parseHeader) {
        csvFormat = csvFormat.withFirstRecordAsHeader();
    } else {/*from w w  w  .  j  ava2s .c om*/
        csvFormat = CSV_DUMP_FORMAT.withHeader(columns);
    }
    try (PreparedStatement prepared = conn.prepareStatement(createInsertStatement(tableName, columns))) {
        try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(sourceFile), csvFormat)) {
            csvParser.forEach(record -> {
                try {
                    for (int i = 0; i < columns.length; i++) {
                        setColumnValue(i, columns[i], record, prepared);
                    }
                    prepared.execute();
                } catch (SQLException e) {
                    log.error("Unable to load table record!", e);
                }
            });
        }
    }
}

From source file:org.trustedanalytics.resourceserver.data.DataProvider.java

public List<List<String>> getParsedContent(String path) throws IllegalArgumentException, IOException {

    InputStream is = inputStreamProvider.getInputStream(new Path(path));

    List<List<String>> result = null;
    try (CSVParser parser = new CSVParser(new InputStreamReader(is), CSVFormat.newFormat(','))) {
        result = parser.getRecords().stream().map(row -> Lists.newArrayList(row.iterator()))
                .collect(Collectors.toList());
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Error parsing CSV file", e);
    }//from  w  ww  .  j av  a 2  s  . c  o  m

    return result;
}