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

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

Introduction

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

Prototype

public CsvSchema withHeader() 

Source Link

Document

Helper method for construcing and returning schema instance that is similar to this one, except that it will be using header line.

Usage

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

protected CsvSchema buildCsvSchema(CsvSchema schema) throws Exception {
    if (schema == null) {
        columns = columns.trim();//ww  w  .  j a  va  2 s.c  om
        if (columns.indexOf(',') < 0 && columns.indexOf(' ') < 0) {
            //no comma and no space, assume it's java class name for schema
            schema = csvMapper.schemaFor(getClass().getClassLoader().loadClass(columns));
        } else {
            //manually build CsvSchema
            final String[] cols = columns.split(",");
            final CsvSchema.Builder builder = new CsvSchema.Builder();
            for (String e : cols) {
                e = e.trim();
                final int lastSpace = e.lastIndexOf(' ');
                if (lastSpace > 0) {
                    final String e1 = e.substring(0, lastSpace).trim();
                    final String e2 = e.substring(lastSpace + 1);
                    builder.addColumn(e1, CsvSchema.ColumnType.valueOf(e2));
                } else {
                    builder.addColumn(e);
                }
            }
            schema = builder.build();
        }
    }

    schema = useHeader ? schema.withHeader() : schema.withoutHeader();

    if (columnSeparator != null) {
        schema = schema.withColumnSeparator(columnSeparator.charAt(0));
    }
    if (quoteChar != null) {
        schema = schema.withQuoteChar(quoteChar.charAt(0));
    }
    if (nullValue != null) {
        schema = schema.withNullValue(nullValue);
    }

    //to allow comments like "# this is comments".
    //comments can be enabled or disabled with com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_YAML_COMMENTS
    //which corresponds to batch property jsonParserFeatures
    return schema.withComments();
}

From source file:org.craftercms.deployer.impl.processors.FileOutputProcessor.java

@Override
protected void doExecute(Deployment deployment) throws DeployerException {
    File outputFile = getOutputFile(deployment);
    CsvSchema formatSchema = objectMapper.schemaFor(Deployment.class);
    boolean useHeaders = !Files.exists(outputFile.toPath());
    try (FileWriter fileWriter = new FileWriter(outputFile, true)) {
        if (useHeaders) {
            formatSchema = formatSchema.withHeader();
        }/*w ww .ja  v a2  s. co m*/
        objectMapper.writer(formatSchema).writeValue(fileWriter, deployment);
    } catch (IOException e) {
        throw new DeployerException("Error while writing deployment output file " + outputFile, e);
    }

    deployment.addParam(OUTPUT_FILE_PARAM_NAME, outputFile);

    logger.info("Successfully wrote deployment output to {}", outputFile);
}