From cb99634ab3d6143dffc90938fc68e15c7f9d25b8 Mon Sep 17 00:00:00 2001 From: Emmanuel Bourg Date: Wed, 9 Nov 2011 16:54:09 +0000 Subject: [PATCH] Renamed CSVStrategy to CSVFormat git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1199842 13f79535-47bb-0310-9956-ffa450edef68 --- .../csv/{CSVStrategy.java => CSVFormat.java} | 92 +++++++++---------- .../org/apache/commons/csv/CSVParser.java | 66 ++++++------- .../org/apache/commons/csv/CSVPrinter.java | 39 ++++---- .../java/org/apache/commons/csv/CSVUtils.java | 10 +- ...SVStrategyTest.java => CSVFormatTest.java} | 24 ++--- .../org/apache/commons/csv/CSVParserTest.java | 58 ++++++------ .../apache/commons/csv/CSVPrinterTest.java | 32 +++---- .../org/apache/commons/csv/CSVUtilsTest.java | 2 +- 8 files changed, 161 insertions(+), 162 deletions(-) rename src/main/java/org/apache/commons/csv/{CSVStrategy.java => CSVFormat.java} (64%) rename src/test/java/org/apache/commons/csv/{CSVStrategyTest.java => CSVFormatTest.java} (60%) diff --git a/src/main/java/org/apache/commons/csv/CSVStrategy.java b/src/main/java/org/apache/commons/csv/CSVFormat.java similarity index 64% rename from src/main/java/org/apache/commons/csv/CSVStrategy.java rename to src/main/java/org/apache/commons/csv/CSVFormat.java index f79a021b..b467e69f 100644 --- a/src/main/java/org/apache/commons/csv/CSVStrategy.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -20,11 +20,11 @@ package org.apache.commons.csv; import java.io.Serializable; /** - * CSVStrategy + * The format specification of a CSV file. * - * Represents the strategy for a CSV. + * This class is immutable. */ -public class CSVStrategy implements Cloneable, Serializable { +public class CSVFormat implements Cloneable, Serializable { private char delimiter = ','; private char encapsulator = '"'; @@ -45,27 +45,27 @@ public class CSVStrategy implements Cloneable, Serializable { public static final char ENCAPSULATOR_DISABLED = (char) -2; /** Standard comma separated format. */ - public static final CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true); + public static final CSVFormat DEFAULT = new CSVFormat(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true); /** Excel file format (using a comma as the value delimiter). */ - public static final CSVStrategy EXCEL_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false, false, false, false); + public static final CSVFormat EXCEL = new CSVFormat(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false, false, false, false); /** Tabulation delimited format. */ - public static final CSVStrategy TDF_STRATEGY = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true); + public static final CSVFormat TDF = new CSVFormat('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true); /** - * Creates a CSVStrategy with the default parameters. + * Creates a CSVFormat with the default parameters. */ - public CSVStrategy() { + public CSVFormat() { } - public CSVStrategy(char delimiter, char encapsulator, char commentStart) { + public CSVFormat(char delimiter, char encapsulator, char commentStart) { this(delimiter, encapsulator, commentStart, ESCAPE_DISABLED, true, true, false, true); } /** - * Customized CSV strategy constructor. + * Customized CSV format constructor. * * @param delimiter a char used for value separation * @param encapsulator a char used as value encapsulation marker @@ -76,7 +76,7 @@ public class CSVStrategy implements Cloneable, Serializable { * @param unicodeEscapesInterpreted TRUE when unicode escapes should be interpreted * @param emptyLinesIgnored TRUE when the parser should skip emtpy lines */ - public CSVStrategy( + public CSVFormat( char delimiter, char encapsulator, char commentStart, @@ -99,30 +99,30 @@ public class CSVStrategy implements Cloneable, Serializable { return delimiter; } - public CSVStrategy withDelimiter(char delimiter) { - CSVStrategy strategy = (CSVStrategy) clone(); + public CSVFormat withDelimiter(char delimiter) { + CSVFormat format = (CSVFormat) clone(); this.delimiter = delimiter; - return strategy; + return format; } public char getEncapsulator() { return encapsulator; } - public CSVStrategy withEncapsulator(char encapsulator) { - CSVStrategy strategy = (CSVStrategy) clone(); - strategy.encapsulator = encapsulator; - return strategy; + public CSVFormat withEncapsulator(char encapsulator) { + CSVFormat format = (CSVFormat) clone(); + format.encapsulator = encapsulator; + return format; } public char getCommentStart() { return commentStart; } - public CSVStrategy withCommentStart(char commentStart) { - CSVStrategy strategy = (CSVStrategy) clone(); - strategy.commentStart = commentStart; - return strategy; + public CSVFormat withCommentStart(char commentStart) { + CSVFormat format = (CSVFormat) clone(); + format.commentStart = commentStart; + return format; } public boolean isCommentingDisabled() { @@ -133,60 +133,60 @@ public class CSVStrategy implements Cloneable, Serializable { return escape; } - public CSVStrategy withEscape(char escape) { - CSVStrategy strategy = (CSVStrategy) clone(); - strategy.escape = escape; - return strategy; + public CSVFormat withEscape(char escape) { + CSVFormat format = (CSVFormat) clone(); + format.escape = escape; + return format; } public boolean isLeadingSpacesIgnored() { return leadingSpacesIgnored; } - public CSVStrategy withLeadingSpacesIgnored(boolean leadingSpacesIgnored) { - CSVStrategy strategy = (CSVStrategy) clone(); - strategy.leadingSpacesIgnored = leadingSpacesIgnored; - return strategy; + public CSVFormat withLeadingSpacesIgnored(boolean leadingSpacesIgnored) { + CSVFormat format = (CSVFormat) clone(); + format.leadingSpacesIgnored = leadingSpacesIgnored; + return format; } public boolean isTrailingSpacesIgnored() { return trailingSpacesIgnored; } - public CSVStrategy withTrailingSpacesIgnored(boolean trailingSpacesIgnored) { - CSVStrategy strategy = (CSVStrategy) clone(); - strategy.trailingSpacesIgnored = trailingSpacesIgnored; - return strategy; + public CSVFormat withTrailingSpacesIgnored(boolean trailingSpacesIgnored) { + CSVFormat format = (CSVFormat) clone(); + format.trailingSpacesIgnored = trailingSpacesIgnored; + return format; } public boolean isUnicodeEscapesInterpreted() { return unicodeEscapesInterpreted; } - public CSVStrategy withUnicodeEscapesInterpreted(boolean unicodeEscapesInterpreted) { - CSVStrategy strategy = (CSVStrategy) clone(); - strategy.unicodeEscapesInterpreted = unicodeEscapesInterpreted; - return strategy; + public CSVFormat withUnicodeEscapesInterpreted(boolean unicodeEscapesInterpreted) { + CSVFormat format = (CSVFormat) clone(); + format.unicodeEscapesInterpreted = unicodeEscapesInterpreted; + return format; } public boolean isEmptyLinesIgnored() { return emptyLinesIgnored; } - public CSVStrategy withEmptyLinesIgnored(boolean emptyLinesIgnored) { - CSVStrategy strategy = (CSVStrategy) clone(); - strategy.emptyLinesIgnored = emptyLinesIgnored; - return strategy; + public CSVFormat withEmptyLinesIgnored(boolean emptyLinesIgnored) { + CSVFormat format = (CSVFormat) clone(); + format.emptyLinesIgnored = emptyLinesIgnored; + return format; } public String getLineSeparator() { return lineSeparator; } - public CSVStrategy withLineSeparator(String lineSeparator) { - CSVStrategy strategy = (CSVStrategy) clone(); - strategy.lineSeparator = lineSeparator; - return strategy; + public CSVFormat withLineSeparator(String lineSeparator) { + CSVFormat format = (CSVFormat) clone(); + format.lineSeparator = lineSeparator; + return format; } protected Object clone() { diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index 89f6d040..0f36fef4 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -27,23 +27,23 @@ import java.util.List; * Parses CSV files according to the specified configuration. * * Because CSV appears in many different dialects, the parser supports many - * configuration settings by allowing the specification of a {@link CSVStrategy}. + * configuration settings by allowing the specification of a {@link CSVFormat}. * *

Parsing of a csv-string having tabs as separators, * '"' as an optional value encapsulator, and comments starting with '#':

*
  *  String[][] data =
- *   (new CSVParser(new StringReader("a\tb\nc\td"), new CSVStrategy('\t','"','#'))).getAllValues();
+ *   (new CSVParser(new StringReader("a\tb\nc\td"), new CSVFormat('\t','"','#'))).getAllValues();
  * 
* *

Parsing of a csv-string in Excel CSV format

*
  *  String[][] data =
- *   (new CSVParser(new StringReader("a;b\nc;d"), CSVStrategy.EXCEL_STRATEGY)).getAllValues();
+ *   (new CSVParser(new StringReader("a;b\nc;d"), CSVFormat.EXCEL)).getAllValues();
  * 
* *

- * Internal parser state is completely covered by the strategy + * Internal parser state is completely covered by the format * and the reader-state.

* *

see package documentation @@ -73,7 +73,7 @@ public class CSVParser { // the input stream private final ExtendedBufferedReader in; - private final CSVStrategy strategy; + private final CSVFormat format; // the following objects are shared to reduce garbage /** @@ -117,23 +117,23 @@ public class CSVParser { // ====================================================== /** - * CSV parser using the default {@link CSVStrategy}. + * CSV parser using the default {@link CSVFormat}. * * @param input a Reader containing "csv-formatted" input */ public CSVParser(Reader input) { - this(input, CSVStrategy.DEFAULT_STRATEGY); + this(input, CSVFormat.DEFAULT); } /** - * Customized CSV parser using the given {@link CSVStrategy} + * Customized CSV parser using the given {@link CSVFormat} * * @param input a Reader containing "csv-formatted" input - * @param strategy the CSVStrategy used for CSV parsing + * @param format the CSVFormat used for CSV parsing */ - public CSVParser(Reader input, CSVStrategy strategy) { + public CSVParser(Reader input, CSVFormat format) { this.in = new ExtendedBufferedReader(input); - this.strategy = strategy; + this.format = format; } // ====================================================== @@ -141,7 +141,7 @@ public class CSVParser { // ====================================================== /** - * Parses the CSV according to the given strategy + * Parses the CSV according to the given format * and returns the content as an array of records * (whereas records are arrays of single values). *

@@ -260,7 +260,7 @@ public class CSVParser { c = in.readAgain(); // empty line detection: eol AND (last char was EOL or beginning) - while (strategy.isEmptyLinesIgnored() && eol + while (format.isEmptyLinesIgnored() && eol && (lastChar == '\n' || lastChar == '\r' || lastChar == ExtendedBufferedReader.UNDEFINED) @@ -278,7 +278,7 @@ public class CSVParser { } // did we reach eof during the last iteration already ? TT_EOF - if (isEndOfFile(lastChar) || (lastChar != strategy.getDelimiter() && isEndOfFile(c))) { + if (isEndOfFile(lastChar) || (lastChar != format.getDelimiter() && isEndOfFile(c))) { tkn.type = TT_EOF; return tkn; } @@ -286,17 +286,17 @@ public class CSVParser { // important: make sure a new char gets consumed in each iteration while (!tkn.isReady && tkn.type != TT_EOF) { // ignore whitespaces at beginning of a token - while (strategy.isLeadingSpacesIgnored() && isWhitespace(c) && !eol) { + while (format.isLeadingSpacesIgnored() && isWhitespace(c) && !eol) { wsBuf.append((char) c); c = in.read(); eol = isEndOfLine(c); } // ok, start of token reached: comment, encapsulated, or token - if (c == strategy.getCommentStart()) { + if (c == format.getCommentStart()) { // ignore everything till end of line and continue (incr linecount) in.readLine(); tkn = nextToken(tkn.reset()); - } else if (c == strategy.getDelimiter()) { + } else if (c == format.getDelimiter()) { // empty token return TT_TOKEN("") tkn.type = TT_TOKEN; tkn.isReady = true; @@ -305,7 +305,7 @@ public class CSVParser { //noop: tkn.content.append(""); tkn.type = TT_EORECORD; tkn.isReady = true; - } else if (c == strategy.getEncapsulator()) { + } else if (c == format.getEncapsulator()) { // consume encapsulated token encapsulatedTokenLexer(tkn, c); } else if (isEndOfFile(c)) { @@ -316,7 +316,7 @@ public class CSVParser { } else { // next token must be a simple token // add removed blanks when not ignoring whitespace chars... - if (!strategy.isLeadingSpacesIgnored()) { + if (!format.isLeadingSpacesIgnored()) { tkn.content.append(wsBuf); } simpleTokenLexer(tkn, c); @@ -354,15 +354,15 @@ public class CSVParser { tkn.type = TT_EOF; tkn.isReady = true; break; - } else if (c == strategy.getDelimiter()) { + } else if (c == format.getDelimiter()) { // end of token tkn.type = TT_TOKEN; tkn.isReady = true; break; - } else if (c == '\\' && strategy.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') { + } else if (c == '\\' && format.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') { // interpret unicode escaped chars (like \u0070 -> p) tkn.content.append((char) unicodeEscapeLexer(c)); - } else if (c == strategy.getEscape()) { + } else if (c == format.getEscape()) { tkn.content.append((char) readEscape(c)); } else { tkn.content.append((char) c); @@ -371,7 +371,7 @@ public class CSVParser { c = in.read(); } - if (strategy.isTrailingSpacesIgnored()) { + if (format.isTrailingSpacesIgnored()) { tkn.content.trimTrailingWhitespace(); } @@ -400,12 +400,12 @@ public class CSVParser { for (; ;) { c = in.read(); - if (c == '\\' && strategy.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') { + if (c == '\\' && format.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') { tkn.content.append((char) unicodeEscapeLexer(c)); - } else if (c == strategy.getEscape()) { + } else if (c == format.getEscape()) { tkn.content.append((char) readEscape(c)); - } else if (c == strategy.getEncapsulator()) { - if (in.lookAhead() == strategy.getEncapsulator()) { + } else if (c == format.getEncapsulator()) { + if (in.lookAhead() == format.getEncapsulator()) { // double or escaped encapsulator -> add single encapsulator to token c = in.read(); tkn.content.append((char) c); @@ -413,7 +413,7 @@ public class CSVParser { // token finish mark (encapsulator) reached: ignore whitespace till delimiter for (; ;) { c = in.read(); - if (c == strategy.getDelimiter()) { + if (c == format.getDelimiter()) { tkn.type = TT_TOKEN; tkn.isReady = true; return tkn; @@ -512,12 +512,12 @@ public class CSVParser { // ====================================================== /** - * Obtain the specified CSV Strategy. This should not be modified. + * Obtain the specified CSV format. * - * @return strategy currently being used + * @return format currently being used */ - public CSVStrategy getStrategy() { - return this.strategy; + public CSVFormat getFormat() { + return this.format; } // ====================================================== @@ -528,7 +528,7 @@ public class CSVParser { * @return true if the given char is a whitespace character */ private boolean isWhitespace(int c) { - return Character.isWhitespace((char) c) && (c != strategy.getDelimiter()); + return Character.isWhitespace((char) c) && (c != format.getDelimiter()); } /** diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java index 99b712bd..544d9eca 100644 --- a/src/main/java/org/apache/commons/csv/CSVPrinter.java +++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java @@ -27,7 +27,7 @@ public class CSVPrinter { /** The place that the values get written. */ private final Writer out; - private final CSVStrategy strategy; + private final CSVFormat format; /** True if we just began a new line. */ private boolean newLine = true; @@ -36,18 +36,17 @@ public class CSVPrinter { private char[] buf = new char[0]; /** - * Create a printer that will print values to the given - * stream following the CSVStrategy. + * Create a printer that will print values to the given stream following the CSVFormat. *

- * Currently, only a pure encapsulation strategy or a pure escaping strategy - * is supported. Hybrid strategies (encapsulation and escaping with a different character) are not supported. + * Currently, only a pure encapsulation format or a pure escaping format + * is supported. Hybrid formats (encapsulation and escaping with a different character) are not supported. * - * @param out stream to which to print. - * @param strategy describes the CSV variation. + * @param out stream to which to print. + * @param format describes the CSV variation. */ - public CSVPrinter(Writer out, CSVStrategy strategy) { + public CSVPrinter(Writer out, CSVFormat format) { this.out = out; - this.strategy = strategy == null ? CSVStrategy.DEFAULT_STRATEGY : strategy; + this.format = format == null ? CSVFormat.DEFAULT : format; } // ====================================================== @@ -58,7 +57,7 @@ public class CSVPrinter { * Output a blank line */ public void println() throws IOException { - out.write(strategy.getLineSeparator()); + out.write(format.getLineSeparator()); newLine = true; } @@ -92,13 +91,13 @@ public class CSVPrinter { * @param comment the comment to output */ public void printlnComment(String comment) throws IOException { - if (this.strategy.isCommentingDisabled()) { + if (this.format.isCommentingDisabled()) { return; } if (!newLine) { println(); } - out.write(this.strategy.getCommentStart()); + out.write(this.format.getCommentStart()); out.write(' '); for (int i = 0; i < comment.length(); i++) { char c = comment.charAt(i); @@ -110,7 +109,7 @@ public class CSVPrinter { // break intentionally excluded. case '\n': println(); - out.write(this.strategy.getCommentStart()); + out.write(this.format.getCommentStart()); out.write(' '); break; default: @@ -129,9 +128,9 @@ public class CSVPrinter { return; } - if (strategy.getEncapsulator() != CSVStrategy.ENCAPSULATOR_DISABLED) { + if (format.getEncapsulator() != CSVFormat.ENCAPSULATOR_DISABLED) { printAndEncapsulate(value, offset, len); - } else if (strategy.getEscape() != CSVStrategy.ESCAPE_DISABLED) { + } else if (format.getEscape() != CSVFormat.ESCAPE_DISABLED) { printAndEscape(value, offset, len); } else { printSep(); @@ -143,7 +142,7 @@ public class CSVPrinter { if (newLine) { newLine = false; } else { - out.write(this.strategy.getDelimiter()); + out.write(this.format.getDelimiter()); } } @@ -154,8 +153,8 @@ public class CSVPrinter { printSep(); - char delim = this.strategy.getDelimiter(); - char escape = this.strategy.getEscape(); + char delim = this.format.getDelimiter(); + char escape = this.format.getEscape(); while (pos < end) { char c = value[pos]; @@ -196,8 +195,8 @@ public class CSVPrinter { printSep(); - char delim = this.strategy.getDelimiter(); - char encapsulator = this.strategy.getEncapsulator(); + char delim = this.format.getDelimiter(); + char encapsulator = this.format.getEncapsulator(); if (len <= 0) { // always quote an empty token that is the first diff --git a/src/main/java/org/apache/commons/csv/CSVUtils.java b/src/main/java/org/apache/commons/csv/CSVUtils.java index 6b6ee5e9..f1374719 100644 --- a/src/main/java/org/apache/commons/csv/CSVUtils.java +++ b/src/main/java/org/apache/commons/csv/CSVUtils.java @@ -48,10 +48,10 @@ public class CSVUtils { * @return the CSV string, will be an empty string if the length of the * value array is 0 */ - public static String printLine(String[] values, CSVStrategy strategy) { + public static String printLine(String[] values, CSVFormat format) { // set up a CSVUtils StringWriter stringWriter = new StringWriter(); - CSVPrinter csvPrinter = new CSVPrinter(stringWriter, strategy); + CSVPrinter csvPrinter = new CSVPrinter(stringWriter, format); // check for null values an "null" as strings and convert them // into the strings "null" and "\"null\"" @@ -78,7 +78,7 @@ public class CSVUtils { // ====================================================== /** - * Parses the given String according to the default {@link CSVStrategy}. + * Parses the given String according to the default {@link CSVFormat}. * * @param s CSV String to be parsed. * @return parsed String matrix (which is never null) @@ -90,7 +90,7 @@ public class CSVUtils { } String[][] result = (new CSVParser(new StringReader(s))).getAllValues(); if (result == null) { - // since CSVStrategy ignores empty lines an empty array is returned + // since CSVFormat ignores empty lines an empty array is returned // (i.e. not "result = new String[][] {{""}};") result = EMPTY_DOUBLE_STRING_ARRAY; } @@ -98,7 +98,7 @@ public class CSVUtils { } /** - * Parses the first line only according to the default {@link CSVStrategy}. + * Parses the first line only according to the default {@link CSVFormat}. * * Parsing empty string will be handled as valid records containing zero * elements, so the following property holds: parseLine("").length == 0. diff --git a/src/test/java/org/apache/commons/csv/CSVStrategyTest.java b/src/test/java/org/apache/commons/csv/CSVFormatTest.java similarity index 60% rename from src/test/java/org/apache/commons/csv/CSVStrategyTest.java rename to src/test/java/org/apache/commons/csv/CSVFormatTest.java index abc6c41c..14d1ad0d 100644 --- a/src/test/java/org/apache/commons/csv/CSVStrategyTest.java +++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java @@ -19,11 +19,11 @@ package org.apache.commons.csv; import junit.framework.TestCase; -public class CSVStrategyTest extends TestCase { +public class CSVFormatTest extends TestCase { public void testImmutalibity() { - CSVStrategy strategy1 = new CSVStrategy('!', '!', '!', '!', true, true, true, true); - CSVStrategy strategy2 = strategy1.withDelimiter('?') + CSVFormat format1 = new CSVFormat('!', '!', '!', '!', true, true, true, true); + CSVFormat format2 = format1.withDelimiter('?') .withEncapsulator('?') .withCommentStart('?') .withLineSeparator("?") @@ -33,16 +33,16 @@ public class CSVStrategyTest extends TestCase { .withEmptyLinesIgnored(false) .withUnicodeEscapesInterpreted(false); - assertNotSame(strategy1.getDelimiter(), strategy2.getDelimiter()); - assertNotSame(strategy1.getEncapsulator(), strategy2.getEncapsulator()); - assertNotSame(strategy1.getCommentStart(), strategy2.getCommentStart()); - assertNotSame(strategy1.getEscape(), strategy2.getEscape()); - assertNotSame(strategy1.getLineSeparator(), strategy2.getLineSeparator()); + assertNotSame(format1.getDelimiter(), format2.getDelimiter()); + assertNotSame(format1.getEncapsulator(), format2.getEncapsulator()); + assertNotSame(format1.getCommentStart(), format2.getCommentStart()); + assertNotSame(format1.getEscape(), format2.getEscape()); + assertNotSame(format1.getLineSeparator(), format2.getLineSeparator()); - assertNotSame(strategy1.isTrailingSpacesIgnored(), strategy2.isTrailingSpacesIgnored()); - assertNotSame(strategy1.isLeadingSpacesIgnored(), strategy2.isLeadingSpacesIgnored()); - assertNotSame(strategy1.isEmptyLinesIgnored(), strategy2.isEmptyLinesIgnored()); - assertNotSame(strategy1.isUnicodeEscapesInterpreted(), strategy2.isUnicodeEscapesInterpreted()); + assertNotSame(format1.isTrailingSpacesIgnored(), format2.isTrailingSpacesIgnored()); + assertNotSame(format1.isLeadingSpacesIgnored(), format2.isLeadingSpacesIgnored()); + assertNotSame(format1.isEmptyLinesIgnored(), format2.isEmptyLinesIgnored()); + assertNotSame(format1.isUnicodeEscapesInterpreted(), format2.isUnicodeEscapesInterpreted()); } } diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index be03c0e4..d7c85c06 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -47,8 +47,8 @@ public class CSVParserTest extends TestCase { super(in); } - TestCSVParser(Reader in, CSVStrategy strategy) { - super(in, strategy); + TestCSVParser(Reader in, CSVFormat format) { + super(in, format); } /** @@ -94,9 +94,9 @@ public class CSVParserTest extends TestCase { * */ String code = "1,2,3,\na,b x,c\n#foo\n\nd,e,\n\n"; - CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY.withCommentStart('#'); + CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#'); - TestCSVParser parser = new TestCSVParser(new StringReader(code), strategy); + TestCSVParser parser = new TestCSVParser(new StringReader(code), format); assertEquals(CSVParser.TT_TOKEN + ";1;", parser.testNextToken()); @@ -121,8 +121,8 @@ public class CSVParserTest extends TestCase { * \,, */ String code = "a,\\,,b\n\\,,"; - CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY.withCommentStart('#'); - TestCSVParser parser = new TestCSVParser(new StringReader(code), strategy); + CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#'); + TestCSVParser parser = new TestCSVParser(new StringReader(code), format); assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken()); // an unquoted single backslash is not an escape char @@ -182,7 +182,7 @@ public class CSVParserTest extends TestCase { * ;; */ String code = "a;'b and '' more\n'\n!comment;;;;\n;;"; - TestCSVParser parser = new TestCSVParser(new StringReader(code), new CSVStrategy(';', '\'', '!')); + TestCSVParser parser = new TestCSVParser(new StringReader(code), new CSVFormat(';', '\'', '!')); assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken()); assertEquals( CSVParser.TT_EORECORD + ";b and ' more\n;", @@ -228,7 +228,7 @@ public class CSVParserTest extends TestCase { } } - public void testExcelStrategy1() throws IOException { + public void testExcelFormat1() throws IOException { String code = "value1,value2,value3,value4\r\na,b,c,d\r\n x,,," + "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n"; @@ -239,7 +239,7 @@ public class CSVParserTest extends TestCase { {""}, {"\"hello\"", " \"world\"", "abc\ndef", ""} }; - CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY); + CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL); String[][] tmp = parser.getAllValues(); assertEquals(res.length, tmp.length); assertTrue(tmp.length > 0); @@ -248,7 +248,7 @@ public class CSVParserTest extends TestCase { } } - public void testExcelStrategy2() throws Exception { + public void testExcelFormat2() throws Exception { String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n"; String[][] res = { {"foo", "baar"}, @@ -257,7 +257,7 @@ public class CSVParserTest extends TestCase { {""}, {"world", ""} }; - CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY); + CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL); String[][] tmp = parser.getAllValues(); assertEquals(res.length, tmp.length); assertTrue(tmp.length > 0); @@ -279,13 +279,13 @@ public class CSVParserTest extends TestCase { }; String[][] res = { {"hello", ""}, - {""}, // ExcelStrategy does not ignore empty lines + {""}, // Excel format does not ignore empty lines {"world", ""} }; String code; for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) { code = codes[codeIndex]; - CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY); + CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL); String[][] tmp = parser.getAllValues(); assertEquals(res.length, tmp.length); assertTrue(tmp.length > 0); @@ -307,7 +307,7 @@ public class CSVParserTest extends TestCase { "hello,\r\n\r\nworld,\"\"" }; String[][] res = { - {"hello", ""}, // CSV Strategy ignores empty lines + {"hello", ""}, // CSV format ignores empty lines {"world", ""} }; String code; @@ -332,13 +332,13 @@ public class CSVParserTest extends TestCase { }; String[][] res = { {"hello", ""}, - {""}, // ExcelStrategy does not ignore empty lines + {""}, // Excel format does not ignore empty lines {""} }; String code; for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) { code = codes[codeIndex]; - CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY); + CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL); String[][] tmp = parser.getAllValues(); assertEquals(res.length, tmp.length); assertTrue(tmp.length > 0); @@ -356,7 +356,7 @@ public class CSVParserTest extends TestCase { "hello,\"\"\n\n\n" }; String[][] res = { - {"hello", ""} // CSV Strategy ignores empty lines + {"hello", ""} // CSV format ignores empty lines }; String code; for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) { @@ -434,9 +434,9 @@ public class CSVParserTest extends TestCase { }; - CSVStrategy strategy = new CSVStrategy(',', '\'', CSVStrategy.COMMENTS_DISABLED, '/', false, false, true, true); + CSVFormat format = new CSVFormat(',', '\'', CSVFormat.COMMENTS_DISABLED, '/', false, false, true, true); - CSVParser parser = new CSVParser(new StringReader(code), strategy); + CSVParser parser = new CSVParser(new StringReader(code), format); String[][] tmp = parser.getAllValues(); assertTrue(tmp.length > 0); for (int i = 0; i < res.length; i++) { @@ -462,9 +462,9 @@ public class CSVParserTest extends TestCase { }; - CSVStrategy strategy = new CSVStrategy(',', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '/', false, false, true, true); + CSVFormat format = new CSVFormat(',', CSVFormat.ENCAPSULATOR_DISABLED, CSVFormat.COMMENTS_DISABLED, '/', false, false, true, true); - CSVParser parser = new CSVParser(new StringReader(code), strategy); + CSVParser parser = new CSVParser(new StringReader(code), format); String[][] tmp = parser.getAllValues(); assertTrue(tmp.length > 0); @@ -475,7 +475,7 @@ public class CSVParserTest extends TestCase { } - public void testDefaultStrategy() throws IOException { + public void testDefaultFormat() throws IOException { String code = "" + "a,b\n" // 1) @@ -488,10 +488,10 @@ public class CSVParserTest extends TestCase { {"", "#"}, }; - CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY; - assertEquals(CSVStrategy.COMMENTS_DISABLED, strategy.getCommentStart()); + CSVFormat format = CSVFormat.DEFAULT; + assertEquals(CSVFormat.COMMENTS_DISABLED, format.getCommentStart()); - CSVParser parser = new CSVParser(new StringReader(code), strategy); + CSVParser parser = new CSVParser(new StringReader(code), format); String[][] tmp = parser.getAllValues(); assertTrue(tmp.length > 0); @@ -505,8 +505,8 @@ public class CSVParserTest extends TestCase { {""}, }; - strategy = new CSVStrategy(',', '"', '#'); - parser = new CSVParser(new StringReader(code), strategy); + format = new CSVFormat(',', '"', '#'); + parser = new CSVParser(new StringReader(code), format); tmp = parser.getAllValues(); if (!CSVPrinterTest.equals(res_comments, tmp)) { @@ -517,7 +517,7 @@ public class CSVParserTest extends TestCase { public void testUnicodeEscape() throws IOException { String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063"; - CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.DEFAULT_STRATEGY.withUnicodeEscapesInterpreted(true)); + CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true)); String[] data = parser.getLine(); assertEquals(2, data.length); assertEquals("abc", data[0]); @@ -557,7 +557,7 @@ public class CSVParserTest extends TestCase { // From SANDBOX-153 public void testDelimiterIsWhitespace() throws IOException { String code = "one\ttwo\t\tfour \t five\t six"; - TestCSVParser parser = new TestCSVParser(new StringReader(code), CSVStrategy.TDF_STRATEGY); + TestCSVParser parser = new TestCSVParser(new StringReader(code), CSVFormat.TDF); assertEquals(CSVParser.TT_TOKEN + ";one;", parser.testNextToken()); assertEquals(CSVParser.TT_TOKEN + ";two;", parser.testNextToken()); assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken()); diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index 17c79c29..0f8d2ce7 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -32,7 +32,7 @@ public class CSVPrinterTest extends TestCase { public void testPrinter1() throws IOException { StringWriter sw = new StringWriter(); - CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY); + CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); String[] line1 = {"a", "b"}; printer.println(line1); assertEquals("a,b" + lineSeparator, sw.toString()); @@ -40,7 +40,7 @@ public class CSVPrinterTest extends TestCase { public void testPrinter2() throws IOException { StringWriter sw = new StringWriter(); - CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY); + CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); String[] line1 = {"a,b", "b"}; printer.println(line1); assertEquals("\"a,b\",b" + lineSeparator, sw.toString()); @@ -48,7 +48,7 @@ public class CSVPrinterTest extends TestCase { public void testPrinter3() throws IOException { StringWriter sw = new StringWriter(); - CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY); + CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); String[] line1 = {"a, b", "b "}; printer.println(line1); assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString()); @@ -56,7 +56,7 @@ public class CSVPrinterTest extends TestCase { public void testPrinter4() throws IOException { StringWriter sw = new StringWriter(); - CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY); + CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); String[] line1 = {"a", "b\"c"}; printer.println(line1); assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString()); @@ -64,7 +64,7 @@ public class CSVPrinterTest extends TestCase { public void testPrinter5() throws IOException { StringWriter sw = new StringWriter(); - CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY); + CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); String[] line1 = {"a", "b\nc"}; printer.println(line1); assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString()); @@ -72,7 +72,7 @@ public class CSVPrinterTest extends TestCase { public void testPrinter6() throws IOException { StringWriter sw = new StringWriter(); - CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY); + CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); String[] line1 = {"a", "b\r\nc"}; printer.println(line1); assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString()); @@ -80,7 +80,7 @@ public class CSVPrinterTest extends TestCase { public void testPrinter7() throws IOException { StringWriter sw = new StringWriter(); - CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY); + CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT); String[] line1 = {"a", "b\\c"}; printer.println(line1); assertEquals("a,b\\c" + lineSeparator, sw.toString()); @@ -88,7 +88,7 @@ public class CSVPrinterTest extends TestCase { public void testExcelPrinter1() throws IOException { StringWriter sw = new StringWriter(); - CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY); + CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL); String[] line1 = {"a", "b"}; printer.println(line1); assertEquals("a,b" + lineSeparator, sw.toString()); @@ -96,7 +96,7 @@ public class CSVPrinterTest extends TestCase { public void testExcelPrinter2() throws IOException { StringWriter sw = new StringWriter(); - CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY); + CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL); String[] line1 = {"a,b", "b"}; printer.println(line1); assertEquals("\"a,b\",b" + lineSeparator, sw.toString()); @@ -105,18 +105,18 @@ public class CSVPrinterTest extends TestCase { public void testRandom() throws Exception { int iter = 10000; - strategy = CSVStrategy.DEFAULT_STRATEGY; + format = CSVFormat.DEFAULT; doRandom(iter); - strategy = CSVStrategy.EXCEL_STRATEGY; + format = CSVFormat.EXCEL; doRandom(iter); - // Strategy for MySQL - strategy = new CSVStrategy('\t', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '\\', false, false, false, false); + // Format for MySQL + format = new CSVFormat('\t', CSVFormat.ENCAPSULATOR_DISABLED, CSVFormat.COMMENTS_DISABLED, '\\', false, false, false, false); doRandom(iter); } Random r = new Random(); - CSVStrategy strategy; + CSVFormat format; public void doRandom(int iter) throws Exception { for (int i = 0; i < iter; i++) { @@ -138,7 +138,7 @@ public class CSVPrinterTest extends TestCase { } StringWriter sw = new StringWriter(); - CSVPrinter printer = new CSVPrinter(sw, strategy); + CSVPrinter printer = new CSVPrinter(sw, format); for (int i = 0; i < nLines; i++) { // for (int j=0; j