Skip to content

Commit 770628d

Browse files
committed
Add org.apache.commons.csv.CSVParser.CSVParser(Reader, CSVFormat, long, long) and remove new setters.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1635129 13f79535-47bb-0310-9956-ffa450edef68
1 parent 04d5979 commit 770628d

4 files changed

Lines changed: 72 additions & 47 deletions

File tree

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,16 @@ public static CSVParser parse(final URL url, final Charset charset, final CSVFor
218218
/** A record buffer for getRecord(). Grows as necessary and is reused. */
219219
private final List<String> record = new ArrayList<String>();
220220

221+
/**
222+
* The next record number to assign.
223+
*/
221224
private long recordNumber;
222225

223226
/**
224-
* Lexer offset if the parser does not start parsing at the beginning of the source. Usually used in combination
227+
* Lexer offset when the parser does not start parsing at the beginning of the source. Usually used in combination
225228
* with {@link #setNextRecordNumber(long)}
226229
*/
227-
private long characterOffset;
230+
private final long characterOffset;
228231

229232
private final Token reusableToken = new Token();
230233

@@ -246,12 +249,41 @@ public static CSVParser parse(final URL url, final Charset charset, final CSVFor
246249
* If there is a problem reading the header or skipping the first record
247250
*/
248251
public CSVParser(final Reader reader, final CSVFormat format) throws IOException {
252+
this(reader, format, 0, 1);
253+
}
254+
255+
/**
256+
* Customized CSV parser using the given {@link CSVFormat}
257+
*
258+
* <p>
259+
* If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
260+
* unless you close the {@code reader}.
261+
* </p>
262+
*
263+
* @param reader
264+
* a Reader containing CSV-formatted input. Must not be null.
265+
* @param format
266+
* the CSVFormat used for CSV parsing. Must not be null.
267+
* @param characterOffset
268+
* Lexer offset when the parser does not start parsing at the beginning of the source.
269+
* @param recordNumber
270+
* The next record number to assign
271+
* @throws IllegalArgumentException
272+
* If the parameters of the format are inconsistent or if either reader or format are null.
273+
* @throws IOException
274+
* If there is a problem reading the header or skipping the first record
275+
* @since 1.1
276+
*/
277+
public CSVParser(final Reader reader, final CSVFormat format, long characterOffset, long recordNumber)
278+
throws IOException {
249279
Assertions.notNull(reader, "reader");
250280
Assertions.notNull(format, "format");
251281

252282
this.format = format;
253283
this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));
254284
this.headerMap = this.initializeHeader();
285+
this.characterOffset = characterOffset;
286+
this.recordNumber = recordNumber - 1;
255287
}
256288

257289
private void addRecordValue() {
@@ -301,43 +333,6 @@ public Map<String, Integer> getHeaderMap() {
301333
return this.headerMap == null ? null : new LinkedHashMap<String, Integer>(this.headerMap);
302334
}
303335

304-
/**
305-
* Sets the record number to be assigned to the next record read.
306-
* <p>
307-
* Use this if the reader is not positioned at the first record when you create the parser. For example, the first
308-
* record read might be the 51st record in the source file.
309-
* </p>
310-
* <p>
311-
* If you want the records to also have the correct character position referring to the underlying source, call
312-
* {@link #setNextCharacterPosition(long)}.
313-
* </p>
314-
*
315-
* @param nextRecordNumber
316-
* the next record number
317-
* @since 1.1
318-
*/
319-
public void setNextRecordNumber(long nextRecordNumber) {
320-
this.recordNumber = nextRecordNumber - 1;
321-
}
322-
323-
/**
324-
* Sets the current position in the source stream regardless of where the parser and lexer start reading.
325-
* <p>
326-
* For example: We open a file and seek to position 5434 in order to start reading at record 42. In order to have
327-
* the parser assign the correct characterPosition to records, we call this method.
328-
* </p>
329-
* <p>
330-
* If you want the records to also have the correct record numbers, call {@link #setNextRecordNumber(long)}
331-
* </p>
332-
*
333-
* @param position
334-
* the new character position
335-
* @since 1.1
336-
*/
337-
public void setNextCharacterPosition(long position) {
338-
this.characterOffset = position - lexer.getCharacterPosition();
339-
}
340-
341336
/**
342337
* Returns the current record number in the input stream.
343338
*
@@ -346,7 +341,7 @@ public void setNextCharacterPosition(long position) {
346341
* the line number.
347342
* </p>
348343
*
349-
* @return current line number
344+
* @return current record number
350345
*/
351346
public long getRecordNumber() {
352347
return this.recordNumber;

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.Flushable;
2727
import java.io.IOException;
2828
import java.sql.ResultSet;
29+
import java.sql.ResultSetMetaData;
2930
import java.sql.SQLException;
3031

3132
/**
@@ -498,7 +499,15 @@ public void printRecords(final Object... values) throws IOException {
498499
* if a database access error occurs
499500
*/
500501
public void printRecords(final ResultSet resultSet) throws SQLException, IOException {
501-
final int columnCount = resultSet.getMetaData().getColumnCount();
502+
ResultSetMetaData metaData = resultSet.getMetaData();
503+
final int columnCount = metaData.getColumnCount();
504+
boolean printHeader = false;
505+
if (printHeader) {
506+
for (int i = 1; i <= columnCount; i++) {
507+
print(metaData.getColumnLabel(i));
508+
}
509+
println();
510+
}
502511
while (resultSet.next()) {
503512
for (int i = 1; i <= columnCount; i++) {
504513
print(resultSet.getString(i));

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,7 @@ private void validateRecordPosition(final String lineSeparator) throws IOExcepti
957957
parser.close();
958958

959959
// now try to read starting at record 3
960-
parser = CSVParser.parse(code.substring((int) positionRecord3), format);
961-
parser.setNextRecordNumber(3);
962-
parser.setNextCharacterPosition(positionRecord3);
960+
parser = new CSVParser(new StringReader(code.substring((int) positionRecord3)), format, positionRecord3, 3);
963961

964962
assertNotNull(record = parser.nextRecord());
965963
assertEquals(3, record.getRecordNumber());

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.List;
3535
import java.util.Random;
3636

37+
import org.junit.Ignore;
3738
import org.junit.Test;
3839

3940
/**
@@ -219,9 +220,7 @@ public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLExc
219220
final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
220221
try {
221222
final Statement stmt = connection.createStatement();
222-
stmt.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
223-
stmt.execute("insert into TEST values(1, 'r1')");
224-
stmt.execute("insert into TEST values(2, 'r2')");
223+
setUpTable(stmt);
225224
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
226225
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
227226
assertEquals("1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString());
@@ -231,6 +230,30 @@ public void testJdbcPrinter() throws IOException, ClassNotFoundException, SQLExc
231230
}
232231
}
233232

233+
private void setUpTable(final Statement stmt) throws SQLException {
234+
stmt.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
235+
stmt.execute("insert into TEST values(1, 'r1')");
236+
stmt.execute("insert into TEST values(2, 'r2')");
237+
}
238+
239+
@Test
240+
@Ignore
241+
public void testJdbcPrinterWithHeaders() throws IOException, ClassNotFoundException, SQLException {
242+
final StringWriter sw = new StringWriter();
243+
Class.forName("org.h2.Driver");
244+
final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;", "sa", "");
245+
try {
246+
final Statement stmt = connection.createStatement();
247+
setUpTable(stmt);
248+
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
249+
printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
250+
assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString());
251+
printer.close();
252+
} finally {
253+
connection.close();
254+
}
255+
}
256+
234257
@Test
235258
public void testMultiLineComment() throws IOException {
236259
final StringWriter sw = new StringWriter();

0 commit comments

Comments
 (0)