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:no.uio.medicine.virsurveillance.parsers.CsvWosParser.java

public void parse(String inputPath) {

    try {//  w w  w .  j  ava 2s  .com
        int year = 2014;//default value
        BufferedReader reader = new BufferedReader(new FileReader(inputPath));
        String line = reader.readLine(); // Read the first/current line.
        try {
            if (line.contains("Selected JCR Year:")) {
                String[] splitedLine = line.split("Selected JCR Year:");
                year = Integer.parseInt(splitedLine[1].split(" ")[1]);
            }
        } catch (Exception e) {
            System.out.println("Journal Citation year selected by default: " + year);
            Logger.getLogger(CsvWosParser.class.getName()).log(Level.WARNING,
                    "Journal Citation year selected by default: " + year, e);

        }

        CSVParser parser = CSVFormat.RFC4180.withDelimiter(',').withIgnoreEmptyLines().withHeader()
                .parse(reader);

        for (CSVRecord csvRecord : parser) {
            if (!csvRecord.get("Rank").contains("Copyright")) {
                PubmedJournal journal = new PubmedJournal(
                        csvRecord.get("Full Journal Title").replaceAll("\\(.*?\\) ?", ""));
                if (csvRecord.isMapped("JCR Abbreviated Title")) {
                    journal.setJournalShortName(csvRecord.get("JCR Abbreviated Title"));
                }
                try {
                    journal.setYearImpactFactor(year, Float.parseFloat(csvRecord.get("Journal Impact Factor")));
                } catch (Exception e) {
                    System.out.println("Impact factor " + csvRecord.get("Journal Impact Factor") + " for "
                            + csvRecord.get("Full Journal Title"));
                    journal.setYearImpactFactor(year, -1);
                }
                this.journals.add(journal);
                this.currentBatchSize++;
                currentBatchSize++;
                if (this.currentBatchSize >= this.batchSize) {
                    currentBatchSize = 0;
                    for (PubmedJournal pm : journals) {
                        this.sqlM.updateJournal(pm, true);
                    }
                    this.journals = new ArrayList<>();
                }
            }

        }

        for (PubmedJournal pm : journals) {
            this.sqlM.updateJournal(pm, true);
        }
        this.journals = new ArrayList<>();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(CsvWosParser.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(CsvWosParser.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(CsvWosParser.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:no.uio.medicine.virsurveillance.parserTests.SaxTest.java

private static ArrayList<String> getDataFromVirusCSV(String inputVirusFile)
        throws FileNotFoundException, IOException {
    ArrayList<String> viruses = new ArrayList<>();

    BufferedReader reader = new BufferedReader(new FileReader(inputVirusFile));
    CSVParser parser = CSVFormat.RFC4180.withDelimiter(';').withIgnoreEmptyLines().withHeader().parse(reader);
    for (CSVRecord csvRecord : parser) {
        if (csvRecord.isMapped("Virus Name")) {
            String currentVirus = csvRecord.get("Virus Name").replace(" virus", "").replace("", "%%");
            viruses.add(currentVirus);//from w w  w  . j ava  2 s  . c o m
        }
    }

    return viruses;

}

From source file:org.apache.jena.atlas.csv.CSVParser.java

private CSVParser(Reader input) {
    try {//  w  ww.j a  v a  2  s.co m
        this.parser = CSVFormat.RFC4180.parse(input);
        this.iterator = parser.iterator();
    } catch (IOException e) {
        throw new CSVParseException("Failed to create the CSV parser: " + e.getMessage(), e);
    }
}

From source file:org.apache.logging.log4j.core.layout.CsvLogEventLayoutTest.java

@Test
public void testLayoutRFC4180() throws Exception {
    testLayout(CSVFormat.RFC4180);
}

From source file:org.apache.nifi.csv.CSVUtils.java

public static CSVFormat createCSVFormat(final PropertyContext context) {
    final String formatName = context.getProperty(CSV_FORMAT).getValue();
    if (formatName.equalsIgnoreCase(CUSTOM.getValue())) {
        return buildCustomFormat(context);
    }/*from w w  w .  jav  a2s  .c o  m*/
    if (formatName.equalsIgnoreCase(RFC_4180.getValue())) {
        return CSVFormat.RFC4180;
    } else if (formatName.equalsIgnoreCase(EXCEL.getValue())) {
        return CSVFormat.EXCEL;
    } else if (formatName.equalsIgnoreCase(TDF.getValue())) {
        return CSVFormat.TDF;
    } else if (formatName.equalsIgnoreCase(MYSQL.getValue())) {
        return CSVFormat.MYSQL;
    } else if (formatName.equalsIgnoreCase(INFORMIX_UNLOAD.getValue())) {
        return CSVFormat.INFORMIX_UNLOAD;
    } else if (formatName.equalsIgnoreCase(INFORMIX_UNLOAD_CSV.getValue())) {
        return CSVFormat.INFORMIX_UNLOAD_CSV;
    } else {
        return CSVFormat.DEFAULT;
    }
}

From source file:org.apache.orc.bench.convert.csv.CsvReader.java

public CsvReader(Path path, TypeDescription schema, Configuration conf, CompressionKind compress)
        throws IOException {
    FileSystem fs = path.getFileSystem(conf);
    InputStream input = compress.read(fs.open(path));
    parser = new CSVParser(new InputStreamReader(input, StandardCharsets.UTF_8), CSVFormat.RFC4180.withHeader())
            .iterator();//from  w  w w. j  av  a  2 s  . c o  m
    List<TypeDescription> columnTypes = schema.getChildren();
    readers = new ColumnReader[columnTypes.size()];
    int c = 0;
    for (TypeDescription columnType : columnTypes) {
        readers[c++] = createReader(columnType);
    }
}

From source file:org.apache.storm.sql.runtime.serde.csv.CsvScheme.java

@Override
public List<Object> deserialize(ByteBuffer ser) {
    try {//from   ww  w . j ava2 s  .c  o  m
        String data = new String(Utils.toByteArray(ser), StandardCharsets.UTF_8);
        CSVParser parser = CSVParser.parse(data, CSVFormat.RFC4180);
        CSVRecord record = parser.getRecords().get(0);
        Preconditions.checkArgument(record.size() == fieldNames.size(), "Invalid schema");

        ArrayList<Object> list = new ArrayList<>(fieldNames.size());
        for (int i = 0; i < record.size(); i++) {
            list.add(record.get(i));
        }
        return list;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.storm.sql.runtime.serde.csv.CsvSerializer.java

@Override
public ByteBuffer write(List<Object> data, ByteBuffer buffer) {
    try {/*from  w  w w.  j a  v  a 2  s . c  om*/
        StringWriter writer = new StringWriter();
        CSVPrinter printer = new CSVPrinter(writer, CSVFormat.RFC4180);
        for (Object o : data) {
            printer.print(o);
        }
        //since using StringWriter, we do not need to close it.
        return ByteBuffer.wrap(writer.getBuffer().toString().getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.etudes.mneme.tool.UploadCsv.java

/**
 * Parse the contents into CSV records.//from  w  w  w. j  a  v  a  2 s.  c  o  m
 */
protected void parse() {
    try {
        Reader in = new StringReader(this.contents);
        CSVParser parser = new CSVParser(in, CSVFormat.RFC4180);
        this.records = parser.getRecords();
        parser.close();
    } catch (IOException e) {
    } finally {

    }
}

From source file:org.jboss.jbossset.CommandLineParser.java

public CommandLineParser(String[] args) {
    this.args = args;

    validCSVFormats = new HashMap<>();
    validCSVFormats.put("excel", CSVFormat.EXCEL);
    validCSVFormats.put("mysql", CSVFormat.MYSQL);
    validCSVFormats.put("rfc4180", CSVFormat.RFC4180);
    validCSVFormats.put("tdf", CSVFormat.TDF);

    addOptions();//from  ww  w. j  a v  a 2s .c o  m
}