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:de.upb.wdqa.wdvd.revisiontags.TagDownloader.java

/**
 * Reads one line of the csv file of the TagDownloader
 *//*w  ww.jav  a2s. c o  m*/
private static void parseRecord(CSVRecord record) {
    long revisionId = Long.parseLong(record.get(0)); // revision id
    String sha1Base16 = record.get(1); // sha1

    //record.get(2); // size

    Set<DbTag> tags = new HashSet<DbTag>();
    for (int i = 3; i < record.size(); i++) {
        String tagName = record.get(i);
        tags.add(tagFactory.getTag(tagName));
        tagDistribution.addValue(tagName);
    }

    try {
        dataStore.putRevision(new DbRevisionImpl(revisionId, SHA1Converter.base16to36(sha1Base16), tags));
    } catch (Exception e) {
        logger.error("", e);
    }
}

From source file:net.sourceforge.ganttproject.io.GanttCSVOpen.java

private static RecordGroup createResourceRecordGroup(final HumanResourceManager resourceManager) {
    return resourceManager == null ? null
            : new RecordGroup(Sets.newHashSet(getFieldNames(ResourceFields.values()))) {
                @Override// w  ww  . j  a v  a 2s . c om
                public void setHeader(List<String> header) {
                    super.setHeader(header);
                    createCustomProperties(getCustomFields(), resourceManager.getCustomPropertyManager());
                }

                @Override
                protected void process(CSVRecord record) {
                    assert record.size() > 0;
                    HumanResource hr = resourceManager.newResourceBuilder()
                            .withName(record.get(ResourceFields.NAME.toString()))
                            .withID(record.get(ResourceFields.ID.toString()))
                            .withEmail(record.get(ResourceFields.EMAIL.toString()))
                            .withPhone(record.get(ResourceFields.PHONE.toString()))
                            .withRole(record.get(ResourceFields.ROLE.toString())).build();
                    for (String customField : getCustomFields()) {
                        String value = record.get(customField);
                        if (value != null) {
                            hr.addCustomProperty(resourceManager.getCustomPropertyManager()
                                    .getCustomPropertyDefinition(customField), value);
                        }
                    }
                }
            };
}

From source file:net.sourceforge.ganttproject.io.GanttCSVOpen.java

private static RecordGroup createTaskRecordGroup(final TaskManager taskManager,
        final HumanResourceManager resourceManager) {
    return new RecordGroup(Sets.newHashSet(getFieldNames(TaskFields.values()))) {
        private Map<Task, String> myAssignmentMap = Maps.newHashMap();
        private Map<Task, String> myPredecessorMap = Maps.newHashMap();
        private Map<String, Task> myTaskIdMap = Maps.newHashMap();

        @Override//from w  ww .j av  a 2s .  c om
        public void setHeader(List<String> header) {
            super.setHeader(header);
            createCustomProperties(getCustomFields(), taskManager.getCustomPropertyManager());
        }

        @Override
        protected void process(CSVRecord record) {
            assert record.size() > 0;
            // Create the task
            TaskManager.TaskBuilder builder = taskManager.newTaskBuilder()
                    .withName(record.get(TaskFields.NAME.toString()))
                    .withStartDate(language.parseDate(record.get(TaskFields.BEGIN_DATE.toString())))
                    .withWebLink(record.get(TaskFields.WEB_LINK.toString()))
                    .withNotes(record.get(TaskFields.NOTES.toString()));
            if (record.get(TaskDefaultColumn.DURATION.getName()) != null) {
                builder = builder.withDuration(
                        taskManager.createLength(record.get(TaskDefaultColumn.DURATION.getName())));
            }
            if (Objects.equal(record.get(TaskFields.BEGIN_DATE.toString()),
                    record.get(TaskFields.END_DATE.toString()))
                    && "0".equals(record.get(TaskDefaultColumn.DURATION.getName()))) {
                builder = builder.withLegacyMilestone();
            }
            if (!Strings.isNullOrEmpty(record.get(TaskFields.COMPLETION.toString()))) {
                builder = builder
                        .withCompletion(Integer.parseInt(record.get(TaskFields.COMPLETION.toString())));
            }
            Task task = builder.build();

            if (record.get(TaskDefaultColumn.ID.getName()) != null) {
                myTaskIdMap.put(record.get(TaskDefaultColumn.ID.getName()), task);
            }
            myAssignmentMap.put(task, record.get(TaskFields.RESOURCES.toString()));
            myPredecessorMap.put(task, record.get(TaskDefaultColumn.PREDECESSORS.getName()));
            for (String customField : getCustomFields()) {
                String value = record.get(customField);
                CustomPropertyDefinition def = taskManager.getCustomPropertyManager()
                        .getCustomPropertyDefinition(customField);
                if (def == null) {
                    GPLogger.logToLogger(
                            "Can't find custom field with name=" + customField + " value=" + value);
                    continue;
                }
                if (value != null) {
                    task.getCustomValues().addCustomProperty(def, value);
                }
            }
        }

        @Override
        protected void postProcess() {
            if (resourceManager != null) {
                Map<String, HumanResource> resourceMap = Maps.uniqueIndex(resourceManager.getResources(),
                        new Function<HumanResource, String>() {
                            @Override
                            public String apply(HumanResource input) {
                                return input.getName();
                            }
                        });
                for (Entry<Task, String> assignment : myAssignmentMap.entrySet()) {
                    String[] names = assignment.getValue().split(";");
                    for (String name : names) {
                        HumanResource resource = resourceMap.get(name);
                        if (resource != null) {
                            assignment.getKey().getAssignmentCollection().addAssignment(resource);
                        }
                    }
                }
            }
            for (Entry<Task, String> predecessor : myPredecessorMap.entrySet()) {
                if (predecessor.getValue() == null) {
                    continue;
                }
                String[] ids = predecessor.getValue().split(";");
                for (String id : ids) {
                    Task dependee = myTaskIdMap.get(id);
                    if (dependee != null) {
                        try {
                            taskManager.getDependencyCollection().createDependency(predecessor.getKey(),
                                    dependee);
                        } catch (TaskDependencyException e) {
                            GPLogger.logToLogger(e);
                        }
                    }
                }
            }
        }
    };
}

From source file:com.house365.build.util.CsvUtil.java

/**
 * ?CSV.//from   w w w .  j  a va  2  s.  co m
 *
 * @param channelsFile
 * @param headerRow
 * @param isPrint      ??..
 * @return ?Csv?
 * @throws FileNotFoundException
 */
public static ArrayList<LinkedHashMap<String, String>> readCsvChannels(File channelsFile, int headerRow,
        boolean isPrint) throws Exception {
    try {
        ArrayList<LinkedHashMap<String, String>> csvContent = new ArrayList<>();
        LinkedHashMap<String, String> rowContent;
        ArrayList<String> headerNames = null;
        if (headerRow > 0) {
            String[] csvHeader = getCsvHeader(channelsFile, headerRow);
            List<String> list = Arrays.asList(csvHeader);
            headerNames = new ArrayList<>(list);
        }
        AutoDetectReader reader = null;
        try {
            reader = new AutoDetectReader(new FileInputStream(channelsFile));
            Iterable<CSVRecord> csvRecords = CSVFormat.EXCEL.parse(reader);
            int i = 0;
            for (CSVRecord record : csvRecords) {
                i++;
                if (i <= headerRow) {
                    continue;
                }
                if (headerNames == null) {
                    headerNames = new ArrayList<>();
                    for (i = 0; i < record.size(); i++) {
                        headerNames.add(i + "");
                    }
                }
                rowContent = new LinkedHashMap<>();
                for (i = 0; i < record.size(); i++) {
                    rowContent.put(headerNames.get(i), record.get(i));
                }
                csvContent.add(rowContent);
            }
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (isPrint) {
            printlnCsv(csvContent);
        }
        return csvContent;
    } catch (IOException e) {
        throw e;
    } catch (TikaException e) {
        throw e;
    }
}

From source file:it.incipict.tool.profiles.util.ProfilesToolUtils.java

public static List<Survey> parseSurvey(File file) throws ProfilesToolException {
    List<Survey> surveys = new ArrayList<Survey>();

    Iterable<CSVRecord> records;
    try {/* w ww . j  a  v  a  2s  .  co m*/
        Reader in = new FileReader(file.getCanonicalPath());
        records = CSVFormat.RFC4180.parse(in);
    } catch (IOException e) {
        throw new ProfilesToolException("error while reading files.", e);
    }

    // loop all surveys in CSV line by line
    // note: a single CSV can contain more surveys
    for (CSVRecord record : records) {
        int line = (int) record.getRecordNumber();
        // skip the first line because it simply contains the headers
        if (line > 1) {
            Survey survey = new Survey();
            List<String> answers = new ArrayList<String>();
            List<String> textualAnswers = new ArrayList<String>();
            // in the Survey model we can define a "RECORD OFFSET" by which
            // the parser must start
            for (int i = survey.RECORD_OFFSET; i < record.size(); i++) {
                String r = record.get(i);

                // if the "r" is empty the user has not responded
                // skip to the next answers
                if (r.isEmpty()) {
                    continue;
                }
                // if "r" isn't a numerical value and it is different than
                // ZERO_STRING (Ref. Survey model)
                // Note: the ZERO_STRING is defined in Survey Class. Its
                // value is Zero.
                if ((!r.matches("\\d*") && r.compareTo(survey.ZERO_STRING) != 0)) {
                    // Otherwise the answer is added to a list of Strings.
                    // this list will appended in queue. (*)
                    String[] str = r.split(";");
                    for (String s : str) {
                        textualAnswers.add("\"" + s + "\"");
                    }
                }
                // if "r" is a numeric value, simply add it in the answers
                // list.
                if (r.compareTo(survey.ZERO_STRING) == 0) {
                    answers.add("0");
                }
                if (r.matches("\\d*")) {
                    answers.add(r);
                }
            }
            // add the textual answers in queue (*)
            answers.addAll(textualAnswers);

            String fname = file.getName();
            survey.setProfile(fname.substring(0, fname.lastIndexOf('.')));
            survey.setAnswers(answers);
            surveys.add(survey);
        }
    }
    return (surveys != null) ? surveys : null;
}

From source file:com.esri.geoevent.datastore.CSVParsingTest.java

@Test
public void testSimpleCSVParse() throws Exception {
    StringReader csvServers = new StringReader("mingz14,getest1w");
    Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(csvServers);
    for (CSVRecord record : records) {
        int size = record.size();
        for (int i = 0; i < size; i++) {
            System.out.println(record.get(i));
        }//from  w ww  .  j  a v  a 2 s.c om
    }
}

From source file:com.univocity.articles.csvcomparison.parser.CommonsCsvParser.java

@Override
public List<String[]> parseRows(File input) throws Exception {
    CSVFormat format = CSVFormat.RFC4180;
    CSVParser parser = CSVParser.parse(input, getEncoding(), format);

    List<String[]> rows = new ArrayList<String[]>();

    for (CSVRecord record : parser) {
        String[] row = new String[record.size()];
        for (int i = 0; i < row.length; i++) {
            row[i] = record.get(i);//from   w  w  w .ja v  a  2 s.c o  m
        }
        rows.add(row);
    }
    return rows;
}

From source file:com.siemens.sw360.importer.ImportCSVTest.java

@Test
public void testReadAsCSVRecords() throws Exception {
    assertEquals(NUMBER_OF_LINES, records.size());
    for (CSVRecord record : records) {
        assertEquals(NUMBER_OF_COLUMNS, record.size());
    }/*  ww  w. j  av a 2  s . co  m*/
}

From source file:com.github.jferard.pgloaderutils.sniffer.csd.CSDValidatorHelper.java

/**
 * @param result the result that will hold the errors
 * @param fields the schema / schema pattern
 * @param record the current record of the stream
 * @param line the current line number/*from w w  w  .  ja va  2  s .  c o  m*/
 * @return the error count, -1 the schema has too many fields
 */
public int validateRecord(CSDValidationResult<F> result, SizedIterable<F> fields, CSVRecord record, int line) {
    if (record.size() < fields.size()) {
        result.schemaHasTooManyFieldsForRecord(line, record);
        return -1;
    }

    int errorCount = 0;
    int j = 0;
    for (F field : fields) {
        String value = record.get(j++);
        if (!field.validate(value)) {
            result.incorrectValue(line, value, field);
            errorCount++;
        }
    }
    return errorCount;
}

From source file:edu.emory.cci.aiw.i2b2etl.ksb.ValueSetSupport.java

void parseId(String id) throws ParseException {
    try (CSVParser csvParser = CSVParser.parse(id, CSV_FORMAT)) {
        List<CSVRecord> records = csvParser.getRecords();
        if (records.size() != 1) {
            return;
        }/*from w  ww.  jav  a2 s  .com*/
        CSVRecord record = records.get(0);
        if (record.size() != 2) {
            return;
        }
        this.declaringPropId = record.get(0);
        this.propertyName = record.get(1);
    } catch (IOException invalid) {
        throw new ParseException("Invalid value set id " + id, 0);
    }
}