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 packageName;
045        private BindyCsvFactory modelFactory;
046    
047        public BindyCsvDataFormat() {
048        }
049    
050        public BindyCsvDataFormat(String packageName) {
051            this.packageName = packageName;
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            List<Map<String, Object>> models = (ArrayList<Map<String, Object>>) body;
059            byte[] bytesCRLF;
060         
061            // Get CRLF
062            bytesCRLF = Converter.getByteReturn(factory.getCarriageReturn());
063    
064            for (Map<String, Object> model : models) {
065                String result = factory.unbind(model);
066                byte[] bytes = exchange.getContext().getTypeConverter().convertTo(byte[].class, exchange, result);
067                outputStream.write(bytes);
068                
069                // Add a carriage return
070                outputStream.write(bytesCRLF);
071            }
072        }
073    
074        public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
075            BindyCsvFactory factory = getFactory(exchange.getContext().getPackageScanClassResolver());
076    
077            // List of Pojos
078            List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
079    
080            // Pojos of the model
081            Map<String, Object> model;
082    
083            InputStreamReader in = new InputStreamReader(inputStream);
084    
085            // Scanner is used to read big file
086            Scanner scanner = new Scanner(in);
087    
088            // Retrieve the separator defined to split the record
089            String separator = factory.getSeparator();
090            ObjectHelper.notEmpty(separator, "The separator has not been defined in the annotation @Record or not instantiated during initModel.");
091    
092            int count = 0;
093            try {
094    
095                // If the first line of the CSV file contains columns name, then we skip this line
096                if (factory.getSkipFirstLine()) {
097                    scanner.nextLine();
098                }
099    
100                while (scanner.hasNextLine()) {
101    
102                    // Read the line
103                    String line = scanner.nextLine().trim();
104                    
105                    if (ObjectHelper.isEmpty(line)) {
106                        // skip if line is empty
107                        continue;
108                    }
109    
110                    if (LOG.isDebugEnabled()) {
111                        LOG.debug("Counter " + count++ + " : content : " + line);
112                    }
113                    
114                    // Create POJO where CSV data will be stored
115                    model = factory.factory();
116    
117                    // Split the CSV record according to the separator defined in
118                    // annotated class @CSVRecord
119                    List<String> result = Arrays.asList(line.split(separator));
120                    
121                    // Bind data from CSV record with model classes
122                    factory.bind(result, model);
123    
124                    // Link objects together
125                    factory.link(model);
126    
127                    // Add objects graph to the list
128                    models.add(model);
129    
130                    if (LOG.isDebugEnabled()) {
131                        LOG.debug("Graph of objects created : " + model);
132                    }
133    
134                }
135    
136                return models;
137    
138            } finally {
139                scanner.close();
140                ObjectHelper.close(in, "in", LOG);
141            }
142    
143        }
144    
145        /**
146         * Method used to create the singleton of the BindyCsvFactory
147         */
148        public BindyCsvFactory getFactory(PackageScanClassResolver resolver) throws Exception {
149            if (modelFactory == null) {
150                modelFactory = new BindyCsvFactory(resolver, packageName);
151            }
152            return modelFactory;
153        }
154    
155        public void setModelFactory(BindyCsvFactory modelFactory) {
156            this.modelFactory = modelFactory;
157        }
158    
159        public String getPackageName() {
160            return packageName;
161        }
162    
163        public void setPackageName(String packageName) {
164            this.packageName = packageName;
165        }
166    
167    }