001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.dataformat.bindy.csv;
018
019 import java.io.InputStream;
020 import java.io.InputStreamReader;
021 import java.io.OutputStream;
022 import java.util.ArrayList;
023 import java.util.Arrays;
024 import java.util.List;
025 import java.util.Map;
026 import java.util.Scanner;
027
028 import org.apache.camel.Exchange;
029 import org.apache.camel.dataformat.bindy.BindyCsvFactory;
030 import org.apache.camel.dataformat.bindy.util.Converter;
031 import org.apache.camel.spi.DataFormat;
032 import org.apache.camel.spi.PackageScanClassResolver;
033 import org.apache.camel.util.ObjectHelper;
034 import org.apache.commons.logging.Log;
035 import org.apache.commons.logging.LogFactory;
036
037 /**
038 * A <a href="http://camel.apache.org/data-format.html">data format</a>
039 * ({@link DataFormat}) using Bindy to marshal to and from CSV files
040 */
041 public class BindyCsvDataFormat implements DataFormat {
042 private static final transient Log LOG = LogFactory.getLog(BindyCsvDataFormat.class);
043
044 private String[] packages;
045 private BindyCsvFactory modelFactory;
046
047 public BindyCsvDataFormat() {
048 }
049
050 public BindyCsvDataFormat(String... packages) {
051 this.packages = packages;
052 }
053
054 @SuppressWarnings("unchecked")
055 public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception {
056
057 BindyCsvFactory factory = getFactory(exchange.getContext().getPackageScanClassResolver());
058 ObjectHelper.notNull(factory, "not instantiated");
059
060 List<Map<String, Object>> models = (ArrayList<Map<String, Object>>) body;
061 byte[] bytesCRLF;
062
063 // Get CRLF
064 bytesCRLF = Converter.getByteReturn(factory.getCarriageReturn());
065
066 for (Map<String, Object> model : models) {
067 String result = factory.unbind(model);
068 byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result);
069 outputStream.write(bytes);
070
071 // Add a carriage return
072 outputStream.write(bytesCRLF);
073 }
074 }
075
076 public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
077 BindyCsvFactory factory = getFactory(exchange.getContext().getPackageScanClassResolver());
078 ObjectHelper.notNull(factory, "not instantiated");
079
080 // List of Pojos
081 List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
082
083 // Pojos of the model
084 Map<String, Object> model;
085
086 InputStreamReader in = new InputStreamReader(inputStream);
087
088 // Scanner is used to read big file
089 Scanner scanner = new Scanner(in);
090
091 // Retrieve the separator defined to split the record
092 String separator = factory.getSeparator();
093 ObjectHelper.notEmpty(separator, "The separator has not been defined in the annotation @CsvRecord or not instantiated during initModel.");
094
095 int count = 0;
096 try {
097
098 // If the first line of the CSV file contains columns name, then we skip this line
099 if (factory.getSkipFirstLine()) {
100 scanner.nextLine();
101 }
102
103 while (scanner.hasNextLine()) {
104
105 // Read the line
106 String line = scanner.nextLine().trim();
107
108 if (ObjectHelper.isEmpty(line)) {
109 // skip if line is empty
110 continue;
111 }
112
113 if (LOG.isDebugEnabled()) {
114 LOG.debug("Counter " + count++ + " : content : " + line);
115 }
116
117 // Create POJO where CSV data will be stored
118 model = factory.factory();
119
120 // Split the CSV record according to the separator defined in
121 // annotated class @CSVRecord
122 String[] tokens = line.split(separator, -1);
123 List<String> result = Arrays.asList(tokens);
124
125 if (result.size() == 0 || result.isEmpty()) {
126 throw new java.lang.IllegalArgumentException("No records have been defined in the CSV !");
127 }
128
129 if (result.size() > 0) {
130
131 if (LOG.isDebugEnabled()) {
132 LOG.debug("Size of the record splitted : " + result.size());
133 }
134
135 // Bind data from CSV record with model classes
136 factory.bind(result, model);
137
138 // Link objects together
139 factory.link(model);
140
141 // Add objects graph to the list
142 models.add(model);
143
144 if (LOG.isDebugEnabled()) {
145 LOG.debug("Graph of objects created : " + model);
146 }
147
148 }
149
150 }
151
152 // Test if models list is empty or not
153 // If this is the case (correspond to an empty stream, ...)
154 if (models.size() == 0) {
155 throw new java.lang.IllegalArgumentException("No records have been defined in the CSV !");
156 } else {
157 return models;
158 }
159
160 } finally {
161 scanner.close();
162 ObjectHelper.close(in, "in", LOG);
163 }
164
165 }
166
167 /**
168 * Method used to create the singleton of the BindyCsvFactory
169 */
170 public BindyCsvFactory getFactory(PackageScanClassResolver resolver) throws Exception {
171 if (modelFactory == null) {
172 modelFactory = new BindyCsvFactory(resolver, packages);
173 }
174 return modelFactory;
175 }
176
177 public void setModelFactory(BindyCsvFactory modelFactory) {
178 this.modelFactory = modelFactory;
179 }
180
181 public String[] getPackages() {
182 return packages;
183 }
184
185 public void setPackages(String[] packages) {
186 this.packages = packages;
187 }
188
189 }