Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D48F098EB for ; Mon, 5 Mar 2012 15:42:28 +0000 (UTC) Received: (qmail 44779 invoked by uid 500); 5 Mar 2012 15:42:28 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 44717 invoked by uid 500); 5 Mar 2012 15:42:28 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 44710 invoked by uid 99); 5 Mar 2012 15:42:28 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Mar 2012 15:42:28 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Mar 2012 15:42:25 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 4AD4F2388865 for ; Mon, 5 Mar 2012 15:42:04 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Mon, 05 Mar 2012 15:42:04 -0000 To: commits@commons.apache.org From: ebourg@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120305154204.4AD4F2388865@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 - * null values are converted to the string "null", - * all strings equal to "null" 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")); + } }