camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r763553 - in /camel/branches/camel-1.x: ./ components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java
Date Thu, 09 Apr 2009 08:16:59 GMT
Author: davsclaus
Date: Thu Apr  9 08:16:59 2009
New Revision: 763553

URL: http://svn.apache.org/viewvc?rev=763553&view=rev
Log:
Merged revisions 763551 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r763551 | davsclaus | 2009-04-09 10:13:42 +0200 (Thu, 09 Apr 2009) | 1 line
  
  CAMEL-1513: Applied patch with thanks to Julien Faissolle
........

Modified:
    camel/branches/camel-1.x/   (props changed)
    camel/branches/camel-1.x/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
    camel/branches/camel-1.x/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Apr  9 08:16:59 2009
@@ -1 +1 @@
-/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909,760937,761194,761536,761583,761607,762047,762633,762650,762935,763095,763484
+/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909,760937,761194,761536,761583,761607,762047,762633,762650,762935,763095,763484,763551

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: camel/branches/camel-1.x/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java?rev=763553&r1=763552&r2=763553&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
(original)
+++ camel/branches/camel-1.x/components/camel-csv/src/main/java/org/apache/camel/dataformat/csv/CsvDataFormat.java
Thu Apr  9 08:16:59 2009
@@ -29,6 +29,7 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.util.ExchangeHelper;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.csv.CSVParser;
 import org.apache.commons.csv.CSVStrategy;
 import org.apache.commons.csv.writer.CSVConfig;
@@ -36,27 +37,33 @@
 import org.apache.commons.csv.writer.CSVWriter;
 
 /**
+ * CSV Data format.
+ * <p/>
+ * By default, columns are autogenerated in the resulting CSV. Subsequent
+ * messages use the previously created columns with new fields being added at
+ * the end of the line. Thus, field order is the same from message to message.
+ * Autogeneration can be disabled. In this case, only the fields defined in
+ * csvConfig are written on the output.
+ *
  * @version $Revision$
  */
 public class CsvDataFormat implements DataFormat {
     private CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
     private CSVConfig config = new CSVConfig();
+    private boolean autogenColumns = true;
 
     public void marshal(Exchange exchange, Object object, OutputStream outputStream) throws
Exception {
+        ObjectHelper.notNull(config, "config");
+
         Map map = ExchangeHelper.convertToMandatoryType(exchange, Map.class, object);
         OutputStreamWriter out = new OutputStreamWriter(outputStream);
         try {
-            CSVConfig conf = getConfig();
-            // lets add fields
-            Set set = map.keySet();
-            for (Object value : set) {
-                if (value != null) {
-                    String text = value.toString();
-                    CSVField field = new CSVField(text);
-                    conf.addField(field);
-                }
+            if (autogenColumns) {
+                // no specific config has been set so lets add fields
+                Set set = map.keySet();
+                updateFieldsInConfig(set, exchange);
             }
-            CSVWriter writer = new CSVWriter(conf);
+            CSVWriter writer = new CSVWriter(config);
             writer.setWriter(out);
             writer.writeRecord(map);
         } finally {
@@ -87,13 +94,6 @@
         }
     }
 
-    public CSVConfig getConfig() {
-        if (config == null) {
-            config = createConfig();
-        }
-        return config;
-    }
-
     public void setConfig(CSVConfig config) {
         this.config = config;
     }
@@ -106,7 +106,34 @@
         this.strategy = strategy;
     }
 
+    public boolean isAutogenColumns() {
+        return autogenColumns;
+    }
+
+    /**
+     * Auto generate columns.
+     *
+     * @param autogenColumns set to false to disallow column autogeneration (default true)
+     */
+    public void setAutogenColumns(boolean autogenColumns) {
+        this.autogenColumns = autogenColumns;
+    }
+
     protected CSVConfig createConfig() {
         return new CSVConfig();
     }
+
+    private synchronized void updateFieldsInConfig(Set set, Exchange exchange) {
+        for (Object value : set) {
+            if (value != null) {
+                String text = exchange.getContext().getTypeConverter().convertTo(String.class,
value);
+                // do not add field twice
+                if (config.getField(text) == null) {
+                    CSVField field = new CSVField(text);
+                    config.addField(field);
+                }
+            }
+        }
+    }
+
 }

Modified: camel/branches/camel-1.x/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java?rev=763553&r1=763552&r2=763553&view=diff
==============================================================================
--- camel/branches/camel-1.x/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java
(original)
+++ camel/branches/camel-1.x/components/camel-csv/src/test/java/org/apache/camel/dataformat/csv/CsvRouteTest.java
Thu Apr  9 08:16:59 2009
@@ -35,13 +35,15 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.util.ObjectHelper;
+import org.apache.commons.csv.writer.CSVConfig;
+import org.apache.commons.csv.writer.CSVField;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -78,6 +80,80 @@
         }
     }
 
+    public void testMultipleMessages() throws Exception {
+        MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:resultMulti",
+                                                               MockEndpoint.class);
+        resultEndpoint.expectedMessageCount(2);
+        Map body1 = new HashMap();
+        body1.put("foo", "abc");
+        body1.put("bar", 123);
+
+        Map body2 = new HashMap();
+        body2.put("foo", "def");
+        body2.put("bar", 456);
+        body2.put("baz", 789);
+
+        template.sendBody("direct:startMulti", body1);
+        template.sendBody("direct:startMulti", body2);
+
+        resultEndpoint.assertIsSatisfied();
+        List<Exchange> list = resultEndpoint.getReceivedExchanges();
+        Message in1 = list.get(0).getIn();
+        String text1 = in1.getBody(String.class);
+
+        log.debug("Received " + text1);
+        assertTrue("First CSV body has wrong value",
+                   Pattern.matches("(abc,123)|(123,abc)", text1.trim()));
+
+        Message in2 = list.get(1).getIn();
+        String text2 = in2.getBody(String.class);
+
+        log.debug("Received " + text2);
+
+        // fields should keep the same order from one call to the other
+        if (text1.trim().equals("abc,123")) {
+            assertEquals("Second CSV body has wrong value",
+                         "def,456,789", text2.trim());
+        } else {
+            assertEquals("Second CSV body has wrong value",
+                         "456,def,789", text2.trim());
+        }
+    }
+
+    public void testPresetConfig() throws Exception {
+        MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:resultMultiCustom",
+                                                               MockEndpoint.class);
+        resultEndpoint.expectedMessageCount(2);
+        Map body1 = new HashMap();
+        body1.put("foo", "abc");
+        body1.put("bar", 123);
+
+        Map body2 = new HashMap();
+        body2.put("foo", "def");
+        body2.put("bar", 456);
+        body2.put("baz", 789);
+        body2.put("buz", "000");
+
+        template.sendBody("direct:startMultiCustom", body1);
+        template.sendBody("direct:startMultiCustom", body2);
+
+        List<Exchange> list = resultEndpoint.getReceivedExchanges();
+        Message in1 = list.get(0).getIn();
+        String text1 = in1.getBody(String.class);
+
+        log.debug("Received " + text1);
+        assertEquals("First CSV body has wrong value",
+                     "abc;;123", text1.trim());
+
+        Message in2 = list.get(1).getIn();
+        String text2 = in2.getBody(String.class);
+
+        log.debug("Received " + text2);
+        assertEquals("Second CSV body has wrong value",
+                     "def;789;456", text2.trim());
+
+    }
+
     public void testUnMarshal() throws Exception {
         MockEndpoint endpoint = getMockEndpoint("mock:daltons");
         endpoint.expectedMessageCount(1);
@@ -101,6 +177,23 @@
                     to("mock:result");
                 // END SNIPPET: marshalRoute
 
+                from("direct:startMulti").
+                    marshal().csv().
+                    to("mock:resultMulti");
+
+                CsvDataFormat customCsv = new CsvDataFormat();
+                CSVConfig custom = new CSVConfig();
+                custom.setDelimiter(';');
+                custom.addField(new CSVField("foo"));
+                custom.addField(new CSVField("baz"));
+                custom.addField(new CSVField("bar"));
+                customCsv.setConfig(custom);
+                customCsv.setAutogenColumns(false);
+
+                from("direct:startMultiCustom").
+                    marshal(customCsv).
+                    to("mock:resultMultiCustom");
+
                 // START SNIPPET: unmarshalRoute
                 from("file:src/test/resources/daltons.csv?noop=true").
                     unmarshal().csv().



Mime
View raw message