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.jvalue.ods.processor.adapter.CsvSourceAdapter.java

@Inject
CsvSourceAdapter(@Assisted DataSource source,
        @Assisted(SourceAdapterFactory.ARGUMENT_SOURCE_URL) String sourceUrl,
        @Assisted(SourceAdapterFactory.ARGUMENT_CSV_FORMAT) String csvFormatString, MetricRegistry registry) {

    super(source, sourceUrl, registry);
    switch (csvFormatString) {
    case "DEFAULT":
        csvFormat = CSVFormat.DEFAULT;/*from   w w w  .  j a v a 2s.c  o m*/
        break;

    case "EXCEL":
        csvFormat = CSVFormat.EXCEL;
        break;

    case "MYSQL":
        csvFormat = CSVFormat.MYSQL;
        break;

    case "RFC4180":
        csvFormat = CSVFormat.RFC4180;
        break;

    case "TDF":
        csvFormat = CSVFormat.TDF;
        break;

    default:
        throw new IllegalArgumentException("unknown csv format \"" + csvFormatString + "\"");
    }
}

From source file:org.languagetool.dev.RuleDetails.java

public static void main(String[] args) throws ParseException, IOException {
    Options options = new Options();
    options.addRequiredOption("l", "language", true, "Language for rules");
    options.addRequiredOption("f", "file", true, "Input file");
    options.addRequiredOption("o", "output", true, "Output file");
    options.addRequiredOption("c", "column", true, "Column in input file");
    options.addOption("n", "ngramPath", true, "Ngram path to activate ngram rules");

    CommandLine cmd = new DefaultParser().parse(options, args);

    String langCode = cmd.getOptionValue('l');
    String inputFile = cmd.getOptionValue('f');
    String outputFile = cmd.getOptionValue('o');
    String column = cmd.getOptionValue('c');
    String ngramPath = cmd.hasOption('n') ? cmd.getOptionValue('n') : null;

    RuleDetails details = new RuleDetails(Languages.getLanguageForShortCode(langCode), ngramPath);

    CSVFormat format = CSVFormat.RFC4180.withFirstRecordAsHeader();

    try (CSVParser parser = CSVParser.parse(new File(inputFile), Charset.defaultCharset(), format)) {
        try (CSVPrinter printer = new CSVPrinter(new BufferedWriter(new FileWriter(outputFile)), format)) {
            Map<String, Integer> oldHeader = parser.getHeaderMap();
            List<String> newHeader = new ArrayList<>(Collections.nCopies(oldHeader.size(), null));

            for (Map.Entry<String, Integer> entry : oldHeader.entrySet()) {
                newHeader.set(entry.getValue(), entry.getKey());
            }//w  w w . jav a2  s .c  om
            newHeader.add("description");
            newHeader.add("category");
            printer.printRecord(newHeader);

            if (!oldHeader.containsKey(column)) {
                throw new RuntimeException("Input file does not contain specified column " + column);
            }

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

            records.stream().sequential().map(record -> {
                String ruleId = record.get(column);
                Rule rule = details.getRuleById(ruleId);
                List<String> transformedValues = new ArrayList<>();
                record.iterator().forEachRemaining(transformedValues::add);
                if (rule == null) {
                    transformedValues.add("");
                    transformedValues.add("");
                } else {
                    transformedValues.add(rule.getDescription());
                    transformedValues.add(rule.getCategory().getId().toString());
                }
                return transformedValues;
            }).forEachOrdered(values -> {
                try {
                    printer.printRecord(values);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    }

}

From source file:org.ohdsi.rabbitInAHat.dataModel.Database.java

public static Database generateModelFromCSV(InputStream stream, String dbName) {
    Database database = new Database();

    database.dbName = dbName.substring(0, dbName.lastIndexOf("."));

    Map<String, Table> nameToTable = new HashMap<String, Table>();
    try {//from  www.j a  v  a 2 s.c  o  m
        for (CSVRecord row : CSVFormat.RFC4180.withHeader().parse(new InputStreamReader(stream))) {

            Table table = nameToTable.get(row.get("TABLE_NAME").toLowerCase());

            if (table == null) {
                table = new Table();
                table.setDb(database);
                table.setName(row.get("TABLE_NAME").toLowerCase());
                nameToTable.put(row.get("TABLE_NAME").toLowerCase(), table);
                database.tables.add(table);
            }
            Field field = new Field(row.get("COLUMN_NAME").toLowerCase(), table);
            field.setNullable(row.get("IS_NULLABLE").equals("YES"));
            field.setType(row.get("DATA_TYPE"));
            field.setDescription(row.get("DESCRIPTION"));
            table.getFields().add(field);
        }
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    }
    return database;
}

From source file:org.ohdsi.whiteRabbit.WhiteRabbitMain.java

private DbSettings getTargetDbSettings() {
    DbSettings dbSettings = new DbSettings();
    if (targetType.getSelectedItem().equals("Delimited text files")) {
        dbSettings.dataType = DbSettings.CSVFILES;

        switch ((String) targetCSVFormat.getSelectedItem()) {
        case "Default (comma, CRLF)":
            dbSettings.csvFormat = CSVFormat.DEFAULT;
            break;
        case "RFC4180":
            dbSettings.csvFormat = CSVFormat.RFC4180;
            break;
        case "Excel CSV":
            dbSettings.csvFormat = CSVFormat.EXCEL;
            break;
        case "TDF (tab, CRLF)":
            dbSettings.csvFormat = CSVFormat.TDF;
            break;
        case "MySQL (tab, LF)":
            dbSettings.csvFormat = CSVFormat.MYSQL;
            break;
        default:/*from   ww w  .ja  v a2  s.c  o m*/
            dbSettings.csvFormat = CSVFormat.RFC4180;
        }

    } else {
        dbSettings.dataType = DbSettings.DATABASE;
        dbSettings.user = targetUserField.getText();
        dbSettings.password = targetPasswordField.getText();
        dbSettings.server = targetServerField.getText();
        dbSettings.database = targetDatabaseField.getText();
        if (targetType.getSelectedItem().toString().equals("MySQL"))
            dbSettings.dbType = DbType.MYSQL;
        else if (targetType.getSelectedItem().toString().equals("Oracle"))
            dbSettings.dbType = DbType.ORACLE;
        else if (sourceType.getSelectedItem().toString().equals("PostgreSQL"))
            dbSettings.dbType = DbType.POSTGRESQL;
        else if (sourceType.getSelectedItem().toString().equals("SQL Server")) {
            dbSettings.dbType = DbType.MSSQL;
            if (sourceUserField.getText().length() != 0) { // Not using windows authentication
                String[] parts = sourceUserField.getText().split("/");
                if (parts.length == 2) {
                    dbSettings.user = parts[1];
                    dbSettings.domain = parts[0];
                }
            }
        } else if (sourceType.getSelectedItem().toString().equals("PDW")) {
            dbSettings.dbType = DbType.PDW;
            if (sourceUserField.getText().length() != 0) { // Not using windows authentication
                String[] parts = sourceUserField.getText().split("/");
                if (parts.length == 2) {
                    dbSettings.user = parts[1];
                    dbSettings.domain = parts[0];
                }
            }
        }

        if (dbSettings.database.trim().length() == 0) {
            String message = "Please specify a name for the target database";
            JOptionPane.showMessageDialog(frame, StringUtilities.wordWrap(message, 80), "Database error",
                    JOptionPane.ERROR_MESSAGE);
            return null;
        }
    }
    return dbSettings;
}

From source file:org.opennms.netmgt.integrations.R.RScriptExecutor.java

/**
 * Convert the CSV string to an immutable table.
 *//* w  ww .  j a v  a 2 s  .c  o m*/
protected static ImmutableTable<Long, String, Double> fromCsv(final String csv) throws IOException {
    ImmutableTable.Builder<Long, String, Double> builder = ImmutableTable.builder();
    try (StringReader reader = new StringReader(csv);
            CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180.withHeader());) {
        long rowIndex = 0;
        Map<String, Integer> headerMap = parser.getHeaderMap();
        for (CSVRecord record : parser) {
            for (String key : headerMap.keySet()) {
                Double value;
                try {
                    value = Double.valueOf(record.get(key));
                } catch (NumberFormatException e) {
                    value = Double.NaN;
                }

                builder.put(rowIndex, key, value);
            }
            rowIndex++;
        }
    }
    return builder.build();
}

From source file:org.opennms.netmgt.integrations.R.RScriptExecutor.java

/**
 * Convert the table to a CSV string./*  ww  w.  jav a2s .co m*/
 */
protected static StringBuilder toCsv(final RowSortedTable<Long, String, Double> table) throws IOException {
    final String columnNames[] = table.columnKeySet().toArray(new String[] {});

    final StringBuilder sb = new StringBuilder();
    final CSVPrinter printer = CSVFormat.RFC4180.withHeader(columnNames).print(sb);

    for (long rowIndex : table.rowKeySet()) {
        for (String columnName : columnNames) {
            Double value = table.get(rowIndex, columnName);
            if (value == null) {
                value = Double.NaN;
            }
            printer.print(value);
        }
        printer.println();
    }

    return sb;
}

From source file:org.opennms.netmgt.jasper.analytics.HWForecastReportTest.java

private void verify() throws Exception {
    Table<Integer, String, Double> forecasts = TreeBasedTable.create();

    try (FileReader reader = new FileReader(m_csvFile);
            CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180.withHeader());) {
        int k = 0;
        for (CSVRecord record : parser) {
            try {
                Double fit = Double.parseDouble(record.get("HWFit"));
                Double lwr = Double.parseDouble(record.get("HWLwr"));
                Double upr = Double.parseDouble(record.get("HWUpr"));

                if (Double.isNaN(fit)) {
                    continue;
                }/*from   w  w  w . j  a  v a  2 s .  co  m*/

                forecasts.put(k, "fit", fit);
                forecasts.put(k, "lwr", lwr);
                forecasts.put(k, "upr", upr);

                k++;
            } catch (NumberFormatException e) {
                // pass
            }
        }
    }

    assertEquals(340, forecasts.rowKeySet().size());
    // First fitted value
    assertEquals(432.526086422424, forecasts.get(0, "fit"), 0.00001);
    // Last fitted value for which there is a known datapoint
    assertEquals(24079.4692522087, forecasts.get(327, "fit"), 0.00001);
    // First forecasted value
    assertEquals(22245.5417010936, forecasts.get(328, "fit"), 0.00001);
}

From source file:org.opennms.netmgt.jasper.measurement.MeasurementQueryExecutorRemoteIT.java

@Test
public void testReportHwForecast() throws IOException, JRException {
    createReport("Forecast", new ReportFiller() {
        @Override//from w w  w  . ja v  a2 s .  c  o  m
        public void fill(Map<String, Object> params) throws Exception {
            params.put(JRParameter.IS_IGNORE_PAGINATION, true);
            params.put("MEASUREMENT_URL", "http://localhost:9999/opennms/rest/measurements");
            params.put("dsName", "ifInOctets");
            params.put("startDate", "1414602000000");
            params.put("endDate", "1417046400000");
        }
    });

    // Verify the results of the generated report
    Table<Integer, String, Double> forecasts = TreeBasedTable.create();

    FileReader reader = new FileReader(createFileName("Forecast", "csv"));
    CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180.withHeader());
    int k = 0;
    for (CSVRecord record : parser) {
        try {
            Double fit = Double.parseDouble(record.get("HWFit"));
            Double lwr = Double.parseDouble(record.get("HWLwr"));
            Double upr = Double.parseDouble(record.get("HWUpr"));

            if (Double.isNaN(fit)) {
                continue;
            }

            forecasts.put(k, "fit", fit);
            forecasts.put(k, "lwr", lwr);
            forecasts.put(k, "upr", upr);

            k++;
        } catch (NumberFormatException e) {
            // pass
        }
    }

    Assert.assertEquals(340, forecasts.rowKeySet().size());
    // First fitted value
    Assert.assertEquals(432.526086422424, forecasts.get(0, "fit"), 0.00001);
    // Last fitted value for which there is a known data point
    Assert.assertEquals(24079.4692522087, forecasts.get(327, "fit"), 0.00001);
    // First forecasted value
    Assert.assertEquals(22245.5417010936, forecasts.get(328, "fit"), 0.00001);
}

From source file:org.qcert.util.DataLoader.java

/**
 * Main program.  /*w  ww.j a  va  2s  . c om*/
 * <p>Command line arguments are
 * <ul><li><b>-dir &lt;path&gt;</b> (optional) the directory in which to find or create all other files, defaults to current directory
 * <li><b>-output &lt;filename&gt;</b> (required) the name of the output file (absolute or relative to <b>-dir</b>)
 * <li><b>-schema &lt;filename&gt;</b> (required) the name of the schema file (absolute or relative to <b>-dir</b>)
 * <li><b>-delimiter &lt;char&gl;</b> (optional) a character to use as delimiter (defaults to comma; if the chose character is shell-sensitive, be sure to quote it)
 * <li>all other arguments are assumed to be type names.  A file of that name with extension <b>.csv</b> must be present in <b>-dir</b>
 * </ul>
 */
public static void main(String[] args) throws Exception {

    /* Parse the command line */
    String directory = null, schema = null, output = null;
    char delimiter = 0;
    List<String> tables = new ArrayList<>();
    boolean dirFlag = false, outFlag = false, schemaFlag = false, delimiterFlag = false;
    for (String arg : args) {
        boolean table = false;
        if (dirFlag)
            directory = arg;
        else if (outFlag)
            output = arg;
        else if (schemaFlag)
            schema = arg;
        else if (delimiterFlag) {
            if (arg.length() != 1)
                throw new IllegalArgumentException("Delimiters must be single character");
            else
                delimiter = arg.charAt(0);
        } else
            table = true;
        dirFlag = outFlag = schemaFlag = delimiterFlag = false;
        if (arg.charAt(0) == '-') {
            switch (arg) {
            case "-dir":
                if (directory != null)
                    throw new IllegalArgumentException("Duplicate -dir");
                else
                    dirFlag = true;
                continue;
            case "-output":
                if (output != null)
                    throw new IllegalArgumentException("Duplicate -output");
                else
                    outFlag = true;
                continue;
            case "-schema":
                if (schema != null)
                    throw new IllegalArgumentException("Duplicate -schema");
                else
                    schemaFlag = true;
                continue;
            case "-delimiter":
                if (delimiter != 0)
                    throw new IllegalArgumentException("Duplicate -delimiter");
                else
                    delimiterFlag = true;
                continue;
            default:
                throw new IllegalArgumentException("Unknown option :" + arg);
            }
        }
        if (table)
            tables.add(arg);
    }
    if (output == null)
        throw new IllegalArgumentException("Output file must be specified");
    if (schema == null)
        throw new IllegalArgumentException("Schema file must be specified");
    File outputFile = new File(output);
    File schemaFile = new File(schema);
    if (directory != null) {
        if (!outputFile.isAbsolute())
            outputFile = new File(directory, output);
        if (!schemaFile.isAbsolute())
            schemaFile = new File(directory, schema);
    }

    /* Canonicalize the file names, read the contents, and associate each with its corresponding table name */
    Map<String, String> tableMap = new HashMap<>();
    for (String table : tables) {
        File toRead = directory == null ? new File(table + ".csv") : new File(directory, table + ".csv");
        String contents = new String(Files.readAllBytes(Paths.get(toRead.getAbsolutePath())));
        tableMap.put(table, contents);
    }

    /* Parse the schema */
    JsonElement jsonSchema = new JsonParser().parse(new FileReader(schemaFile));

    /* Process, using callable service */
    CSVFormat format = CSVFormat.RFC4180;
    if (delimiter != 0)
        format = format.withDelimiter(delimiter);
    JsonObject ans = loadData(tableMap, jsonSchema, format);

    /* Write the result */
    FileWriter wtr = new FileWriter(outputFile);
    JsonWriter json = new JsonWriter(wtr);
    json.setLenient(true);
    json.setIndent("  ");
    Streams.write(ans, json);
    json.close();
}

From source file:org.sonar.scanner.config.DefaultConfiguration.java

public static String[] parseAsCsv(String key, String value) {
    List<String> result = new ArrayList<>();
    try (CSVParser csvParser = CSVFormat.RFC4180.withHeader((String) null).withIgnoreEmptyLines()
            .withIgnoreSurroundingSpaces().parse(new StringReader(value))) {
        List<CSVRecord> records = csvParser.getRecords();
        if (records.isEmpty()) {
            return ArrayUtils.EMPTY_STRING_ARRAY;
        }/*from   ww w.ja  va 2s  .  c o m*/
        processRecords(result, records);
        return result.toArray(new String[result.size()]);
    } catch (IOException e) {
        throw new IllegalStateException(
                "Property: '" + key + "' doesn't contain a valid CSV value: '" + value + "'", e);
    }
}