Example usage for org.apache.commons.csv QuoteMode NONE

List of usage examples for org.apache.commons.csv QuoteMode NONE

Introduction

In this page you can find the example usage for org.apache.commons.csv QuoteMode NONE.

Prototype

QuoteMode NONE

To view the source code for org.apache.commons.csv QuoteMode NONE.

Click Source Link

Document

Never quotes fields.

Usage

From source file:com.github.douglasjunior.japriori.datatarget.CSVDataTarget.java

public CSVDataTarget(File file, char delimiter, Charset charset) throws IOException {
    this.file = file;
    this.charset = charset;
    this.csvFormat = CSVFormat.DEFAULT.withDelimiter(delimiter).withIgnoreEmptyLines().withEscape('\\')
            .withQuoteMode(QuoteMode.NONE).withSkipHeaderRecord();
    open();/*  w ww .j a v a  2s. c o  m*/
}

From source file:com.anhth12.lambda.common.text.TextUtils.java

public static String joinPMMLDelimitedNumbers(Iterable<? extends Number> elements) {
    // bit of a workaround because NON_NUMERIC quote mode still quote "-1"!
    CSVFormat format = formatForDelimiter(' ').withQuoteMode(QuoteMode.NONE);
    // No quoting, no need to convert quoting
    return doJoinDelimited(elements, format);
}

From source file:com.bigtester.ate.tcg.controller.TrainingFileDB.java

/**
 * Gets the CSV format./*from   w w  w . ja  v a 2  s .co m*/
 *
 * @return the CSV format
 * @throws IOException
 */
public static CSVFormat getCSVFormat() throws IOException {
    // Create the CSVFormat object with "\n" as a record delimiter
    CSVFormat csvFileFormat = CSVFormat.TDF // NOPMD
            .withRecordSeparator(NEW_LINE_SEPARATOR);
    csvFileFormat = csvFileFormat.withEscape('^');
    csvFileFormat = csvFileFormat.withQuoteMode(QuoteMode.NONE);
    if (null == csvFileFormat)
        throw new IOException();
    return csvFileFormat;
}

From source file:com.lithium.flow.util.CsvFormats.java

@Nullable
private static QuoteMode getQuoteMode(@Nonnull Config config, @Nonnull String key) {
    switch (config.getString(key, "default")) {
    case "default":
        return null;
    case "all":
        return QuoteMode.ALL;
    case "minimal":
        return QuoteMode.MINIMAL;
    case "non_numeric":
        return QuoteMode.NON_NUMERIC;
    case "none":
        return QuoteMode.NONE;
    default://from   ww w .  j a  va  2 s  .co m
        return null;
    }
}

From source file:com.cloudera.oryx.common.text.TextUtils.java

/**
 * @param elements numbers to join by space to make one line of text
 * @return one line of text, formatted according to PMML quoting rules
 *//*from  w w w  . j a v a  2 s  .co m*/
public static String joinPMMLDelimitedNumbers(Iterable<? extends Number> elements) {
    // bit of a workaround because NON_NUMERIC quote mode still quote "-1"!
    CSVFormat format = formatForDelimiter(' ').withQuoteMode(QuoteMode.NONE);
    // No quoting, no need to convert quoting
    return doJoinDelimited(elements, format);
}

From source file:io.dockstore.client.cli.nested.AbstractEntryClient.java

private void launchCwl(String entry, final List<String> args) throws ApiException, IOException {
    boolean isLocalEntry = false;
    if (args.contains("--local-entry")) {
        isLocalEntry = true;//from  w w  w  .j  av  a2s .  com
    }

    final String yamlRun = optVal(args, "--yaml", null);
    String jsonRun = optVal(args, "--json", null);
    final String csvRuns = optVal(args, "--tsv", null);

    if (!(yamlRun != null ^ jsonRun != null ^ csvRuns != null)) {
        errorMessage("One of  --json, --yaml, and --tsv is required", CLIENT_ERROR);
    }

    final File tempDir = Files.createTempDir();
    File tempCWL;
    if (!isLocalEntry) {
        tempCWL = File.createTempFile("temp", ".cwl", tempDir);
    } else {
        tempCWL = new File(entry);
    }

    if (!isLocalEntry) {
        final SourceFile cwlFromServer = getDescriptorFromServer(entry, "cwl");
        Files.write(cwlFromServer.getContent(), tempCWL, StandardCharsets.UTF_8);
        downloadDescriptors(entry, "cwl", tempDir);
    }
    jsonRun = convertYamlToJson(yamlRun, jsonRun);

    final Gson gson = io.cwl.avro.CWL.getTypeSafeCWLToolDocument();
    if (jsonRun != null) {
        // if the root document is an array, this indicates multiple runs
        JsonParser parser = new JsonParser();
        final JsonElement parsed = parser
                .parse(new InputStreamReader(new FileInputStream(jsonRun), StandardCharsets.UTF_8));
        if (parsed.isJsonArray()) {
            final JsonArray asJsonArray = parsed.getAsJsonArray();
            for (JsonElement element : asJsonArray) {
                final String finalString = gson.toJson(element);
                final File tempJson = File.createTempFile("parameter", ".json", Files.createTempDir());
                FileUtils.write(tempJson, finalString, StandardCharsets.UTF_8);
                final LauncherCWL cwlLauncher = new LauncherCWL(getConfigFile(), tempCWL.getAbsolutePath(),
                        tempJson.getAbsolutePath());
                if (this instanceof WorkflowClient) {
                    cwlLauncher.run(Workflow.class);
                } else {
                    cwlLauncher.run(CommandLineTool.class);
                }
            }
        } else {
            final LauncherCWL cwlLauncher = new LauncherCWL(getConfigFile(), tempCWL.getAbsolutePath(),
                    jsonRun);
            if (this instanceof WorkflowClient) {
                cwlLauncher.run(Workflow.class);
            } else {
                cwlLauncher.run(CommandLineTool.class);
            }
        }
    } else if (csvRuns != null) {
        final File csvData = new File(csvRuns);
        try (CSVParser parser = CSVParser.parse(csvData, StandardCharsets.UTF_8,
                CSVFormat.DEFAULT.withDelimiter('\t').withEscape('\\').withQuoteMode(QuoteMode.NONE))) {
            // grab header
            final Iterator<CSVRecord> iterator = parser.iterator();
            final CSVRecord headers = iterator.next();
            // ignore row with type information
            iterator.next();
            // process rows
            while (iterator.hasNext()) {
                final CSVRecord csvRecord = iterator.next();
                final File tempJson = File.createTempFile("temp", ".json", Files.createTempDir());
                StringBuilder buffer = new StringBuilder();
                buffer.append("{");
                for (int i = 0; i < csvRecord.size(); i++) {
                    buffer.append("\"").append(headers.get(i)).append("\"");
                    buffer.append(":");
                    // if the type is an array, just pass it through
                    buffer.append(csvRecord.get(i));

                    if (i < csvRecord.size() - 1) {
                        buffer.append(",");
                    }
                }
                buffer.append("}");
                // prettify it
                JsonParser prettyParser = new JsonParser();
                JsonObject json = prettyParser.parse(buffer.toString()).getAsJsonObject();
                final String finalString = gson.toJson(json);

                // write it out
                FileUtils.write(tempJson, finalString, StandardCharsets.UTF_8);

                // final String stringMapAsString = gson.toJson(stringMap);
                // Files.write(stringMapAsString, tempJson, StandardCharsets.UTF_8);
                final LauncherCWL cwlLauncher = new LauncherCWL(this.getConfigFile(), tempCWL.getAbsolutePath(),
                        tempJson.getAbsolutePath());
                if (this instanceof WorkflowClient) {
                    cwlLauncher.run(Workflow.class);
                } else {
                    cwlLauncher.run(CommandLineTool.class);
                }
            }
        }
    } else {
        errorMessage("Missing required parameters, one of  --json or --tsv is required", CLIENT_ERROR);
    }

}

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

@Test
public void testExtraFieldInWriteRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE)
            .withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new HashMap<>();
    values.put("id", "1");
    values.put("name", "John");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
            RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(),
            RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();//w w w .  j  av a  2s  . c  o m
        writer.write(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id\n1\n", output);
}

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

@Test
public void testExtraFieldInWriteRawRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE)
            .withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    values.put("name", "John");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
            RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(),
            RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();/*from  w  w  w. j  av a 2  s .co  m*/
        writer.writeRawRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name\n1,John\n", output);
}

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

@Test
public void testMissingFieldWriteRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE)
            .withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
            RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(),
            RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();/*from   w ww. ja  v a  2s .  co  m*/
        writer.writeRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name\n1,\n", output);
}

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

@Test
public void testMissingFieldWriteRawRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE)
            .withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
            RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(),
            RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();// www . ja v a 2  s  .c om
        writer.writeRawRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name\n1,\n", output);
}