commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1306947 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/ test/java/org/apache/commons/csv/
Date Thu, 29 Mar 2012 16:03:04 GMT
Author: sebb
Date: Thu Mar 29 16:03:03 2012
New Revision: 1306947

URL: http://svn.apache.org/viewvc?rev=1306947&view=rev
Log:
CSV-85 Allow comments to be returned in CSVRecord

Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVLexer.java
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexerTest.java

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVLexer.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVLexer.java?rev=1306947&r1=1306946&r2=1306947&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVLexer.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVLexer.java Thu Mar 29
16:03:03 2012
@@ -77,7 +77,8 @@ class CSVLexer extends Lexer {
         }
 
         if (isStartOfLine(lastChar) && isCommentStart(c)) {
-            in.readLine();
+            String comment = in.readLine().trim();
+            tkn.content.append(comment);
             tkn.type = COMMENT;
             return tkn;
         }

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=1306947&r1=1306946&r2=1306947&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 Thu Mar 29
16:03:03 2012
@@ -133,8 +133,9 @@ public class CSVParser implements Iterab
      * @throws IOException on parse error or input read-failure
      */
     CSVRecord getRecord() throws IOException {
-        CSVRecord result = new CSVRecord(null, headerMapping);
+        CSVRecord result = new CSVRecord(null, headerMapping, null);
         record.clear();
+        StringBuilder sb = null;
         do {
             reusableToken.reset();
             lexer.nextToken(reusableToken);
@@ -155,13 +156,20 @@ public class CSVParser implements Iterab
                 case INVALID:
                     throw new IOException("(line " + getLineNumber() + ") invalid parse sequence");
                 case COMMENT: // Ignored currently
+                    if (sb == null) { // first comment for this record
+                        sb = new StringBuilder();
+                    } else {
+                        sb.append("\n");
+                    }
+                    sb.append(reusableToken.content);
                     reusableToken.type = TOKEN; // Read another token
                     break;
             }
         } while (reusableToken.type == TOKEN);
         
         if (!record.isEmpty()) {
-            result = new CSVRecord(record.toArray(new String[record.size()]), headerMapping);
+            result = new CSVRecord(record.toArray(new String[record.size()]), headerMapping,

+                    sb == null ? null : sb.toString());
         }
         return result;
     }

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java?rev=1306947&r1=1306946&r2=1306947&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java Thu Mar 29
16:03:03 2012
@@ -35,9 +35,13 @@ public class CSVRecord implements Serial
     /** The column name to index mapping. */
     private final Map<String, Integer> mapping;
 
-    CSVRecord(String[] values, Map<String, Integer> mapping) {
+    /** The accumulated comments (if any) */
+    private final String comment;
+
+    CSVRecord(String[] values, Map<String, Integer> mapping, String comment) {
         this.values = values != null ? values : EMPTY_STRING_ARRAY;
         this.mapping = mapping;
+        this.comment = comment;
     }
 
     /**
@@ -74,6 +78,9 @@ public class CSVRecord implements Serial
         return values;
     }
 
+    public String getComment() {
+        return comment;
+    }
     /**
      * Returns the number of values in this record.
      */

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java?rev=1306947&r1=1306946&r2=1306947&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFileParserTest.java Thu
Mar 29 16:03:03 2012
@@ -89,6 +89,7 @@ public class CSVFileParserTest {
          // first line starts with csv data file name
         BufferedReader csvFile = new BufferedReader(new FileReader(new File(BASE, split[0])));
         CSVFormat fmt = CSVFormat.PRISTINE.withDelimiter(',').withEncapsulator('"');
+        boolean checkComments = false;
         for(int i=1; i < split.length; i++) {
             final String option = split[i];
             String[] option_parts = option.split("=",2);
@@ -98,6 +99,8 @@ public class CSVFileParserTest {
                 fmt = fmt.withSurroundingSpacesIgnored(Boolean.parseBoolean(option_parts[1]));
             } else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
                 fmt = fmt.withCommentStart(option_parts[1].charAt(0));
+            } else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
+                checkComments = true;
             } else {
                 fail(testName+" unexpected option: "+option);
             }
@@ -108,6 +111,12 @@ public class CSVFileParserTest {
         // Now parse the file and compare against the expected results
         for(CSVRecord rec : fmt.parse(csvFile)) {
             String parsed = rec.toString();
+            if (checkComments) {
+                final String comment = rec.getComment().replace("\n", "\\n");
+                if (comment != null) {
+                    parsed += "#" + comment;
+                }
+            }
             int count = rec.size();
             assertEquals(testName, readTestData(), count+":"+parsed);
         }

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexerTest.java?rev=1306947&r1=1306946&r2=1306947&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexerTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVLexerTest.java Thu Mar
29 16:03:03 2012
@@ -63,7 +63,7 @@ public class CSVLexerTest {
                 "a,b x,c#no-comment\n"+    // 2
                 "\n"+
                 "\n"+
-                "#foo\n"+                  // 3
+                "# foo \n"+                // 3
                 "\n"+                      // 4
                 "d,e,#no-comment\n"+       // 5
                 "\n"+
@@ -85,13 +85,13 @@ public class CSVLexerTest {
         assertTokenEquals(TOKEN, "a", parser.nextToken(new Token()));
         assertTokenEquals(TOKEN, "b x", parser.nextToken(new Token()));
         assertTokenEquals(EORECORD, "c#no-comment", parser.nextToken(new Token())); // 2
-        assertTokenEquals(COMMENT, "", parser.nextToken(new Token()));              // 3
+        assertTokenEquals(COMMENT, "foo", parser.nextToken(new Token()));              //
3
         // 4 empty line, ignored                                                    // 4
         assertTokenEquals(TOKEN, "d", parser.nextToken(new Token()));
         assertTokenEquals(TOKEN, "e", parser.nextToken(new Token()));
         assertTokenEquals(EORECORD, "#no-comment", parser.nextToken(new Token()));  // 5
-        assertTokenEquals(COMMENT, "", parser.nextToken(new Token()));              // 6
-        assertTokenEquals(COMMENT, "", parser.nextToken(new Token()));              // 7
+        assertTokenEquals(COMMENT, "penultimate comment", parser.nextToken(new Token()));
             // 6
+        assertTokenEquals(COMMENT, "Final comment", parser.nextToken(new Token()));     
        // 7
         assertTokenEquals(EOF, "", parser.nextToken(new Token()));
         assertTokenEquals(EOF, "", parser.nextToken(new Token()));
 
@@ -130,7 +130,7 @@ public class CSVLexerTest {
         assertTokenEquals(TOKEN, "a", parser.nextToken(new Token()));
         assertTokenEquals(TOKEN, "b x", parser.nextToken(new Token()));
         assertTokenEquals(EORECORD, "c#no-comment", parser.nextToken(new Token())); // 2
-        assertTokenEquals(COMMENT, "", parser.nextToken(new Token()));              // 3
+        assertTokenEquals(COMMENT, "foo", parser.nextToken(new Token()));           // 3
         assertTokenEquals(EORECORD, "", parser.nextToken(new Token()));             // 4
         assertTokenEquals(EORECORD, "", parser.nextToken(new Token()));             // 4b
         assertTokenEquals(TOKEN, "d", parser.nextToken(new Token()));
@@ -138,10 +138,10 @@ public class CSVLexerTest {
         assertTokenEquals(EORECORD, "#no-comment", parser.nextToken(new Token()));  // 5
         assertTokenEquals(EORECORD, "", parser.nextToken(new Token()));             // 5b
         assertTokenEquals(EORECORD, "", parser.nextToken(new Token()));             // 5c
-        assertTokenEquals(COMMENT, "", parser.nextToken(new Token()));              // 6
+        assertTokenEquals(COMMENT, "penultimate comment", parser.nextToken(new Token()));
             // 6
         assertTokenEquals(EORECORD, "", parser.nextToken(new Token()));             // 6b
         assertTokenEquals(EORECORD, "", parser.nextToken(new Token()));             // 6c
-        assertTokenEquals(COMMENT, "", parser.nextToken(new Token()));              // 7
+        assertTokenEquals(COMMENT, "Final comment", parser.nextToken(new Token()));     
        // 7
         assertTokenEquals(EOF, "", parser.nextToken(new Token()));
         assertTokenEquals(EOF, "", parser.nextToken(new Token()));
 



Mime
View raw message