mirror of
https://github.com/apache/commons-csv.git
synced 2025-02-07 18:49:24 +00:00
Renamed CSVStrategy to CSVFormat
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1199842 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
42476f4b08
commit
cb99634ab3
@ -20,11 +20,11 @@ package org.apache.commons.csv;
|
|||||||
import java.io.Serializable;
|
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 delimiter = ',';
|
||||||
private char encapsulator = '"';
|
private char encapsulator = '"';
|
||||||
@ -45,27 +45,27 @@ public class CSVStrategy implements Cloneable, Serializable {
|
|||||||
public static final char ENCAPSULATOR_DISABLED = (char) -2;
|
public static final char ENCAPSULATOR_DISABLED = (char) -2;
|
||||||
|
|
||||||
/** Standard comma separated format. */
|
/** 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). */
|
/** 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. */
|
/** 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);
|
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 delimiter a char used for value separation
|
||||||
* @param encapsulator a char used as value encapsulation marker
|
* @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 unicodeEscapesInterpreted TRUE when unicode escapes should be interpreted
|
||||||
* @param emptyLinesIgnored TRUE when the parser should skip emtpy lines
|
* @param emptyLinesIgnored TRUE when the parser should skip emtpy lines
|
||||||
*/
|
*/
|
||||||
public CSVStrategy(
|
public CSVFormat(
|
||||||
char delimiter,
|
char delimiter,
|
||||||
char encapsulator,
|
char encapsulator,
|
||||||
char commentStart,
|
char commentStart,
|
||||||
@ -99,30 +99,30 @@ public class CSVStrategy implements Cloneable, Serializable {
|
|||||||
return delimiter;
|
return delimiter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSVStrategy withDelimiter(char delimiter) {
|
public CSVFormat withDelimiter(char delimiter) {
|
||||||
CSVStrategy strategy = (CSVStrategy) clone();
|
CSVFormat format = (CSVFormat) clone();
|
||||||
this.delimiter = delimiter;
|
this.delimiter = delimiter;
|
||||||
return strategy;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public char getEncapsulator() {
|
public char getEncapsulator() {
|
||||||
return encapsulator;
|
return encapsulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSVStrategy withEncapsulator(char encapsulator) {
|
public CSVFormat withEncapsulator(char encapsulator) {
|
||||||
CSVStrategy strategy = (CSVStrategy) clone();
|
CSVFormat format = (CSVFormat) clone();
|
||||||
strategy.encapsulator = encapsulator;
|
format.encapsulator = encapsulator;
|
||||||
return strategy;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public char getCommentStart() {
|
public char getCommentStart() {
|
||||||
return commentStart;
|
return commentStart;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSVStrategy withCommentStart(char commentStart) {
|
public CSVFormat withCommentStart(char commentStart) {
|
||||||
CSVStrategy strategy = (CSVStrategy) clone();
|
CSVFormat format = (CSVFormat) clone();
|
||||||
strategy.commentStart = commentStart;
|
format.commentStart = commentStart;
|
||||||
return strategy;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isCommentingDisabled() {
|
public boolean isCommentingDisabled() {
|
||||||
@ -133,60 +133,60 @@ public class CSVStrategy implements Cloneable, Serializable {
|
|||||||
return escape;
|
return escape;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSVStrategy withEscape(char escape) {
|
public CSVFormat withEscape(char escape) {
|
||||||
CSVStrategy strategy = (CSVStrategy) clone();
|
CSVFormat format = (CSVFormat) clone();
|
||||||
strategy.escape = escape;
|
format.escape = escape;
|
||||||
return strategy;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLeadingSpacesIgnored() {
|
public boolean isLeadingSpacesIgnored() {
|
||||||
return leadingSpacesIgnored;
|
return leadingSpacesIgnored;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSVStrategy withLeadingSpacesIgnored(boolean leadingSpacesIgnored) {
|
public CSVFormat withLeadingSpacesIgnored(boolean leadingSpacesIgnored) {
|
||||||
CSVStrategy strategy = (CSVStrategy) clone();
|
CSVFormat format = (CSVFormat) clone();
|
||||||
strategy.leadingSpacesIgnored = leadingSpacesIgnored;
|
format.leadingSpacesIgnored = leadingSpacesIgnored;
|
||||||
return strategy;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isTrailingSpacesIgnored() {
|
public boolean isTrailingSpacesIgnored() {
|
||||||
return trailingSpacesIgnored;
|
return trailingSpacesIgnored;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSVStrategy withTrailingSpacesIgnored(boolean trailingSpacesIgnored) {
|
public CSVFormat withTrailingSpacesIgnored(boolean trailingSpacesIgnored) {
|
||||||
CSVStrategy strategy = (CSVStrategy) clone();
|
CSVFormat format = (CSVFormat) clone();
|
||||||
strategy.trailingSpacesIgnored = trailingSpacesIgnored;
|
format.trailingSpacesIgnored = trailingSpacesIgnored;
|
||||||
return strategy;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isUnicodeEscapesInterpreted() {
|
public boolean isUnicodeEscapesInterpreted() {
|
||||||
return unicodeEscapesInterpreted;
|
return unicodeEscapesInterpreted;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSVStrategy withUnicodeEscapesInterpreted(boolean unicodeEscapesInterpreted) {
|
public CSVFormat withUnicodeEscapesInterpreted(boolean unicodeEscapesInterpreted) {
|
||||||
CSVStrategy strategy = (CSVStrategy) clone();
|
CSVFormat format = (CSVFormat) clone();
|
||||||
strategy.unicodeEscapesInterpreted = unicodeEscapesInterpreted;
|
format.unicodeEscapesInterpreted = unicodeEscapesInterpreted;
|
||||||
return strategy;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmptyLinesIgnored() {
|
public boolean isEmptyLinesIgnored() {
|
||||||
return emptyLinesIgnored;
|
return emptyLinesIgnored;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSVStrategy withEmptyLinesIgnored(boolean emptyLinesIgnored) {
|
public CSVFormat withEmptyLinesIgnored(boolean emptyLinesIgnored) {
|
||||||
CSVStrategy strategy = (CSVStrategy) clone();
|
CSVFormat format = (CSVFormat) clone();
|
||||||
strategy.emptyLinesIgnored = emptyLinesIgnored;
|
format.emptyLinesIgnored = emptyLinesIgnored;
|
||||||
return strategy;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLineSeparator() {
|
public String getLineSeparator() {
|
||||||
return lineSeparator;
|
return lineSeparator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CSVStrategy withLineSeparator(String lineSeparator) {
|
public CSVFormat withLineSeparator(String lineSeparator) {
|
||||||
CSVStrategy strategy = (CSVStrategy) clone();
|
CSVFormat format = (CSVFormat) clone();
|
||||||
strategy.lineSeparator = lineSeparator;
|
format.lineSeparator = lineSeparator;
|
||||||
return strategy;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Object clone() {
|
protected Object clone() {
|
@ -27,23 +27,23 @@ import java.util.List;
|
|||||||
* Parses CSV files according to the specified configuration.
|
* Parses CSV files according to the specified configuration.
|
||||||
*
|
*
|
||||||
* Because CSV appears in many different dialects, the parser supports many
|
* 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}.
|
||||||
*
|
*
|
||||||
* <p>Parsing of a csv-string having tabs as separators,
|
* <p>Parsing of a csv-string having tabs as separators,
|
||||||
* '"' as an optional value encapsulator, and comments starting with '#':</p>
|
* '"' as an optional value encapsulator, and comments starting with '#':</p>
|
||||||
* <pre>
|
* <pre>
|
||||||
* String[][] data =
|
* 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();
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* <p>Parsing of a csv-string in Excel CSV format</p>
|
* <p>Parsing of a csv-string in Excel CSV format</p>
|
||||||
* <pre>
|
* <pre>
|
||||||
* String[][] data =
|
* 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();
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* Internal parser state is completely covered by the strategy
|
* Internal parser state is completely covered by the format
|
||||||
* and the reader-state.</p>
|
* and the reader-state.</p>
|
||||||
*
|
*
|
||||||
* <p>see <a href="package-summary.html">package documentation</a>
|
* <p>see <a href="package-summary.html">package documentation</a>
|
||||||
@ -73,7 +73,7 @@ public class CSVParser {
|
|||||||
// the input stream
|
// the input stream
|
||||||
private final ExtendedBufferedReader in;
|
private final ExtendedBufferedReader in;
|
||||||
|
|
||||||
private final CSVStrategy strategy;
|
private final CSVFormat format;
|
||||||
|
|
||||||
// the following objects are shared to reduce garbage
|
// 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
|
* @param input a Reader containing "csv-formatted" input
|
||||||
*/
|
*/
|
||||||
public CSVParser(Reader 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 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.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
|
* and returns the content as an array of records
|
||||||
* (whereas records are arrays of single values).
|
* (whereas records are arrays of single values).
|
||||||
* <p/>
|
* <p/>
|
||||||
@ -260,7 +260,7 @@ public class CSVParser {
|
|||||||
c = in.readAgain();
|
c = in.readAgain();
|
||||||
|
|
||||||
// empty line detection: eol AND (last char was EOL or beginning)
|
// empty line detection: eol AND (last char was EOL or beginning)
|
||||||
while (strategy.isEmptyLinesIgnored() && eol
|
while (format.isEmptyLinesIgnored() && eol
|
||||||
&& (lastChar == '\n'
|
&& (lastChar == '\n'
|
||||||
|| lastChar == '\r'
|
|| lastChar == '\r'
|
||||||
|| lastChar == ExtendedBufferedReader.UNDEFINED)
|
|| lastChar == ExtendedBufferedReader.UNDEFINED)
|
||||||
@ -278,7 +278,7 @@ public class CSVParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// did we reach eof during the last iteration already ? TT_EOF
|
// 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;
|
tkn.type = TT_EOF;
|
||||||
return tkn;
|
return tkn;
|
||||||
}
|
}
|
||||||
@ -286,17 +286,17 @@ public class CSVParser {
|
|||||||
// important: make sure a new char gets consumed in each iteration
|
// important: make sure a new char gets consumed in each iteration
|
||||||
while (!tkn.isReady && tkn.type != TT_EOF) {
|
while (!tkn.isReady && tkn.type != TT_EOF) {
|
||||||
// ignore whitespaces at beginning of a token
|
// ignore whitespaces at beginning of a token
|
||||||
while (strategy.isLeadingSpacesIgnored() && isWhitespace(c) && !eol) {
|
while (format.isLeadingSpacesIgnored() && isWhitespace(c) && !eol) {
|
||||||
wsBuf.append((char) c);
|
wsBuf.append((char) c);
|
||||||
c = in.read();
|
c = in.read();
|
||||||
eol = isEndOfLine(c);
|
eol = isEndOfLine(c);
|
||||||
}
|
}
|
||||||
// ok, start of token reached: comment, encapsulated, or token
|
// 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)
|
// ignore everything till end of line and continue (incr linecount)
|
||||||
in.readLine();
|
in.readLine();
|
||||||
tkn = nextToken(tkn.reset());
|
tkn = nextToken(tkn.reset());
|
||||||
} else if (c == strategy.getDelimiter()) {
|
} else if (c == format.getDelimiter()) {
|
||||||
// empty token return TT_TOKEN("")
|
// empty token return TT_TOKEN("")
|
||||||
tkn.type = TT_TOKEN;
|
tkn.type = TT_TOKEN;
|
||||||
tkn.isReady = true;
|
tkn.isReady = true;
|
||||||
@ -305,7 +305,7 @@ public class CSVParser {
|
|||||||
//noop: tkn.content.append("");
|
//noop: tkn.content.append("");
|
||||||
tkn.type = TT_EORECORD;
|
tkn.type = TT_EORECORD;
|
||||||
tkn.isReady = true;
|
tkn.isReady = true;
|
||||||
} else if (c == strategy.getEncapsulator()) {
|
} else if (c == format.getEncapsulator()) {
|
||||||
// consume encapsulated token
|
// consume encapsulated token
|
||||||
encapsulatedTokenLexer(tkn, c);
|
encapsulatedTokenLexer(tkn, c);
|
||||||
} else if (isEndOfFile(c)) {
|
} else if (isEndOfFile(c)) {
|
||||||
@ -316,7 +316,7 @@ public class CSVParser {
|
|||||||
} else {
|
} else {
|
||||||
// next token must be a simple token
|
// next token must be a simple token
|
||||||
// add removed blanks when not ignoring whitespace chars...
|
// add removed blanks when not ignoring whitespace chars...
|
||||||
if (!strategy.isLeadingSpacesIgnored()) {
|
if (!format.isLeadingSpacesIgnored()) {
|
||||||
tkn.content.append(wsBuf);
|
tkn.content.append(wsBuf);
|
||||||
}
|
}
|
||||||
simpleTokenLexer(tkn, c);
|
simpleTokenLexer(tkn, c);
|
||||||
@ -354,15 +354,15 @@ public class CSVParser {
|
|||||||
tkn.type = TT_EOF;
|
tkn.type = TT_EOF;
|
||||||
tkn.isReady = true;
|
tkn.isReady = true;
|
||||||
break;
|
break;
|
||||||
} else if (c == strategy.getDelimiter()) {
|
} else if (c == format.getDelimiter()) {
|
||||||
// end of token
|
// end of token
|
||||||
tkn.type = TT_TOKEN;
|
tkn.type = TT_TOKEN;
|
||||||
tkn.isReady = true;
|
tkn.isReady = true;
|
||||||
break;
|
break;
|
||||||
} else if (c == '\\' && strategy.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') {
|
} else if (c == '\\' && format.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') {
|
||||||
// interpret unicode escaped chars (like \u0070 -> p)
|
// interpret unicode escaped chars (like \u0070 -> p)
|
||||||
tkn.content.append((char) unicodeEscapeLexer(c));
|
tkn.content.append((char) unicodeEscapeLexer(c));
|
||||||
} else if (c == strategy.getEscape()) {
|
} else if (c == format.getEscape()) {
|
||||||
tkn.content.append((char) readEscape(c));
|
tkn.content.append((char) readEscape(c));
|
||||||
} else {
|
} else {
|
||||||
tkn.content.append((char) c);
|
tkn.content.append((char) c);
|
||||||
@ -371,7 +371,7 @@ public class CSVParser {
|
|||||||
c = in.read();
|
c = in.read();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strategy.isTrailingSpacesIgnored()) {
|
if (format.isTrailingSpacesIgnored()) {
|
||||||
tkn.content.trimTrailingWhitespace();
|
tkn.content.trimTrailingWhitespace();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -400,12 +400,12 @@ public class CSVParser {
|
|||||||
for (; ;) {
|
for (; ;) {
|
||||||
c = in.read();
|
c = in.read();
|
||||||
|
|
||||||
if (c == '\\' && strategy.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') {
|
if (c == '\\' && format.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') {
|
||||||
tkn.content.append((char) unicodeEscapeLexer(c));
|
tkn.content.append((char) unicodeEscapeLexer(c));
|
||||||
} else if (c == strategy.getEscape()) {
|
} else if (c == format.getEscape()) {
|
||||||
tkn.content.append((char) readEscape(c));
|
tkn.content.append((char) readEscape(c));
|
||||||
} else if (c == strategy.getEncapsulator()) {
|
} else if (c == format.getEncapsulator()) {
|
||||||
if (in.lookAhead() == strategy.getEncapsulator()) {
|
if (in.lookAhead() == format.getEncapsulator()) {
|
||||||
// double or escaped encapsulator -> add single encapsulator to token
|
// double or escaped encapsulator -> add single encapsulator to token
|
||||||
c = in.read();
|
c = in.read();
|
||||||
tkn.content.append((char) c);
|
tkn.content.append((char) c);
|
||||||
@ -413,7 +413,7 @@ public class CSVParser {
|
|||||||
// token finish mark (encapsulator) reached: ignore whitespace till delimiter
|
// token finish mark (encapsulator) reached: ignore whitespace till delimiter
|
||||||
for (; ;) {
|
for (; ;) {
|
||||||
c = in.read();
|
c = in.read();
|
||||||
if (c == strategy.getDelimiter()) {
|
if (c == format.getDelimiter()) {
|
||||||
tkn.type = TT_TOKEN;
|
tkn.type = TT_TOKEN;
|
||||||
tkn.isReady = true;
|
tkn.isReady = true;
|
||||||
return tkn;
|
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() {
|
public CSVFormat getFormat() {
|
||||||
return this.strategy;
|
return this.format;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
@ -528,7 +528,7 @@ public class CSVParser {
|
|||||||
* @return true if the given char is a whitespace character
|
* @return true if the given char is a whitespace character
|
||||||
*/
|
*/
|
||||||
private boolean isWhitespace(int c) {
|
private boolean isWhitespace(int c) {
|
||||||
return Character.isWhitespace((char) c) && (c != strategy.getDelimiter());
|
return Character.isWhitespace((char) c) && (c != format.getDelimiter());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,7 +27,7 @@ public class CSVPrinter {
|
|||||||
|
|
||||||
/** The place that the values get written. */
|
/** The place that the values get written. */
|
||||||
private final Writer out;
|
private final Writer out;
|
||||||
private final CSVStrategy strategy;
|
private final CSVFormat format;
|
||||||
|
|
||||||
/** True if we just began a new line. */
|
/** True if we just began a new line. */
|
||||||
private boolean newLine = true;
|
private boolean newLine = true;
|
||||||
@ -36,18 +36,17 @@ public class CSVPrinter {
|
|||||||
private char[] buf = new char[0];
|
private char[] buf = new char[0];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a printer that will print values to the given
|
* Create a printer that will print values to the given stream following the CSVFormat.
|
||||||
* stream following the CSVStrategy.
|
|
||||||
* <p/>
|
* <p/>
|
||||||
* Currently, only a pure encapsulation strategy or a pure escaping strategy
|
* Currently, only a pure encapsulation format or a pure escaping format
|
||||||
* is supported. Hybrid strategies (encapsulation and escaping with a different character) are not supported.
|
* is supported. Hybrid formats (encapsulation and escaping with a different character) are not supported.
|
||||||
*
|
*
|
||||||
* @param out stream to which to print.
|
* @param out stream to which to print.
|
||||||
* @param strategy describes the CSV variation.
|
* @param format describes the CSV variation.
|
||||||
*/
|
*/
|
||||||
public CSVPrinter(Writer out, CSVStrategy strategy) {
|
public CSVPrinter(Writer out, CSVFormat format) {
|
||||||
this.out = out;
|
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
|
* Output a blank line
|
||||||
*/
|
*/
|
||||||
public void println() throws IOException {
|
public void println() throws IOException {
|
||||||
out.write(strategy.getLineSeparator());
|
out.write(format.getLineSeparator());
|
||||||
newLine = true;
|
newLine = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,13 +91,13 @@ public class CSVPrinter {
|
|||||||
* @param comment the comment to output
|
* @param comment the comment to output
|
||||||
*/
|
*/
|
||||||
public void printlnComment(String comment) throws IOException {
|
public void printlnComment(String comment) throws IOException {
|
||||||
if (this.strategy.isCommentingDisabled()) {
|
if (this.format.isCommentingDisabled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!newLine) {
|
if (!newLine) {
|
||||||
println();
|
println();
|
||||||
}
|
}
|
||||||
out.write(this.strategy.getCommentStart());
|
out.write(this.format.getCommentStart());
|
||||||
out.write(' ');
|
out.write(' ');
|
||||||
for (int i = 0; i < comment.length(); i++) {
|
for (int i = 0; i < comment.length(); i++) {
|
||||||
char c = comment.charAt(i);
|
char c = comment.charAt(i);
|
||||||
@ -110,7 +109,7 @@ public class CSVPrinter {
|
|||||||
// break intentionally excluded.
|
// break intentionally excluded.
|
||||||
case '\n':
|
case '\n':
|
||||||
println();
|
println();
|
||||||
out.write(this.strategy.getCommentStart());
|
out.write(this.format.getCommentStart());
|
||||||
out.write(' ');
|
out.write(' ');
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -129,9 +128,9 @@ public class CSVPrinter {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strategy.getEncapsulator() != CSVStrategy.ENCAPSULATOR_DISABLED) {
|
if (format.getEncapsulator() != CSVFormat.ENCAPSULATOR_DISABLED) {
|
||||||
printAndEncapsulate(value, offset, len);
|
printAndEncapsulate(value, offset, len);
|
||||||
} else if (strategy.getEscape() != CSVStrategy.ESCAPE_DISABLED) {
|
} else if (format.getEscape() != CSVFormat.ESCAPE_DISABLED) {
|
||||||
printAndEscape(value, offset, len);
|
printAndEscape(value, offset, len);
|
||||||
} else {
|
} else {
|
||||||
printSep();
|
printSep();
|
||||||
@ -143,7 +142,7 @@ public class CSVPrinter {
|
|||||||
if (newLine) {
|
if (newLine) {
|
||||||
newLine = false;
|
newLine = false;
|
||||||
} else {
|
} else {
|
||||||
out.write(this.strategy.getDelimiter());
|
out.write(this.format.getDelimiter());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,8 +153,8 @@ public class CSVPrinter {
|
|||||||
|
|
||||||
printSep();
|
printSep();
|
||||||
|
|
||||||
char delim = this.strategy.getDelimiter();
|
char delim = this.format.getDelimiter();
|
||||||
char escape = this.strategy.getEscape();
|
char escape = this.format.getEscape();
|
||||||
|
|
||||||
while (pos < end) {
|
while (pos < end) {
|
||||||
char c = value[pos];
|
char c = value[pos];
|
||||||
@ -196,8 +195,8 @@ public class CSVPrinter {
|
|||||||
|
|
||||||
printSep();
|
printSep();
|
||||||
|
|
||||||
char delim = this.strategy.getDelimiter();
|
char delim = this.format.getDelimiter();
|
||||||
char encapsulator = this.strategy.getEncapsulator();
|
char encapsulator = this.format.getEncapsulator();
|
||||||
|
|
||||||
if (len <= 0) {
|
if (len <= 0) {
|
||||||
// always quote an empty token that is the first
|
// always quote an empty token that is the first
|
||||||
|
@ -48,10 +48,10 @@ public class CSVUtils {
|
|||||||
* @return the CSV string, will be an empty string if the length of the
|
* @return the CSV string, will be an empty string if the length of the
|
||||||
* value array is 0
|
* value array is 0
|
||||||
*/
|
*/
|
||||||
public static String printLine(String[] values, CSVStrategy strategy) {
|
public static String printLine(String[] values, CSVFormat format) {
|
||||||
// set up a CSVUtils
|
// set up a CSVUtils
|
||||||
StringWriter stringWriter = new StringWriter();
|
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
|
// check for null values an "null" as strings and convert them
|
||||||
// into the strings "null" and "\"null\""
|
// 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.
|
* @param s CSV String to be parsed.
|
||||||
* @return parsed String matrix (which is never null)
|
* @return parsed String matrix (which is never null)
|
||||||
@ -90,7 +90,7 @@ public class CSVUtils {
|
|||||||
}
|
}
|
||||||
String[][] result = (new CSVParser(new StringReader(s))).getAllValues();
|
String[][] result = (new CSVParser(new StringReader(s))).getAllValues();
|
||||||
if (result == null) {
|
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[][] {{""}};")
|
// (i.e. not "result = new String[][] {{""}};")
|
||||||
result = EMPTY_DOUBLE_STRING_ARRAY;
|
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
|
* Parsing empty string will be handled as valid records containing zero
|
||||||
* elements, so the following property holds: parseLine("").length == 0.
|
* elements, so the following property holds: parseLine("").length == 0.
|
||||||
|
@ -19,11 +19,11 @@ package org.apache.commons.csv;
|
|||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
public class CSVStrategyTest extends TestCase {
|
public class CSVFormatTest extends TestCase {
|
||||||
|
|
||||||
public void testImmutalibity() {
|
public void testImmutalibity() {
|
||||||
CSVStrategy strategy1 = new CSVStrategy('!', '!', '!', '!', true, true, true, true);
|
CSVFormat format1 = new CSVFormat('!', '!', '!', '!', true, true, true, true);
|
||||||
CSVStrategy strategy2 = strategy1.withDelimiter('?')
|
CSVFormat format2 = format1.withDelimiter('?')
|
||||||
.withEncapsulator('?')
|
.withEncapsulator('?')
|
||||||
.withCommentStart('?')
|
.withCommentStart('?')
|
||||||
.withLineSeparator("?")
|
.withLineSeparator("?")
|
||||||
@ -33,16 +33,16 @@ public class CSVStrategyTest extends TestCase {
|
|||||||
.withEmptyLinesIgnored(false)
|
.withEmptyLinesIgnored(false)
|
||||||
.withUnicodeEscapesInterpreted(false);
|
.withUnicodeEscapesInterpreted(false);
|
||||||
|
|
||||||
assertNotSame(strategy1.getDelimiter(), strategy2.getDelimiter());
|
assertNotSame(format1.getDelimiter(), format2.getDelimiter());
|
||||||
assertNotSame(strategy1.getEncapsulator(), strategy2.getEncapsulator());
|
assertNotSame(format1.getEncapsulator(), format2.getEncapsulator());
|
||||||
assertNotSame(strategy1.getCommentStart(), strategy2.getCommentStart());
|
assertNotSame(format1.getCommentStart(), format2.getCommentStart());
|
||||||
assertNotSame(strategy1.getEscape(), strategy2.getEscape());
|
assertNotSame(format1.getEscape(), format2.getEscape());
|
||||||
assertNotSame(strategy1.getLineSeparator(), strategy2.getLineSeparator());
|
assertNotSame(format1.getLineSeparator(), format2.getLineSeparator());
|
||||||
|
|
||||||
assertNotSame(strategy1.isTrailingSpacesIgnored(), strategy2.isTrailingSpacesIgnored());
|
assertNotSame(format1.isTrailingSpacesIgnored(), format2.isTrailingSpacesIgnored());
|
||||||
assertNotSame(strategy1.isLeadingSpacesIgnored(), strategy2.isLeadingSpacesIgnored());
|
assertNotSame(format1.isLeadingSpacesIgnored(), format2.isLeadingSpacesIgnored());
|
||||||
assertNotSame(strategy1.isEmptyLinesIgnored(), strategy2.isEmptyLinesIgnored());
|
assertNotSame(format1.isEmptyLinesIgnored(), format2.isEmptyLinesIgnored());
|
||||||
assertNotSame(strategy1.isUnicodeEscapesInterpreted(), strategy2.isUnicodeEscapesInterpreted());
|
assertNotSame(format1.isUnicodeEscapesInterpreted(), format2.isUnicodeEscapesInterpreted());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -47,8 +47,8 @@ public class CSVParserTest extends TestCase {
|
|||||||
super(in);
|
super(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
TestCSVParser(Reader in, CSVStrategy strategy) {
|
TestCSVParser(Reader in, CSVFormat format) {
|
||||||
super(in, strategy);
|
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";
|
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());
|
assertEquals(CSVParser.TT_TOKEN + ";1;", parser.testNextToken());
|
||||||
@ -121,8 +121,8 @@ public class CSVParserTest extends TestCase {
|
|||||||
* \,,
|
* \,,
|
||||||
*/
|
*/
|
||||||
String code = "a,\\,,b\n\\,,";
|
String code = "a,\\,,b\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 + ";a;", parser.testNextToken());
|
assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
|
||||||
// an unquoted single backslash is not an escape char
|
// 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;;";
|
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_TOKEN + ";a;", parser.testNextToken());
|
||||||
assertEquals(
|
assertEquals(
|
||||||
CSVParser.TT_EORECORD + ";b and ' more\n;",
|
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 =
|
String code =
|
||||||
"value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
|
"value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
|
||||||
+ "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
|
+ "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
|
||||||
@ -239,7 +239,7 @@ public class CSVParserTest extends TestCase {
|
|||||||
{""},
|
{""},
|
||||||
{"\"hello\"", " \"world\"", "abc\ndef", ""}
|
{"\"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();
|
String[][] tmp = parser.getAllValues();
|
||||||
assertEquals(res.length, tmp.length);
|
assertEquals(res.length, tmp.length);
|
||||||
assertTrue(tmp.length > 0);
|
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 code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
|
||||||
String[][] res = {
|
String[][] res = {
|
||||||
{"foo", "baar"},
|
{"foo", "baar"},
|
||||||
@ -257,7 +257,7 @@ public class CSVParserTest extends TestCase {
|
|||||||
{""},
|
{""},
|
||||||
{"world", ""}
|
{"world", ""}
|
||||||
};
|
};
|
||||||
CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY);
|
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
|
||||||
String[][] tmp = parser.getAllValues();
|
String[][] tmp = parser.getAllValues();
|
||||||
assertEquals(res.length, tmp.length);
|
assertEquals(res.length, tmp.length);
|
||||||
assertTrue(tmp.length > 0);
|
assertTrue(tmp.length > 0);
|
||||||
@ -279,13 +279,13 @@ public class CSVParserTest extends TestCase {
|
|||||||
};
|
};
|
||||||
String[][] res = {
|
String[][] res = {
|
||||||
{"hello", ""},
|
{"hello", ""},
|
||||||
{""}, // ExcelStrategy does not ignore empty lines
|
{""}, // Excel format does not ignore empty lines
|
||||||
{"world", ""}
|
{"world", ""}
|
||||||
};
|
};
|
||||||
String code;
|
String code;
|
||||||
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
|
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
|
||||||
code = codes[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();
|
String[][] tmp = parser.getAllValues();
|
||||||
assertEquals(res.length, tmp.length);
|
assertEquals(res.length, tmp.length);
|
||||||
assertTrue(tmp.length > 0);
|
assertTrue(tmp.length > 0);
|
||||||
@ -307,7 +307,7 @@ public class CSVParserTest extends TestCase {
|
|||||||
"hello,\r\n\r\nworld,\"\""
|
"hello,\r\n\r\nworld,\"\""
|
||||||
};
|
};
|
||||||
String[][] res = {
|
String[][] res = {
|
||||||
{"hello", ""}, // CSV Strategy ignores empty lines
|
{"hello", ""}, // CSV format ignores empty lines
|
||||||
{"world", ""}
|
{"world", ""}
|
||||||
};
|
};
|
||||||
String code;
|
String code;
|
||||||
@ -332,13 +332,13 @@ public class CSVParserTest extends TestCase {
|
|||||||
};
|
};
|
||||||
String[][] res = {
|
String[][] res = {
|
||||||
{"hello", ""},
|
{"hello", ""},
|
||||||
{""}, // ExcelStrategy does not ignore empty lines
|
{""}, // Excel format does not ignore empty lines
|
||||||
{""}
|
{""}
|
||||||
};
|
};
|
||||||
String code;
|
String code;
|
||||||
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
|
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
|
||||||
code = codes[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();
|
String[][] tmp = parser.getAllValues();
|
||||||
assertEquals(res.length, tmp.length);
|
assertEquals(res.length, tmp.length);
|
||||||
assertTrue(tmp.length > 0);
|
assertTrue(tmp.length > 0);
|
||||||
@ -356,7 +356,7 @@ public class CSVParserTest extends TestCase {
|
|||||||
"hello,\"\"\n\n\n"
|
"hello,\"\"\n\n\n"
|
||||||
};
|
};
|
||||||
String[][] res = {
|
String[][] res = {
|
||||||
{"hello", ""} // CSV Strategy ignores empty lines
|
{"hello", ""} // CSV format ignores empty lines
|
||||||
};
|
};
|
||||||
String code;
|
String code;
|
||||||
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
|
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();
|
String[][] tmp = parser.getAllValues();
|
||||||
assertTrue(tmp.length > 0);
|
assertTrue(tmp.length > 0);
|
||||||
for (int i = 0; i < res.length; i++) {
|
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();
|
String[][] tmp = parser.getAllValues();
|
||||||
assertTrue(tmp.length > 0);
|
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 = ""
|
String code = ""
|
||||||
+ "a,b\n" // 1)
|
+ "a,b\n" // 1)
|
||||||
@ -488,10 +488,10 @@ public class CSVParserTest extends TestCase {
|
|||||||
{"", "#"},
|
{"", "#"},
|
||||||
};
|
};
|
||||||
|
|
||||||
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
|
CSVFormat format = CSVFormat.DEFAULT;
|
||||||
assertEquals(CSVStrategy.COMMENTS_DISABLED, strategy.getCommentStart());
|
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();
|
String[][] tmp = parser.getAllValues();
|
||||||
assertTrue(tmp.length > 0);
|
assertTrue(tmp.length > 0);
|
||||||
|
|
||||||
@ -505,8 +505,8 @@ public class CSVParserTest extends TestCase {
|
|||||||
{""},
|
{""},
|
||||||
};
|
};
|
||||||
|
|
||||||
strategy = new CSVStrategy(',', '"', '#');
|
format = new CSVFormat(',', '"', '#');
|
||||||
parser = new CSVParser(new StringReader(code), strategy);
|
parser = new CSVParser(new StringReader(code), format);
|
||||||
tmp = parser.getAllValues();
|
tmp = parser.getAllValues();
|
||||||
|
|
||||||
if (!CSVPrinterTest.equals(res_comments, tmp)) {
|
if (!CSVPrinterTest.equals(res_comments, tmp)) {
|
||||||
@ -517,7 +517,7 @@ public class CSVParserTest extends TestCase {
|
|||||||
|
|
||||||
public void testUnicodeEscape() throws IOException {
|
public void testUnicodeEscape() throws IOException {
|
||||||
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
|
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();
|
String[] data = parser.getLine();
|
||||||
assertEquals(2, data.length);
|
assertEquals(2, data.length);
|
||||||
assertEquals("abc", data[0]);
|
assertEquals("abc", data[0]);
|
||||||
@ -557,7 +557,7 @@ public class CSVParserTest extends TestCase {
|
|||||||
// From SANDBOX-153
|
// From SANDBOX-153
|
||||||
public void testDelimiterIsWhitespace() throws IOException {
|
public void testDelimiterIsWhitespace() throws IOException {
|
||||||
String code = "one\ttwo\t\tfour \t five\t six";
|
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 + ";one;", parser.testNextToken());
|
||||||
assertEquals(CSVParser.TT_TOKEN + ";two;", parser.testNextToken());
|
assertEquals(CSVParser.TT_TOKEN + ";two;", parser.testNextToken());
|
||||||
assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
|
assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
|
||||||
|
@ -32,7 +32,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
public void testPrinter1() throws IOException {
|
public void testPrinter1() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||||
String[] line1 = {"a", "b"};
|
String[] line1 = {"a", "b"};
|
||||||
printer.println(line1);
|
printer.println(line1);
|
||||||
assertEquals("a,b" + lineSeparator, sw.toString());
|
assertEquals("a,b" + lineSeparator, sw.toString());
|
||||||
@ -40,7 +40,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
public void testPrinter2() throws IOException {
|
public void testPrinter2() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||||
String[] line1 = {"a,b", "b"};
|
String[] line1 = {"a,b", "b"};
|
||||||
printer.println(line1);
|
printer.println(line1);
|
||||||
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
||||||
@ -48,7 +48,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
public void testPrinter3() throws IOException {
|
public void testPrinter3() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||||
String[] line1 = {"a, b", "b "};
|
String[] line1 = {"a, b", "b "};
|
||||||
printer.println(line1);
|
printer.println(line1);
|
||||||
assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
|
assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
|
||||||
@ -56,7 +56,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
public void testPrinter4() throws IOException {
|
public void testPrinter4() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||||
String[] line1 = {"a", "b\"c"};
|
String[] line1 = {"a", "b\"c"};
|
||||||
printer.println(line1);
|
printer.println(line1);
|
||||||
assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
|
assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
|
||||||
@ -64,7 +64,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
public void testPrinter5() throws IOException {
|
public void testPrinter5() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||||
String[] line1 = {"a", "b\nc"};
|
String[] line1 = {"a", "b\nc"};
|
||||||
printer.println(line1);
|
printer.println(line1);
|
||||||
assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
|
assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
|
||||||
@ -72,7 +72,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
public void testPrinter6() throws IOException {
|
public void testPrinter6() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
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"};
|
String[] line1 = {"a", "b\r\nc"};
|
||||||
printer.println(line1);
|
printer.println(line1);
|
||||||
assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
|
assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
|
||||||
@ -80,7 +80,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
public void testPrinter7() throws IOException {
|
public void testPrinter7() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||||
String[] line1 = {"a", "b\\c"};
|
String[] line1 = {"a", "b\\c"};
|
||||||
printer.println(line1);
|
printer.println(line1);
|
||||||
assertEquals("a,b\\c" + lineSeparator, sw.toString());
|
assertEquals("a,b\\c" + lineSeparator, sw.toString());
|
||||||
@ -88,7 +88,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
public void testExcelPrinter1() throws IOException {
|
public void testExcelPrinter1() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY);
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
|
||||||
String[] line1 = {"a", "b"};
|
String[] line1 = {"a", "b"};
|
||||||
printer.println(line1);
|
printer.println(line1);
|
||||||
assertEquals("a,b" + lineSeparator, sw.toString());
|
assertEquals("a,b" + lineSeparator, sw.toString());
|
||||||
@ -96,7 +96,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
public void testExcelPrinter2() throws IOException {
|
public void testExcelPrinter2() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY);
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
|
||||||
String[] line1 = {"a,b", "b"};
|
String[] line1 = {"a,b", "b"};
|
||||||
printer.println(line1);
|
printer.println(line1);
|
||||||
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
||||||
@ -105,18 +105,18 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
public void testRandom() throws Exception {
|
public void testRandom() throws Exception {
|
||||||
int iter = 10000;
|
int iter = 10000;
|
||||||
strategy = CSVStrategy.DEFAULT_STRATEGY;
|
format = CSVFormat.DEFAULT;
|
||||||
doRandom(iter);
|
doRandom(iter);
|
||||||
strategy = CSVStrategy.EXCEL_STRATEGY;
|
format = CSVFormat.EXCEL;
|
||||||
doRandom(iter);
|
doRandom(iter);
|
||||||
|
|
||||||
// Strategy for MySQL
|
// Format for MySQL
|
||||||
strategy = new CSVStrategy('\t', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '\\', false, false, false, false);
|
format = new CSVFormat('\t', CSVFormat.ENCAPSULATOR_DISABLED, CSVFormat.COMMENTS_DISABLED, '\\', false, false, false, false);
|
||||||
doRandom(iter);
|
doRandom(iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
CSVStrategy strategy;
|
CSVFormat format;
|
||||||
|
|
||||||
public void doRandom(int iter) throws Exception {
|
public void doRandom(int iter) throws Exception {
|
||||||
for (int i = 0; i < iter; i++) {
|
for (int i = 0; i < iter; i++) {
|
||||||
@ -138,7 +138,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
StringWriter sw = new StringWriter();
|
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 i = 0; i < nLines; i++) {
|
||||||
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
|
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
|
||||||
@ -151,7 +151,7 @@ public class CSVPrinterTest extends TestCase {
|
|||||||
|
|
||||||
StringReader reader = new StringReader(result);
|
StringReader reader = new StringReader(result);
|
||||||
|
|
||||||
CSVParser parser = new CSVParser(reader, strategy);
|
CSVParser parser = new CSVParser(reader, format);
|
||||||
String[][] parseResult = parser.getAllValues();
|
String[][] parseResult = parser.getAllValues();
|
||||||
|
|
||||||
if (!equals(lines, parseResult)) {
|
if (!equals(lines, parseResult)) {
|
||||||
|
@ -81,7 +81,7 @@ public class CSVUtilsTest extends TestCase {
|
|||||||
|
|
||||||
public void testParse6() throws IOException {
|
public void testParse6() throws IOException {
|
||||||
String[][] data = CSVUtils.parse("");
|
String[][] data = CSVUtils.parse("");
|
||||||
// default strategy is CSV, which ignores empty lines
|
// default format is CSV, which ignores empty lines
|
||||||
assertEquals(0, data.length);
|
assertEquals(0, data.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user