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 if (factory.getGenerateHeaderColumnNames()) {
067
068 String result = factory.generateHeader();
069 byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result);
070 outputStream.write(bytes);
071
072 // Add a carriage return
073 outputStream.write(bytesCRLF);
074 }
075
076 for (Map<String, Object> model : models) {
077
078 String result = factory.unbind(model);
079
080 byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result);
081 outputStream.write(bytes);
082
083 // Add a carriage return
084 outputStream.write(bytesCRLF);
085 }
086 }
087
088 public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
089 BindyCsvFactory factory = getFactory(exchange.getContext().getPackageScanClassResolver());
090 ObjectHelper.notNull(factory, "not instantiated");
091
092 // List of Pojos
093 List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
094
095 // Pojos of the model
096 Map<String, Object> model;
097
098 InputStreamReader in = new InputStreamReader(inputStream);
099
100 // Scanner is used to read big file
101 Scanner scanner = new Scanner(in);
102
103 // Retrieve the separator defined to split the record
104 String separator = factory.getSeparator();
105 ObjectHelper.notEmpty(separator, "The separator has not been defined in the annotation @CsvRecord or not instantiated during initModel.");
106
107 int count = 0;
108
109 try {
110
111 // If the first line of the CSV file contains columns name, then we
112 // skip this line
113 if (factory.getSkipFirstLine()) {
114
115 // Check if scanner is empty
116 if (scanner.hasNextLine()) {
117 scanner.nextLine();
118 }
119 }
120
121 while (scanner.hasNextLine()) {
122
123 // Read the line
124 String line = scanner.nextLine().trim();
125
126 if (ObjectHelper.isEmpty(line)) {
127 // skip if line is empty
128 continue;
129 }
130
131 // Increment counter
132 count++;
133
134 // Create POJO where CSV data will be stored
135 model = factory.factory();
136
137 // Split the CSV record according to the separator defined in
138 // annotated class @CSVRecord
139 String[] tokens = line.split(separator, -1);
140 List<String> result = Arrays.asList(tokens);
141
142 if (result.size() == 0 || result.isEmpty()) {
143 throw new java.lang.IllegalArgumentException("No records have been defined in the CSV !");
144 }
145
146 if (result.size() > 0) {
147
148 if (LOG.isDebugEnabled()) {
149 LOG.debug("Size of the record splitted : " + result.size());
150 }
151
152 // Bind data from CSV record with model classes
153 factory.bind(result, model, count);
154
155 // Link objects together
156 factory.link(model);
157
158 // Add objects graph to the list
159 models.add(model);
160
161 if (LOG.isDebugEnabled()) {
162 LOG.debug("Graph of objects created : " + model);
163 }
164
165 }
166
167 }
168
169 // Test if models list is empty or not
170 // If this is the case (correspond to an empty stream, ...)
171 if (models.size() == 0) {
172 throw new java.lang.IllegalArgumentException("No records have been defined in the CSV !");
173 } else {
174 return models;
175 }
176
177 } finally {
178 scanner.close();
179 ObjectHelper.close(in, "in", LOG);
180 }
181
182 }
183
184 /**
185 * Method used to create the singleton of the BindyCsvFactory
186 */
187 public BindyCsvFactory getFactory(PackageScanClassResolver resolver) throws Exception {
188 if (modelFactory == null) {
189 modelFactory = new BindyCsvFactory(resolver, packages);
190 }
191 return modelFactory;
192 }
193
194 public void setModelFactory(BindyCsvFactory modelFactory) {
195 this.modelFactory = modelFactory;
196 }
197
198 public String[] getPackages() {
199 return packages;
200 }
201
202 public void setPackages(String[] packages) {
203 this.packages = packages;
204 }
205
206 }