Add org.apache.commons.csv.CSVFormat.CSVFormatBuilder.parse(Reader).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1461202 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e33bedefce
commit
df08acad74
|
@ -32,14 +32,19 @@ import java.io.StringWriter;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The format specification of a CSV file.
|
* Specifies the format of a CSV file and parses input.
|
||||||
* <p>
|
* <p>
|
||||||
* This class is immutable.
|
* This class is immutable.
|
||||||
* </p>
|
* </p>
|
||||||
* <p>
|
|
||||||
* You can extend a format through a builder. For example, to extend the Excel format with columns header, you write:
|
* You can extend a format through a builder. For example, to extend the Excel format with columns header, you write:
|
||||||
* </p>
|
* </p>
|
||||||
* <pre>CSVFormat.EXCEL.toBuilder().withHeader("Col1", "Col2", "Col3").build();</pre>
|
* <pre>CSVFormat.EXCEL.toBuilder().withHeader("Col1", "Col2", "Col3").build();</pre>
|
||||||
|
* <p>
|
||||||
|
* You can parse through a format. For example, to parse an Excel file with columns header, you write:
|
||||||
|
* </p>
|
||||||
|
* <pre>Reader in = ...;
|
||||||
|
*CSVFormat.EXCEL.toBuilder().withHeader("Col1", "Col2", "Col3").parse(in);</pre>
|
||||||
|
* <p>
|
||||||
*
|
*
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
|
@ -346,6 +351,7 @@ public class CSVFormat implements Serializable {
|
||||||
*
|
*
|
||||||
* @param in
|
* @param in
|
||||||
* the input stream
|
* the input stream
|
||||||
|
* @return a stream of CSVRecord
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public Iterable<CSVRecord> parse(final Reader in) throws IOException {
|
public Iterable<CSVRecord> parse(final Reader in) throws IOException {
|
||||||
|
@ -585,6 +591,19 @@ public class CSVFormat implements Serializable {
|
||||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, header);
|
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, header);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the specified content. Short-hand for:
|
||||||
|
* <pre>format.build().parse(in);</pre>
|
||||||
|
*
|
||||||
|
* @param in
|
||||||
|
* the input stream
|
||||||
|
* @return a CSVRecord stream
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public Iterable<CSVRecord> parse(final Reader in) throws IOException {
|
||||||
|
return this.build().parse(in);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies the consistency of the parameters and throws an IllegalStateException if necessary.
|
* Verifies the consistency of the parameters and throws an IllegalStateException if necessary.
|
||||||
*
|
*
|
||||||
|
|
|
@ -91,17 +91,17 @@ public class CSVFileParserTest {
|
||||||
// first line starts with csv data file name
|
// first line starts with csv data file name
|
||||||
final BufferedReader csvFile = new BufferedReader(new FileReader(new File(BASE, split[0])));
|
final BufferedReader csvFile = new BufferedReader(new FileReader(new File(BASE, split[0])));
|
||||||
final CSVFormatBuilder builder = CSVFormat.newBuilder(',').withQuoteChar('"');
|
final CSVFormatBuilder builder = CSVFormat.newBuilder(',').withQuoteChar('"');
|
||||||
CSVFormat fmt = builder.build();
|
CSVFormat format = builder.build();
|
||||||
boolean checkComments = false;
|
boolean checkComments = false;
|
||||||
for(int i=1; i < split.length; i++) {
|
for(int i=1; i < split.length; i++) {
|
||||||
final String option = split[i];
|
final String option = split[i];
|
||||||
final String[] option_parts = option.split("=",2);
|
final String[] option_parts = option.split("=",2);
|
||||||
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){
|
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){
|
||||||
fmt = builder.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1])).build();
|
format = builder.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1])).build();
|
||||||
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
|
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
|
||||||
fmt = builder.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1])).build();
|
format = builder.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1])).build();
|
||||||
} else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
|
} else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
|
||||||
fmt = builder.withCommentStart(option_parts[1].charAt(0)).build();
|
format = builder.withCommentStart(option_parts[1].charAt(0)).build();
|
||||||
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
|
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
|
||||||
checkComments = true;
|
checkComments = true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -109,18 +109,18 @@ public class CSVFileParserTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
line = readTestData(); // get string version of format
|
line = readTestData(); // get string version of format
|
||||||
assertEquals(testName+" Expected format ", line, fmt.toString());
|
assertEquals(testName+" Expected format ", line, format.toString());
|
||||||
|
|
||||||
// Now parse the file and compare against the expected results
|
// Now parse the file and compare against the expected results
|
||||||
for(final CSVRecord rec : fmt.parse(csvFile)) {
|
for(final CSVRecord record : format.parse(csvFile)) {
|
||||||
String parsed = rec.toString();
|
String parsed = record.toString();
|
||||||
if (checkComments) {
|
if (checkComments) {
|
||||||
final String comment = rec.getComment().replace("\n", "\\n");
|
final String comment = record.getComment().replace("\n", "\\n");
|
||||||
if (comment != null) {
|
if (comment != null) {
|
||||||
parsed += "#" + comment;
|
parsed += "#" + comment;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final int count = rec.size();
|
final int count = record.size();
|
||||||
assertEquals(testName, readTestData(), count+":"+parsed);
|
assertEquals(testName, readTestData(), count+":"+parsed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -481,7 +481,7 @@ public class CSVParserTest {
|
||||||
public void testHeader() throws Exception {
|
public void testHeader() throws Exception {
|
||||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||||
|
|
||||||
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader().build().parse(in).iterator();
|
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader().parse(in).iterator();
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
assertTrue(records.hasNext());
|
assertTrue(records.hasNext());
|
||||||
|
@ -498,7 +498,7 @@ public class CSVParserTest {
|
||||||
public void testHeaderComment() throws Exception {
|
public void testHeaderComment() throws Exception {
|
||||||
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
|
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
|
||||||
|
|
||||||
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withCommentStart('#').withHeader().build().parse(in).iterator();
|
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withCommentStart('#').withHeader().parse(in).iterator();
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
assertTrue(records.hasNext());
|
assertTrue(records.hasNext());
|
||||||
|
@ -515,7 +515,7 @@ public class CSVParserTest {
|
||||||
public void testProvidedHeader() throws Exception {
|
public void testProvidedHeader() throws Exception {
|
||||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||||
|
|
||||||
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").build().parse(in).iterator();
|
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").parse(in).iterator();
|
||||||
|
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
assertTrue(records.hasNext());
|
assertTrue(records.hasNext());
|
||||||
|
@ -536,7 +536,7 @@ public class CSVParserTest {
|
||||||
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
|
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
|
||||||
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
|
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
|
||||||
|
|
||||||
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").build().parse(in).iterator();
|
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").parse(in).iterator();
|
||||||
|
|
||||||
// header record
|
// header record
|
||||||
assertTrue(records.hasNext());
|
assertTrue(records.hasNext());
|
||||||
|
|
Loading…
Reference in New Issue