Renamed CSVParser.getLine() into getRecord() to avoid confusions since a record can span several lines
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1298333 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a65806a126
commit
2ec4c994c0
|
@ -68,7 +68,7 @@ public class CSVParser implements Iterable<String[]> {
|
||||||
|
|
||||||
// the following objects are shared to reduce garbage
|
// the following objects are shared to reduce garbage
|
||||||
|
|
||||||
/** A record buffer for getLine(). Grows as necessary and is reused. */
|
/** A record buffer for getRecord(). Grows as necessary and is reused. */
|
||||||
private final List<String> record = new ArrayList<String>();
|
private final List<String> record = new ArrayList<String>();
|
||||||
private final Token reusableToken = new Token();
|
private final Token reusableToken = new Token();
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ public class CSVParser implements Iterable<String[]> {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the CSV according to the given format and returns the content
|
* Parses the CSV input according to the given format and returns the content
|
||||||
* as an array of records (whereas records are arrays of single values).
|
* as an array of records (whereas records are arrays of single values).
|
||||||
* <p/>
|
* <p/>
|
||||||
* The returned content starts at the current parse-position in the stream.
|
* The returned content starts at the current parse-position in the stream.
|
||||||
|
@ -122,26 +122,26 @@ public class CSVParser implements Iterable<String[]> {
|
||||||
*/
|
*/
|
||||||
public String[][] getRecords() throws IOException {
|
public String[][] getRecords() throws IOException {
|
||||||
List<String[]> records = new ArrayList<String[]>();
|
List<String[]> records = new ArrayList<String[]>();
|
||||||
String[] values;
|
String[] record;
|
||||||
String[][] ret = null;
|
while ((record = getRecord()) != null) {
|
||||||
while ((values = getLine()) != null) {
|
records.add(record);
|
||||||
records.add(values);
|
|
||||||
}
|
}
|
||||||
if (records.size() > 0) {
|
|
||||||
ret = new String[records.size()][];
|
if (!records.isEmpty()) {
|
||||||
records.toArray(ret);
|
return records.toArray(new String[records.size()][]);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses from the current point in the stream til the end of the current line.
|
* Parses the next record from the current point in the stream.
|
||||||
*
|
*
|
||||||
* @return array of values til end of line ('null' when end of file has been reached)
|
* @return the record as an array of values, or <tt>null</tt> if the end of the stream has been reached
|
||||||
* @throws IOException on parse error or input read-failure
|
* @throws IOException on parse error or input read-failure
|
||||||
*/
|
*/
|
||||||
String[] getLine() throws IOException {
|
String[] getRecord() throws IOException {
|
||||||
String[] ret = EMPTY_STRING_ARRAY;
|
String[] result = EMPTY_STRING_ARRAY;
|
||||||
record.clear();
|
record.clear();
|
||||||
while (true) {
|
while (true) {
|
||||||
reusableToken.reset();
|
reusableToken.reset();
|
||||||
|
@ -157,11 +157,10 @@ public class CSVParser implements Iterable<String[]> {
|
||||||
if (reusableToken.isReady) {
|
if (reusableToken.isReady) {
|
||||||
record.add(reusableToken.content.toString());
|
record.add(reusableToken.content.toString());
|
||||||
} else {
|
} else {
|
||||||
ret = null;
|
result = null;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case INVALID:
|
case INVALID:
|
||||||
default:
|
|
||||||
// error: throw IOException
|
// error: throw IOException
|
||||||
throw new IOException("(line " + getLineNumber() + ") invalid parse sequence");
|
throw new IOException("(line " + getLineNumber() + ") invalid parse sequence");
|
||||||
// unreachable: break;
|
// unreachable: break;
|
||||||
|
@ -171,9 +170,9 @@ public class CSVParser implements Iterable<String[]> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!record.isEmpty()) {
|
if (!record.isEmpty()) {
|
||||||
ret = record.toArray(new String[record.size()]);
|
result = record.toArray(new String[record.size()]);
|
||||||
}
|
}
|
||||||
return ret;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -209,7 +208,7 @@ public class CSVParser implements Iterable<String[]> {
|
||||||
|
|
||||||
private String[] getNextLine() {
|
private String[] getNextLine() {
|
||||||
try {
|
try {
|
||||||
return getLine();
|
return getRecord();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,10 +54,10 @@ public class CSVParserTest extends TestCase {
|
||||||
public void testGetLine() throws IOException {
|
public void testGetLine() throws IOException {
|
||||||
CSVParser parser = new CSVParser(new StringReader(code));
|
CSVParser parser = new CSVParser(new StringReader(code));
|
||||||
for (String[] re : res) {
|
for (String[] re : res) {
|
||||||
assertTrue(Arrays.equals(re, parser.getLine()));
|
assertTrue(Arrays.equals(re, parser.getRecord()));
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue(parser.getLine() == null);
|
assertTrue(parser.getRecord() == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetRecords() throws IOException {
|
public void testGetRecords() throws IOException {
|
||||||
|
|
Loading…
Reference in New Issue