Author: ebourg Date: Thu Nov 10 00:26:09 2011 New Revision: 1200061 URL: http://svn.apache.org/viewvc?rev=1200061&view=rev Log: Reduced the constants in CSVFormat for disabling the features to only one
Modified: commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java Modified: commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1200061&r1=1200060&r2=1200061&view=diff ============================================================================== --- commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java (original) +++ commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java Thu Nov 10 00:26:09 2011 @@ -28,30 +28,31 @@ public class CSVFormat implements Clonea private char delimiter = ','; private char encapsulator = '"'; - private char commentStart = COMMENTS_DISABLED; - private char escape = ESCAPE_DISABLED; + private char commentStart = DISABLED; + private char escape = DISABLED; private boolean leadingSpacesIgnored = true; private boolean trailingSpacesIgnored = true; private boolean unicodeEscapesInterpreted = false; private boolean emptyLinesIgnored = true; private String lineSeparator = "\n"; - // -2 is used to signal disabled, because it won't be confused with - // an EOF signal (-1), and because \ufffe in UTF-16 would be - // encoded as two chars (using surrogates) and thus there should never - // be a collision with a real text char. - public static final char COMMENTS_DISABLED = (char) -2; - public static final char ESCAPE_DISABLED = (char) -2; - public static final char ENCAPSULATOR_DISABLED = (char) -2; + + /** + * Constant char to be used for disabling comments, escapes and encapsulation. + * The value -2 is used because it won't be confused with an EOF signal (-1), + * and because the unicode value FFFE would be encoded as two chars (using surrogates) + * and thus there should never be a collision with a real text char. + */ + public static final char DISABLED = '\ufffe'; /** Standard comma separated format. */ - public static final CSVFormat DEFAULT = new CSVFormat(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true); - + public static final CSVFormat DEFAULT = new CSVFormat(',', '"', DISABLED, DISABLED, true, true, false, true); + /** Excel file format (using a comma as the value delimiter). */ - public static final CSVFormat EXCEL = new CSVFormat(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false, false, false, false); - + public static final CSVFormat EXCEL = new CSVFormat(',', '"', DISABLED, DISABLED, false, false, false, false); + /** Tabulation delimited format. */ - public static final CSVFormat TDF = new CSVFormat('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true); + public static final CSVFormat TDF = new CSVFormat('\t', '"', DISABLED, DISABLED, true, true, false, true); /** @@ -61,7 +62,7 @@ public class CSVFormat implements Clonea } public CSVFormat(char delimiter, char encapsulator, char commentStart) { - this(delimiter, encapsulator, commentStart, ESCAPE_DISABLED, true, true, false, true); + this(delimiter, encapsulator, commentStart, DISABLED, true, true, false, true); } /** @@ -115,6 +116,10 @@ public class CSVFormat implements Clonea return format; } + boolean isEncapsulating() { + return this.encapsulator != DISABLED; + } + public char getCommentStart() { return commentStart; } @@ -126,7 +131,7 @@ public class CSVFormat implements Clonea } public boolean isCommentingDisabled() { - return this.commentStart == COMMENTS_DISABLED; + return this.commentStart == DISABLED; } public char getEscape() { @@ -139,6 +144,10 @@ public class CSVFormat implements Clonea return format; } + boolean isEscaping() { + return this.escape != DISABLED; + } + public boolean isLeadingSpacesIgnored() { return leadingSpacesIgnored; } Modified: commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java?rev=1200061&r1=1200060&r2=1200061&view=diff ============================================================================== --- commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java (original) +++ commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java Thu Nov 10 00:26:09 2011 @@ -127,10 +127,10 @@ public class CSVPrinter { out.write(value, offset, len); return; } - - if (format.getEncapsulator() != CSVFormat.ENCAPSULATOR_DISABLED) { + + if (format.isEncapsulating()) { printAndEncapsulate(value, offset, len); - } else if (format.getEscape() != CSVFormat.ESCAPE_DISABLED) { + } else if (format.isEscaping()) { printAndEscape(value, offset, len); } else { printSep(); Modified: commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1200061&r1=1200060&r2=1200061&view=diff ============================================================================== --- commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java (original) +++ commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java Thu Nov 10 00:26:09 2011 @@ -438,7 +438,7 @@ public class CSVParserTest extends TestC }; - CSVFormat format = new CSVFormat(',', '\'', CSVFormat.COMMENTS_DISABLED, '/', false, false, true, true); + CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/', false, false, true, true); CSVParser parser = new CSVParser(new StringReader(code), format); String[][] tmp = parser.getRecords(); @@ -466,7 +466,7 @@ public class CSVParserTest extends TestC }; - CSVFormat format = new CSVFormat(',', CSVFormat.ENCAPSULATOR_DISABLED, CSVFormat.COMMENTS_DISABLED, '/', false, false, true, true); + CSVFormat format = new CSVFormat(',', CSVFormat.DISABLED, CSVFormat.DISABLED, '/', false, false, true, true); CSVParser parser = new CSVParser(new StringReader(code), format); String[][] tmp = parser.getRecords(); @@ -493,7 +493,7 @@ public class CSVParserTest extends TestC }; CSVFormat format = CSVFormat.DEFAULT; - assertEquals(CSVFormat.COMMENTS_DISABLED, format.getCommentStart()); + assertEquals(CSVFormat.DISABLED, format.getCommentStart()); CSVParser parser = new CSVParser(new StringReader(code), format); String[][] tmp = parser.getRecords(); Modified: commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1200061&r1=1200060&r2=1200061&view=diff ============================================================================== --- commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java (original) +++ commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java Thu Nov 10 00:26:09 2011 @@ -111,7 +111,7 @@ public class CSVPrinterTest extends Test doRandom(iter); // Format for MySQL - format = new CSVFormat('\t', CSVFormat.ENCAPSULATOR_DISABLED, CSVFormat.COMMENTS_DISABLED, '\\', false, false, false, false); + format = new CSVFormat('\t', CSVFormat.DISABLED, CSVFormat.DISABLED, '\\', false, false, false, false); doRandom(iter); }