Java Code Examples for org.apache.commons.csv.CSVFormat#withEscape()

The following examples show how to use org.apache.commons.csv.CSVFormat#withEscape() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractCsvLayout.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
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);
    }
    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;
}
 
Example 2
Source File: ToCsvPragmaInstance.java    From CQL with GNU Affero General Public License v3.0 5 votes vote down vote up
public static CSVFormat getFormat(AqlOptions op) {
	String format0 = "Default";
	CSVFormat format = CSVFormat.valueOf(format0);

	format = format.withDelimiter((Character) op.getOrDefault(AqlOption.csv_field_delim_char));
	format = format.withQuote((Character) op.getOrDefault(AqlOption.csv_quote_char));
	format = format.withEscape((Character) op.getOrDefault(AqlOption.csv_escape_char));
	format = format.withQuoteMode(QuoteMode.ALL);
	format = format.withNullString(null);

	return format;
}
 
Example 3
Source File: CSVCommonsLoader.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * default settings
 * delimiter = ','
 * quoteChar = '"',
 * escape = null
 * recordSeparator = CRLF, CR, or LF
 * ignore empty lines allows the last data line to have a recordSeparator
 *
 * @return CSVFormat based on constructor settings.
 */
private CSVFormat buildFormat() {
    CSVFormat format = CSVFormat.DEFAULT
            .withIgnoreEmptyLines(true)
            .withDelimiter(asControlCharacter(fieldDelimiter))
            .withQuote(asControlCharacter(quoteCharacter));

    if (escapeCharacter != null) {
        format = format.withEscape(asControlCharacter(escapeCharacter));
    }

    switch(headerSource) {
    case FROM_TABLE:
        // obtain headers from table, so format should not expect a header.
        break;
    case IN_LINE:
        // an empty string array triggers csv loader to grab the first line as the header
        format = format.withHeader(new String[0]);
        break;
    case SUPPLIED_BY_USER:
        // a populated string array supplied by the user
        format = format.withHeader(columns.toArray(new String[columns.size()]));
        break;
    default:
        throw new RuntimeException("Header source was unable to be inferred.");

    }
    return format;
}
 
Example 4
Source File: CSVCommonsLoader.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * default settings
 * delimiter = ','
 * quoteChar = '"',
 * escape = null
 * recordSeparator = CRLF, CR, or LF
 * ignore empty lines allows the last data line to have a recordSeparator
 *
 * @return CSVFormat based on constructor settings.
 */
private CSVFormat buildFormat() {
    CSVFormat format = CSVFormat.DEFAULT
            .withIgnoreEmptyLines(true)
            .withDelimiter(asControlCharacter(fieldDelimiter))
            .withQuote(asControlCharacter(quoteCharacter));

    if (escapeCharacter != null) {
        format = format.withEscape(asControlCharacter(escapeCharacter));
    }

    switch(headerSource) {
    case FROM_TABLE:
        // obtain headers from table, so format should not expect a header.
        break;
    case IN_LINE:
        // an empty string array triggers csv loader to grab the first line as the header
        format = format.withHeader(new String[0]);
        break;
    case SUPPLIED_BY_USER:
        // a populated string array supplied by the user
        format = format.withHeader(columns.toArray(new String[columns.size()]));
        break;
    default:
        throw new RuntimeException("Header source was unable to be inferred.");

    }
    return format;
}
 
Example 5
Source File: CSVUtils.java    From nifi with Apache License 2.0 4 votes vote down vote up
private static CSVFormat buildCustomFormat(final PropertyContext context, final Map<String, String> variables) {
    final Character valueSeparator = getCharUnescapedJava(context, VALUE_SEPARATOR, variables);
    CSVFormat format = CSVFormat.newFormat(valueSeparator)
        .withAllowMissingColumnNames()
        .withIgnoreEmptyLines();

    final PropertyValue firstLineIsHeaderPropertyValue = context.getProperty(FIRST_LINE_IS_HEADER);
    if (firstLineIsHeaderPropertyValue.getValue() != null && firstLineIsHeaderPropertyValue.asBoolean()) {
        format = format.withFirstRecordAsHeader();
    }

    final Character quoteChar = getCharUnescaped(context, QUOTE_CHAR, variables);
    format = format.withQuote(quoteChar);

    final Character escapeChar = getCharUnescaped(context, ESCAPE_CHAR, variables);
    format = format.withEscape(escapeChar);

    format = format.withTrim(context.getProperty(TRIM_FIELDS).asBoolean());

    if (context.getProperty(COMMENT_MARKER).isSet()) {
        final Character commentMarker = getCharUnescaped(context, COMMENT_MARKER, variables);
        if (commentMarker != null) {
            format = format.withCommentMarker(commentMarker);
        }
    }
    if (context.getProperty(NULL_STRING).isSet()) {
        format = format.withNullString(unescape(context.getProperty(NULL_STRING).getValue()));
    }

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

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

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

    return format;
}
 
Example 6
Source File: CsvLoader.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@JsonCreator
public CsvLoader(@JsonProperty("config") Map<String, String> config) {
  CSVFormat format = CSVFormat.EXCEL;
  this.config = config;
  if (config.containsKey("delimiter")) {
    format = format.withDelimiter(onlyChar(config, "delimiter"));
  }
  if (config.containsKey("quoteChar")) {
    format = format.withQuote(onlyChar(config, "quoteChar"));
  }
  if (config.containsKey("quoteMode")) {
    format = format.withQuoteMode(QuoteMode.valueOf(config.get("quoteMode")));
  }
  if (config.containsKey("commentStart")) {
    format = format.withCommentMarker(onlyChar(config, "commentStart"));
  }
  if (config.containsKey("escape")) {
    format = format.withEscape(onlyChar(config, "escape"));
  }
  if (config.containsKey("ignoreSurroundingSpaces")) {
    format = format.withIgnoreSurroundingSpaces(config.get("ignoreSurroundingSpaces").equals("true"));
  }
  if (config.containsKey("ignoreEmptyLines")) {
    format = format.withIgnoreEmptyLines(config.get("ignoreEmptyLines").equals("true"));
  }
  if (config.containsKey("recordSeparator")) {
    format = format.withRecordSeparator(config.get("recordSeparator"));
  }
  if (config.containsKey("nullString")) {
    format = format.withNullString(config.get("nullString"));
  }
  if (config.containsKey("trim")) {
    format = format.withTrim(config.get("trim").equals("true"));
  }
  if (config.containsKey("trailingDelimiter")) {
    format = format.withTrailingDelimiter(config.get("trailingDelimiter").equals("true"));
  }
  this.format = format
    .withAllowMissingColumnNames()
    .withHeader();
}
 
Example 7
Source File: CSVFormatFactory.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
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));
        }
        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;
    }