outputStream.write(bytesCRLF);
}
}
public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
BindyCsvFactory factory = (BindyCsvFactory)getFactory(exchange.getContext().getPackageScanClassResolver());
ObjectHelper.notNull(factory, "not instantiated");
// List of Pojos
List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
// Pojos of the model
Map<String, Object> model;
InputStreamReader in = new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange));
// Scanner is used to read big file
Scanner scanner = new Scanner(in);
// Retrieve the separator defined to split the record
String separator = factory.getSeparator();
String quote = factory .getQuote();
ObjectHelper.notNull(separator, "The separator has not been defined in the annotation @CsvRecord or not instantiated during initModel.");
int count = 0;
try {
// If the first line of the CSV file contains columns name, then we
// skip this line
if (factory.getSkipFirstLine()) {
// Check if scanner is empty
if (scanner.hasNextLine()) {
scanner.nextLine();
}
}
while (scanner.hasNextLine()) {
// Read the line
String line = scanner.nextLine().trim();
if (ObjectHelper.isEmpty(line)) {
// skip if line is empty
continue;
}
// Increment counter
count++;
// Create POJO where CSV data will be stored
model = factory.factory();
// Split the CSV record according to the separator defined in
// annotated class @CSVRecord
String[] tokens = line.split(separator, -1);
List<String> result = Arrays.asList(tokens);
// must unquote tokens before use
result = unquoteTokens(result, separator, quote);
if (result.size() == 0 || result.isEmpty()) {
throw new java.lang.IllegalArgumentException("No records have been defined in the CSV");
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Size of the record splitted : {}", result.size());
}
if (factory.getAutospanLine()) {
result = autospanLine(result, factory.getMaxpos(), separator);
}
// Bind data from CSV record with model classes
factory.bind(result, model, count);
// Link objects together
factory.link(model);
// Add objects graph to the list
models.add(model);
LOG.debug("Graph of objects created: {}", model);