You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/10/14 06:53:51 UTC

svn commit: r1398008 - in /commons/proper/csv/trunk/src: main/java/org/apache/commons/csv/CSVFormat.java test/java/org/apache/commons/csv/CSVFormatTest.java

Author: ggregory
Date: Sun Oct 14 04:53:51 2012
New Revision: 1398008

URL: http://svn.apache.org/viewvc?rev=1398008&view=rev
Log:
Add quote policy to format. (Considering renaming "encapsulator" to "quoteChar" so we have quoteChar and quotePolicy. Encapsulator makes me want to ask "encapsulate what"? fieldEncapsulator would be better but so verbose, quoteChar feels more to the point to me. )

Modified:
    commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java

Modified: commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1398008&r1=1398007&r2=1398008&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java (original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java Sun Oct 14 04:53:51 2012
@@ -47,11 +47,12 @@ public class CSVFormat implements Serial
     private final boolean ignoreEmptyLines;
     private final String lineSeparator; // for outputs
     private final String[] header;
+    private final Quote quotePolicy;
 
     /**
      * Starting format; used for creating other formats.
      */
-    static final CSVFormat PRISTINE = new CSVFormat(COMMA, null, null, null, false, false, null, null);
+    static final CSVFormat PRISTINE = new CSVFormat(COMMA, null, null, null, null, false, false, null, null);
 
     /**
      * Standard comma separated format, as for {@link #RFC4180} but allowing blank lines.
@@ -130,6 +131,8 @@ public class CSVFormat implements Serial
      *            the char used for value separation
      * @param encapsulator
      *            the char used as value encapsulation marker
+     * @param quotePolicy 
+     *            the quote policy
      * @param commentStart
      *            the char used for comment identification
      * @param escape
@@ -143,10 +146,12 @@ public class CSVFormat implements Serial
      * @param header
      *            the header
      */
-    public CSVFormat(final char delimiter, final Character encapsulator, final Character commentStart, final Character escape, final 
-            boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String lineSeparator, final String[] header) {
+    public CSVFormat(final char delimiter, final Character encapsulator, final Quote quotePolicy, final Character commentStart, final Character escape, final 
+                    boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String lineSeparator, 
+            final String[] header) {
         this.delimiter = delimiter;
         this.encapsulator = encapsulator;
+        this.quotePolicy = quotePolicy;
         this.commentStart = commentStart;
         this.escape = escape;
         this.ignoreSurroundingSpaces = ignoreSurroundingSpaces;
@@ -231,8 +236,8 @@ public class CSVFormat implements Serial
         if (isLineBreak(delimiter)) {
             throw new IllegalArgumentException("The delimiter cannot be a line break");
         }
-        return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
-                ignoreEmptyLines, lineSeparator, header);
+        return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
+                ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
     }
 
     /**
@@ -270,8 +275,8 @@ public class CSVFormat implements Serial
         if (isLineBreak(encapsulator)) {
             throw new IllegalArgumentException("The encapsulator cannot be a line break");
         }
-        return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
-                ignoreEmptyLines, lineSeparator, header);
+        return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
+                ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
     }
 
     /**
@@ -322,8 +327,8 @@ public class CSVFormat implements Serial
         if (isLineBreak(commentStart)) {
             throw new IllegalArgumentException("The comment start character cannot be a line break");
         }
-        return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
-                ignoreEmptyLines, lineSeparator, header);
+        return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
+                ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
     }
 
     /**
@@ -372,8 +377,8 @@ public class CSVFormat implements Serial
         if (isLineBreak(escape)) {
             throw new IllegalArgumentException("The escape character cannot be a line break");
         }
-        return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
-                ignoreEmptyLines, lineSeparator, header);
+        return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
+                ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
     }
 
     /**
@@ -404,8 +409,8 @@ public class CSVFormat implements Serial
      * @return A copy of this format with the specified trimming behavior.
      */
     public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
-        return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
-                ignoreEmptyLines, lineSeparator, header);
+        return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
+                ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
     }
 
     /**
@@ -427,8 +432,8 @@ public class CSVFormat implements Serial
      * @return A copy of this format with the specified empty line skipping behavior.
      */
     public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
-        return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
-                ignoreEmptyLines, lineSeparator, header);
+        return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
+                ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
     }
 
     /**
@@ -449,8 +454,8 @@ public class CSVFormat implements Serial
      * @return A copy of this format using the specified output line separator
      */
     public CSVFormat withLineSeparator(final char lineSeparator) {
-        return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
-                ignoreEmptyLines, String.valueOf(lineSeparator), header);
+        return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
+                ignoreSurroundingSpaces, ignoreEmptyLines, String.valueOf(lineSeparator), header);
     }
 
     /**
@@ -462,8 +467,21 @@ public class CSVFormat implements Serial
      * @return A copy of this format using the specified output line separator
      */
     public CSVFormat withLineSeparator(final String lineSeparator) {
-        return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
-                ignoreEmptyLines, lineSeparator, header);
+        return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
+                ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
+    }
+
+    /**
+     * Returns a copy of this format using the specified output quote policy.
+     *
+     * @param quotePolicy
+     *            the quote policy to be used for output.
+     *
+     * @return A copy of this format using the specified output line separator
+     */
+    public CSVFormat withQuotePolicy(final Quote quotePolicy) {
+        return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
+                ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
     }
 
     String[] getHeader() {
@@ -490,8 +508,8 @@ public class CSVFormat implements Serial
      * @return A copy of this format using the specified header
      */
     public CSVFormat withHeader(final String... header) {
-        return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
-                ignoreEmptyLines, lineSeparator, header);
+        return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
+                ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
     }
 
     /**
@@ -546,4 +564,13 @@ public class CSVFormat implements Serial
         return sb.toString();
     }
 
+    /**
+     * Returns the quote policy output fields.
+     *
+     * @return the quote policy
+     */
+    public Quote getQuotePolicy() {
+        return quotePolicy;
+    }
+
 }

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java?rev=1398008&r1=1398007&r2=1398008&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java Sun Oct 14 04:53:51 2012
@@ -35,7 +35,7 @@ public class CSVFormatTest {
 
     @Test
     public void testImmutalibity() {
-        final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CRLF, null);
+        final CSVFormat format = new CSVFormat('!', '!', Quote.MINIMAL, '!', '!', true, true, CRLF, null);
 
         format.withDelimiter('?');
         format.withEncapsulator('?');
@@ -44,6 +44,7 @@ public class CSVFormatTest {
         format.withEscape('?');
         format.withIgnoreSurroundingSpaces(false);
         format.withIgnoreEmptyLines(false);
+        format.withQuotePolicy(Quote.ALL);
 
         assertEquals('!', format.getDelimiter());
         assertEquals('!', format.getEncapsulator().charValue());
@@ -53,11 +54,13 @@ public class CSVFormatTest {
 
         assertTrue(format.getIgnoreSurroundingSpaces());
         assertTrue(format.getIgnoreEmptyLines());
+
+        assertEquals(Quote.MINIMAL, format.getQuotePolicy());
     }
 
     @Test
     public void testMutators() {
-        final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CRLF, null);
+        final CSVFormat format = new CSVFormat('!', '!', null, '!', '!', true, true, CRLF, null);
 
         assertEquals('?', format.withDelimiter('?').getDelimiter());
         assertEquals('?', format.withEncapsulator('?').getEncapsulator().charValue());
@@ -67,6 +70,8 @@ public class CSVFormatTest {
 
         assertFalse(format.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
         assertFalse(format.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
+
+        assertEquals(Quote.ALL, format.withQuotePolicy(Quote.ALL).getQuotePolicy());
     }
 
     @Test