After discussion on ML (http://apache-commons.680414.n4.nabble.com/CSV-org-apache-commons-csv-CSVFormat-DEFAULT-td4647843.html) from 6 days ago, handle this TODO, and renamed DEFAULT to RFC4180_EMPTY_LINES.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1461134 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
eb13cb152a
commit
40948cdcec
|
@ -79,7 +79,7 @@ public class CSVFormat implements Serializable {
|
|||
* <li>withLineSeparator(CRLF)</li>
|
||||
* </ul>
|
||||
*/
|
||||
public static final CSVFormat DEFAULT = // TODO rename to something more meaningful
|
||||
public static final CSVFormat RFC4180_EMPTY_LINES =
|
||||
newBuilder()
|
||||
.build();
|
||||
|
||||
|
@ -161,7 +161,7 @@ public class CSVFormat implements Serializable {
|
|||
* <li>withLineSeparator(CRLF)</li>
|
||||
* </ul>
|
||||
*
|
||||
* Shortcut for {@code CSVFormat.newBuilder(CSVFormat.DEFAULT)}
|
||||
* Shortcut for {@code CSVFormat.newBuilder(CSVFormat.RFC4180_EMPTY_LINES)}
|
||||
*
|
||||
* @return a standard comma separated format builder, as for {@link #RFC4180} but allowing empty lines.
|
||||
*/
|
||||
|
|
|
@ -89,7 +89,7 @@ public class CSVParser implements Iterable<CSVRecord> {
|
|||
* thrown if the parameters of the format are inconsistent
|
||||
*/
|
||||
public CSVParser(final Reader input) throws IOException {
|
||||
this(input, CSVFormat.DEFAULT);
|
||||
this(input, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -52,13 +52,13 @@ public class CSVPrinter implements Flushable, Closeable {
|
|||
* @param out
|
||||
* stream to which to print.
|
||||
* @param format
|
||||
* the CSV format. If null the default format is used ({@link CSVFormat#DEFAULT})
|
||||
* the CSV format. If null the default format is used ({@link CSVFormat#RFC4180_EMPTY_LINES})
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the parameters of the format are inconsistent
|
||||
*/
|
||||
public CSVPrinter(final Appendable out, final CSVFormat format) {
|
||||
this.out = out;
|
||||
this.format = format == null ? CSVFormat.DEFAULT : format;
|
||||
this.format = format == null ? CSVFormat.RFC4180_EMPTY_LINES : format;
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
|
|
|
@ -68,7 +68,7 @@
|
|||
* <p>Example usage:</p>
|
||||
* <blockquote><pre>
|
||||
* Reader in = new StringReader("a,b,c");
|
||||
* for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
|
||||
* for (CSVRecord record : CSVFormat.RFC4180_EMPTY_LINES.parse(in)) {
|
||||
* for (String field : record) {
|
||||
* System.out.print("\"" + field + "\", ");
|
||||
* }
|
||||
|
|
|
@ -37,7 +37,7 @@ public class CSVFormatTest {
|
|||
|
||||
@Test
|
||||
public void testFormat() {
|
||||
final CSVFormat format = CSVFormat.DEFAULT;
|
||||
final CSVFormat format = CSVFormat.RFC4180_EMPTY_LINES;
|
||||
|
||||
assertEquals("", format.format());
|
||||
assertEquals("a,b,c", format.format("a", "b", "c"));
|
||||
|
@ -50,7 +50,7 @@ public class CSVFormatTest {
|
|||
final ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
final ObjectOutputStream oos = new ObjectOutputStream(out);
|
||||
oos.writeObject(CSVFormat.DEFAULT);
|
||||
oos.writeObject(CSVFormat.RFC4180_EMPTY_LINES);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
|
@ -58,18 +58,18 @@ public class CSVFormatTest {
|
|||
final CSVFormat format = (CSVFormat) in.readObject();
|
||||
|
||||
assertNotNull(format);
|
||||
assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter());
|
||||
assertEquals("encapsulator", CSVFormat.DEFAULT.getQuoteChar(), format.getQuoteChar());
|
||||
assertEquals("comment start", CSVFormat.DEFAULT.getCommentStart(), format.getCommentStart());
|
||||
assertEquals("line separator", CSVFormat.DEFAULT.getRecordSeparator(), format.getRecordSeparator());
|
||||
assertEquals("escape", CSVFormat.DEFAULT.getEscape(), format.getEscape());
|
||||
assertEquals("trim", CSVFormat.DEFAULT.getIgnoreSurroundingSpaces(), format.getIgnoreSurroundingSpaces());
|
||||
assertEquals("empty lines", CSVFormat.DEFAULT.getIgnoreEmptyLines(), format.getIgnoreEmptyLines());
|
||||
assertEquals("delimiter", CSVFormat.RFC4180_EMPTY_LINES.getDelimiter(), format.getDelimiter());
|
||||
assertEquals("encapsulator", CSVFormat.RFC4180_EMPTY_LINES.getQuoteChar(), format.getQuoteChar());
|
||||
assertEquals("comment start", CSVFormat.RFC4180_EMPTY_LINES.getCommentStart(), format.getCommentStart());
|
||||
assertEquals("line separator", CSVFormat.RFC4180_EMPTY_LINES.getRecordSeparator(), format.getRecordSeparator());
|
||||
assertEquals("escape", CSVFormat.RFC4180_EMPTY_LINES.getEscape(), format.getEscape());
|
||||
assertEquals("trim", CSVFormat.RFC4180_EMPTY_LINES.getIgnoreSurroundingSpaces(), format.getIgnoreSurroundingSpaces());
|
||||
assertEquals("empty lines", CSVFormat.RFC4180_EMPTY_LINES.getIgnoreEmptyLines(), format.getIgnoreEmptyLines());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
final CSVFormat right = CSVFormat.DEFAULT;
|
||||
final CSVFormat right = CSVFormat.RFC4180_EMPTY_LINES;
|
||||
final CSVFormat left = CSVFormat.newBuilder().build();
|
||||
|
||||
assertFalse(right.equals(null));
|
||||
|
|
|
@ -178,7 +178,7 @@ public class CSVLexerTest {
|
|||
* \,,
|
||||
*/
|
||||
final String code = "a,\\,,b\\\n\\,,";
|
||||
final CSVFormat format = CSVFormat.DEFAULT;
|
||||
final CSVFormat format = CSVFormat.RFC4180_EMPTY_LINES;
|
||||
assertFalse(format.isEscaping());
|
||||
final Lexer parser = getLexer(code, format);
|
||||
|
||||
|
@ -242,7 +242,7 @@ public class CSVLexerTest {
|
|||
@Test
|
||||
public void testNextToken5() throws IOException {
|
||||
final String code = "a,\"foo\n\",b\n\"foo\n baar ,,,\"\n\"\n\t \n\"";
|
||||
final Lexer parser = getLexer(code, CSVFormat.DEFAULT);
|
||||
final Lexer parser = getLexer(code, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
assertTokenEquals(TOKEN, "a", parser.nextToken(new Token()));
|
||||
assertTokenEquals(TOKEN, "foo\n", parser.nextToken(new Token()));
|
||||
assertTokenEquals(EORECORD, "b", parser.nextToken(new Token()));
|
||||
|
|
|
@ -234,13 +234,13 @@ public class CSVParserTest {
|
|||
|
||||
@Test
|
||||
public void testEmptyFile() throws Exception {
|
||||
final CSVParser parser = new CSVParser("", CSVFormat.DEFAULT);
|
||||
final CSVParser parser = new CSVParser("", CSVFormat.RFC4180_EMPTY_LINES);
|
||||
assertNull(parser.nextRecord());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCSV57() throws Exception {
|
||||
final CSVParser parser = new CSVParser("", CSVFormat.DEFAULT);
|
||||
final CSVParser parser = new CSVParser("", CSVFormat.RFC4180_EMPTY_LINES);
|
||||
final List<CSVRecord> list = parser.getRecords();
|
||||
assertNotNull(list);
|
||||
assertEquals(0, list.size());
|
||||
|
@ -366,7 +366,7 @@ public class CSVParserTest {
|
|||
{"# Final comment"}
|
||||
};
|
||||
|
||||
CSVFormat format = CSVFormat.DEFAULT;
|
||||
CSVFormat format = CSVFormat.RFC4180_EMPTY_LINES;
|
||||
assertFalse(format.isCommentingEnabled());
|
||||
|
||||
CSVParser parser = new CSVParser(code, format);
|
||||
|
@ -427,7 +427,7 @@ public class CSVParserTest {
|
|||
|
||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
|
||||
for (final CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
|
||||
for (final CSVRecord record : CSVFormat.RFC4180_EMPTY_LINES.parse(in)) {
|
||||
records.add(record);
|
||||
}
|
||||
|
||||
|
@ -440,9 +440,9 @@ public class CSVParserTest {
|
|||
@Test
|
||||
public void testRoundtrip() throws Exception {
|
||||
final StringWriter out = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(out, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
final String input = "a,b,c\r\n1,2,3\r\nx,y,z\r\n";
|
||||
for (final CSVRecord record : CSVFormat.DEFAULT.parse(new StringReader(input))) {
|
||||
for (final CSVRecord record : CSVFormat.RFC4180_EMPTY_LINES.parse(new StringReader(input))) {
|
||||
printer.printRecord(record);
|
||||
}
|
||||
assertEquals(input, out.toString());
|
||||
|
@ -453,7 +453,7 @@ public class CSVParserTest {
|
|||
public void testIterator() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
|
||||
final Iterator<CSVRecord> iterator = CSVFormat.DEFAULT.parse(in).iterator();
|
||||
final Iterator<CSVRecord> iterator = CSVFormat.RFC4180_EMPTY_LINES.parse(in).iterator();
|
||||
|
||||
assertTrue(iterator.hasNext());
|
||||
try {
|
||||
|
|
|
@ -51,7 +51,7 @@ public class CSVPrinterTest {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
String recordSeparator = CSVFormat.DEFAULT.getRecordSeparator();
|
||||
String recordSeparator = CSVFormat.RFC4180_EMPTY_LINES.getRecordSeparator();
|
||||
|
||||
public void doOneRandom(final CSVFormat format) throws Exception {
|
||||
final Random r = new Random();
|
||||
|
@ -144,7 +144,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testDisabledComment() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
printer.printComment("This is a comment");
|
||||
|
||||
assertEquals("", sw.toString());
|
||||
|
@ -216,7 +216,7 @@ public class CSVPrinterTest {
|
|||
stmt.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
|
||||
stmt.execute("insert into TEST values(1, 'r1')");
|
||||
stmt.execute("insert into TEST values(2, 'r2')");
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
|
||||
assertEquals("1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -238,7 +238,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPrinter1() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
printer.printRecord("a", "b");
|
||||
assertEquals("a,b" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -247,7 +247,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPrinter2() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
printer.printRecord("a,b", "b");
|
||||
assertEquals("\"a,b\",b" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -256,7 +256,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPrinter3() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
printer.printRecord("a, b", "b ");
|
||||
assertEquals("\"a, b\",\"b \"" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -265,7 +265,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPrinter4() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
printer.printRecord("a", "b\"c");
|
||||
assertEquals("a,\"b\"\"c\"" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -274,7 +274,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPrinter5() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
printer.printRecord("a", "b\nc");
|
||||
assertEquals("a,\"b\nc\"" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -283,7 +283,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPrinter6() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
printer.printRecord("a", "b\r\nc");
|
||||
assertEquals("a,\"b\r\nc\"" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -292,7 +292,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPrinter7() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
printer.printRecord("a", "b\\c");
|
||||
assertEquals("a,b\\c" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -301,7 +301,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPrintNullValues() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180_EMPTY_LINES);
|
||||
printer.printRecord("a", null, "b");
|
||||
assertEquals("a,,b" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -328,7 +328,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testRandom() throws Exception {
|
||||
final int iter = 10000;
|
||||
doRandom(CSVFormat.DEFAULT, iter);
|
||||
doRandom(CSVFormat.RFC4180_EMPTY_LINES, iter);
|
||||
doRandom(CSVFormat.EXCEL, iter);
|
||||
doRandom(CSVFormat.MYSQL, iter);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue