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.HashMap;
025    import java.util.Iterator;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Scanner;
029    
030    import org.apache.camel.Exchange;
031    import org.apache.camel.dataformat.bindy.BindyCsvFactory;
032    import org.apache.camel.dataformat.bindy.util.Converter;
033    import org.apache.camel.spi.DataFormat;
034    import org.apache.camel.spi.PackageScanClassResolver;
035    import org.apache.camel.util.IOHelper;
036    import org.apache.camel.util.ObjectHelper;
037    import org.apache.commons.logging.Log;
038    import org.apache.commons.logging.LogFactory;
039    
040    /**
041     * A <a href="http://camel.apache.org/data-format.html">data format</a> (
042     * {@link DataFormat}) using Bindy to marshal to and from CSV files
043     */
044    public class BindyCsvDataFormat implements DataFormat {
045        private static final transient Log LOG = LogFactory.getLog(BindyCsvDataFormat.class);
046    
047        private String[] packages;
048        private BindyCsvFactory modelFactory;
049    
050        public BindyCsvDataFormat() {
051        }
052    
053        public BindyCsvDataFormat(String... packages) {
054            this.packages = packages;
055        }
056    
057        @SuppressWarnings("unchecked")
058        public void marshal(Exchange exchange, Object body, OutputStream outputStream) throws Exception {
059    
060            BindyCsvFactory factory = getFactory(exchange.getContext().getPackageScanClassResolver());
061            ObjectHelper.notNull(factory, "not instantiated");
062    
063            // Get CRLF
064            byte[] 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            List<Map<String, Object>> models;
077    
078            // the body is not a prepared list so help a bit here and create one for us
079            if (exchange.getContext().getTypeConverter().convertTo(List.class, body) == null) {
080                models = new ArrayList<Map<String, Object>>();
081                Iterator it = ObjectHelper.createIterator(body);
082                while (it.hasNext()) {
083                    Object model = it.next();
084                    String name = model.getClass().getName();
085                    Map<String, Object> row = new HashMap<String, Object>();
086                    row.put(name, body);
087                    models.add(row);
088                }
089            } else {
090                // cast to the expected type
091                models = (List<Map<String, Object>>) body;
092            }
093    
094            for (Map<String, Object> model : models) {
095    
096                String result = factory.unbind(model);
097    
098                byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result);
099                outputStream.write(bytes);
100    
101                // Add a carriage return
102                outputStream.write(bytesCRLF);
103            }
104        }
105    
106        public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
107            BindyCsvFactory factory = getFactory(exchange.getContext().getPackageScanClassResolver());
108            ObjectHelper.notNull(factory, "not instantiated");
109    
110            // List of Pojos
111            List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
112    
113            // Pojos of the model
114            Map<String, Object> model;
115    
116            InputStreamReader in = new InputStreamReader(inputStream);
117    
118            // Scanner is used to read big file
119            Scanner scanner = new Scanner(in);
120    
121            // Retrieve the separator defined to split the record
122            String separator = factory.getSeparator();
123            ObjectHelper.notNull(separator, "The separator has not been defined in the annotation @CsvRecord or not instantiated during initModel.");
124    
125            int count = 0;
126    
127            try {
128    
129                // If the first line of the CSV file contains columns name, then we
130                // skip this line
131                if (factory.getSkipFirstLine()) {
132    
133                    // Check if scanner is empty
134                    if (scanner.hasNextLine()) {
135                        scanner.nextLine();
136                    }
137                }
138    
139                while (scanner.hasNextLine()) {
140    
141                    // Read the line
142                    String line = scanner.nextLine().trim();
143    
144                    if (ObjectHelper.isEmpty(line)) {
145                        // skip if line is empty
146                        continue;
147                    }
148    
149                    // Increment counter
150                    count++;
151    
152                    // Create POJO where CSV data will be stored
153                    model = factory.factory();
154                    
155                    // Added for camel- jira ticket
156                    // We will remove the first and last character  of the line
157                    // when the separator contains quotes, double quotes 
158                    // e.g. ',' or "," ...
159                    // REMARK : We take the assumption that the data fields are
160                    // quoted or double quoted like that 
161                    // e.g : "1 ", "street 1, NY", "USA"
162                    if (separator.length() > 1) {
163                        String tempLine = line.substring(1, line.length() - 1);
164                        line = tempLine;
165                    }
166                    // Split the CSV record according to the separator defined in
167                    // annotated class @CSVRecord
168                    String[] tokens = line.split(separator, -1);
169                    List<String> result = Arrays.asList(tokens);
170    
171                    if (result.size() == 0 || result.isEmpty()) {
172                        throw new java.lang.IllegalArgumentException("No records have been defined in the CSV !");
173                    }
174    
175                    if (result.size() > 0) {
176    
177                        if (LOG.isDebugEnabled()) {
178                            LOG.debug("Size of the record splitted : " + result.size());
179                        }
180    
181                        // Bind data from CSV record with model classes
182                        factory.bind(result, model, count);
183    
184                        // Link objects together
185                        factory.link(model);
186    
187                        // Add objects graph to the list
188                        models.add(model);
189    
190                        if (LOG.isDebugEnabled()) {
191                            LOG.debug("Graph of objects created : " + model);
192                        }
193    
194                    }
195    
196                }
197    
198                // Test if models list is empty or not
199                // If this is the case (correspond to an empty stream, ...)
200                if (models.size() == 0) {
201                    throw new java.lang.IllegalArgumentException("No records have been defined in the CSV !");
202                } else {
203                    return models;
204                }
205    
206            } finally {
207                scanner.close();
208                IOHelper.close(in, "in", LOG);
209            }
210    
211        }
212    
213        /**
214         * Method used to create the singleton of the BindyCsvFactory
215         */
216        public BindyCsvFactory getFactory(PackageScanClassResolver resolver) throws Exception {
217            if (modelFactory == null) {
218                modelFactory = new BindyCsvFactory(resolver, packages);
219            }
220            return modelFactory;
221        }
222    
223        public void setModelFactory(BindyCsvFactory modelFactory) {
224            this.modelFactory = modelFactory;
225        }
226    
227        public String[] getPackages() {
228            return packages;
229        }
230    
231        public void setPackages(String[] packages) {
232            this.packages = packages;
233        }
234    
235    }