Example usage for org.apache.commons.csv.writer CSVWriter CSVWriter

List of usage examples for org.apache.commons.csv.writer CSVWriter CSVWriter

Introduction

In this page you can find the example usage for org.apache.commons.csv.writer CSVWriter CSVWriter.

Prototype

public CSVWriter(CSVConfig config) 

Source Link

Usage

From source file:org.apache.camel.dataformat.csv.CsvDataFormat.java

public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws Exception {
    if (delimiter != null) {
        config.setDelimiter(delimiter.charAt(0));
    }// w ww. ja v a2 s. co m

    OutputStreamWriter out = new OutputStreamWriter(outputStream, IOHelper.getCharsetName(exchange));
    CSVWriter csv = new CSVWriter(config);
    csv.setWriter(out);

    try {
        List<?> list = ExchangeHelper.convertToType(exchange, List.class, object);
        if (list != null) {
            for (Object child : list) {
                Map<?, ?> row = ExchangeHelper.convertToMandatoryType(exchange, Map.class, child);
                doMarshalRecord(exchange, row, out, csv);
            }
        } else {
            Map<?, ?> row = ExchangeHelper.convertToMandatoryType(exchange, Map.class, object);
            doMarshalRecord(exchange, row, out, csv);
        }
    } finally {
        IOHelper.close(out);
    }
}