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

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

Introduction

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

Prototype

public CSVFormat withNullString(final String nullString) 

Source Link

Document

Performs conversions to and from null for strings on input and output.

Usage

From source file:org.apache.batchee.csv.CSVFormatFactory.java

static CSVFormat newFormat(final String format, final String delimiter, final String quoteCharacter,
        final String quoteMode, final String commentMarker, final String escapeCharacter,
        final String ignoreSurroundingSpaces, final String ignoreEmptyLines, final String recordSeparator,
        final String nullString, final String headerComments, final String header,
        final String skipHeaderRecord, final String allowMissingColumnNames, final String readHeaders) {
    //CHECKSTYLE:ON
    CSVFormat out = format == null ? CSVFormat.DEFAULT : CSVFormat.valueOf(format);
    if (delimiter != null) {
        out = out.withDelimiter(delimiter.charAt(0));
    }/* w  w  w.  j  a v a  2s  .  c o m*/
    if (quoteCharacter != null) {
        out = out.withQuote(quoteCharacter.charAt(0));
    }
    if (quoteMode != null) {
        out = out.withQuoteMode(QuoteMode.valueOf(quoteMode));
    }
    if (commentMarker != null) {
        out = out.withCommentMarker(commentMarker.charAt(0));
    }
    if (escapeCharacter != null) {
        out = out.withEscape(escapeCharacter.charAt(0));
    }
    if (ignoreSurroundingSpaces != null) {
        out = out.withIgnoreSurroundingSpaces(Boolean.parseBoolean(ignoreSurroundingSpaces));
    }
    if (ignoreEmptyLines != null) {
        out = out.withIgnoreEmptyLines(Boolean.parseBoolean(ignoreEmptyLines));
    }
    if (recordSeparator != null) {
        if ("\\n".equals(recordSeparator)) {
            out = out.withRecordSeparator('\n');
        } else if ("\\r\\n".equals(recordSeparator)) {
            out = out.withRecordSeparator("\r\n");
        } else {
            out = out.withRecordSeparator(recordSeparator);
        }
    }
    if (nullString != null) {
        out = out.withNullString(nullString);
    }
    if (headerComments != null && !headerComments.trim().isEmpty()) {
        out = out.withHeaderComments(headerComments.split(" *, *"));
    }
    if (Boolean.parseBoolean(readHeaders)) {
        out = out.withHeader();
    }
    if (header != null && !header.trim().isEmpty()) {
        try { // headers can have CSV header names so parse it there
            final Iterator<CSVRecord> iterator = out.withHeader(new String[0])
                    .parse(new StringReader(header + '\n' + header)).iterator();
            final CSVRecord record = iterator.next();
            final List<String> list = new ArrayList<String>(record.size());
            for (final String h : record) {
                list.add(h);
            }
            out = out.withHeader(list.toArray(new String[record.size()]));
        } catch (final IOException e) { // can't occur actually
            out = out.withHeader(header.split(" *, *"));
        }
    }
    if (skipHeaderRecord != null) {
        out = out.withSkipHeaderRecord(Boolean.parseBoolean(skipHeaderRecord));
    }
    if (allowMissingColumnNames != null) {
        out = out.withAllowMissingColumnNames(Boolean.parseBoolean(allowMissingColumnNames));
    }
    return out;
}

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

protected static CSVFormat createFormat(final String format, final Character delimiter, final Character escape,
        final Character quote, final QuoteMode quoteMode, final String nullString,
        final String recordSeparator) {
    CSVFormat csvFormat = CSVFormat.valueOf(format);
    if (isNotNul(delimiter)) {
        csvFormat = csvFormat.withDelimiter(delimiter);
    }/*from   w w w.  jav  a 2  s  .co  m*/
    if (isNotNul(escape)) {
        csvFormat = csvFormat.withEscape(escape);
    }
    if (isNotNul(quote)) {
        csvFormat = csvFormat.withQuote(quote);
    }
    if (quoteMode != null) {
        csvFormat = csvFormat.withQuoteMode(quoteMode);
    }
    if (nullString != null) {
        csvFormat = csvFormat.withNullString(nullString);
    }
    if (recordSeparator != null) {
        csvFormat = csvFormat.withRecordSeparator(recordSeparator);
    }
    return csvFormat;
}

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

private static CSVFormat buildCustomFormat(final PropertyContext context) {
    final char valueSeparator = getUnescapedChar(context, VALUE_SEPARATOR);
    CSVFormat format = CSVFormat.newFormat(valueSeparator).withAllowMissingColumnNames().withIgnoreEmptyLines();

    final PropertyValue skipHeaderPropertyValue = context.getProperty(FIRST_LINE_IS_HEADER);
    if (skipHeaderPropertyValue.getValue() != null && skipHeaderPropertyValue.asBoolean()) {
        format = format.withFirstRecordAsHeader();
    }/*from  ww  w  .  j a  v  a 2 s.  c  o  m*/

    format = format.withQuote(getChar(context, QUOTE_CHAR));
    format = format.withEscape(getChar(context, ESCAPE_CHAR));
    format = format.withTrim(context.getProperty(TRIM_FIELDS).asBoolean());

    if (context.getProperty(COMMENT_MARKER).isSet()) {
        format = format.withCommentMarker(getChar(context, COMMENT_MARKER));
    }
    if (context.getProperty(NULL_STRING).isSet()) {
        format = format.withNullString(CSVUtils.unescape(context.getProperty(NULL_STRING).getValue()));
    }

    final PropertyValue quoteValue = context.getProperty(QUOTE_MODE);
    if (quoteValue != null) {
        final QuoteMode quoteMode = QuoteMode.valueOf(quoteValue.getValue());
        format = format.withQuoteMode(quoteMode);
    }

    final PropertyValue trailingDelimiterValue = context.getProperty(TRAILING_DELIMITER);
    if (trailingDelimiterValue != null) {
        final boolean trailingDelimiter = trailingDelimiterValue.asBoolean();
        format = format.withTrailingDelimiter(trailingDelimiter);
    }

    final PropertyValue recordSeparator = context.getProperty(RECORD_SEPARATOR);
    if (recordSeparator != null) {
        final String separator = unescape(recordSeparator.getValue());
        format = format.withRecordSeparator(separator);
    }

    return format;
}