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.hoteia.qalingo.translation.LoaderTranslationUtil.java

public static final void buildMessagesProperties(String currentPath, String project, String filePath,
        List<String> activedLanguages, String defaultLanguage, String inputEncoding, String outputEncoding) {
    try {/*w  ww.  j  a v a2 s. c om*/
        String newPath = currentPath + project + LoaderTranslation.PROPERTIES_PATH;
        File folderProject = new File(newPath);
        if (!folderProject.exists()) {
            folderProject.mkdirs();
        }

        InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(filePath)),
                inputEncoding);
        LOG.info("File CSV encoding: " + inputEncoding);
        LOG.info("File properties encoding: " + outputEncoding);

        CSVFormat csvFormat = CSVFormat.DEFAULT;

        CSVParser readerCSV = new CSVParser(reader, csvFormat);
        List<CSVRecord> records = null;
        try {
            records = readerCSV.getRecords();
        } catch (Exception e) {
            LOG.error("Failed to load: " + filePath, e);
        }

        String prefixFileName = "";
        CSVRecord firstLineRecord = records.get(0);
        String[] prefixFileNameTemp = firstLineRecord.get(0).split("## Filename :");
        if (prefixFileNameTemp.length > 1) {
            prefixFileName = prefixFileNameTemp[1].trim();
        }

        String prefixKey = "";
        CSVRecord secondLineRecord = records.get(1);
        String[] prefixKeyTemp = secondLineRecord.get(0).split("## PrefixKey :");
        if (prefixKeyTemp.length > 1) {
            prefixKey = prefixKeyTemp[1].trim();
        }

        Map<String, Integer> availableLanguages = new HashMap<String, Integer>();
        for (CSVRecord record : records) {
            String firstCell = record.get(0);
            if (firstCell.contains("Prefix") && record.size() > 1) {
                String secondCell = record.get(1);
                if (secondCell.contains("Key")) {
                    for (int i = 2; i < record.size(); i++) {
                        String languageCode = record.get(i);
                        availableLanguages.put(languageCode, new Integer(i));
                    }
                    break;
                }
            }
        }

        // BUILD DEFAULT FILE
        String fileFullPath = newPath + "/" + prefixFileName + ".properties";
        Integer defaultLanguagePosition = availableLanguages.get(defaultLanguage);
        buildMessagesProperties(records, fileFullPath, prefixKey, defaultLanguage, defaultLanguagePosition,
                outputEncoding, true);

        for (Iterator<String> iterator = availableLanguages.keySet().iterator(); iterator.hasNext();) {
            String languageCode = (String) iterator.next();
            if (activedLanguages.contains(languageCode)) {
                Integer languagePosition = availableLanguages.get(languageCode);
                String languegFileFullPath = newPath + "/" + prefixFileName + "_" + languageCode
                        + ".properties";
                buildMessagesProperties(records, languegFileFullPath, prefixKey, languageCode,
                        languagePosition.intValue(), outputEncoding, false);
            }
        }
        LOG.info(newPath + "/" + prefixFileName + ".properties");

    } catch (Exception e) {
        LOG.info("Exception", e);
    }
}

From source file:org.italiangrid.storm.webdav.authz.vomap.MapfileVOMembershipSource.java

@Override
public Set<String> getVOMembers() {

    long startTime = System.currentTimeMillis();

    Set<String> subjects = new HashSet<String>();

    CSVParser parser = getParser();

    try {/*w w  w.  j av  a 2s . c om*/

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

        for (CSVRecord r : records) {

            if (logger.isDebugEnabled()) {
                logger.debug("Parsed record: {} for VO {}", r, voName);
            }

            if (!isValidCSVRecord(r)) {
                break;
            }

            String subject = r.get(0);

            if (logger.isDebugEnabled()) {
                logger.debug("Parsed subject {} as member of VO {}", subject, voName);
            }

            @SuppressWarnings("deprecation")
            String rfcSubject = OpensslNameUtils.opensslToRfc2253(subject);

            if (logger.isDebugEnabled()) {
                logger.debug("Converted subject {} to rfc format {}", subject, rfcSubject);
            }

            subjects.add(rfcSubject);
        }

    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    long totalTime = System.currentTimeMillis() - startTime;

    logger.debug("Parsing VO {} members from {} took {} msecs.", voName, mapFile, totalTime);

    return subjects;
}

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));// ww w .  j a  v  a2 s  .co  m
                sb.append("* ");
            } else {
                sb.append("| ");
                sb.append(h.get(i));
                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)
 *//*  w  ww. j  ava2  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   ww  w . j a  va2 s .  co  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 //from  w w  w.  j  a v a  2 s . c o m
 * @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  va 2  s  .  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

protected void doImport(CSVParser parser) {
    log.info(String.format("Importing CSV file: %s", getBlob().getFilename()));
    Map<String, Integer> header = parser.getHeaderMap();
    if (header == null) {
        logError(0, "No header line, empty file?", LABEL_CSV_IMPORTER_EMPTY_FILE);
        return;// w ww.j a v a 2s  . c om
    }
    if (!header.containsKey(CSV_NAME_COL)) {
        logError(0, "Missing 'name' column", LABEL_CSV_IMPORTER_MISSING_NAME_COLUMN);
        return;
    }
    hasTypeColumn = header.containsKey(CSV_TYPE_COL);

    try {
        int batchSize = options.getBatchSize();
        Iterable<CSVRecord> it = parser;
        if (computeTotal) {
            try {
                List<CSVRecord> l = parser.getRecords();
                total = l.size();
                it = l;
            } catch (IOException e) {
                log.warn("Could not compute total number of document to be imported");
            }
        }
        for (CSVRecord record : it) {
            if (record.size() == 0) {
                // empty record
                importLogs.add(new CSVImportLog(getLineNumber(record), Status.SKIPPED, "Empty record",
                        LABEL_CSV_IMPORTER_EMPTY_LINE));
                continue;
            }
            try {
                if (importRecord(record, header)) {
                    docsCreatedCount++;
                    getStore().putParameter(id, "status",
                            new CSVImportStatus(CSVImportStatus.State.RUNNING, docsCreatedCount, total));
                    if (docsCreatedCount % batchSize == 0) {
                        commitOrRollbackTransaction();
                        startTransaction();
                    }
                }
            } catch (NuxeoException e) {
                // try next line
                Throwable unwrappedException = unwrapException(e);
                logError(getLineNumber(parser), "Error while importing line: %s",
                        LABEL_CSV_IMPORTER_ERROR_IMPORTING_LINE, unwrappedException.getMessage());
                log.debug(unwrappedException, unwrappedException);
            }
        }

        try {
            session.save();
        } catch (NuxeoException e) {
            Throwable ue = unwrapException(e);
            logError(getLineNumber(parser), "Unable to save: %s", LABEL_CSV_IMPORTER_UNABLE_TO_SAVE,
                    ue.getMessage());
            log.debug(ue, ue);
        }
    } finally {
        commitOrRollbackTransaction();
        startTransaction();
    }
    log.info(String.format("Done importing CSV file: %s", getBlob().getFilename()));
}

From source file:org.nuxeo.ecm.user.center.profile.UserProfileImporter.java

public void doImport(CoreSession session, CSVParser parser, UserProfileService userProfileService)
        throws IOException {
    log.info(String.format("Importing CSV file: %s", dataFileName));

    DocumentType docType = Framework.getLocalService(SchemaManager.class)
            .getDocumentType(UserProfileConstants.USER_PROFILE_DOCTYPE);
    if (docType == null) {
        log.error("The type " + UserProfileConstants.USER_PROFILE_DOCTYPE + " does not exist");
        return;//from   w w  w. ja v  a2 s. c om
    }

    Map<String, Integer> header = parser.getHeaderMap();

    if (header == null) {
        // empty file?
        log.error("No header line, empty file?");
        return;
    }

    // find the index for the required name and type values
    Integer nameIndex = header.get(UserProfileImporter.USER_PROFILE_IMPORTER_USERNAME_COL);
    if (nameIndex == null) {
        log.error("Missing 'username' column");
        return;
    }

    long docsUpdatedCount = 0;
    totalRecords = parser.getRecordNumber();
    try {
        int batchSize = config.getBatchSize();
        long lineNumber = 0;

        for (CSVRecord record : parser.getRecords()) {
            lineNumber++;
            currentRecord = lineNumber;

            try {
                if (importLine(record, lineNumber, nameIndex, docType, session, userProfileService, header)) {
                    docsUpdatedCount++;
                    if (docsUpdatedCount % batchSize == 0) {
                        commitOrRollbackTransaction();
                        startTransaction();
                    }
                }
            } catch (NuxeoException e) {
                // try next line
                Throwable unwrappedException = unwrapException(e);
                logImportError(lineNumber, "Error while importing line: %s", unwrappedException.getMessage());
                log.debug(unwrappedException, unwrappedException);
            }
        }

        session.save();
    } finally {
        commitOrRollbackTransaction();
        startTransaction();
    }
    log.info(String.format("Done importing %s entries from CSV file: %s", docsUpdatedCount, dataFileName));
}

From source file:org.openlmis.fulfillment.Resource2Db.java

Pair<List<String>, List<Object[]>> resourceCsvToBatchedPair(final Resource resource) throws IOException {
    XLOGGER.entry(resource.getDescription());

    // parse CSV//  w w w. ja  v  a 2 s. c o  m
    try (InputStreamReader isReader = new InputStreamReader(
            new BOMInputStream(resource.getInputStream(), ByteOrderMark.UTF_8))) {
        CSVParser parser = CSVFormat.DEFAULT.withHeader().withNullString("").parse(isReader);

        // read header row
        MutablePair<List<String>, List<Object[]>> readData = new MutablePair<>();
        readData.setLeft(new ArrayList<>(parser.getHeaderMap().keySet()));
        XLOGGER.info("Read header: " + readData.getLeft());

        // read data rows
        List<Object[]> rows = new ArrayList<>();
        for (CSVRecord record : parser.getRecords()) {
            if (!record.isConsistent()) {
                throw new IllegalArgumentException("CSV record inconsistent: " + record);
            }

            List theRow = IteratorUtils.toList(record.iterator());
            rows.add(theRow.toArray());
        }
        readData.setRight(rows);

        XLOGGER.exit("Records read: " + readData.getRight().size());
        return readData;
    }
}