Example usage for org.apache.commons.csv CSVRecord size

List of usage examples for org.apache.commons.csv CSVRecord size

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of values in this record.

Usage

From source file:org.onebusaway.admin.service.bundle.impl.FixedRouteParserServiceImpl.java

/**
 * Parses a csv record representing one line of a FixedRouteDataValidation
 * report.  If the line is part of the mode currently being processed, it
 * is added to that DataValidationMode object. If it is for a new mode, the
 * current mode is added to the parsedModes list and the record being 
 * parsed becomes the new current mode./*from   w w w  . j av  a  2 s. c o m*/
 *
 * @param record the record to be parsed
 * @param currentMode the DataValidationMode currently being built
 * @param parsedModes the list of modes already created.
 * @return the DataValidationMode currently being built
 */
private DataValidationMode parseRecord(CSVRecord record, DataValidationMode currentMode,
        List<DataValidationMode> parsedModes) {
    if (record.size() < 8 || record.get(4).isEmpty() || !record.get(4).matches("^\\d+$")) { //Stop count should be numeric
        return currentMode;
    }
    // Create the StopCt for this line (every line should have a stop count)
    DataValidationStopCt currentStopCt = new DataValidationStopCt();
    currentStopCt.setStopCt(Integer.parseInt(record.get(4)));
    int[] stopCtTrips = { 0, 0, 0 };
    for (int i = 0; i < 3; i++) {
        try {
            int tripCt = Integer.parseInt(record.get(5 + i));
            stopCtTrips[i] = tripCt;
        } catch (NumberFormatException ex) {
            // Do nothing, leave array value at 0.
        }
    }
    currentStopCt.setTripCts(stopCtTrips);
    String modeName = record.get(0);
    String routeName = record.get(1);
    String headsign = record.get(2);
    String dirName = record.get(3);

    // If routeName is prefixed with the route number, extract the route number
    String routeNum = "";
    if (routeName.length() > 0) {
        int idx = routeName.substring(0, Math.min(5, routeName.length())).indexOf("-");
        if (idx > 0) {
            routeNum = routeName.substring(0, idx).trim();
            routeName = routeName.substring(idx + 1);
        }
    }

    if (modeName.length() > 0) { // new mode
        if (routeName.isEmpty()) {
            return currentMode; // this shouldn't happen.  Any line with a mode
                                // name should also have a route name.
        }
        if (currentMode != null) {
            parsedModes.add(currentMode);
        }
        currentMode = new DataValidationMode(modeName, routeNum, routeName, headsign, dirName);
        currentRoute = currentMode.getRoutes().first();
        currentHeadsign = currentRoute.getHeadsignCounts().first();
        currentDirection = currentHeadsign.getDirCounts().first();
        SortedSet<DataValidationStopCt> stopCountsList = currentDirection.getStopCounts();
        stopCountsList.add(currentStopCt);
    } else if (routeName.length() > 0) {
        // New route for current mode
        currentRoute = new DataValidationRouteCounts(routeNum, routeName, headsign, dirName);
        currentMode.getRoutes().add(currentRoute);
        currentHeadsign = currentRoute.getHeadsignCounts().first();
        currentDirection = currentHeadsign.getDirCounts().first();
        SortedSet<DataValidationStopCt> stopCountsList = currentDirection.getStopCounts();
        stopCountsList.add(currentStopCt);
    } else if (headsign.length() > 0) {
        currentHeadsign = new DataValidationHeadsignCts(headsign, dirName);
        currentRoute.getHeadsignCounts().add(currentHeadsign);
        currentDirection = currentHeadsign.getDirCounts().first();
        SortedSet<DataValidationStopCt> stopCountsList = currentDirection.getStopCounts();
        stopCountsList.add(currentStopCt);
    } else if (dirName.length() > 0) {
        currentDirection = new DataValidationDirectionCts(dirName);
        currentHeadsign.getDirCounts().add(currentDirection);
        SortedSet<DataValidationStopCt> stopCountsList = currentDirection.getStopCounts();
        stopCountsList.add(currentStopCt);
    } else if (dirName.isEmpty()) {
        SortedSet<DataValidationStopCt> stopCountsList = currentDirection.getStopCounts();
        stopCountsList.add(currentStopCt);
    }
    return currentMode;
}

From source file:org.onebusaway.admin.service.impl.BundleCheckParserServiceImpl.java

private BundleValidationParseResults parseRecord(CSVRecord record, BundleValidationParseResults parseResults) {
    // Verify that second field contains a valid test.
    if (record.size() < 2 || !validTests.contains(record.get(1).toLowerCase())) {
        BundleValidationParseError parseError = new BundleValidationParseError();
        parseError.setLinenum((int) record.getRecordNumber());
        parseError.setErrorMessage(PARSE_ERROR);
        parseError.setOffendingLine(record.toString());
        parseResults.getParseErrors().add(parseError);
        return parseResults;
    }//from ww  w  .j  av a 2 s. c o  m

    ParsedBundleValidationCheck parsedCheck = new ParsedBundleValidationCheck();
    parsedCheck.setLinenum((int) record.getRecordNumber());
    parsedCheck.setAgencyId(record.get(0));
    parsedCheck.setSpecificTest(record.get(1));
    if (record.get(2) != null) {
        parsedCheck.setRouteName(record.get(2));
    }
    if (record.get(3) != null) {
        parsedCheck.setRouteId(record.get(3));
    }
    if (record.get(4) != null) {
        parsedCheck.setStopName(record.get(4));
    }
    if (record.get(5) != null) {
        parsedCheck.setStopId(record.get(5));
    }
    if (record.get(6) != null) {
        parsedCheck.setDate(record.get(6));
    }
    if (record.get(7) != null) {
        parsedCheck.setDepartureTime(record.get(7));
    }
    parseResults.getParsedBundleChecks().add(parsedCheck);
    return parseResults;
}

From source file:org.phenotips.vocabulary.AbstractCSVAnnotationsExtension.java

/**
 * Helper method that gets the cell on the specified column, as string, if it exists, without throwing exceptions.
 *
 * @param row the {@link CSVRecord row} currently being processed
 * @param colNumber the number of the column of interest
 * @return the value on the target column, if such value exists, {@code null} otherwise
 */// ww w.ja  va2  s .c o m
protected String getRowItem(@Nonnull final CSVRecord row, final int colNumber) {
    if (colNumber < row.size()) {
        return row.get(colNumber);
    }
    return null;
}

From source file:org.seasr.meandre.components.transform.text.CSVTextToTokenCounts.java

@Override
public void executeCallBack(ComponentContext cc) throws Exception {
    Hashtable<String, Integer> htCounts = new Hashtable<String, Integer>();

    for (String text : DataTypeParser.parseAsString(cc.getDataComponentFromInput(IN_TEXT))) {
        //           boolean skippedHeader = false;
        //String[][] data = ... .getAllValues();
        //           CSVParser parser = new CSVParser(new StringReader(text), strategy); 
        //           CSVParser parser = new CSVParser(new StringReader(text), format); 
        //           String[] tokens = uninitialisedLine;
        //           while (tokens != null) {
        console.finer("received text:\n" + text + "\n");
        for (CSVRecord tokens : format.parse(new StringReader(text))) {
            //              tokens = parser.getLine();
            //              if (tokens == null) break;
            //               if (bHeader && !skippedHeader) {
            //                   skippedHeader = true;
            //                   continue;
            //               }
            //               String token = tokens[tokenPos];
            console.fine("processing row " + tokens.toString());
            if (tokens.size() <= tokenPos || tokens.size() <= countPos) {
                console.warning(//from  ww  w.  ja  v  a 2s.  c o m
                        String.format("csv row %d too short (%d) for count pos %d or token pos %d - discarding",
                                tokens.getRecordNumber(), tokens.size(), countPos, tokenPos));
                continue;
            }
            String token = tokens.get(tokenPos);
            int count = 0;
            try {
                count = Integer.parseInt(tokens.get(countPos));
            } catch (NumberFormatException e) {
                console.warning(String.format("Token '%s' had malformed count '%s' - assigning zero!", token,
                        tokens.get(countPos)));
            }

            if (htCounts.containsKey(token))
                console.warning(String.format(
                        "Token '%s' occurs more than once in the dataset - replacing previous count %d with %d...",
                        token, htCounts.get(token), count));

            htCounts.put(token, count);
        }
    }
    cc.pushDataComponentToOutput(OUT_TOKEN_COUNTS, BasicDataTypesTools.mapToIntegerMap(htCounts, bOrdered));
}

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

@Override
public Input parseInput(InputDefinition definition) {
    CSVParser parser = null;//from w w w. jav a 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.db.version.v51.FeedFileSourcesBinaryData.java

private byte[] toBinary(Long fileSourceId, @Nullable String data) {
    DbFileSources.Data.Builder dataBuilder = DbFileSources.Data.newBuilder();
    CSVParser parser = null;//from w  w  w .j  a v a2 s.co  m
    try {
        if (data != null) {
            parser = CSVParser.parse(data, CSVFormat.DEFAULT);
            Iterator<CSVRecord> rows = parser.iterator();
            int line = 1;
            while (rows.hasNext()) {
                CSVRecord row = rows.next();
                if (row.size() == 16) {

                    DbFileSources.Line.Builder lineBuilder = dataBuilder.addLinesBuilder();
                    lineBuilder.setLine(line);
                    String s = row.get(0);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setScmRevision(s);
                    }
                    s = row.get(1);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setScmAuthor(s);
                    }
                    Date scmDate = DateUtils.parseDateTimeQuietly(row.get(2));
                    if (scmDate != null) {
                        lineBuilder.setScmDate(scmDate.getTime());
                    }
                    s = row.get(3);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setUtLineHits(Integer.parseInt(s));
                    }
                    s = row.get(4);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setUtConditions(Integer.parseInt(s));
                    }
                    s = row.get(5);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setUtCoveredConditions(Integer.parseInt(s));
                    }
                    s = row.get(6);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setItLineHits(Integer.parseInt(s));
                    }
                    s = row.get(7);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setItConditions(Integer.parseInt(s));
                    }
                    s = row.get(8);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setItCoveredConditions(Integer.parseInt(s));
                    }
                    s = row.get(9);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setOverallLineHits(Integer.parseInt(s));
                    }
                    s = row.get(10);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setOverallConditions(Integer.parseInt(s));
                    }
                    s = row.get(11);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setOverallCoveredConditions(Integer.parseInt(s));
                    }
                    s = row.get(12);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setHighlighting(s);
                    }
                    s = row.get(13);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setSymbols(s);
                    }
                    s = row.get(14);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.addAllDuplication(splitIntegers(s));
                    }
                    s = row.get(15);
                    if (s != null) {
                        lineBuilder.setSource(s);
                    }
                }
                line++;
            }
        }
        return FileSourceDto.encodeSourceData(dataBuilder.build());
    } catch (Exception e) {
        throw new IllegalStateException(
                "Invalid FILE_SOURCES.DATA on row with ID " + fileSourceId + ": " + data, e);
    } finally {
        IOUtils.closeQuietly(parser);
    }
}

From source file:org.sonar.server.db.migrations.v51.FeedFileSourcesBinaryData.java

private byte[] toBinary(Long fileSourceId, @Nullable String data) {
    FileSourceDb.Data.Builder dataBuilder = FileSourceDb.Data.newBuilder();
    CSVParser parser = null;//from w  ww . j a v  a 2 s.c o m
    try {
        if (data != null) {
            parser = CSVParser.parse(data, CSVFormat.DEFAULT);
            Iterator<CSVRecord> rows = parser.iterator();
            int line = 1;
            while (rows.hasNext()) {
                CSVRecord row = rows.next();
                if (row.size() == 16) {

                    FileSourceDb.Line.Builder lineBuilder = dataBuilder.addLinesBuilder();
                    lineBuilder.setLine(line);
                    String s = row.get(0);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setScmRevision(s);
                    }
                    s = row.get(1);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setScmAuthor(s);
                    }
                    s = row.get(2);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setScmDate(DateUtils.parseDateTimeQuietly(s).getTime());
                    }
                    s = row.get(3);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setUtLineHits(Integer.parseInt(s));
                    }
                    s = row.get(4);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setUtConditions(Integer.parseInt(s));
                    }
                    s = row.get(5);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setUtCoveredConditions(Integer.parseInt(s));
                    }
                    s = row.get(6);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setItLineHits(Integer.parseInt(s));
                    }
                    s = row.get(7);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setItConditions(Integer.parseInt(s));
                    }
                    s = row.get(8);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setItCoveredConditions(Integer.parseInt(s));
                    }
                    s = row.get(9);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setOverallLineHits(Integer.parseInt(s));
                    }
                    s = row.get(10);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setOverallConditions(Integer.parseInt(s));
                    }
                    s = row.get(11);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setOverallCoveredConditions(Integer.parseInt(s));
                    }
                    s = row.get(12);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setHighlighting(s);
                    }
                    s = row.get(13);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.setSymbols(s);
                    }
                    s = row.get(14);
                    if (StringUtils.isNotEmpty(s)) {
                        lineBuilder.addAllDuplication(splitIntegers(s));
                    }
                    s = row.get(15);
                    if (s != null) {
                        lineBuilder.setSource(s);
                    }
                }
                line++;
            }
        }
        return FileSourceDto.encodeSourceData(dataBuilder.build());
    } catch (Exception e) {
        throw new IllegalStateException(
                "Invalid FILE_SOURCES.DATA on row with ID " + fileSourceId + ": " + data, e);
    } finally {
        IOUtils.closeQuietly(parser);
    }
}

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();
    }//  www.ja  va 2 s . c  o  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.talend.components.localio.runtime.fixed.FixedDatasetRuntime.java

@Override
public Schema getSchema() {
    switch (properties.format.getValue()) {
    case CSV://from   w w  w.  j a v  a2  s .c  o m
        // Try to get the schema from the specified value.
        String csvSchema = properties.csvSchema.getValue();
        if (!csvSchema.trim().isEmpty()) {
            try {
                CSVRecord r = CSVFormat.RFC4180 //
                        .withDelimiter(properties.getFieldDelimiter().charAt(0)) //
                        .withRecordSeparator(properties.getRecordDelimiter()) //
                        .parse(new StringReader(csvSchema)).iterator().next();
                return CsvRecordToIndexedRecordConverter.inferSchema(r);
            } catch (Exception e) {
                throw LocalIOErrorCode.createCannotParseSchema(e, csvSchema);
            }
        }
        // Fall back to a schema based on the number of columns.
        try {
            int maxSize = 0;
            for (CSVRecord r : CSVFormat.RFC4180 //
                    .withDelimiter(properties.getFieldDelimiter().charAt(0)) //
                    .withRecordSeparator(properties.getRecordDelimiter())
                    .parse(new StringReader(properties.values.getValue())))
                maxSize = Math.max(maxSize, r.size());
            if (maxSize == 0)
                throw LocalIOErrorCode.requireAtLeastOneRecord(new RuntimeException());
            return CsvRecordToIndexedRecordConverter.inferSchema(maxSize);
        } catch (IOException e) {
            throw LocalIOErrorCode.createCannotParseSchema(e, properties.values.getValue());
        }
    case JSON:
        if (properties.values.getValue().trim().isEmpty())
            throw LocalIOErrorCode.requireAtLeastOneRecord(new RuntimeException());
        return getValues(1).get(0).getSchema();
    case AVRO:
        try {
            return new Schema.Parser().parse(properties.schema.getValue());
        } catch (Exception e) {
            throw LocalIOErrorCode.createCannotParseSchema(e, properties.schema.getValue());
        }
    }
    throw LocalIOErrorCode.createCannotParseSchema(null, properties.schema.getValue());
}

From source file:org.talend.components.simplefileio.runtime.SimpleFileIOAvroRegistry.java

/**
 * Infers an Avro schema for the given String array. This can be an expensive operation so the schema should be
 * cached where possible. This is always an {@link Schema.Type#RECORD}.
 *
 * @param in the DescribeSObjectResult to analyse.
 * @return the schema for data given from the object.
 *///ww w  .j av  a 2 s . c o m
private Schema inferCsvRecord(CSVRecord in) {
    List<Schema.Field> fields = new ArrayList<>();

    SchemaBuilder.FieldAssembler<Schema> fa = SchemaBuilder.record(RECORD_NAME).fields();
    for (int i = 0; i < in.size(); i++) {
        fa = fa.name(FIELD_PREFIX + i).type(Schema.create(Schema.Type.STRING)).noDefault();
    }
    return fa.endRecord();
}