You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/10/14 23:01:21 UTC
svn commit: r1398133 - in /commons/proper/csv/trunk/src:
main/java/org/apache/commons/csv/CSVPrinter.java
test/java/org/apache/commons/csv/CSVPrinterTest.java
Author: ggregory
Date: Sun Oct 14 21:01:21 2012
New Revision: 1398133
URL: http://svn.apache.org/viewvc?rev=1398133&view=rev
Log:
Implement Quote.ALL. Bullet-proof a unit test.
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java?rev=1398133&r1=1398132&r2=1398133&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java Sun Oct 14 21:01:21 2012
@@ -219,53 +219,57 @@ public class CSVPrinter {
final char delimChar = format.getDelimiter();
final char quoteChar = format.getQuoteChar();
- if (len <= 0) {
- // always quote an empty token that is the first
- // on the line, as it may be the only thing on the
- // line. If it were not quoted in that case,
- // an empty line has no tokens.
- if (first) {
- quote = true;
- }
+ if (format.getQuotePolicy() == Quote.ALL) {
+ quote = true;
} else {
- char c = value.charAt(pos);
-
- // Hmmm, where did this rule come from?
- if (first && (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || (c > 'z'))) {
- quote = true;
- // } else if (c == ' ' || c == '\f' || c == '\t') {
- } else if (c <= COMMENT) {
- // Some other chars at the start of a value caused the parser to fail, so for now
- // encapsulate if we start in anything less than '#'. We are being conservative
- // by including the default comment char too.
- quote = true;
+ if (len <= 0) {
+ // always quote an empty token that is the first
+ // on the line, as it may be the only thing on the
+ // line. If it were not quoted in that case,
+ // an empty line has no tokens.
+ if (first) {
+ quote = true;
+ }
} else {
- while (pos < end) {
- c = value.charAt(pos);
- if (c == LF || c == CR || c == quoteChar || c == delimChar) {
- quote = true;
- break;
+ char c = value.charAt(pos);
+
+ // Hmmm, where did this rule come from?
+ if (first && (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || (c > 'z'))) {
+ quote = true;
+ // } else if (c == ' ' || c == '\f' || c == '\t') {
+ } else if (c <= COMMENT) {
+ // Some other chars at the start of a value caused the parser to fail, so for now
+ // encapsulate if we start in anything less than '#'. We are being conservative
+ // by including the default comment char too.
+ quote = true;
+ } else {
+ while (pos < end) {
+ c = value.charAt(pos);
+ if (c == LF || c == CR || c == quoteChar || c == delimChar) {
+ quote = true;
+ break;
+ }
+ pos++;
}
- pos++;
- }
- if (!quote) {
- pos = end - 1;
- c = value.charAt(pos);
- // if (c == ' ' || c == '\f' || c == '\t') {
- // Some other chars at the end caused the parser to fail, so for now
- // encapsulate if we end in anything less than ' '
- if (c <= SP) {
- quote = true;
+ if (!quote) {
+ pos = end - 1;
+ c = value.charAt(pos);
+ // if (c == ' ' || c == '\f' || c == '\t') {
+ // Some other chars at the end caused the parser to fail, so for now
+ // encapsulate if we end in anything less than ' '
+ if (c <= SP) {
+ quote = true;
+ }
}
}
}
- }
- if (!quote) {
- // no encapsulation needed - write out the original value
- out.append(value, start, end);
- return;
+ if (!quote) {
+ // no encapsulation needed - write out the original value
+ out.append(value, start, end);
+ return;
+ }
}
// we hit something that needed encapsulation
Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1398133&r1=1398132&r2=1398133&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java Sun Oct 14 21:01:21 2012
@@ -77,6 +77,14 @@ public class CSVPrinterTest {
}
@Test
+ public void testPrinterQuoteAll() throws IOException {
+ final StringWriter sw = new StringWriter();
+ final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuotePolicy(Quote.ALL));
+ printer.printRecord("a", "b\nc", "d");
+ assertEquals("\"a\",\"b\nc\",\"d\"" + lineSeparator, sw.toString());
+ }
+
+ @Test
public void testPrinter6() throws IOException {
final StringWriter sw = new StringWriter();
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
@@ -87,15 +95,19 @@ public class CSVPrinterTest {
@Test
public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLException {
final StringWriter sw = new StringWriter();
- final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
Class.forName("org.h2.Driver");
final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
- final Statement stmt = connection.createStatement();
- 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')");
- printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
- assertEquals("1,r1" + lineSeparator + "2,r2" + lineSeparator, sw.toString());
+ try {
+ final Statement stmt = connection.createStatement();
+ 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);
+ printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
+ assertEquals("1,r1" + lineSeparator + "2,r2" + lineSeparator, sw.toString());
+ } finally {
+ connection.close();
+ }
}
@Test