mirror of
https://github.com/apache/commons-csv.git
synced 2025-02-07 18:49:24 +00:00
Refactor to only have minimal CSVParser constructors in favor of CSVParser factory methods for String, resource path, URL, and File. Update some tests to use the APIs.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1508544 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
61fdb7a03a
commit
69997467d4
@ -452,6 +452,10 @@ public class CSVFormat implements Serializable {
|
||||
/**
|
||||
* Parses the specified content.
|
||||
*
|
||||
* <p>
|
||||
* See also the various static parse methods on {@link CSVParser}.
|
||||
* </p>
|
||||
*
|
||||
* @param in
|
||||
* the input stream
|
||||
* @return a parser over a stream of {@link #CSVRecord}s.
|
||||
|
@ -20,9 +20,14 @@ package org.apache.commons.csv;
|
||||
import static org.apache.commons.csv.Token.Type.TOKEN;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -32,15 +37,15 @@ import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Parses CSV files according to the specified configuration.
|
||||
*
|
||||
*
|
||||
* Because CSV appears in many different dialects, the parser supports many configuration settings by allowing the
|
||||
* specification of a {@link CSVFormat}.
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* To parse a CSV input with tabs as separators, '"' (double-quote) as an optional value encapsulator,
|
||||
* and comments starting with '#', you write:
|
||||
* To parse a CSV input with tabs as separators, '"' (double-quote) as an optional value encapsulator, and comments
|
||||
* starting with '#', you write:
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* Reader in = new StringReader("a\tb\nc\td");
|
||||
* Iterable<CSVRecord> parser = CSVFormat.DEFAULT
|
||||
@ -51,11 +56,11 @@ import java.util.NoSuchElementException;
|
||||
* ...
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* To parse CSV input in a given format like Excel, you write:
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* Reader in = new StringReader("a;b\nc;d");
|
||||
* Iterable<CSVRecord> parser = CSVFormat.EXCEL.parse(in);
|
||||
@ -66,37 +71,142 @@ import java.util.NoSuchElementException;
|
||||
* <p>
|
||||
* You may also get a List of records:
|
||||
* </p>
|
||||
*
|
||||
* <pre>
|
||||
* Reader in = new StringReader("a;b\nc;d");
|
||||
* Reader in = new StringReader("a;b\nc;d");
|
||||
* CSVParser parser = new CSVParser(in, CSVFormat.EXCEL);
|
||||
* List<CSVRecord> list = parser.getRecords();
|
||||
* </pre>
|
||||
* <p>
|
||||
* See also the various static parse methods on this class.
|
||||
* </p>
|
||||
* <p>
|
||||
* Internal parser state is completely covered by the format and the reader-state.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* <p>
|
||||
* see <a href="package-summary.html">package documentation</a> for more details
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
|
||||
private final CSVFormat format;
|
||||
private final Map<String, Integer> headerMap;
|
||||
private final Lexer lexer;
|
||||
/** A record buffer for getRecord(). Grows as necessary and is reused. */
|
||||
private final List<String> record = new ArrayList<String>();
|
||||
/**
|
||||
* Creates a parser for the given resource.
|
||||
*
|
||||
* <p>
|
||||
* If you do not read all records from the given source, you should call {@link #close()} on the parser.
|
||||
* </p>
|
||||
*
|
||||
* @param resource
|
||||
* a resource path
|
||||
* @param charset
|
||||
* the charset for the resource
|
||||
* @param classLoader
|
||||
* the class loader to load the resource.
|
||||
* @param format
|
||||
* the CSVFormat used for CSV parsing
|
||||
* @return a new parser
|
||||
* @throws IOException
|
||||
* If an I/O error occurs
|
||||
*/
|
||||
public static CSVParser parseResource(String resource, Charset charset, ClassLoader classLoader,
|
||||
final CSVFormat format) throws IOException {
|
||||
return parseURL(classLoader.getResource(resource), charset, format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a parser for the given {@link String} using the default format {@link CSVFormat#DEFAULT}.
|
||||
*
|
||||
* @param string
|
||||
* a CSV string
|
||||
* @return a new parser
|
||||
* @throws IOException
|
||||
* If an I/O error occurs
|
||||
*/
|
||||
public static CSVParser parseString(String string) throws IOException {
|
||||
return parseString(string, CSVFormat.DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a parser for the given {@link String}.
|
||||
*
|
||||
* @param string
|
||||
* a CSV string
|
||||
* @param format
|
||||
* the CSVFormat used for CSV parsing
|
||||
* @return a new parser
|
||||
* @throws IOException
|
||||
* If an I/O error occurs
|
||||
*/
|
||||
public static CSVParser parseString(String string, final CSVFormat format) throws IOException {
|
||||
return new CSVParser(new StringReader(string), format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a parser for the given {@link File}.
|
||||
*
|
||||
* @param file
|
||||
* a CSV file
|
||||
* @param format
|
||||
* the CSVFormat used for CSV parsing
|
||||
* @return a new parser
|
||||
* @throws IOException
|
||||
* If an I/O error occurs
|
||||
*/
|
||||
public static CSVParser parseFile(File file, final CSVFormat format) throws IOException {
|
||||
return new CSVParser(new FileReader(file), format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a parser for the given URL.
|
||||
*
|
||||
* <p>
|
||||
* If you do not read all records from the given {@code url}, you should call {@link #close()} on the parser, unless
|
||||
* you close the {@code url}.
|
||||
* </p>
|
||||
*
|
||||
* @param url
|
||||
* a URL
|
||||
* @param charset
|
||||
* the charset for the resource, if {@code null}, uses {@code UTF-8}. UTF-8 is one of the encodings
|
||||
* required by the Java specification.
|
||||
* @param classLoader
|
||||
* the class loader to load the resource.
|
||||
* @param format
|
||||
* the CSVFormat used for CSV parsing
|
||||
* @return a new parser
|
||||
* @throws IOException
|
||||
* If an I/O error occurs
|
||||
*/
|
||||
public static CSVParser parseURL(URL url, Charset charset, final CSVFormat format) throws IOException {
|
||||
return new CSVParser(new InputStreamReader(url.openStream(), charset == null ? Charset.forName("UTF-8")
|
||||
: charset), format);
|
||||
}
|
||||
|
||||
// the following objects are shared to reduce garbage
|
||||
|
||||
private final CSVFormat format;
|
||||
private final Map<String, Integer> headerMap;
|
||||
|
||||
private final Lexer lexer;
|
||||
|
||||
/** A record buffer for getRecord(). Grows as necessary and is reused. */
|
||||
private final List<String> record = new ArrayList<String>();
|
||||
|
||||
private long recordNumber;
|
||||
|
||||
private final Token reusableToken = new Token();
|
||||
|
||||
/**
|
||||
* CSV parser using the default {@link CSVFormat}.
|
||||
* CSV parser using the default format {@link CSVFormat#DEFAULT}.
|
||||
*
|
||||
* <p>
|
||||
* If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
|
||||
* unless you close the {@code reader}.
|
||||
* </p>
|
||||
*
|
||||
* @param input
|
||||
* a Reader containing "csv-formatted" input
|
||||
* @throws IllegalArgumentException
|
||||
@ -110,8 +220,13 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
|
||||
/**
|
||||
* Customized CSV parser using the given {@link CSVFormat}
|
||||
*
|
||||
* @param input
|
||||
*
|
||||
* <p>
|
||||
* If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
|
||||
* unless you close the {@code reader}.
|
||||
* </p>
|
||||
*
|
||||
* @param reader
|
||||
* a Reader containing CSV-formatted input
|
||||
* @param format
|
||||
* the CSVFormat used for CSV parsing
|
||||
@ -120,36 +235,20 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
* @throws IOException
|
||||
* If an I/O error occurs
|
||||
*/
|
||||
public CSVParser(final Reader input, final CSVFormat format) throws IOException {
|
||||
public CSVParser(final Reader reader, final CSVFormat format) throws IOException {
|
||||
format.validate();
|
||||
this.format = format;
|
||||
this.lexer = new CSVLexer(format, new ExtendedBufferedReader(input));
|
||||
this.headerMap = initializeHeader();
|
||||
}
|
||||
|
||||
/**
|
||||
* Customized CSV parser using the given {@link CSVFormat}
|
||||
*
|
||||
* @param input
|
||||
* a String containing "csv-formatted" input
|
||||
* @param format
|
||||
* the CSVFormat used for CSV parsing
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the parameters of the format are inconsistent
|
||||
* @throws IOException
|
||||
* If an I/O error occurs
|
||||
*/
|
||||
public CSVParser(final String input, final CSVFormat format) throws IOException {
|
||||
this(new StringReader(input), format);
|
||||
this.lexer = new CSVLexer(format, new ExtendedBufferedReader(reader));
|
||||
this.headerMap = this.initializeHeader();
|
||||
}
|
||||
|
||||
private void addRecordValue() {
|
||||
final String input = reusableToken.content.toString();
|
||||
final String input = this.reusableToken.content.toString();
|
||||
final String nullString = this.format.getNullString();
|
||||
if (nullString == null) {
|
||||
record.add(input);
|
||||
this.record.add(input);
|
||||
} else {
|
||||
record.add(input.equalsIgnoreCase(nullString) ? null : input);
|
||||
this.record.add(input.equalsIgnoreCase(nullString) ? null : input);
|
||||
}}
|
||||
|
||||
/**
|
||||
@ -159,8 +258,8 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
* If an I/O error occurs
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
if (lexer != null) {
|
||||
lexer.close();
|
||||
if (this.lexer != null) {
|
||||
this.lexer.close();
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,7 +271,7 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
* @return current line number
|
||||
*/
|
||||
public long getCurrentLineNumber() {
|
||||
return lexer.getCurrentLineNumber();
|
||||
return this.lexer.getCurrentLineNumber();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -184,7 +283,7 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
* @return a copy of the header map that iterates in column order.
|
||||
*/
|
||||
public Map<String, Integer> getHeaderMap() {
|
||||
return new LinkedHashMap<String, Integer>(headerMap);
|
||||
return new LinkedHashMap<String, Integer>(this.headerMap);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -195,7 +294,7 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
* @return current line number
|
||||
*/
|
||||
public long getRecordNumber() {
|
||||
return recordNumber;
|
||||
return this.recordNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -211,7 +310,7 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
public List<CSVRecord> getRecords() throws IOException {
|
||||
final List<CSVRecord> records = new ArrayList<CSVRecord>();
|
||||
CSVRecord rec;
|
||||
while ((rec = nextRecord()) != null) {
|
||||
while ((rec = this.nextRecord()) != null) {
|
||||
records.add(rec);
|
||||
}
|
||||
return records;
|
||||
@ -222,18 +321,18 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
*/
|
||||
private Map<String, Integer> initializeHeader() throws IOException {
|
||||
Map<String, Integer> hdrMap = null;
|
||||
if (format.getHeader() != null) {
|
||||
if (this.format.getHeader() != null) {
|
||||
hdrMap = new LinkedHashMap<String, Integer>();
|
||||
|
||||
String[] header = null;
|
||||
if (format.getHeader().length == 0) {
|
||||
if (this.format.getHeader().length == 0) {
|
||||
// read the header from the first line of the file
|
||||
final CSVRecord record = nextRecord();
|
||||
final CSVRecord record = this.nextRecord();
|
||||
if (record != null) {
|
||||
header = record.values();
|
||||
}
|
||||
} else {
|
||||
header = format.getHeader();
|
||||
header = this.format.getHeader();
|
||||
}
|
||||
|
||||
// build the name to index mappings
|
||||
@ -247,10 +346,10 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
}
|
||||
|
||||
public boolean isClosed() {
|
||||
return lexer.isClosed();
|
||||
return this.lexer.isClosed();
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Returns an iterator on the records. IOExceptions occurring during the iteration are wrapped in a
|
||||
* RuntimeException.
|
||||
*/
|
||||
@ -260,7 +359,7 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
|
||||
private CSVRecord getNextRecord() {
|
||||
try {
|
||||
return nextRecord();
|
||||
return CSVParser.this.nextRecord();
|
||||
} catch (final IOException e) {
|
||||
// TODO: This is not great, throw an ISE instead?
|
||||
throw new RuntimeException(e);
|
||||
@ -268,26 +367,26 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
if (isClosed()) {
|
||||
if (CSVParser.this.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
if (current == null) {
|
||||
current = getNextRecord();
|
||||
if (this.current == null) {
|
||||
this.current = this.getNextRecord();
|
||||
}
|
||||
|
||||
return current != null;
|
||||
return this.current != null;
|
||||
}
|
||||
|
||||
public CSVRecord next() {
|
||||
if (isClosed()) {
|
||||
if (CSVParser.this.isClosed()) {
|
||||
return null;
|
||||
}
|
||||
CSVRecord next = current;
|
||||
current = null;
|
||||
CSVRecord next = this.current;
|
||||
this.current = null;
|
||||
|
||||
if (next == null) {
|
||||
// hasNext() wasn't called before
|
||||
next = getNextRecord();
|
||||
next = this.getNextRecord();
|
||||
if (next == null) {
|
||||
throw new NoSuchElementException("No more CSV records available");
|
||||
}
|
||||
@ -302,7 +401,7 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Parses the next record from the current point in the stream.
|
||||
*
|
||||
* @return the record as an array of values, or <tt>null</tt> if the end of the stream has been reached
|
||||
@ -311,12 +410,12 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
*/
|
||||
CSVRecord nextRecord() throws IOException {
|
||||
CSVRecord result = null;
|
||||
record.clear();
|
||||
this.record.clear();
|
||||
StringBuilder sb = null;
|
||||
do {
|
||||
reusableToken.reset();
|
||||
lexer.nextToken(reusableToken);
|
||||
switch (reusableToken.type) {
|
||||
this.reusableToken.reset();
|
||||
this.lexer.nextToken(this.reusableToken);
|
||||
switch (this.reusableToken.type) {
|
||||
case TOKEN:
|
||||
this.addRecordValue();
|
||||
break;
|
||||
@ -324,28 +423,28 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
||||
this.addRecordValue();
|
||||
break;
|
||||
case EOF:
|
||||
if (reusableToken.isReady) {
|
||||
if (this.reusableToken.isReady) {
|
||||
this.addRecordValue();
|
||||
}
|
||||
break;
|
||||
case INVALID:
|
||||
throw new IOException("(line " + getCurrentLineNumber() + ") invalid parse sequence");
|
||||
throw new IOException("(line " + this.getCurrentLineNumber() + ") invalid parse sequence");
|
||||
case COMMENT: // Ignored currently
|
||||
if (sb == null) { // first comment for this record
|
||||
sb = new StringBuilder();
|
||||
} else {
|
||||
sb.append(Constants.LF);
|
||||
}
|
||||
sb.append(reusableToken.content);
|
||||
reusableToken.type = TOKEN; // Read another token
|
||||
sb.append(this.reusableToken.content);
|
||||
this.reusableToken.type = TOKEN; // Read another token
|
||||
break;
|
||||
}
|
||||
} while (reusableToken.type == TOKEN);
|
||||
} while (this.reusableToken.type == TOKEN);
|
||||
|
||||
if (!record.isEmpty()) {
|
||||
recordNumber++;
|
||||
if (!this.record.isEmpty()) {
|
||||
this.recordNumber++;
|
||||
final String comment = sb == null ? null : sb.toString();
|
||||
result = new CSVRecord(record.toArray(new String[record.size()]), headerMap, comment, this.recordNumber);
|
||||
result = new CSVRecord(this.record.toArray(new String[this.record.size()]), this.headerMap, comment, this.recordNumber);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@ -88,7 +89,6 @@ public class CSVFileParserTest {
|
||||
final String[] split = line.split(" ");
|
||||
assertTrue(testName+" require 1 param", split.length >= 1);
|
||||
// first line starts with csv data file name
|
||||
final BufferedReader csvFileReader = new BufferedReader(new FileReader(new File(BASE, split[0])));
|
||||
CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('"');
|
||||
boolean checkComments = false;
|
||||
for(int i=1; i < split.length; i++) {
|
||||
@ -110,7 +110,9 @@ public class CSVFileParserTest {
|
||||
assertEquals(testName+" Expected format ", line, format.toString());
|
||||
|
||||
// Now parse the file and compare against the expected results
|
||||
for(final CSVRecord record : format.parse(csvFileReader)) {
|
||||
// We use a buffered reader internally so no need to create one here.
|
||||
CSVParser parser = CSVParser.parseFile(new File(BASE, split[0]), format);
|
||||
for(final CSVRecord record : parser) {
|
||||
String parsed = record.toString();
|
||||
if (checkComments) {
|
||||
final String comment = record.getComment().replace("\n", "\\n");
|
||||
@ -122,4 +124,47 @@ public class CSVFileParserTest {
|
||||
assertEquals(testName, readTestData(), count+":"+parsed);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCSVResource() throws Exception {
|
||||
String line = readTestData();
|
||||
assertNotNull("file must contain config line", line);
|
||||
final String[] split = line.split(" ");
|
||||
assertTrue(testName + " require 1 param", split.length >= 1);
|
||||
// first line starts with csv data file name
|
||||
CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('"');
|
||||
boolean checkComments = false;
|
||||
for (int i = 1; i < split.length; i++) {
|
||||
final String option = split[i];
|
||||
final String[] option_parts = option.split("=", 2);
|
||||
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])) {
|
||||
format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1]));
|
||||
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
|
||||
format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1]));
|
||||
} else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
|
||||
format = format.withCommentStart(option_parts[1].charAt(0));
|
||||
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
|
||||
checkComments = true;
|
||||
} else {
|
||||
fail(testName + " unexpected option: " + option);
|
||||
}
|
||||
}
|
||||
line = readTestData(); // get string version of format
|
||||
assertEquals(testName + " Expected format ", line, format.toString());
|
||||
|
||||
// Now parse the file and compare against the expected results
|
||||
CSVParser parser = CSVParser.parseResource("CSVFileParser/" + split[0], Charset.forName("UTF-8"),
|
||||
this.getClass().getClassLoader(), format);
|
||||
for (final CSVRecord record : parser) {
|
||||
String parsed = record.toString();
|
||||
if (checkComments) {
|
||||
final String comment = record.getComment().replace("\n", "\\n");
|
||||
if (comment != null) {
|
||||
parsed += "#" + comment;
|
||||
}
|
||||
}
|
||||
final int count = record.size();
|
||||
assertEquals(testName, readTestData(), count + ":" + parsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ public class CSVParserTest {
|
||||
|
||||
@Test
|
||||
public void testGetLine() throws IOException {
|
||||
final CSVParser parser = new CSVParser(new StringReader(CSVINPUT), CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
|
||||
final CSVParser parser = CSVParser.parseString(CSVINPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
|
||||
for (final String[] re : RESULT) {
|
||||
assertArrayEquals(re, parser.nextRecord().values());
|
||||
}
|
||||
@ -80,7 +80,7 @@ public class CSVParserTest {
|
||||
|
||||
@Test
|
||||
public void testGetRecords() throws IOException {
|
||||
final CSVParser parser = new CSVParser(new StringReader(CSVINPUT), CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
|
||||
final CSVParser parser = CSVParser.parseString(CSVINPUT, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(RESULT.length, records.size());
|
||||
assertTrue(records.size() > 0);
|
||||
@ -101,7 +101,7 @@ public class CSVParserTest {
|
||||
{""},
|
||||
{"\"hello\"", " \"world\"", "abc\ndef", ""}
|
||||
};
|
||||
final CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
|
||||
final CSVParser parser = CSVParser.parseString(code, CSVFormat.EXCEL);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(res.length, records.size());
|
||||
assertTrue(records.size() > 0);
|
||||
@ -120,7 +120,7 @@ public class CSVParserTest {
|
||||
{""},
|
||||
{"world", ""}
|
||||
};
|
||||
final CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
|
||||
final CSVParser parser = CSVParser.parseString(code, CSVFormat.EXCEL);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(res.length, records.size());
|
||||
assertTrue(records.size() > 0);
|
||||
@ -148,7 +148,7 @@ public class CSVParserTest {
|
||||
};
|
||||
|
||||
for (final String code : codes) {
|
||||
final CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
|
||||
final CSVParser parser = CSVParser.parseString(code, CSVFormat.EXCEL);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(res.length, records.size());
|
||||
assertTrue(records.size() > 0);
|
||||
@ -175,7 +175,7 @@ public class CSVParserTest {
|
||||
{"world", ""}
|
||||
};
|
||||
for (final String code : codes) {
|
||||
final CSVParser parser = new CSVParser(new StringReader(code));
|
||||
final CSVParser parser = CSVParser.parseString(code);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(res.length, records.size());
|
||||
assertTrue(records.size() > 0);
|
||||
@ -199,7 +199,7 @@ public class CSVParserTest {
|
||||
{""}
|
||||
};
|
||||
for (final String code : codes) {
|
||||
final CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
|
||||
final CSVParser parser = CSVParser.parseString(code, CSVFormat.EXCEL);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(res.length, records.size());
|
||||
assertTrue(records.size() > 0);
|
||||
@ -221,7 +221,7 @@ public class CSVParserTest {
|
||||
{"hello", ""} // CSV format ignores empty lines
|
||||
};
|
||||
for (final String code : codes) {
|
||||
final CSVParser parser = new CSVParser(new StringReader(code));
|
||||
final CSVParser parser = CSVParser.parseString(code, CSVFormat.DEFAULT);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(res.length, records.size());
|
||||
assertTrue(records.size() > 0);
|
||||
@ -233,13 +233,13 @@ public class CSVParserTest {
|
||||
|
||||
@Test
|
||||
public void testEmptyFile() throws Exception {
|
||||
final CSVParser parser = new CSVParser("", CSVFormat.DEFAULT);
|
||||
final CSVParser parser = CSVParser.parseString("", CSVFormat.DEFAULT);
|
||||
assertNull(parser.nextRecord());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCSV57() throws Exception {
|
||||
final CSVParser parser = new CSVParser("", CSVFormat.DEFAULT);
|
||||
final CSVParser parser = CSVParser.parseString("", CSVFormat.DEFAULT);
|
||||
final List<CSVRecord> list = parser.getRecords();
|
||||
assertNotNull(list);
|
||||
assertEquals(0, list.size());
|
||||
@ -269,7 +269,7 @@ public class CSVParserTest {
|
||||
{"a\\", "b"}, // a backslash must be returnd
|
||||
{"a\\\\,b"} // backslash in quotes only escapes a delimiter (",")
|
||||
};
|
||||
final CSVParser parser = new CSVParser(new StringReader(code));
|
||||
final CSVParser parser = CSVParser.parseString(code);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(res.length, records.size());
|
||||
assertTrue(records.size() > 0);
|
||||
@ -314,7 +314,7 @@ public class CSVParserTest {
|
||||
final CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('\'')
|
||||
.withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
|
||||
|
||||
final CSVParser parser = new CSVParser(code, format);
|
||||
final CSVParser parser = CSVParser.parseString(code, format);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertTrue(records.size() > 0);
|
||||
|
||||
@ -343,7 +343,7 @@ public class CSVParserTest {
|
||||
final CSVFormat format = CSVFormat.newFormat(',')
|
||||
.withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
|
||||
|
||||
final CSVParser parser = new CSVParser(code, format);
|
||||
final CSVParser parser = CSVParser.parseString(code, format);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertTrue(records.size() > 0);
|
||||
|
||||
@ -368,7 +368,7 @@ public class CSVParserTest {
|
||||
CSVFormat format = CSVFormat.DEFAULT;
|
||||
assertFalse(format.isCommentingEnabled());
|
||||
|
||||
CSVParser parser = new CSVParser(code, format);
|
||||
CSVParser parser = CSVParser.parseString(code, format);
|
||||
List<CSVRecord> records = parser.getRecords();
|
||||
assertTrue(records.size() > 0);
|
||||
|
||||
@ -380,7 +380,7 @@ public class CSVParserTest {
|
||||
};
|
||||
|
||||
format = CSVFormat.DEFAULT.withCommentStart('#');
|
||||
parser = new CSVParser(code, format);
|
||||
parser = CSVParser.parseString(code, format);
|
||||
records = parser.getRecords();
|
||||
|
||||
Utils.compare("Failed to parse with comments", res_comments, records);
|
||||
@ -389,7 +389,7 @@ public class CSVParserTest {
|
||||
@Test
|
||||
public void testCarriageReturnLineFeedEndings() throws IOException {
|
||||
final String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
|
||||
final CSVParser parser = new CSVParser(new StringReader(code));
|
||||
final CSVParser parser = CSVParser.parseString(code, CSVFormat.DEFAULT);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(4, records.size());
|
||||
}
|
||||
@ -410,7 +410,7 @@ public class CSVParserTest {
|
||||
@Test
|
||||
public void testCarriageReturnEndings() throws IOException {
|
||||
final String code = "foo\rbaar,\rhello,world\r,kanu";
|
||||
final CSVParser parser = new CSVParser(new StringReader(code));
|
||||
final CSVParser parser = CSVParser.parseString(code);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(4, records.size());
|
||||
}
|
||||
@ -418,7 +418,7 @@ public class CSVParserTest {
|
||||
@Test
|
||||
public void testLineFeedEndings() throws IOException {
|
||||
final String code = "foo\nbaar,\nhello,world\n,kanu";
|
||||
final CSVParser parser = new CSVParser(new StringReader(code));
|
||||
final CSVParser parser = CSVParser.parseString(code);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(4, records.size());
|
||||
}
|
||||
@ -428,7 +428,7 @@ public class CSVParserTest {
|
||||
final String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
|
||||
//String code = "world\r\n\n";
|
||||
//String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
|
||||
final CSVParser parser = new CSVParser(new StringReader(code));
|
||||
final CSVParser parser = CSVParser.parseString(code);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(3, records.size());
|
||||
}
|
||||
@ -454,7 +454,7 @@ public class CSVParserTest {
|
||||
final StringWriter out = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT);
|
||||
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 : CSVParser.parseString(input, CSVFormat.DEFAULT)) {
|
||||
printer.printRecord(record);
|
||||
}
|
||||
assertEquals(input, out.toString());
|
||||
@ -594,7 +594,7 @@ public class CSVParserTest {
|
||||
|
||||
@Test
|
||||
public void testGetHeaderMap() throws Exception {
|
||||
final CSVParser parser = new CSVParser("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader("A", "B", "C"));
|
||||
final CSVParser parser = CSVParser.parseString("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader("A", "B", "C"));
|
||||
final Map<String, Integer> headerMap = parser.getHeaderMap();
|
||||
final Iterator<String> columnNames = headerMap.keySet().iterator();
|
||||
// Headers are iterated in column order.
|
||||
@ -637,7 +637,7 @@ public class CSVParserTest {
|
||||
|
||||
@Test
|
||||
public void testGetRecordWithMultiiLineValues() throws Exception {
|
||||
final CSVParser parser = new CSVParser("\"a\r\n1\",\"a\r\n2\"" + CRLF + "\"b\r\n1\",\"b\r\n2\"" + CRLF + "\"c\r\n1\",\"c\r\n2\"",
|
||||
final CSVParser parser = CSVParser.parseString("\"a\r\n1\",\"a\r\n2\"" + CRLF + "\"b\r\n1\",\"b\r\n2\"" + CRLF + "\"c\r\n1\",\"c\r\n2\"",
|
||||
CSVFormat.DEFAULT.withRecordSeparator(CRLF));
|
||||
CSVRecord record;
|
||||
assertEquals(0, parser.getRecordNumber());
|
||||
@ -676,7 +676,7 @@ public class CSVParserTest {
|
||||
}
|
||||
|
||||
private void validateRecordNumbers(final String lineSeparator) throws IOException {
|
||||
final CSVParser parser = new CSVParser("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
|
||||
final CSVParser parser = CSVParser.parseString("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
|
||||
CSVRecord record;
|
||||
assertEquals(0, parser.getRecordNumber());
|
||||
assertNotNull(record = parser.nextRecord());
|
||||
@ -693,7 +693,7 @@ public class CSVParserTest {
|
||||
}
|
||||
|
||||
private void validateLineNumbers(final String lineSeparator) throws IOException {
|
||||
final CSVParser parser = new CSVParser("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
|
||||
final CSVParser parser = CSVParser.parseString("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
|
||||
assertEquals(0, parser.getCurrentLineNumber());
|
||||
assertNotNull(parser.nextRecord());
|
||||
assertEquals(1, parser.getCurrentLineNumber());
|
||||
|
@ -85,7 +85,7 @@ public class CSVPrinterTest {
|
||||
final String result = sw.toString();
|
||||
// System.out.println("### :" + printable(result));
|
||||
|
||||
final CSVParser parser = new CSVParser(result, format);
|
||||
final CSVParser parser = CSVParser.parseString(result, format);
|
||||
final List<CSVRecord> parseResult = parser.getRecords();
|
||||
|
||||
Utils.compare("Printer output :" + printable(result), lines, parseResult);
|
||||
|
Loading…
x
Reference in New Issue
Block a user