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

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

Introduction

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

Prototype

public CSVFormat withQuoteMode(final QuoteMode quoteModePolicy) 

Source Link

Document

Sets the output quote policy of the format to the specified value.

Usage

From source file:org.structr.function.FromCsvFunction.java

@Override
public Object apply(ActionContext ctx, final GraphObject entity, final Object[] sources) {

    if (sources != null && sources.length > 0) {

        if (sources[0] != null) {

            try {

                final List<Map<String, String>> objects = new LinkedList<>();
                final String source = sources[0].toString();
                String delimiter = ";";
                String quoteChar = "\"";
                String recordSeparator = "\n";

                switch (sources.length) {

                case 4:
                    recordSeparator = (String) sources[3];
                case 3:
                    quoteChar = (String) sources[2];
                case 2:
                    delimiter = (String) sources[1];
                    break;
                }/*ww w  .j ava 2  s .co m*/

                CSVFormat format = CSVFormat.newFormat(delimiter.charAt(0)).withHeader();
                format = format.withQuote(quoteChar.charAt(0));
                format = format.withRecordSeparator(recordSeparator);
                format = format.withIgnoreEmptyLines(true);
                format = format.withIgnoreSurroundingSpaces(true);
                format = format.withSkipHeaderRecord(true);
                format = format.withQuoteMode(QuoteMode.ALL);

                CSVParser parser = new CSVParser(new StringReader(source), format);
                for (final CSVRecord record : parser.getRecords()) {

                    objects.add(record.toMap());
                }

                return objects;

            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

        return "";
    }

    return usage(ctx.isJavaScriptContext());
}

From source file:trainer.userinput.TrainingFileDB.java

public static CSVFormat getCSVFormat() {
    // Create the CSVFormat object with "\n" as a record delimiter
    CSVFormat csvFileFormat = CSVFormat.TDF.withRecordSeparator(NEW_LINE_SEPARATOR);
    csvFileFormat = csvFileFormat.withEscape('^');
    csvFileFormat = csvFileFormat.withQuoteMode(QuoteMode.NONE);
    return csvFileFormat;
}