commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1635129 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/ test/java/org/apache/commons/csv/
Date Wed, 29 Oct 2014 13:33:05 GMT
Author: ggregory
Date: Wed Oct 29 13:33:04 2014
New Revision: 1635129

URL: http://svn.apache.org/r1635129
Log:
Add org.apache.commons.csv.CSVParser.CSVParser(Reader, CSVFormat, long, long) and remove new
setters.

Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java?rev=1635129&r1=1635128&r2=1635129&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java Wed Oct 29
13:33:04 2014
@@ -218,13 +218,16 @@ public final class CSVParser implements 
     /** A record buffer for getRecord(). Grows as necessary and is reused. */
     private final List<String> record = new ArrayList<String>();
 
+    /**
+     * The next record number to assign.
+     */
     private long recordNumber;
     
     /**
-     * Lexer offset if the parser does not start parsing at the beginning of the source.
Usually used in combination
+     * Lexer offset when the parser does not start parsing at the beginning of the source.
Usually used in combination
      * with {@link #setNextRecordNumber(long)}
      */
-    private long characterOffset;
+    private final long characterOffset;
 
     private final Token reusableToken = new Token();
 
@@ -246,12 +249,41 @@ public final class CSVParser implements 
      *             If there is a problem reading the header or skipping the first record
      */
     public CSVParser(final Reader reader, final CSVFormat format) throws IOException {
+        this(reader, format, 0, 1);
+    }
+
+    /**
+     * Customized CSV parser using the given {@link CSVFormat}
+     *
+     * <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. Must not be null.
+     * @param format
+     *            the CSVFormat used for CSV parsing. Must not be null.
+     * @param characterOffset
+     *            Lexer offset when the parser does not start parsing at the beginning of
the source.
+     * @param recordNumber
+     *            The next record number to assign
+     * @throws IllegalArgumentException
+     *             If the parameters of the format are inconsistent or if either reader or
format are null.
+     * @throws IOException
+     *             If there is a problem reading the header or skipping the first record
+     * @since 1.1
+     */
+    public CSVParser(final Reader reader, final CSVFormat format, long characterOffset, long
recordNumber)
+            throws IOException {
         Assertions.notNull(reader, "reader");
         Assertions.notNull(format, "format");
 
         this.format = format;
         this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));
         this.headerMap = this.initializeHeader();
+        this.characterOffset = characterOffset;
+        this.recordNumber = recordNumber - 1;
     }
 
     private void addRecordValue() {
@@ -302,43 +334,6 @@ public final class CSVParser implements 
     }
 
     /**
-     * Sets the record number to be assigned to the next record read.
-     * <p>
-     * Use this if the reader is not positioned at the first record when you create the parser.
For example, the first
-     * record read might be the 51st record in the source file.
-     * </p>
-     * <p>
-     * If you want the records to also have the correct character position referring to the
underlying source, call
-     * {@link #setNextCharacterPosition(long)}.
-     * </p>
-     * 
-     * @param nextRecordNumber
-     *            the next record number
-     * @since 1.1
-     */
-    public void setNextRecordNumber(long nextRecordNumber) {
-        this.recordNumber = nextRecordNumber - 1;
-    }
-    
-    /**
-     * Sets the current position in the source stream regardless of where the parser and
lexer start reading.
-     * <p>
-     * For example: We open a file and seek to position 5434 in order to start reading at
record 42. In order to have
-     * the parser assign the correct characterPosition to records, we call this method.
-     * </p>
-     * <p>
-     * If you want the records to also have the correct record numbers, call {@link #setNextRecordNumber(long)}
-     * </p>
-     * 
-     * @param position
-     *            the new character position
-     * @since 1.1
-     */
-    public void setNextCharacterPosition(long position) {
-        this.characterOffset = position - lexer.getCharacterPosition();
-    }
-    
-    /**
      * Returns the current record number in the input stream.
      *
      * <p>
@@ -346,7 +341,7 @@ public final class CSVParser implements 
      * the line number.
      * </p>
      *
-     * @return current line number
+     * @return current record number
      */
     public long getRecordNumber() {
         return this.recordNumber;

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java?rev=1635129&r1=1635128&r2=1635129&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java Wed Oct
29 13:33:04 2014
@@ -26,6 +26,7 @@ import java.io.Closeable;
 import java.io.Flushable;
 import java.io.IOException;
 import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
 import java.sql.SQLException;
 
 /**
@@ -498,7 +499,15 @@ public final class CSVPrinter implements
      *             if a database access error occurs
      */
     public void printRecords(final ResultSet resultSet) throws SQLException, IOException
{
-        final int columnCount = resultSet.getMetaData().getColumnCount();
+        ResultSetMetaData metaData = resultSet.getMetaData();
+        final int columnCount = metaData.getColumnCount();
+        boolean printHeader = false;
+        if (printHeader) {
+            for (int i = 1; i <= columnCount; i++) {
+                print(metaData.getColumnLabel(i));
+            }
+            println();
+        }
         while (resultSet.next()) {
             for (int i = 1; i <= columnCount; i++) {
                 print(resultSet.getString(i));

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1635129&r1=1635128&r2=1635129&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Wed Oct
29 13:33:04 2014
@@ -957,9 +957,7 @@ public class CSVParserTest {
         parser.close();
 
         // now try to read starting at record 3
-        parser = CSVParser.parse(code.substring((int) positionRecord3), format);
-        parser.setNextRecordNumber(3);
-        parser.setNextCharacterPosition(positionRecord3);
+        parser = new CSVParser(new StringReader(code.substring((int) positionRecord3)), format,
positionRecord3, 3);
 
         assertNotNull(record = parser.nextRecord());
         assertEquals(3, record.getRecordNumber());

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1635129&r1=1635128&r2=1635129&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java Wed
Oct 29 13:33:04 2014
@@ -34,6 +34,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Random;
 
+import org.junit.Ignore;
 import org.junit.Test;
 
 /**
@@ -219,9 +220,7 @@ public class CSVPrinterTest {
         final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;",
"sa", "");
         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')");
+            setUpTable(stmt);
             final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
             printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
             assertEquals("1,r1" + recordSeparator + "2,r2" + recordSeparator, sw.toString());
@@ -231,6 +230,30 @@ public class CSVPrinterTest {
         }
     }
 
+    private void setUpTable(final Statement stmt) throws SQLException {
+        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')");
+    }
+
+    @Test
+    @Ignore
+    public void testJdbcPrinterWithHeaders() throws IOException, ClassNotFoundException,
SQLException {
+        final StringWriter sw = new StringWriter();
+        Class.forName("org.h2.Driver");
+        final Connection connection = DriverManager.getConnection("jdbc:h2:mem:my_test;",
"sa", "");
+        try {
+            final Statement stmt = connection.createStatement();
+            setUpTable(stmt);
+            final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
+            printer.printRecords(stmt.executeQuery("select ID, NAME from TEST"));
+            assertEquals("ID,NAME" + recordSeparator + "1,r1" + recordSeparator + "2,r2"
+ recordSeparator, sw.toString());
+            printer.close();
+        } finally {
+            connection.close();
+        }
+    }
+
     @Test
     public void testMultiLineComment() throws IOException {
         final StringWriter sw = new StringWriter();



Mime
View raw message