outputStream.write(bytesCRLF);
}
}
public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
BindyCsvFactory factory = 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);
// Scanner is used to read big file
Scanner scanner = new Scanner(in);
// Retrieve the separator defined to split the record
String separator = factory.getSeparator();
ObjectHelper.notEmpty(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();
// Added for camel- jira ticket
// We will remove the first and last character of the line
// when the separator contains quotes, double quotes
// e.g. ',' or "," ...
// REMARK : We take the assumption that the data fields are
// quoted or double quoted like that
// e.g : "1 ", "street 1, NY", "USA"
if (separator.length() > 1) {
String tempLine = line.substring(1, line.length() - 1);
line = tempLine;
}
// 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);
if (result.size() == 0 || result.isEmpty()) {
throw new java.lang.IllegalArgumentException("No records have been defined in the CSV !");
}
if (result.size() > 0) {
if (LOG.isDebugEnabled()) {
LOG.debug("Size of the record splitted : " + result.size());
}
// 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);
if (LOG.isDebugEnabled()) {