Skip to content

Commit 1a35541

Browse files
committed
Added a convenient format() method in CSVFormat replacing CSVUtils.printLine()
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1297091 13f79535-47bb-0310-9956-ffa450edef68
1 parent a53c76b commit 1a35541

3 files changed

Lines changed: 26 additions & 36 deletions

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import java.io.IOException;
2021
import java.io.Reader;
2122
import java.io.Serializable;
23+
import java.io.StringWriter;
2224

2325
/**
2426
* The format specification of a CSV file.
@@ -215,6 +217,22 @@ public Iterable<String[]> parse(Reader in) {
215217
return new CSVParser(in, this);
216218
}
217219

220+
/**
221+
* Format the specified values.
222+
*
223+
* @param values the values to format
224+
*/
225+
public String format(String... values) {
226+
StringWriter out = new StringWriter();
227+
try {
228+
new CSVPrinter(out, this).println(values);
229+
} catch (IOException e) {
230+
// should not happen
231+
}
232+
233+
return out.toString().trim();
234+
}
235+
218236
protected CSVFormat clone() {
219237
try {
220238
return (CSVFormat) super.clone();

src/main/java/org/apache/commons/csv/CSVUtils.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.io.StringReader;
21-
import java.io.StringWriter;
2221

2322
/**
2423
* Utility methods for dealing with CSV files
@@ -38,41 +37,6 @@ public class CSVUtils {
3837
public CSVUtils() {
3938
}
4039

41-
/**
42-
* Converts an array of string values into a single CSV line. All
43-
* <code>null</code> values are converted to the string <code>"null"</code>,
44-
* all strings equal to <code>"null"</code> will additionally get quotes
45-
* around.
46-
*
47-
* @param values the value array
48-
* @return the CSV string, will be an empty string if the length of the
49-
* value array is 0
50-
*/
51-
public static String printLine(CSVFormat format, String... values) {
52-
// set up a CSVUtils
53-
StringWriter stringWriter = new StringWriter();
54-
CSVPrinter csvPrinter = new CSVPrinter(stringWriter, format);
55-
56-
// check for null values an "null" as strings and convert them
57-
// into the strings "null" and "\"null\""
58-
for (int i = 0; i < values.length; i++) {
59-
if (values[i] == null) {
60-
values[i] = "null";
61-
} else if (values[i].equals("null")) {
62-
values[i] = "\"null\"";
63-
}
64-
}
65-
66-
// convert to CSV
67-
try {
68-
csvPrinter.println(values);
69-
} catch (IOException e) {
70-
// should not happen with StringWriter
71-
}
72-
// as the resulting string has \r\n at the end, we will trim that away
73-
return stringWriter.toString().trim();
74-
}
75-
7640
// ======================================================
7741
// static parsers
7842
// ======================================================

src/test/java/org/apache/commons/csv/CSVFormatTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ public void testMutators() {
6060
assertEquals(false, format.withEmptyLinesIgnored(false).isEmptyLinesIgnored());
6161
assertEquals(false, format.withUnicodeEscapesInterpreted(false).isUnicodeEscapesInterpreted());
6262
}
63+
64+
public void testFormat() {
65+
CSVFormat format = CSVFormat.DEFAULT;
66+
67+
assertEquals("", format.format());
68+
assertEquals("a,b,c", format.format("a", "b", "c"));
69+
assertEquals("\"x,y\",z", format.format("x,y", "z"));
70+
}
6371
}

0 commit comments

Comments
 (0)