Example usage for com.fasterxml.jackson.dataformat.csv CsvSchema withEscapeChar

List of usage examples for com.fasterxml.jackson.dataformat.csv CsvSchema withEscapeChar

Introduction

In this page you can find the example usage for com.fasterxml.jackson.dataformat.csv CsvSchema withEscapeChar.

Prototype

public CsvSchema withEscapeChar(char c) 

Source Link

Usage

From source file:org.jberet.support.io.JacksonCsvItemReader.java

@Override
public void open(final Serializable checkpoint) throws Exception {
    if (end == 0) {
        end = Integer.MAX_VALUE;//from  w w w.j av a 2 s  .co  m
    }
    if (checkpoint != null) {
        start = (Integer) checkpoint;
    }
    if (start > end) {
        throw SupportMessages.MESSAGES.invalidStartPosition((Integer) checkpoint, start, end);
    }
    init();
    csvParser = (CsvParser) JsonItemReader.configureJsonParser(this, inputDecorator,
            deserializationProblemHandlers, jsonParserFeatures);

    if (csvParserFeatures != null) {
        for (final Map.Entry<String, String> e : csvParserFeatures.entrySet()) {
            final String key = e.getKey();
            final String value = e.getValue();
            final CsvParser.Feature feature;
            try {
                feature = CsvParser.Feature.valueOf(key);
            } catch (final Exception e1) {
                throw SupportMessages.MESSAGES.unrecognizedReaderWriterProperty(key, value);
            }
            if ("true".equals(value)) {
                if (!feature.enabledByDefault()) {
                    csvParser.configure(feature, true);
                }
            } else if ("false".equals(value)) {
                if (feature.enabledByDefault()) {
                    csvParser.configure(feature, false);
                }
            } else {
                throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, value, key);
            }
        }
    }

    rawAccess = beanType == List.class || beanType == String[].class;
    if (!rawAccess) {
        CsvSchema schema;
        if (columns != null) {
            schema = buildCsvSchema(null);
        } else {
            //columns not defined, but beanType is either Map.class or Pojo.class or JsonNode.class
            if (useHeader) {
                schema = buildCsvSchema(CsvSchema.emptySchema());
            } else {
                throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, columns, "columns");
            }
        }

        if (escapeChar != null) {
            schema = schema.withEscapeChar(escapeChar.charAt(0));
        }
        if (skipFirstDataRow != null) {
            schema = schema.withSkipFirstDataRow(Boolean.parseBoolean(skipFirstDataRow.trim()));
        }
        csvParser.setSchema(schema);
    }
}