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

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

Introduction

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

Prototype

public String get(final String name) 

Source Link

Document

Returns a value by name.

Usage

From source file:org.logstash.dependencies.ReportGenerator.java

private void readLicenseMapping(InputStream stream, Map<String, LicenseUrlPair> licenseMapping)
        throws IOException {
    Reader in = new InputStreamReader(stream);
    for (CSVRecord record : CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in)) {
        String dependencyNameAndVersion = record.get(0);
        if (dependencyNameAndVersion != null && !dependencyNameAndVersion.equals("")) {
            licenseMapping.put(dependencyNameAndVersion, new LicenseUrlPair(record.get(2), record.get(1)));
        }/*from w w  w .ja  v  a2  s  . c o  m*/
    }
}

From source file:org.mercycorps.translationcards.txcmaker.GetTxcServlet.java

private void produceTxcJson(Drive drive, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    TxcPortingUtility.ExportSpec exportSpec = new TxcPortingUtility.ExportSpec()
            .setDeckLabel(req.getParameter("deckName")).setPublisher(req.getParameter("publisher"))
            .setDeckId(req.getParameter("deckId")).setLicenseUrl(req.getParameter("licenseUrl"))
            .setLocked(req.getParameter("locked") != null);
    String spreadsheetFileId = req.getParameter("docId");
    Drive.Files.Export sheetExport = drive.files().export(spreadsheetFileId, CSV_EXPORT_TYPE);
    Reader reader = new InputStreamReader(sheetExport.executeMediaAsInputStream());
    CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader());
    try {/*  ww w . java  2s .co m*/
        for (CSVRecord row : parser) {
            String language = row.get(SRC_HEADER_LANGUAGE);
            TxcPortingUtility.CardSpec card = new TxcPortingUtility.CardSpec()
                    .setLabel(row.get(SRC_HEADER_LABEL)).setFilename(row.get(SRC_HEADER_FILENAME))
                    .setTranslationText(row.get(SRC_HEADER_TRANSLATION_TEXT));
            exportSpec.addCard(language, card);
        }
    } finally {
        parser.close();
        reader.close();
    }
    resp.getWriter().println(TxcPortingUtility.buildTxcJson(exportSpec));
}

From source file:org.mitre.ptmatchadapter.fril.RecordMatchResultsBuilder.java

/**
 *
 * @param bundle/*  w  w  w.  j  a va2s  . c om*/
 *          Bundle to which an entry for each linked record will be added
 * @throws IOException
 *           thrown when the file containing the linked results is not found
 *           or could not be processed
 */
private void addLinkedRecordEntries(Bundle bundle) throws IOException {
    if (duplicatesFile != null) {
        final Reader in = new FileReader(duplicatesFile);
        try {
            final Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);

            String refRecordUrl = null;
            String curDupId = "0";

            // see https://www.hl7.org/fhir/valueset-patient-mpi-match.html
            final CodeType certain = new CodeType("certain");
            final CodeType probable = new CodeType("probable");
            final CodeType possible = new CodeType("possible");
            final CodeType certainlyNot = new CodeType("certainly-not");

            for (CSVRecord record : records) {
                String duplicateId = record.get(DUPLICATE_ID_COL);
                String scoreStr = record.get(SCORE_COL);
                String fullUrl = record.get(FULL_URL_COL);

                if (curDupId.equals(duplicateId)) {
                    if (refRecordUrl == null) {
                        LOG.warn("Unexpected condition, curDupId {}, duplicateId {}", curDupId, duplicateId);
                        continue;
                    }

                    BundleEntryComponent entry = new BundleEntryComponent();
                    entry.setFullUrl(refRecordUrl);

                    BundleEntrySearchComponent search = new BundleEntrySearchComponent();
                    // fril returns results 0 - 100; normalize to 0 - 1;
                    double score = Double.valueOf(scoreStr).doubleValue() / 100.;
                    search.setScoreElement(new DecimalType(score));

                    // TODO Add Extension that maps score value to a term (e.g.,
                    // probable)
                    Extension searchExt = new Extension(
                            new UriType("http://hl7.org/fhir/StructureDefinition/patient-mpi-match"));
                    if (score > 0.85) {
                        searchExt.setValue(certain);
                    } else if (score > 0.65) {
                        searchExt.setValue(probable);
                    } else if (score > .45) {
                        searchExt.setValue(possible);
                    } else {
                        searchExt.setValue(certainlyNot);
                    }
                    search.addExtension(searchExt);
                    entry.setSearch(search);

                    // Add information about the resource type

                    BundleLinkComponent link = new BundleLinkComponent(new StringType("type"),
                            new UriType("http://hl7.org/fhir/Patient"));
                    entry.addLink(link);

                    // Add the link to the duplicate record
                    link = new BundleLinkComponent(new StringType("related"), new UriType(fullUrl));
                    entry.addLink(link);

                    bundle.addEntry(entry);
                } else {
                    // new set of duplicates
                    curDupId = duplicateId;
                    refRecordUrl = fullUrl;
                }
            }
        } finally {
            in.close();
        }
    }
}

From source file:org.n52.wps.csv2wiki.CSV2TWikiProcess.java

private String transformWithParser(CSVParser parser) throws IOException {
    StringBuilder sb = new StringBuilder();

    boolean header = true;
    for (CSVRecord h : parser.getRecords()) {
        for (int i = 0; i < h.size(); i++) {
            if (header) {
                sb.append("| *");
                sb.append(h.get(i));
                sb.append("* ");
            } else {
                sb.append("| ");
                sb.append(h.get(i));// w w  w .  java2s  .c  o  m
                sb.append(" ");
            }

        }
        sb.append("|");
        sb.append(System.getProperty("line.separator"));
        header = false;
    }

    return sb.toString();
}

From source file:org.neo4art.colour.manager.VanGoghArtworksColourAnalysisDefaultManager.java

/**
 * @see org.neo4art.colour.manager.VanGoghArtworksColourAnalysisManager#loadArtworksFromFile(java.lang.String)
 */// ww  w .  j  a  v a  2  s  .  com
@Override
public List<Artwork> loadArtworksFromFile(String fileName) throws IOException {
    List<Artwork> artworks = null;

    URL url = getClass().getClassLoader().getResource(fileName);

    CSVParser csvParser = CSVParser.parse(url, Charset.defaultCharset(),
            CSVFormat.EXCEL.withIgnoreSurroundingSpaces(true));

    List<CSVRecord> cvsRecords = csvParser.getRecords();

    if (CollectionUtils.isNotEmpty(cvsRecords) && CollectionUtils.size(cvsRecords) > 1) {
        artworks = new ArrayList<Artwork>();

        for (int i = 1; i < cvsRecords.size(); i++) {
            CSVRecord csvRecord = cvsRecords.get(i);

            Artwork artwork = new Artwork();

            Calendar completionDate = Calendar.getInstance();
            completionDate.set(Calendar.YEAR, Integer.parseInt(csvRecord.get(2)));
            completionDate.set(Calendar.MONTH, Integer.parseInt(csvRecord.get(3)));

            artwork.setTitle(csvRecord.get(0));
            artwork.setType(csvRecord.get(1));
            artwork.setYear(new Date(Integer.parseInt(csvRecord.get(2)), Calendar.JANUARY, 1));
            artwork.setCompletionDate(completionDate.getTime());
            artwork.setImageFile(csvRecord.get(4));
            artwork.setCatalogue("F: " + csvRecord.get(5) + ", JH: " + csvRecord.get(6));

            artworks.add(artwork);
        }
    }

    return artworks;
}

From source file:org.neo4art.core.service.ArtistsArtworkCatalogTest.java

@Test
public void shouldSaveColours() {
    try {/*from   w w w . j  av a2  s  . c  o  m*/
        URL url = getClass().getClassLoader().getResource("artists-artworks-catalog.csv");
        CSVParser csvParser = CSVParser.parse(url, Charset.forName("ISO-8859-1"), CSVFormat.EXCEL);

        List<CSVRecord> records = csvParser.getRecords();

        if (CollectionUtils.isNotEmpty(records)) {
            // AUTHOR;BORN-DIED;TITLE;DATE;TECHNIQUE;LOCATION;URL;FORM;TYPE;SCHOOL;TIMEFRAME

            for (int i = 1; i < records.size(); i++) {
                CSVRecord csvRecord = records.get(i);

                String record0 = csvRecord.get(0);

                if (record0.contains(",")) {
                    String[] author = StringUtils.split(record0, ',');
                    System.out.println(WordUtils.capitalizeFully(StringUtils.trim(author[1])));
                    System.out.println(WordUtils.capitalizeFully(StringUtils.trim(author[0])));
                    System.out.println();
                } else {
                    System.out.println(csvRecord.get(0));
                    System.out.println();
                }
                /*
                String record1 = csvRecord.get(1);
                System.out.println("--|" + record1 + "|--");
                        
                if (record1.startsWith("(b. ") && record1.contains("d.") && record1.contains(")"))
                {
                  record1 = StringUtils.remove(record1, '(');
                  record1 = StringUtils.remove(record1, ')');
                  System.out.println(record1);
                  String[] bornDied = StringUtils.split(record1, ',');
                  System.out.println(bornDied[0].trim().substring(2).trim());
                  System.out.println(bornDied[1].trim());
                  System.out.println(bornDied[2].trim().substring(2).trim());
                  System.out.println(bornDied[3].trim());
                }
                else
                {
                  System.out.println(csvRecord.get(1));            
                }
                        
                System.out.println(csvRecord.get(2));
                System.out.println(csvRecord.get(3));
                System.out.println(csvRecord.get(4));
                System.out.println(csvRecord.get(5));
                System.out.println(csvRecord.get(6));
                System.out.println(csvRecord.get(7));
                System.out.println(csvRecord.get(8));
                System.out.println(csvRecord.get(9));
                System.out.println(csvRecord.get(10));
                System.out.println();
                */
            }
        }
    } catch (Exception e) {
        e.printStackTrace();

        Assert.fail(e.getMessage());
    }
}

From source file:org.neo4art.core.service.ColourDefaultService.java

/**
 * @throws IOException //  w w  w .jav a  2  s  .c  om
 * @see org.neo4art.core.service.ColourService#getColours()
 */
@Override
public List<Colour> getColours() throws IOException {
    List<Colour> result = null;

    URL url = getClass().getClassLoader().getResource("colours.csv");
    CSVParser csvParser = CSVParser.parse(url, Charset.defaultCharset(), CSVFormat.EXCEL.withDelimiter(',')
            .withQuote('\'').withEscape('\\').withIgnoreSurroundingSpaces(true));

    List<CSVRecord> records = csvParser.getRecords();

    if (CollectionUtils.isNotEmpty(records)) {
        result = new ArrayList<Colour>();

        for (CSVRecord csvRecord : records) {
            int r = Integer.parseInt(csvRecord.get(1).substring(1, 3), 16);
            int g = Integer.parseInt(csvRecord.get(1).substring(3, 5), 16);
            int b = Integer.parseInt(csvRecord.get(1).substring(5, 7), 16);

            result.add(new Colour(csvRecord.get(0), r, g, b));
        }
    }

    return result;
}

From source file:org.neo4art.literature.manager.VanGoghLettersManager.java

public List<SentimentAnalysis> loadSentimentsFromFile(String fileName) throws IOException {

    List<SentimentAnalysis> sentimentAnalysisList = new ArrayList<SentimentAnalysis>();

    URL url = getClass().getClassLoader().getResource(fileName);

    CSVParser csvParser = CSVParser.parse(url, Charset.defaultCharset(),
            CSVFormat.EXCEL.withIgnoreSurroundingSpaces(true));

    List<CSVRecord> cvsRecords = csvParser.getRecords();

    if (CollectionUtils.isNotEmpty(cvsRecords) && CollectionUtils.size(cvsRecords) > 1) {

        for (int i = 1; i < cvsRecords.size(); i++) {
            CSVRecord csvRecord = cvsRecords.get(i);

            SentimentAnalysis sentimentAnalysis = new SentimentAnalysis();
            Letter letter = new Letter();
            letter.setTitle(csvRecord.get(0));
            String polarityType = csvRecord.get(1);
            String polarity = "";

            if (polarityType.equalsIgnoreCase("0")) {
                polarity = "neutral";
            } else if (polarityType.equalsIgnoreCase("1") || polarityType.equalsIgnoreCase("2")) {
                polarity = "negative";
            } else if (polarityType.equalsIgnoreCase("3") || polarityType.equalsIgnoreCase("4")) {
                polarity = "positive";
            }/* w w w  .j a  v a 2s  .  c  om*/

            letter.setDate(csvRecord.get(2));

            sentimentAnalysis.setPolarity(polarity);
            sentimentAnalysis.setSource(letter);

            sentimentAnalysisList.add(sentimentAnalysis);
        }
    }

    return sentimentAnalysisList;
}

From source file:org.nuxeo.ecm.csv.core.CSVImporterWork.java

/**
 * Import a line from the CSV file./*from w ww.j av  a2  s. c o m*/
 *
 * @return {@code true} if a document has been created or updated, {@code false} otherwise.
 * @since 6.0
 */
protected boolean importRecord(CSVRecord record, Map<String, Integer> header) {
    String name = record.get(CSV_NAME_COL);
    if (StringUtils.isBlank(name)) {
        log.debug("record.isSet=" + record.isSet(CSV_NAME_COL));
        logError(getLineNumber(record), "Missing 'name' value", LABEL_CSV_IMPORTER_MISSING_NAME_VALUE);
        return false;
    }

    Path targetPath = new Path(parentPath).append(name);
    name = targetPath.lastSegment();
    String newParentPath = targetPath.removeLastSegments(1).toString();
    boolean exists = options.getCSVImporterDocumentFactory().exists(session, newParentPath, name, null);

    DocumentRef docRef = null;
    String type = null;
    if (exists) {
        docRef = new PathRef(targetPath.toString());
        type = session.getDocument(docRef).getType();
    } else {
        if (hasTypeColumn) {
            type = record.get(CSV_TYPE_COL);
        }
        if (StringUtils.isBlank(type)) {
            log.debug("record.isSet=" + record.isSet(CSV_TYPE_COL));
            logError(getLineNumber(record), "Missing 'type' value", LABEL_CSV_IMPORTER_MISSING_TYPE_VALUE);
            return false;
        }
    }

    DocumentType docType = Framework.getService(SchemaManager.class).getDocumentType(type);
    if (docType == null) {
        logError(getLineNumber(record), "The type '%s' does not exist", LABEL_CSV_IMPORTER_NOT_EXISTING_TYPE,
                type);
        return false;
    }
    Map<String, Serializable> properties = computePropertiesMap(record, docType, header);
    if (properties == null) {
        // skip this line
        return false;
    }

    long lineNumber = getLineNumber(record);
    if (exists) {
        return updateDocument(lineNumber, docRef, properties);
    } else {
        return createDocument(lineNumber, newParentPath, name, type, properties);
    }
}

From source file:org.nuxeo.ecm.csv.core.CSVImporterWork.java

/**
 * @since 6.0//from  ww  w .j a  va 2  s  .  com
 */
protected Map<String, Serializable> computePropertiesMap(CSVRecord record, CompositeType compositeType,
        Map<String, Integer> header) {
    Map<String, Serializable> values = new HashMap<>();
    for (String headerValue : header.keySet()) {
        String lineValue = record.get(headerValue);
        lineValue = lineValue.trim();
        String fieldName = headerValue;
        if (!CSV_NAME_COL.equals(headerValue) && !CSV_TYPE_COL.equals(headerValue)) {
            if (AUTHORIZED_HEADERS.contains(headerValue) && !StringUtils.isBlank(lineValue)) {
                values.put(headerValue, lineValue);
            } else {
                if (!compositeType.hasField(fieldName)) {
                    fieldName = fieldName.split(":")[1];
                }
                if (compositeType.hasField(fieldName) && !StringUtils.isBlank(lineValue)) {
                    Serializable convertedValue = convertValue(compositeType, fieldName, headerValue, lineValue,
                            getLineNumber(record));
                    if (convertedValue == null) {
                        return null;
                    }
                    values.put(headerValue, convertedValue);
                }
            }
        }
    }
    return values;
}