Implement Quote.ALL. Bullet-proof a unit test.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1398133 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
cdef24d918
commit
2f2d9aa780
|
@ -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;
|
||||
} else {
|
||||
while (pos < end) {
|
||||
c = value.charAt(pos);
|
||||
if (c == LF || c == CR || c == quoteChar || c == delimChar) {
|
||||
quote = true;
|
||||
break;
|
||||
}
|
||||
pos++;
|
||||
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 {
|
||||
char c = value.charAt(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;
|
||||
// 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++;
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -76,6 +76,14 @@ public class CSVPrinterTest {
|
|||
assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
@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();
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue