You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2012/03/05 16:42:04 UTC

svn commit: r1297091 - in /commons/sandbox/csv/trunk/src: main/java/org/apache/commons/csv/CSVFormat.java main/java/org/apache/commons/csv/CSVUtils.java test/java/org/apache/commons/csv/CSVFormatTest.java

Author: ebourg
Date: Mon Mar  5 15:42:03 2012
New Revision: 1297091

URL: http://svn.apache.org/viewvc?rev=1297091&view=rev
Log:
Added a convenient format() method in CSVFormat replacing CSVUtils.printLine()

Modified:
    commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
    commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVUtils.java
    commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java

Modified: commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1297091&r1=1297090&r2=1297091&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java (original)
+++ commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java Mon Mar  5 15:42:03 2012
@@ -17,8 +17,10 @@
 
 package org.apache.commons.csv;
 
+import java.io.IOException;
 import java.io.Reader;
 import java.io.Serializable;
+import java.io.StringWriter;
 
 /**
  * The format specification of a CSV file.
@@ -215,6 +217,22 @@ public class CSVFormat implements Clonea
         return new CSVParser(in, this);
     }
 
+    /**
+     * Format the specified values.
+     * 
+     * @param values the values to format
+     */
+    public String format(String... values) {
+        StringWriter out = new StringWriter();
+        try {
+            new CSVPrinter(out, this).println(values);
+        } catch (IOException e) {
+            // should not happen
+        }
+        
+        return out.toString().trim();
+    }
+
     protected CSVFormat clone() {
         try {
             return (CSVFormat) super.clone();

Modified: commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVUtils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVUtils.java?rev=1297091&r1=1297090&r2=1297091&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVUtils.java (original)
+++ commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVUtils.java Mon Mar  5 15:42:03 2012
@@ -18,7 +18,6 @@ package org.apache.commons.csv;
 
 import java.io.IOException;
 import java.io.StringReader;
-import java.io.StringWriter;
 
 /**
  * Utility methods for dealing with CSV files
@@ -38,41 +37,6 @@ public class CSVUtils {
     public CSVUtils() {
     }
 
-    /**
-     * Converts an array of string values into a single CSV line. All
-     * <code>null</code> values are converted to the string <code>"null"</code>,
-     * all strings equal to <code>"null"</code> will additionally get quotes
-     * around.
-     *
-     * @param values the value array
-     * @return the CSV string, will be an empty string if the length of the
-     *         value array is 0
-     */
-    public static String printLine(CSVFormat format, String... values) {
-        // set up a CSVUtils
-        StringWriter stringWriter = new StringWriter();
-        CSVPrinter csvPrinter = new CSVPrinter(stringWriter, format);
-
-        // check for null values an "null" as strings and convert them
-        // into the strings "null" and "\"null\""
-        for (int i = 0; i < values.length; i++) {
-            if (values[i] == null) {
-                values[i] = "null";
-            } else if (values[i].equals("null")) {
-                values[i] = "\"null\"";
-            }
-        }
-
-        // convert to CSV
-        try {
-            csvPrinter.println(values);
-        } catch (IOException e) {
-            // should not happen with StringWriter
-        }
-        // as the resulting string has \r\n at the end, we will trim that away
-        return stringWriter.toString().trim();
-    }
-
     // ======================================================
     //  static parsers
     // ======================================================

Modified: commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java?rev=1297091&r1=1297090&r2=1297091&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java (original)
+++ commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java Mon Mar  5 15:42:03 2012
@@ -60,4 +60,12 @@ public class CSVFormatTest extends TestC
         assertEquals(false, format.withEmptyLinesIgnored(false).isEmptyLinesIgnored());
         assertEquals(false, format.withUnicodeEscapesInterpreted(false).isUnicodeEscapesInterpreted());
     }
+
+    public void testFormat() {
+        CSVFormat format = CSVFormat.DEFAULT;
+        
+        assertEquals("", format.format());
+        assertEquals("a,b,c", format.format("a", "b", "c"));
+        assertEquals("\"x,y\",z", format.format("x,y", "z"));
+    }
 }