Example usage for org.apache.commons.csv CSVFormat RFC4180

List of usage examples for org.apache.commons.csv CSVFormat RFC4180

Introduction

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

Prototype

CSVFormat RFC4180

To view the source code for org.apache.commons.csv CSVFormat RFC4180.

Click Source Link

Document

Comma separated format as defined by <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>.

Usage

From source file:org.wso2.carbon.notebook.core.util.paragraph.PreprocessorUtils.java

/**
 * Take a sample from the table and preprocess according to the parameters set
 *
 * @return preprocessed lines/*from w  w  w .j  a v  a2 s . c  o m*/
 */
public static JavaRDD<String[]> preProcess(int tenantID, String tableName, List<Feature> features,
        String headerLine) throws PreprocessorException, AnalyticsException {
    JavaRDD<String[]> preprocessedLines = null;
    JavaRDD<String> lines = MLUtils.getLinesFromDASTable(tableName, tenantID,
            ServiceHolder.getSparkContextService().getJavaSparkContext());

    try {
        HeaderFilter headerFilter = new HeaderFilter.Builder().init(headerLine).build();
        JavaRDD<String> data = lines.filter(headerFilter);

        LineToTokens lineToTokens = new LineToTokens.Builder()
                .init(String.valueOf(CSVFormat.RFC4180.getDelimiter())).build();
        JavaRDD<String[]> tokens = data.map(lineToTokens);

        //generate Descriptive Statistics for each column
        List<DescriptiveStatistics> descriptiveStats = generateDescriptiveStat(tokens, features, tableName);

        Map<String, Double> meanOfEachColumn = setMeanOfEachColumn(features, descriptiveStats);

        DiscardedRowsFilter discardedRowsFilter = new DiscardedRowsFilter.Builder().init(features).build();
        RemoveDiscardedFeatures removeDiscardedFeatures = new RemoveDiscardedFeatures.Builder().init(features)
                .build();
        MeanImputation meanImputationFilter = new MeanImputation.Builder().init(meanOfEachColumn, features)
                .build();
        RemoveResponseColumn responseColumnFilter = new RemoveResponseColumn();

        preprocessedLines = tokens.filter(discardedRowsFilter).map(removeDiscardedFeatures)
                .map(responseColumnFilter).map(meanImputationFilter).cache();
    } finally {
        if (lines != null) {
            lines.unpersist();
        }
    }
    return preprocessedLines;
}

From source file:org.zanata.adapter.glossary.GlossaryCSVReader.java

public Map<LocaleId, List<GlossaryEntry>> extractGlossary(Reader reader, String qualifiedName)
        throws IOException {
    try {//from  w w w  . j  a  v a  2s  . c o  m
        Iterable<CSVRecord> rawRecords = CSVFormat.RFC4180.parse(reader);
        List<CSVRecord> records = Lists.newArrayList(rawRecords);

        validateCSVEntries(records);
        Map<String, Integer> descriptionMap = setupDescMap(records);
        Map<Integer, LocaleId> localeColMap = setupLocalesMap(records, descriptionMap);

        LocaleId srcLocale = localeColMap.get(0);

        if (!srcLang.equals(srcLocale)) {
            throw new RuntimeException("input source language '" + srcLang
                    + "' does not match source language in file '" + srcLocale + "'");
        }
        Map<LocaleId, List<GlossaryEntry>> results = Maps.newHashMap();

        for (int i = 1; i < records.size(); i++) {
            CSVRecord row = records.get(i);
            for (int x = 1; x < row.size() && localeColMap.containsKey(x); x++) {

                GlossaryEntry entry = new GlossaryEntry();
                entry.setSrcLang(srcLocale);
                entry.setPos(row.get(descriptionMap.get(POS)));
                entry.setDescription(row.get(descriptionMap.get(DESC)));
                entry.setQualifiedName(new QualifiedName(qualifiedName));

                GlossaryTerm srcTerm = new GlossaryTerm();
                srcTerm.setLocale(srcLocale);
                srcTerm.setContent(row.get(0));

                entry.getGlossaryTerms().add(srcTerm);

                LocaleId transLocaleId = localeColMap.get(x);
                String transContent = row.get(x);

                GlossaryTerm transTerm = new GlossaryTerm();
                transTerm.setLocale(transLocaleId);
                transTerm.setContent(transContent);

                entry.getGlossaryTerms().add(transTerm);
                List<GlossaryEntry> list = results.get(transLocaleId);
                if (list == null) {
                    list = Lists.newArrayList();
                }
                list.add(entry);
                results.put(transLocaleId, list);
            }
        }
        return results;
    } finally {
        reader.close();
    }
}

From source file:poe.trade.assist.Main.java

private void saveSearchList(SearchPane searchPane) {
    List<Search> list = new ArrayList<>(searchPane.searchTable.getMasterItems());
    if (list != null) {
        //         Gson gson = new Gson();
        List<String[]> persistList = list.stream().map(e -> e.toSearchPersist()).map(e -> e.toCSVArray())
                .collect(toList());/* ww  w . j av  a 2s  .  com*/
        if (!persistList.isEmpty()) {
            File file = getSearchFile();
            try (BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
                CSVPrinter json = CSVFormat.RFC4180.withHeader("Name", "Tags", "URL", "Auto", "Sort").print(br);
                json.printRecords(persistList);
                json.flush();
                //         String json = gson.toJson(persistList);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                Dialogs.showError(e1);
                e1.printStackTrace();
            }
        }

        //         saveSearchesToFile(json);
    }
}

From source file:poe.trade.assist.Main.java

private List<CSVRecord> loadCSVRaw() {
    List<CSVRecord> records = null;
    try {//from   w w  w  .j  av a  2 s  .  c  o m
        String searchFileFromTextField = StringUtils.trimToEmpty(searchFileTextField.getText());
        if (searchFileFromTextField.isEmpty()
                || searchFileFromTextField.equalsIgnoreCase(LOCAL_SEARCH_FILE_NAME)) {
            File file = getSearchFile();
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                CSVParser csvParser = CSVFormat.RFC4180.withHeader().parse(br);
                records = csvParser.getRecords();
            }
        } else {
            String url = searchFileFromTextField;
            if (url.contains("google") && url.contains("/edit")) {
                // handle google spreadsheet url that is not an export url
                // https://docs.google.com/spreadsheets/d/1V8r0mIn5njpmVYwFWpqnptAMI6udrIaqhCby1i79UGw/edit#gid=0
                url = StringUtils.substringBeforeLast(url, "/edit");
                url += "/export?gid=0&format=csv";
            }
            try (BufferedReader br = new BufferedReader(new InputStreamReader(new URL(url).openStream()))) {
                CSVParser csvParser = CSVFormat.RFC4180.withHeader().parse(br);
                records = csvParser.getRecords();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        Dialogs.showError(e);
    }
    return records;
}

From source file:proci.gui.Importa.java

private void doImport() {
    jepData.setText("");
    text = new StringBuilder("<html><body>");
    try {//from  w w w .  j  av a2 s  .c om
        conn = DBHandler.getInstance().getConnection();
        stmt = conn.createStatement();
        conn.setAutoCommit(false);
        int delRec = 0;
        delRec = stmt.executeUpdate("delete from SOCIO");
        text.append("Cancellati: ").append(delRec).append(" soci!<br>");
        delRec = stmt.executeUpdate("delete from DOTAZIONE");
        text.append("Cancellate: ").append(delRec).append(" dotazioni!<br>");
        delRec = stmt.executeUpdate("delete from INTERVENTO");
        text.append("Cancellati: ").append(delRec).append(" interventi!<br>");
        delRec = stmt.executeUpdate("delete from ESERCITAZIONE");
        text.append("Cancellate: ").append(delRec).append(" esercitazioni!<br>");
        delRec = stmt.executeUpdate("delete from SPECIALIZZAZIONE");
        text.append("Cancellate: ").append(delRec).append(" specializzazioni!<br>");
        delRec = stmt.executeUpdate("delete from SODO");
        delRec = stmt.executeUpdate("delete from SOIN");
        delRec = stmt.executeUpdate("delete from SOES");
        delRec = stmt.executeUpdate("delete from SOSP");
        // elimina foto dei soci
        File imagedir = new File(app.getAppPath(EDirectories.IMAGES_PHOTOS));
        File[] images = imagedir.listFiles();
        for (File f : images) {
            f.delete();
        }
        text.append("Eliminate: ").append(images.length).append(" immagini<br>");
        // elimina miniature
        imagedir = new File(app.getAppPath(EDirectories.IMAGES_THUMBS));
        images = imagedir.listFiles();
        for (File f : images) {
            f.delete();
        }
        text.append("Eliminate: ").append(images.length).append(" miniature<br>");
        //
        psSocio = conn
                .prepareStatement("insert into SOCIO values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        psDot = conn.prepareStatement("insert into DOTAZIONE values (?,?,?)");
        psInt = conn.prepareStatement("insert into INTERVENTO values (?,?)");
        psEse = conn.prepareStatement("insert into ESERCITAZIONE values (?,?)");
        psSpe = conn.prepareStatement("insert into SPECIALIZZAZIONE values (?,?)");
        psSodo = conn.prepareStatement("insert into SODO values (?,?,?)");
        psSoin = conn.prepareStatement("insert into SOIN values (?,?)");
        psSoes = conn.prepareStatement("insert into SOES values (?,?)");
        psSosp = conn.prepareStatement("insert into SOSP values (?,?)");
        //
        File csvData = new File(filename);
        CSVParser parser = CSVParser.parse(csvData, Charset.forName("UTF-8"), CSVFormat.RFC4180);
        for (CSVRecord csvRecord : parser) {
            // trappola intestazione
            if (csvRecord.get(0).equalsIgnoreCase("IDSocio")) {
                continue;
            }
            logger.info("Insert Socio: {}", csvRecord);
            insertSocio(csvRecord);
            //                        break;
            //                    case 2:
            //                        insertDot(line);
            //                        break;
            //                    case 3:
            //                        insertInt(line);
            //                        break;
            //                    case 4:
            //                        insertEse(line);
            //                        break;
            //                    case 5:
            //                        insertSpe(line);
            //                        break;
            //                    case 6:
            //                        insertSodo(line);
            //                        break;
            //                    case 7:
            //                        insertSoin(line);
            //                        break;
            //                    case 8:
            //                        insertSoes(line);
            //                        break;
            //                    case 9:
            //                        insertSosp(line);
            //                        break;
            //                }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "<html><font color='red'>ERRORE!</font>" + e, "ERRORE",
                JOptionPane.ERROR_MESSAGE);
        text.append("ERRORE: " + e);
        jepData.setText(text.toString());
        return;
    }
    try {
        conn.commit();
    } catch (SQLException e) {
    }
    jepData.setText(text.toString());
}

From source file:ro.fortsoft.kempes.demo.EvomagEventHandler.java

private void writeToCsv(Product product) throws IOException {
    // TODO improve
    if (csvPrinter == null) {
        CSVFormat csvFormat = CSVFormat.RFC4180.withHeader().withDelimiter(',');
        csvPrinter = new CSVPrinter(new FileWriter(CSV_FILE), csvFormat.withDelimiter('#'));
        csvPrinter.printRecord("Name", "Price");
    }/*  ww w .  j a va2  s  . com*/

    List<String> data = new ArrayList<String>();
    data.add(product.getName());
    data.add(String.valueOf(product.getPrice()));
    csvPrinter.printRecord(data);
    csvPrinter.flush();
    // TODO
    //        csvPrinter.close();
}

From source file:test.com.xceptance.xlt.common.actions.LWSimpleURLTest.java

/**
 * Create records from string//from  w  w w  .j  av  a  2 s .c o  m
 */
private List<CSVRecord> createRecords(final String... records) throws IOException {
    final StringBuilder fullRecord = new StringBuilder();

    for (final String record : records) {
        fullRecord.append(record.replace("{url}", testUrl));
        fullRecord.append("\n");
    }

    final CSVFormat csvFormat = CSVFormat.RFC4180.toBuilder().withIgnoreEmptyLines(true).withCommentStart('#')
            .withHeader().build();
    final CSVParser parser = new CSVParser(fullRecord.toString(), csvFormat);

    return parser.getRecords();
}

From source file:test.com.xceptance.xlt.common.util.AbstractCSVBasedURLAction_Test.java

/**
 * Create records from string//from w  ww  .  j a va  2 s.c om
 */
protected List<CSVRecord> createRecords(final String... records) throws IOException {
    final StringBuilder fullRecord = new StringBuilder();

    for (final String record : records) {
        fullRecord.append(record);
        fullRecord.append("\n");
    }

    final CSVFormat csvFormat = CSVFormat.RFC4180.toBuilder().withIgnoreEmptyLines(true).withCommentStart('#')
            .withHeader().build();
    final CSVParser parser = new CSVParser(fullRecord.toString(), csvFormat);

    return parser.getRecords();
}

From source file:test.com.xceptance.xlt.common.util.CSVBasedURLActionTest.java

/**
 * Create records from string/*  w w  w. ja va  2  s .c  o  m*/
 */
private List<CSVRecord> createRecords(final String... records) throws IOException {
    final StringBuilder fullRecord = new StringBuilder();

    for (final String record : records) {
        fullRecord.append(record);
        fullRecord.append("\n");
    }

    final CSVFormat csvFormat = CSVFormat.RFC4180.toBuilder().withIgnoreEmptyLines(true).withCommentStart('#')
            .withHeader().build();
    final CSVParser parser = new CSVParser(fullRecord.toString(), csvFormat);

    return parser.getRecords();
}

From source file:testes.CveCsvReader.java

public static void main(String[] args) {
    int contCandidate = 0, contEntry = 0, contReserved = 0, contReject = 0, contDisputed = 0, contEqual = 0;
    String last = "";

    try {//from   ww w.j a v  a  2  s .c om
        File csvData = new File("dataset/base_dados_cve.csv");
        //            File csvDataOut = new File("dataset/cve_out.csv");
        //            FileWriter outFile = new FileWriter(csvDataOut);
        //            CSVPrinter csvPrinter = new CSVPrinter((Appendable) outFile, CSVFormat.RFC4180);

        CSVParser parser = CSVParser.parse(csvData, Charset.forName("ISO-8859-1"), CSVFormat.RFC4180);
        for (CSVRecord csvRecord : parser) {
            //System.out.println("Nmero de campos: " + csvRecord.size());
            //System.out.println(csvRecord.get(0));
            if (csvRecord.get(1).equals("Candidate")) {
                contCandidate++;
            } else if (csvRecord.get(1).equals("Entry")) {
                contEntry++;
            }

            if (csvRecord.get(2).startsWith("** RESERVED **")) {
                contReserved++;
            } else if (csvRecord.get(2).startsWith("** REJECT **")) {
                contReject++;
            } else if (csvRecord.get(2).startsWith("** DISPUTED **")) {
                contDisputed++;
            } else {
                if (last.equals(csvRecord.get(2))) {
                    contEqual++;
                } else {
                    //                      csvPrinter.printRecord(csvRecord);
                }

                last = csvRecord.get(2);
            }
        }
        System.out.println("Nmero de Registros: " + parser.getRecordNumber());
        //csvPrinter.close();

    } catch (IOException ex) {
        Logger.getLogger(CveCsvReader.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("Nmero CANDIDATE: " + contCandidate);
    System.out.println("Nmero ENTRY: " + contEntry);

    System.out.println("Nmero REJECT: " + contReject);
    System.out.println("Nmero RESERVED: " + contReserved);
    System.out.println("Nmero DISPUTED: " + contDisputed);

    System.out.println("Nmero IGUAIS: " + contEqual);
}