[CSV-99] Revert Builder implementation in CSVFormat.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1508470 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7dbc976992
commit
75f39a81a7
|
@ -50,377 +50,6 @@ import java.util.Arrays;
|
|||
*/
|
||||
public class CSVFormat implements Serializable {
|
||||
|
||||
/**
|
||||
* Builds CSVFormat objects.
|
||||
*/
|
||||
public static class CSVFormatBuilder {
|
||||
|
||||
private char delimiter;
|
||||
private Character quoteChar;
|
||||
private Quote quotePolicy;
|
||||
private Character commentStart;
|
||||
private Character escape;
|
||||
private boolean ignoreSurroundingSpaces; // Should leading/trailing spaces be ignored around values?
|
||||
private boolean ignoreEmptyLines;
|
||||
private String recordSeparator; // for outputs
|
||||
private String nullString;
|
||||
private String[] header;
|
||||
|
||||
/**
|
||||
* Creates a basic CSVFormatBuilder.
|
||||
*
|
||||
* @param delimiter
|
||||
* the char used for value separation, must not be a line break character
|
||||
* @throws IllegalArgumentException if the delimiter is a line break character
|
||||
*/
|
||||
// package protected to give access without needing a synthetic accessor
|
||||
CSVFormatBuilder(final char delimiter) {
|
||||
this(delimiter, null, null, null, null, false, false, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a customized CSV format.
|
||||
*
|
||||
* @param delimiter
|
||||
* the char used for value separation, must not be a line break character
|
||||
* @param quoteChar
|
||||
* the char used as value encapsulation marker
|
||||
* @param quotePolicy
|
||||
* the quote policy
|
||||
* @param commentStart
|
||||
* the char used for comment identification
|
||||
* @param escape
|
||||
* the char used to escape special characters in values
|
||||
* @param ignoreSurroundingSpaces
|
||||
* <tt>true</tt> when whitespaces enclosing values should be ignored
|
||||
* @param ignoreEmptyLines
|
||||
* <tt>true</tt> when the parser should skip empty lines
|
||||
* @param recordSeparator
|
||||
* the record separator to use for output
|
||||
* @param nullString
|
||||
* the String to convert to and from {@code null}. No substitution occurs if {@code null}
|
||||
* @param header
|
||||
* the header
|
||||
* @throws IllegalArgumentException if the delimiter is a line break character
|
||||
*/
|
||||
// package protected for use by test code
|
||||
CSVFormatBuilder(final char delimiter, final Character quoteChar,
|
||||
final Quote quotePolicy, final Character commentStart,
|
||||
final Character escape, final boolean ignoreSurroundingSpaces,
|
||||
final boolean ignoreEmptyLines, final String recordSeparator,
|
||||
String nullString, final String[] header) {
|
||||
if (isLineBreak(delimiter)) {
|
||||
throw new IllegalArgumentException("The delimiter cannot be a line break");
|
||||
}
|
||||
this.delimiter = delimiter;
|
||||
this.quoteChar = quoteChar;
|
||||
this.quotePolicy = quotePolicy;
|
||||
this.commentStart = commentStart;
|
||||
this.escape = escape;
|
||||
this.ignoreSurroundingSpaces = ignoreSurroundingSpaces;
|
||||
this.ignoreEmptyLines = ignoreEmptyLines;
|
||||
this.recordSeparator = recordSeparator;
|
||||
this.nullString = nullString;
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a CSVFormatBuilder, using the values of the given CSVFormat.
|
||||
*
|
||||
* @param format
|
||||
* The format to use values from
|
||||
*/
|
||||
@SuppressWarnings("synthetic-access") // TODO fields could be made package-protected
|
||||
// package protected to give access without needing a synthetic accessor
|
||||
CSVFormatBuilder(final CSVFormat format) {
|
||||
this(format.delimiter, format.quoteChar, format.quotePolicy,
|
||||
format.commentStart, format.escape,
|
||||
format.ignoreSurroundingSpaces, format.ignoreEmptyLines,
|
||||
format.recordSeparator, format.nullString, format.header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a new CSVFormat configured with the values from this builder.
|
||||
*
|
||||
* @return a new CSVFormat
|
||||
*/
|
||||
public CSVFormat build() {
|
||||
validate();
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString,
|
||||
header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the specified content. Short-hand for:
|
||||
* <pre>format.build().parse(in);</pre>
|
||||
*
|
||||
* @param in
|
||||
* the input stream
|
||||
* @return a CSVRecord stream
|
||||
* @throws IOException
|
||||
* If an I/O error occurs
|
||||
*/
|
||||
public Iterable<CSVRecord> parse(final Reader in) throws IOException {
|
||||
return this.build().parse(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the consistency of the parameters and throws an IllegalStateException if necessary.
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
*/
|
||||
private void validate() throws IllegalStateException {
|
||||
if (quoteChar != null && delimiter == quoteChar.charValue()) {
|
||||
throw new IllegalStateException(
|
||||
"The quoteChar character and the delimiter cannot be the same ('" + quoteChar + "')");
|
||||
}
|
||||
|
||||
if (escape != null && delimiter == escape.charValue()) {
|
||||
throw new IllegalStateException(
|
||||
"The escape character and the delimiter cannot be the same ('" + escape + "')");
|
||||
}
|
||||
|
||||
if (commentStart != null && delimiter == commentStart.charValue()) {
|
||||
throw new IllegalStateException(
|
||||
"The comment start character and the delimiter cannot be the same ('" + commentStart + "')");
|
||||
}
|
||||
|
||||
if (quoteChar != null && quoteChar.equals(commentStart)) {
|
||||
throw new IllegalStateException(
|
||||
"The comment start character and the quoteChar cannot be the same ('" + commentStart + "')");
|
||||
}
|
||||
|
||||
if (escape != null && escape.equals(commentStart)) {
|
||||
throw new IllegalStateException(
|
||||
"The comment start and the escape character cannot be the same ('" + commentStart + "')");
|
||||
}
|
||||
|
||||
if (escape == null && quotePolicy == Quote.NONE) {
|
||||
throw new IllegalStateException("No quotes mode set but no escape character is set");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the comment start marker of the format to the specified character.
|
||||
*
|
||||
* Note that the comment introducer character is only recognised at the start of a line.
|
||||
*
|
||||
* @param commentStart
|
||||
* the comment start marker
|
||||
* @return This builder with the specified character as the comment start marker
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormatBuilder withCommentStart(final char commentStart) {
|
||||
return withCommentStart(Character.valueOf(commentStart));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the comment start marker of the format to the specified character.
|
||||
*
|
||||
* Note that the comment introducer character is only recognised at the start of a line.
|
||||
*
|
||||
* @param commentStart
|
||||
* the comment start marker
|
||||
* @return This builder with the specified character as the comment start marker
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormatBuilder withCommentStart(final Character commentStart) {
|
||||
if (isLineBreak(commentStart)) {
|
||||
throw new IllegalArgumentException("The comment start character cannot be a line break");
|
||||
}
|
||||
this.commentStart = commentStart;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the delimiter of the format to the specified character.
|
||||
*
|
||||
* @param delimiter
|
||||
* the delimiter character
|
||||
* @return This builder with the specified character as delimiter
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormatBuilder withDelimiter(final char delimiter) {
|
||||
if (isLineBreak(delimiter)) {
|
||||
throw new IllegalArgumentException("The delimiter cannot be a line break");
|
||||
}
|
||||
this.delimiter = delimiter;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the escape character of the format to the specified character.
|
||||
*
|
||||
* @param escape
|
||||
* the escape character
|
||||
* @return This builder with the specified character as the escape character
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormatBuilder withEscape(final char escape) {
|
||||
return withEscape(Character.valueOf(escape));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the escape character of the format to the specified character.
|
||||
*
|
||||
* @param escape
|
||||
* the escape character
|
||||
* @return This builder with the specified character as the escape character
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormatBuilder withEscape(final Character escape) {
|
||||
if (isLineBreak(escape)) {
|
||||
throw new IllegalArgumentException("The escape character cannot be a line break");
|
||||
}
|
||||
this.escape = escape;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the header of the format. The header can either be parsed automatically from the
|
||||
* input file with:
|
||||
*
|
||||
* <pre>
|
||||
* CSVFormat format = aformat.withHeader();
|
||||
* </pre>
|
||||
*
|
||||
* or specified manually with:
|
||||
*
|
||||
* <pre>
|
||||
* CSVFormat format = aformat.withHeader("name", "email", "phone");
|
||||
* </pre>
|
||||
*
|
||||
* @param header
|
||||
* the header, <tt>null</tt> if disabled, empty if parsed automatically, user specified otherwise.
|
||||
*
|
||||
* @return This builder with the specified header
|
||||
*/
|
||||
public CSVFormatBuilder withHeader(final String... header) {
|
||||
this.header = header;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the empty line skipping behavior of the format.
|
||||
*
|
||||
* @param ignoreEmptyLines
|
||||
* the empty line skipping behavior, <tt>true</tt> to ignore the empty lines between the records,
|
||||
* <tt>false</tt> to translate empty lines to empty records.
|
||||
* @return This builder with the specified empty line skipping behavior.
|
||||
*/
|
||||
public CSVFormatBuilder withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
|
||||
this.ignoreEmptyLines = ignoreEmptyLines;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the trimming behavior of the format.
|
||||
*
|
||||
* @param ignoreSurroundingSpaces
|
||||
* the trimming behavior, <tt>true</tt> to remove the surrounding spaces, <tt>false</tt> to leave the
|
||||
* spaces as is.
|
||||
* @return This builder with the specified trimming behavior.
|
||||
*/
|
||||
public CSVFormatBuilder withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
|
||||
this.ignoreSurroundingSpaces = ignoreSurroundingSpaces;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs conversions to and from null for strings on input and output.
|
||||
* <ul>
|
||||
* <li>
|
||||
* <strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading
|
||||
* records.</li>
|
||||
* <li>
|
||||
* <strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param nullString
|
||||
* the String to convert to and from {@code null}. No substitution occurs if {@code null}
|
||||
*
|
||||
* @return This builder with the the specified null conversion string.
|
||||
*/
|
||||
public CSVFormatBuilder withNullString(final String nullString) {
|
||||
this.nullString = nullString;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the quoteChar of the format to the specified character.
|
||||
*
|
||||
* @param quoteChar
|
||||
* the quoteChar character
|
||||
* @return This builder with the specified character as quoteChar
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormatBuilder withQuoteChar(final char quoteChar) {
|
||||
return withQuoteChar(Character.valueOf(quoteChar));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the quoteChar of the format to the specified character.
|
||||
*
|
||||
* @param quoteChar
|
||||
* the quoteChar character
|
||||
* @return This builder with the specified character as quoteChar
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormatBuilder withQuoteChar(final Character quoteChar) {
|
||||
if (isLineBreak(quoteChar)) {
|
||||
throw new IllegalArgumentException("The quoteChar cannot be a line break");
|
||||
}
|
||||
this.quoteChar = quoteChar;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the output quote policy of the format to the specified value.
|
||||
*
|
||||
* @param quotePolicy
|
||||
* the quote policy to use for output.
|
||||
*
|
||||
* @return This builder with the specified quote policy
|
||||
*/
|
||||
public CSVFormatBuilder withQuotePolicy(final Quote quotePolicy) {
|
||||
this.quotePolicy = quotePolicy;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the record separator of the format to the specified character.
|
||||
*
|
||||
* @param recordSeparator
|
||||
* the record separator to use for output.
|
||||
*
|
||||
* @return This builder with the the specified output record separator
|
||||
*/
|
||||
public CSVFormatBuilder withRecordSeparator(final char recordSeparator) {
|
||||
return withRecordSeparator(String.valueOf(recordSeparator));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the record separator of the format to the specified String.
|
||||
*
|
||||
* @param recordSeparator
|
||||
* the record separator to use for output.
|
||||
*
|
||||
* @return This builder with the the specified output record separator
|
||||
*/
|
||||
public CSVFormatBuilder withRecordSeparator(final String recordSeparator) {
|
||||
this.recordSeparator = recordSeparator;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* Returns true if the given character is a line break character.
|
||||
|
@ -434,22 +63,6 @@ public class CSVFormat implements Serializable {
|
|||
static boolean isLineBreak(final Character c) {
|
||||
return c != null && isLineBreak(c.charValue());
|
||||
}
|
||||
/**
|
||||
* Creates a standard comma separated format builder, as for {@link #RFC4180} but allowing empty lines.
|
||||
* <ul>
|
||||
* <li>withDelimiter(',')</li>
|
||||
* <li>withQuoteChar('"')</li>
|
||||
* <li>withEmptyLinesIgnored(true)</li>
|
||||
* <li>withRecordSeparator(CRLF)</li>
|
||||
* </ul>
|
||||
*
|
||||
* Shortcut for {@code CSVFormat.newBuilder(CSVFormat.DEFAULT)}
|
||||
*
|
||||
* @return a standard comma separated format builder, as for {@link #RFC4180} but allowing empty lines.
|
||||
*/
|
||||
public static CSVFormatBuilder newBuilder() {
|
||||
return new CSVFormatBuilder(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true, CRLF, null, null);
|
||||
}
|
||||
|
||||
private final char delimiter;
|
||||
private final Character quoteChar;
|
||||
|
@ -462,20 +75,6 @@ public class CSVFormat implements Serializable {
|
|||
private final String nullString;
|
||||
private final String[] header;
|
||||
|
||||
/**
|
||||
* Comma separated format as defined by <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>.
|
||||
* <h3>RFC 4180:</h3>
|
||||
* <ul>
|
||||
* <li>withDelimiter(',')</li>
|
||||
* <li>withQuoteChar('"')</li>
|
||||
* <li>withRecordSeparator(CRLF)</li>
|
||||
* </ul>
|
||||
*/
|
||||
public static final CSVFormat RFC4180 =
|
||||
newBuilder()
|
||||
.withIgnoreEmptyLines(false)
|
||||
.build();
|
||||
|
||||
/**
|
||||
* Standard comma separated format, as for {@link #RFC4180} but allowing empty lines.
|
||||
* <h3>RFC 4180:</h3>
|
||||
|
@ -489,9 +88,18 @@ public class CSVFormat implements Serializable {
|
|||
* <li>withIgnoreEmptyLines(true)</li>
|
||||
* </ul>
|
||||
*/
|
||||
public static final CSVFormat DEFAULT =
|
||||
newBuilder()
|
||||
.build();
|
||||
public static final CSVFormat DEFAULT = new CSVFormat(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true, CRLF, null, null);
|
||||
|
||||
/**
|
||||
* Comma separated format as defined by <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>.
|
||||
* <h3>RFC 4180:</h3>
|
||||
* <ul>
|
||||
* <li>withDelimiter(',')</li>
|
||||
* <li>withQuoteChar('"')</li>
|
||||
* <li>withRecordSeparator(CRLF)</li>
|
||||
* </ul>
|
||||
*/
|
||||
public static final CSVFormat RFC4180 = DEFAULT.withIgnoreEmptyLines(false);
|
||||
|
||||
/**
|
||||
* Excel file format (using a comma as the value delimiter). Note that the actual value delimiter used by Excel is
|
||||
|
@ -510,17 +118,13 @@ public class CSVFormat implements Serializable {
|
|||
* </ul>
|
||||
* Note: this is currently the same as RFC4180
|
||||
*/
|
||||
public static final CSVFormat EXCEL =
|
||||
newBuilder()
|
||||
.withIgnoreEmptyLines(false)
|
||||
.build();
|
||||
public static final CSVFormat EXCEL = DEFAULT.withIgnoreEmptyLines(false);
|
||||
|
||||
/** Tab-delimited format, with quote; leading and trailing spaces ignored. */
|
||||
public static final CSVFormat TDF =
|
||||
newBuilder()
|
||||
DEFAULT
|
||||
.withDelimiter(TAB)
|
||||
.withIgnoreSurroundingSpaces(true)
|
||||
.build();
|
||||
.withIgnoreSurroundingSpaces(true);
|
||||
|
||||
/**
|
||||
* Default MySQL format used by the <tt>SELECT INTO OUTFILE</tt> and <tt>LOAD DATA INFILE</tt> operations. This is
|
||||
|
@ -531,13 +135,12 @@ public class CSVFormat implements Serializable {
|
|||
* http://dev.mysql.com/doc/refman/5.1/en/load-data.html</a>
|
||||
*/
|
||||
public static final CSVFormat MYSQL =
|
||||
newBuilder()
|
||||
DEFAULT
|
||||
.withDelimiter(TAB)
|
||||
.withQuoteChar(null)
|
||||
.withEscape(BACKSLASH)
|
||||
.withIgnoreEmptyLines(false)
|
||||
.withRecordSeparator(LF)
|
||||
.build();
|
||||
.withQuoteChar(null)
|
||||
.withRecordSeparator(LF);
|
||||
|
||||
/**
|
||||
* Returns true if the given character is a line break character.
|
||||
|
@ -553,15 +156,15 @@ public class CSVFormat implements Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Creates a new CSV format builder.
|
||||
* Creates a new CSV format with the specified delimiter.
|
||||
*
|
||||
* @param delimiter
|
||||
* the char used for value separation, must not be a line break character
|
||||
* @return a new CSV format builder.
|
||||
* @return a new CSV format.
|
||||
* @throws IllegalArgumentException if the delimiter is a line break character
|
||||
*/
|
||||
public static CSVFormatBuilder newBuilder(final char delimiter) {
|
||||
return new CSVFormatBuilder(delimiter);
|
||||
public static CSVFormat newFormat(final char delimiter) {
|
||||
return new CSVFormat(delimiter, null, null, null, null, false, false, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -569,10 +172,10 @@ public class CSVFormat implements Serializable {
|
|||
*
|
||||
* @param format
|
||||
* The format to use values from
|
||||
* @return a new CSVFormatBuilder
|
||||
* @return a new CSVFormat
|
||||
*/
|
||||
public static CSVFormatBuilder newBuilder(final CSVFormat format) {
|
||||
return new CSVFormatBuilder(format);
|
||||
public static CSVFormat copy(final CSVFormat format) {
|
||||
return new CSVFormat(format);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -621,6 +224,12 @@ public class CSVFormat implements Serializable {
|
|||
this.header = header == null ? null : header.clone();
|
||||
}
|
||||
|
||||
CSVFormat(final CSVFormat format) {
|
||||
this(format.getDelimiter(), format.getQuoteChar(), format.getQuotePolicy(), format.getCommentStart(),
|
||||
format.getEscape(), format.getIgnoreSurroundingSpaces(), format.getIgnoreEmptyLines(),
|
||||
format.getRecordSeparator(), format.getNullString(), format.getHeader());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj) {
|
||||
|
@ -853,15 +462,6 @@ public class CSVFormat implements Serializable {
|
|||
return new CSVParser(in, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a builder based on this format.
|
||||
*
|
||||
* @return a new builder
|
||||
*/
|
||||
public CSVFormatBuilder toBuilder() {
|
||||
return new CSVFormatBuilder(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
|
@ -886,4 +486,259 @@ public class CSVFormat implements Serializable {
|
|||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the consistency of the parameters and throws an IllegalStateException if necessary.
|
||||
*
|
||||
* @throws IllegalStateException
|
||||
*/
|
||||
void validate() throws IllegalStateException {
|
||||
if (quoteChar != null && delimiter == quoteChar.charValue()) {
|
||||
throw new IllegalStateException(
|
||||
"The quoteChar character and the delimiter cannot be the same ('" + quoteChar + "')");
|
||||
}
|
||||
|
||||
if (escape != null && delimiter == escape.charValue()) {
|
||||
throw new IllegalStateException(
|
||||
"The escape character and the delimiter cannot be the same ('" + escape + "')");
|
||||
}
|
||||
|
||||
if (commentStart != null && delimiter == commentStart.charValue()) {
|
||||
throw new IllegalStateException(
|
||||
"The comment start character and the delimiter cannot be the same ('" + commentStart + "')");
|
||||
}
|
||||
|
||||
if (quoteChar != null && quoteChar.equals(commentStart)) {
|
||||
throw new IllegalStateException(
|
||||
"The comment start character and the quoteChar cannot be the same ('" + commentStart + "')");
|
||||
}
|
||||
|
||||
if (escape != null && escape.equals(commentStart)) {
|
||||
throw new IllegalStateException(
|
||||
"The comment start and the escape character cannot be the same ('" + commentStart + "')");
|
||||
}
|
||||
|
||||
if (escape == null && quotePolicy == Quote.NONE) {
|
||||
throw new IllegalStateException("No quotes mode set but no escape character is set");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the comment start marker of the format to the specified character.
|
||||
*
|
||||
* Note that the comment start character is only recognised at the start of a line.
|
||||
*
|
||||
* @param commentStart
|
||||
* the comment start marker
|
||||
* @return A new CSVFormat that is equal to this one but with the specified character as the comment start marker
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormat withCommentStart(final char commentStart) {
|
||||
return withCommentStart(Character.valueOf(commentStart));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the comment start marker of the format to the specified character.
|
||||
*
|
||||
* Note that the comment start character is only recognised at the start of a line.
|
||||
*
|
||||
* @param commentStart
|
||||
* the comment start marker
|
||||
* @return A new CSVFormat that is equal to this one but with the specified character as the comment start marker
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormat withCommentStart(final Character commentStart) {
|
||||
if (isLineBreak(commentStart)) {
|
||||
throw new IllegalArgumentException("The comment start character cannot be a line break");
|
||||
}
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the delimiter of the format to the specified character.
|
||||
*
|
||||
* @param delimiter
|
||||
* the delimiter character
|
||||
* @return A new CSVFormat that is equal to this with the specified character as delimiter
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormat withDelimiter(final char delimiter) {
|
||||
if (isLineBreak(delimiter)) {
|
||||
throw new IllegalArgumentException("The delimiter cannot be a line break");
|
||||
}
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the escape character of the format to the specified character.
|
||||
*
|
||||
* @param escape
|
||||
* the escape character
|
||||
* @return A new CSVFormat that is equal to his but with the specified character as the escape character
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormat withEscape(final char escape) {
|
||||
return withEscape(Character.valueOf(escape));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the escape character of the format to the specified character.
|
||||
*
|
||||
* @param escape
|
||||
* the escape character
|
||||
* @return A new CSVFormat that is equal to this but with the specified character as the escape character
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormat withEscape(final Character escape) {
|
||||
if (isLineBreak(escape)) {
|
||||
throw new IllegalArgumentException("The escape character cannot be a line break");
|
||||
}
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the header of the format. The header can either be parsed automatically from the
|
||||
* input file with:
|
||||
*
|
||||
* <pre>
|
||||
* CSVFormat format = aformat.withHeader();
|
||||
* </pre>
|
||||
*
|
||||
* or specified manually with:
|
||||
*
|
||||
* <pre>
|
||||
* CSVFormat format = aformat.withHeader("name", "email", "phone");
|
||||
* </pre>
|
||||
*
|
||||
* @param header
|
||||
* the header, <tt>null</tt> if disabled, empty if parsed automatically, user specified otherwise.
|
||||
*
|
||||
* @return A new CSVFormat that is equal to this but with the specified header
|
||||
*/
|
||||
public CSVFormat withHeader(final String... header) {
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the empty line skipping behavior of the format.
|
||||
*
|
||||
* @param ignoreEmptyLines
|
||||
* the empty line skipping behavior, <tt>true</tt> to ignore the empty lines between the records,
|
||||
* <tt>false</tt> to translate empty lines to empty records.
|
||||
* @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
|
||||
*/
|
||||
public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the trimming behavior of the format.
|
||||
*
|
||||
* @param ignoreSurroundingSpaces
|
||||
* the trimming behavior, <tt>true</tt> to remove the surrounding spaces, <tt>false</tt> to leave the
|
||||
* spaces as is.
|
||||
* @return A new CSVFormat that is equal to this but with the specified trimming behavior.
|
||||
*/
|
||||
public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs conversions to and from null for strings on input and output.
|
||||
* <ul>
|
||||
* <li>
|
||||
* <strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading
|
||||
* records.</li>
|
||||
* <li>
|
||||
* <strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param nullString
|
||||
* the String to convert to and from {@code null}. No substitution occurs if {@code null}
|
||||
*
|
||||
* @return A new CSVFormat that is equal to this but with the specified null conversion string.
|
||||
*/
|
||||
public CSVFormat withNullString(final String nullString) {
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the quoteChar of the format to the specified character.
|
||||
*
|
||||
* @param quoteChar
|
||||
* the quoteChar character
|
||||
* @return A new CSVFormat that is equal to this but with the specified character as quoteChar
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormat withQuoteChar(final char quoteChar) {
|
||||
return withQuoteChar(Character.valueOf(quoteChar));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the quoteChar of the format to the specified character.
|
||||
*
|
||||
* @param quoteChar
|
||||
* the quoteChar character
|
||||
* @return A new CSVFormat that is equal to this but with the specified character as quoteChar
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the specified character is a line break
|
||||
*/
|
||||
public CSVFormat withQuoteChar(final Character quoteChar) {
|
||||
if (isLineBreak(quoteChar)) {
|
||||
throw new IllegalArgumentException("The quoteChar cannot be a line break");
|
||||
}
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the output quote policy of the format to the specified value.
|
||||
*
|
||||
* @param quotePolicy
|
||||
* the quote policy to use for output.
|
||||
*
|
||||
* @return A new CSVFormat that is equal to this but with the specified quote policy
|
||||
*/
|
||||
public CSVFormat withQuotePolicy(final Quote quotePolicy) {
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the record separator of the format to the specified character.
|
||||
*
|
||||
* @param recordSeparator
|
||||
* the record separator to use for output.
|
||||
*
|
||||
* @return A new CSVFormat that is equal to this but with the the specified output record separator
|
||||
*/
|
||||
public CSVFormat withRecordSeparator(final char recordSeparator) {
|
||||
return withRecordSeparator(String.valueOf(recordSeparator));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the record separator of the format to the specified String.
|
||||
*
|
||||
* @param recordSeparator
|
||||
* the record separator to use for output.
|
||||
*
|
||||
* @return A new CSVFormat that is equal to this but with the the specified output record separator
|
||||
*/
|
||||
public CSVFormat withRecordSeparator(final String recordSeparator) {
|
||||
return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
|
||||
ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, header);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ import java.util.NoSuchElementException;
|
|||
*
|
||||
* <pre>
|
||||
* Reader in = new StringReader("a\tb\nc\td");
|
||||
* Iterable<CSVRecord> parser = CSVFormat.newBuilder()
|
||||
* Iterable<CSVRecord> parser = CSVFormat.DEFAULT
|
||||
* .withCommentStart('#')
|
||||
* .withDelimiter('\t')
|
||||
* .withQuoteChar('"').parse(in);
|
||||
|
@ -120,8 +120,9 @@ public class CSVParser implements Iterable<CSVRecord> {
|
|||
* If an I/O error occurs
|
||||
*/
|
||||
public CSVParser(final Reader input, final CSVFormat format) throws IOException {
|
||||
this.lexer = new CSVLexer(format, new ExtendedBufferedReader(input));
|
||||
format.validate();
|
||||
this.format = format;
|
||||
this.lexer = new CSVLexer(format, new ExtendedBufferedReader(input));
|
||||
this.headerMap = initializeHeader();
|
||||
}
|
||||
|
||||
|
|
|
@ -58,6 +58,7 @@ public class CSVPrinter implements Flushable, Closeable {
|
|||
public CSVPrinter(final Appendable out, final CSVFormat format) {
|
||||
this.out = out;
|
||||
this.format = format == null ? CSVFormat.DEFAULT : format;
|
||||
format.validate();
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
|
|
|
@ -32,7 +32,6 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.csv.CSVFormat.CSVFormatBuilder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
@ -90,18 +89,17 @@ public class CSVFileParserTest {
|
|||
assertTrue(testName+" require 1 param", split.length >= 1);
|
||||
// first line starts with csv data file name
|
||||
final BufferedReader csvFile = new BufferedReader(new FileReader(new File(BASE, split[0])));
|
||||
final CSVFormatBuilder builder = CSVFormat.newBuilder(',').withQuoteChar('"');
|
||||
CSVFormat format = builder.build();
|
||||
CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('"');
|
||||
boolean checkComments = false;
|
||||
for(int i=1; i < split.length; i++) {
|
||||
final String option = split[i];
|
||||
final String[] option_parts = option.split("=",2);
|
||||
if ("IgnoreEmpty".equalsIgnoreCase(option_parts[0])){
|
||||
format = builder.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1])).build();
|
||||
format = format.withIgnoreEmptyLines(Boolean.parseBoolean(option_parts[1]));
|
||||
} else if ("IgnoreSpaces".equalsIgnoreCase(option_parts[0])) {
|
||||
format = builder.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1])).build();
|
||||
format = format.withIgnoreSurroundingSpaces(Boolean.parseBoolean(option_parts[1]));
|
||||
} else if ("CommentStart".equalsIgnoreCase(option_parts[0])) {
|
||||
format = builder.withCommentStart(option_parts[1].charAt(0)).build();
|
||||
format = format.withCommentStart(option_parts[1].charAt(0));
|
||||
} else if ("CheckComments".equalsIgnoreCase(option_parts[0])) {
|
||||
checkComments = true;
|
||||
} else {
|
||||
|
|
|
@ -1,202 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.apache.commons.csv;
|
||||
|
||||
import static org.apache.commons.csv.CSVFormat.RFC4180;
|
||||
import static org.apache.commons.csv.Constants.CR;
|
||||
import static org.apache.commons.csv.Constants.CRLF;
|
||||
import static org.apache.commons.csv.Constants.LF;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.apache.commons.csv.CSVFormat.CSVFormatBuilder;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
public class CSVFormatBuilderTest {
|
||||
|
||||
private CSVFormatBuilder builder;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
builder = new CSVFormatBuilder('+', Character.valueOf('!'), null, Character.valueOf('#'), Character.valueOf('!'), true, true, CRLF, Constants.EMPTY, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommentStart() {
|
||||
assertEquals('?', builder.withCommentStart('?').build().getCommentStart().charValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopiedFormatIsEqualToOriginal() {
|
||||
final CSVFormat copyOfRCF4180 = CSVFormat.newBuilder(RFC4180).build();
|
||||
assertEquals(RFC4180, copyOfRCF4180);
|
||||
final CSVFormat copy2OfRCF4180 = RFC4180.toBuilder().build();
|
||||
assertEquals(RFC4180, copy2OfRCF4180);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopiedFormatWithChanges() {
|
||||
final CSVFormat newFormat = CSVFormat.newBuilder(RFC4180).withDelimiter('!').build();
|
||||
assertTrue(newFormat.getDelimiter() != RFC4180.getDelimiter());
|
||||
final CSVFormat newFormat2 = RFC4180.toBuilder().withDelimiter('!').build();
|
||||
assertTrue(newFormat2.getDelimiter() != RFC4180.getDelimiter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelimiter() {
|
||||
assertEquals('?', builder.withDelimiter('?').build().getDelimiter());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testDelimiterSameAsCommentStartThrowsException() {
|
||||
builder.withDelimiter('!').withCommentStart('!').build();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testDelimiterSameAsEscapeThrowsException() {
|
||||
builder.withDelimiter('!').withEscape('!').build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscape() {
|
||||
assertEquals('?', builder.withEscape('?').build().getEscape().charValue());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testEscapeSameAsCommentStartThrowsException() {
|
||||
builder.withEscape('!').withCommentStart('!').build();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() {
|
||||
// Cannot assume that callers won't use different Character objects
|
||||
builder.withEscape(new Character('!')).withCommentStart(new Character('!')).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderReferenceCannotEscape() {
|
||||
final String[] header = new String[]{"one", "tow", "three"};
|
||||
builder.withHeader(header);
|
||||
|
||||
final CSVFormat firstFormat = builder.build();
|
||||
final CSVFormat secondFormat = builder.build();
|
||||
assertNotSame(header, firstFormat.getHeader());
|
||||
assertNotSame(firstFormat, secondFormat.getHeader());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreEmptyLines() {
|
||||
assertFalse(builder.withIgnoreEmptyLines(false).build().getIgnoreEmptyLines());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreSurroundingSpaces() {
|
||||
assertFalse(builder.withIgnoreSurroundingSpaces(false).build().getIgnoreSurroundingSpaces());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNewFormatCRThrowsException() {
|
||||
CSVFormat.newBuilder(CR);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNewFormatLFThrowsException() {
|
||||
CSVFormat.newBuilder(LF);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuoteChar() {
|
||||
assertEquals('?', builder.withQuoteChar('?').build().getQuoteChar().charValue());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testQuoteCharSameAsCommentStartThrowsException() {
|
||||
builder.withQuoteChar('!').withCommentStart('!').build();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType() {
|
||||
// Cannot assume that callers won't use different Character objects
|
||||
builder.withQuoteChar(new Character('!')).withCommentStart('!').build();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testQuoteCharSameAsDelimiterThrowsException() {
|
||||
builder.withQuoteChar('!').withDelimiter('!').build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuotePolicy() {
|
||||
assertEquals(Quote.ALL, builder.withQuotePolicy(Quote.ALL).build().getQuotePolicy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultQuotePolicy() {
|
||||
assertNull(builder.build().getQuotePolicy());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testQuotePolicyNoneWithoutEscapeThrowsException() {
|
||||
CSVFormat.newBuilder('!').withQuotePolicy(Quote.NONE).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRecoardSeparator() {
|
||||
assertEquals("?", builder.withRecordSeparator("?").build().getRecordSeparator());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRFC4180() {
|
||||
assertEquals(null, RFC4180.getCommentStart());
|
||||
assertEquals(',', RFC4180.getDelimiter());
|
||||
assertEquals(null, RFC4180.getEscape());
|
||||
assertFalse(RFC4180.getIgnoreEmptyLines());
|
||||
assertEquals(Character.valueOf('"'), RFC4180.getQuoteChar());
|
||||
assertEquals(null, RFC4180.getQuotePolicy());
|
||||
assertEquals("\r\n", RFC4180.getRecordSeparator());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testWithCommentStartCRThrowsException() {
|
||||
builder.withCommentStart(CR).build();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testWithDelimiterLFThrowsException() {
|
||||
builder.withDelimiter(LF).build();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testWithEscapeCRThrowsExceptions() {
|
||||
builder.withEscape(CR).build();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testWithQuoteLFThrowsException() {
|
||||
builder.withQuoteChar(LF).build();
|
||||
}
|
||||
}
|
|
@ -17,9 +17,15 @@
|
|||
|
||||
package org.apache.commons.csv;
|
||||
|
||||
import static org.apache.commons.csv.CSVFormat.RFC4180;
|
||||
import static org.apache.commons.csv.Constants.CR;
|
||||
import static org.apache.commons.csv.Constants.LF;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
@ -70,7 +76,7 @@ public class CSVFormatTest {
|
|||
@Test
|
||||
public void testEquals() {
|
||||
final CSVFormat right = CSVFormat.DEFAULT;
|
||||
final CSVFormat left = CSVFormat.newBuilder().build();
|
||||
final CSVFormat left = CSVFormat.copy(right);
|
||||
|
||||
assertFalse(right.equals(null));
|
||||
assertFalse(right.equals("A String Instance"));
|
||||
|
@ -85,132 +91,253 @@ public class CSVFormatTest {
|
|||
|
||||
@Test
|
||||
public void testEqualsDelimiter() {
|
||||
final CSVFormat right = CSVFormat.newBuilder('!').build();
|
||||
final CSVFormat left = CSVFormat.newBuilder('?').build();
|
||||
final CSVFormat right = CSVFormat.newFormat('!');
|
||||
final CSVFormat left = CSVFormat.newFormat('?');
|
||||
|
||||
assertNotEquals(right, left);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsQuoteChar() {
|
||||
final CSVFormat right = CSVFormat.newBuilder('\'').withQuoteChar('"').build();
|
||||
final CSVFormat left = CSVFormat.newBuilder(right).withQuoteChar('!').build();
|
||||
final CSVFormat right = CSVFormat.newFormat('\'').withQuoteChar('"');
|
||||
final CSVFormat left = CSVFormat.copy(right).withQuoteChar('!');
|
||||
|
||||
assertNotEquals(right, left);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsQuotePolicy() {
|
||||
final CSVFormat right = CSVFormat.newBuilder('\'')
|
||||
final CSVFormat right = CSVFormat.newFormat('\'')
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL)
|
||||
.build();
|
||||
final CSVFormat left = CSVFormat.newBuilder(right)
|
||||
.withQuotePolicy(Quote.MINIMAL)
|
||||
.build();
|
||||
.withQuotePolicy(Quote.ALL);
|
||||
final CSVFormat left = CSVFormat.copy(right)
|
||||
.withQuotePolicy(Quote.MINIMAL);
|
||||
|
||||
assertNotEquals(right, left);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsCommentStart() {
|
||||
final CSVFormat right = CSVFormat.newBuilder('\'')
|
||||
final CSVFormat right = CSVFormat.newFormat('\'')
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL)
|
||||
.withCommentStart('#')
|
||||
.build();
|
||||
final CSVFormat left = CSVFormat.newBuilder(right)
|
||||
.withCommentStart('!')
|
||||
.build();
|
||||
.withQuotePolicy(Quote.ALL);
|
||||
final CSVFormat left = CSVFormat.copy(right)
|
||||
.withCommentStart('!');
|
||||
|
||||
assertNotEquals(right, left);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsEscape() {
|
||||
final CSVFormat right = CSVFormat.newBuilder('\'')
|
||||
final CSVFormat right = CSVFormat.newFormat('\'')
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL)
|
||||
.withCommentStart('#')
|
||||
.withEscape('+')
|
||||
.build();
|
||||
final CSVFormat left = CSVFormat.newBuilder(right)
|
||||
.withEscape('!')
|
||||
.build();
|
||||
.withQuotePolicy(Quote.ALL);
|
||||
final CSVFormat left = CSVFormat.copy(right)
|
||||
.withEscape('!');
|
||||
|
||||
assertNotEquals(right, left);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsIgnoreSurroundingSpaces() {
|
||||
final CSVFormat right = CSVFormat.newBuilder('\'')
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL)
|
||||
final CSVFormat right = CSVFormat.newFormat('\'')
|
||||
.withCommentStart('#')
|
||||
.withEscape('+')
|
||||
.withIgnoreSurroundingSpaces(true)
|
||||
.build();
|
||||
final CSVFormat left = CSVFormat.newBuilder(right)
|
||||
.withIgnoreSurroundingSpaces(false)
|
||||
.build();
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL);
|
||||
final CSVFormat left = CSVFormat.copy(right)
|
||||
.withIgnoreSurroundingSpaces(false);
|
||||
|
||||
assertNotEquals(right, left);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsIgnoreEmptyLines() {
|
||||
final CSVFormat right = CSVFormat.newBuilder('\'')
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL)
|
||||
final CSVFormat right = CSVFormat.newFormat('\'')
|
||||
.withCommentStart('#')
|
||||
.withEscape('+')
|
||||
.withIgnoreSurroundingSpaces(true)
|
||||
.withIgnoreEmptyLines(true)
|
||||
.build();
|
||||
final CSVFormat left = CSVFormat.newBuilder(right)
|
||||
.withIgnoreEmptyLines(false)
|
||||
.build();
|
||||
.withIgnoreSurroundingSpaces(true)
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL);
|
||||
final CSVFormat left = CSVFormat.copy(right)
|
||||
.withIgnoreEmptyLines(false);
|
||||
|
||||
assertNotEquals(right, left);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsRecordSeparator() {
|
||||
final CSVFormat right = CSVFormat.newBuilder('\'')
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL)
|
||||
final CSVFormat right = CSVFormat.newFormat('\'')
|
||||
.withRecordSeparator('*')
|
||||
.withCommentStart('#')
|
||||
.withEscape('+')
|
||||
.withIgnoreSurroundingSpaces(true)
|
||||
.withIgnoreEmptyLines(true)
|
||||
.withRecordSeparator('*')
|
||||
.build();
|
||||
final CSVFormat left = CSVFormat.newBuilder(right)
|
||||
.withRecordSeparator('!')
|
||||
.build();
|
||||
.withIgnoreSurroundingSpaces(true)
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL);
|
||||
final CSVFormat left = CSVFormat.copy(right)
|
||||
.withRecordSeparator('!');
|
||||
|
||||
assertNotEquals(right, left);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsHeader() {
|
||||
final CSVFormat right = CSVFormat.newBuilder('\'')
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL)
|
||||
final CSVFormat right = CSVFormat.newFormat('\'')
|
||||
.withRecordSeparator('*')
|
||||
.withCommentStart('#')
|
||||
.withEscape('+')
|
||||
.withIgnoreSurroundingSpaces(true)
|
||||
.withIgnoreEmptyLines(true)
|
||||
.withRecordSeparator('*')
|
||||
.withHeader("One", "Two", "Three")
|
||||
.build();
|
||||
final CSVFormat left = CSVFormat.newBuilder(right)
|
||||
.withHeader("Three", "Two", "One")
|
||||
.build();
|
||||
.withIgnoreEmptyLines(true)
|
||||
.withIgnoreSurroundingSpaces(true)
|
||||
.withQuoteChar('"')
|
||||
.withQuotePolicy(Quote.ALL);
|
||||
final CSVFormat left = CSVFormat.copy(right)
|
||||
.withHeader("Three", "Two", "One");
|
||||
|
||||
assertNotEquals(right, left);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithCommentStart() throws Exception {
|
||||
CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentStart('#');
|
||||
assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentStart());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithDelimiter() throws Exception {
|
||||
CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!');
|
||||
assertEquals('!', formatWithDelimiter.getDelimiter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithEscape() throws Exception {
|
||||
CSVFormat formatWithEscape = CSVFormat.DEFAULT.withEscape('&');
|
||||
assertEquals(Character.valueOf('&'), formatWithEscape.getEscape());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithHeader() throws Exception {
|
||||
String[] header = new String[]{"one", "two", "three"};
|
||||
CSVFormat formatWithHeader = CSVFormat.DEFAULT.withHeader(header);
|
||||
assertArrayEquals(header, formatWithHeader.getHeader());
|
||||
assertNotSame(header, formatWithHeader.getHeader());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithIgnoreEmptyLines() throws Exception {
|
||||
assertFalse(CSVFormat.DEFAULT.withIgnoreEmptyLines(false).getIgnoreEmptyLines());
|
||||
assertTrue(CSVFormat.DEFAULT.withIgnoreEmptyLines(true).getIgnoreEmptyLines());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithIgnoreSurround() throws Exception {
|
||||
assertFalse(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false).getIgnoreSurroundingSpaces());
|
||||
assertTrue(CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true).getIgnoreSurroundingSpaces());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithNullString() throws Exception {
|
||||
CSVFormat formatWithNullString = CSVFormat.DEFAULT.withNullString("null");
|
||||
assertEquals("null", formatWithNullString.getNullString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithQuoteChar() throws Exception {
|
||||
CSVFormat formatWithQuoteChar = CSVFormat.DEFAULT.withQuoteChar('"');
|
||||
assertEquals(Character.valueOf('"'), formatWithQuoteChar.getQuoteChar());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithQuotePolicy() throws Exception {
|
||||
CSVFormat formatWithQuotePolicy = CSVFormat.DEFAULT.withQuotePolicy(Quote.ALL);
|
||||
assertEquals(Quote.ALL, formatWithQuotePolicy.getQuotePolicy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithRecordSeparator() throws Exception {
|
||||
CSVFormat formatWithRecordSeparator = CSVFormat.DEFAULT.withRecordSeparator('!');
|
||||
assertEquals("!", formatWithRecordSeparator.getRecordSeparator());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testDelimiterSameAsCommentStartThrowsException() {
|
||||
CSVFormat.DEFAULT.withDelimiter('!').withCommentStart('!').validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testDelimiterSameAsEscapeThrowsException() {
|
||||
CSVFormat.DEFAULT.withDelimiter('!').withEscape('!').validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testEscapeSameAsCommentStartThrowsException() {
|
||||
CSVFormat.DEFAULT.withEscape('!').withCommentStart('!').validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() {
|
||||
// Cannot assume that callers won't use different Character objects
|
||||
CSVFormat.DEFAULT.withEscape(new Character('!')).withCommentStart(new Character('!')).validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testQuoteCharSameAsCommentStartThrowsException() {
|
||||
CSVFormat.DEFAULT.withQuoteChar('!').withCommentStart('!').validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testQuoteCharSameAsCommentStartThrowsExceptionForWrapperType() {
|
||||
// Cannot assume that callers won't use different Character objects
|
||||
CSVFormat.DEFAULT.withQuoteChar(new Character('!')).withCommentStart('!').validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testQuoteCharSameAsDelimiterThrowsException() {
|
||||
CSVFormat.DEFAULT.withQuoteChar('!').withDelimiter('!').validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testQuotePolicyNoneWithoutEscapeThrowsException() {
|
||||
CSVFormat.newFormat('!').withQuotePolicy(Quote.NONE).validate();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRFC4180() {
|
||||
assertEquals(null, RFC4180.getCommentStart());
|
||||
assertEquals(',', RFC4180.getDelimiter());
|
||||
assertEquals(null, RFC4180.getEscape());
|
||||
assertFalse(RFC4180.getIgnoreEmptyLines());
|
||||
assertEquals(Character.valueOf('"'), RFC4180.getQuoteChar());
|
||||
assertEquals(null, RFC4180.getQuotePolicy());
|
||||
assertEquals("\r\n", RFC4180.getRecordSeparator());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testWithCommentStartCRThrowsException() {
|
||||
CSVFormat.DEFAULT.withCommentStart(CR).validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testWithDelimiterLFThrowsException() {
|
||||
CSVFormat.DEFAULT.withDelimiter(LF).validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testWithEscapeCRThrowsExceptions() {
|
||||
CSVFormat.DEFAULT.withEscape(CR).validate();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testWithQuoteLFThrowsException() {
|
||||
CSVFormat.DEFAULT.withQuoteChar(LF).validate();
|
||||
}
|
||||
|
||||
private static void assertNotEquals(final Object right, final Object left) {
|
||||
assertFalse(right.equals(left));
|
||||
assertFalse(left.equals(right));
|
||||
|
|
|
@ -49,7 +49,7 @@ public class CSVLexerTest {
|
|||
|
||||
@Before
|
||||
public void setUp() {
|
||||
formatWithEscaping = CSVFormat.newBuilder().withEscape('\\').build();
|
||||
formatWithEscaping = CSVFormat.DEFAULT.withEscape('\\');
|
||||
}
|
||||
|
||||
private Lexer getLexer(final String input, final CSVFormat format) {
|
||||
|
@ -59,7 +59,7 @@ public class CSVLexerTest {
|
|||
@Test
|
||||
public void testSurroundingSpacesAreDeleted() throws IOException {
|
||||
final String code = "noSpaces, leadingSpaces,trailingSpaces , surroundingSpaces , ,,";
|
||||
final Lexer parser = getLexer(code, CSVFormat.newBuilder().withIgnoreSurroundingSpaces(true).build());
|
||||
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "noSpaces"));
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingSpaces"));
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingSpaces"));
|
||||
|
@ -72,7 +72,7 @@ public class CSVLexerTest {
|
|||
@Test
|
||||
public void testSurroundingTabsAreDeleted() throws IOException {
|
||||
final String code = "noTabs,\tleadingTab,trailingTab\t,\tsurroundingTabs\t,\t\t,,";
|
||||
final Lexer parser = getLexer(code, CSVFormat.newBuilder().withIgnoreSurroundingSpaces(true).build());
|
||||
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "noTabs"));
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "leadingTab"));
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "trailingTab"));
|
||||
|
@ -98,7 +98,7 @@ public class CSVLexerTest {
|
|||
"\n"+
|
||||
"\n"+
|
||||
"\n";
|
||||
final CSVFormat format = CSVFormat.newBuilder().withIgnoreEmptyLines(true).build();
|
||||
final CSVFormat format = CSVFormat.DEFAULT.withIgnoreEmptyLines(true);
|
||||
final Lexer parser = getLexer(code, format);
|
||||
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
|
||||
|
@ -122,7 +122,7 @@ public class CSVLexerTest {
|
|||
"third,line,#no-comment\n"+
|
||||
"# penultimate comment\n"+
|
||||
"# Final comment\n";
|
||||
final CSVFormat format = CSVFormat.newBuilder().withCommentStart('#').build();
|
||||
final CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#');
|
||||
final Lexer parser = getLexer(code, format);
|
||||
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
|
||||
|
@ -158,7 +158,7 @@ public class CSVLexerTest {
|
|||
"\n"+ // 6b
|
||||
"\n"+ // 6c
|
||||
"# Final comment\n"; // 7
|
||||
final CSVFormat format = CSVFormat.newBuilder().withCommentStart('#').withIgnoreEmptyLines(false).build();
|
||||
final CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#').withIgnoreEmptyLines(false);
|
||||
assertFalse("Should not ignore empty lines", format.getIgnoreEmptyLines());
|
||||
|
||||
final Lexer parser = getLexer(code, format);
|
||||
|
@ -219,7 +219,7 @@ public class CSVLexerTest {
|
|||
* \,,
|
||||
*/
|
||||
final String code = "a,\\,,b\\\\\n\\,,\\\nc,d\\\r\ne";
|
||||
final CSVFormat format = formatWithEscaping.toBuilder().withIgnoreEmptyLines(false).build();
|
||||
final CSVFormat format = formatWithEscaping.withIgnoreEmptyLines(false);
|
||||
assertTrue(format.isEscaping());
|
||||
final Lexer parser = getLexer(code, format);
|
||||
|
||||
|
@ -241,7 +241,7 @@ public class CSVLexerTest {
|
|||
* a, " foo " ,b
|
||||
*/
|
||||
final String code = "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na, \" foo \" ,b";
|
||||
final Lexer parser = getLexer(code, CSVFormat.newBuilder().withIgnoreSurroundingSpaces(true).build());
|
||||
final Lexer parser = getLexer(code, CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "foo"));
|
||||
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b"));
|
||||
|
@ -279,7 +279,7 @@ public class CSVLexerTest {
|
|||
* ;;
|
||||
*/
|
||||
final String code = "a;'b and '' more\n'\n!comment;;;;\n;;";
|
||||
final CSVFormat format = CSVFormat.newBuilder().withDelimiter(';').withQuoteChar('\'').withCommentStart('!').build();
|
||||
final CSVFormat format = CSVFormat.DEFAULT.withQuoteChar('\'').withCommentStart('!').withDelimiter(';');
|
||||
final Lexer parser = getLexer(code, format);
|
||||
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
|
||||
assertThat(parser.nextToken(new Token()), matches(EORECORD, "b and ' more\n"));
|
||||
|
@ -344,13 +344,13 @@ public class CSVLexerTest {
|
|||
@Test
|
||||
public void testEscapedControlCharacter() throws Exception {
|
||||
// we are explicitly using an escape different from \ here
|
||||
final Lexer lexer = getLexer("character!rEscaped", CSVFormat.newBuilder().withEscape('!').build());
|
||||
final Lexer lexer = getLexer("character!rEscaped", CSVFormat.DEFAULT.withEscape('!'));
|
||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + CR + "Escaped"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscapedControlCharacter2() throws Exception {
|
||||
final Lexer lexer = getLexer("character\\rEscaped", CSVFormat.newBuilder().withEscape('\\').build());
|
||||
final Lexer lexer = getLexer("character\\rEscaped", CSVFormat.DEFAULT.withEscape('\\'));
|
||||
assertThat(lexer.nextToken(new Token()), hasContent("character" + CR + "Escaped"));
|
||||
}
|
||||
|
||||
|
|
|
@ -71,7 +71,7 @@ public class CSVParserTest {
|
|||
|
||||
@Test
|
||||
public void testGetLine() throws IOException {
|
||||
final CSVParser parser = new CSVParser(new StringReader(CSVINPUT), CSVFormat.newBuilder().withIgnoreSurroundingSpaces(true).build());
|
||||
final CSVParser parser = new CSVParser(new StringReader(CSVINPUT), CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
|
||||
for (final String[] re : RESULT) {
|
||||
assertArrayEquals(re, parser.nextRecord().values());
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ public class CSVParserTest {
|
|||
|
||||
@Test
|
||||
public void testGetRecords() throws IOException {
|
||||
final CSVParser parser = new CSVParser(new StringReader(CSVINPUT), CSVFormat.newBuilder().withIgnoreSurroundingSpaces(true).build());
|
||||
final CSVParser parser = new CSVParser(new StringReader(CSVINPUT), CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(true));
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
assertEquals(RESULT.length, records.size());
|
||||
assertTrue(records.size() > 0);
|
||||
|
@ -312,8 +312,8 @@ public class CSVParserTest {
|
|||
};
|
||||
|
||||
|
||||
final CSVFormat format = CSVFormat.newBuilder(',').withQuoteChar('\'').withEscape('/')
|
||||
.withIgnoreEmptyLines(true).withRecordSeparator(CRLF).build();
|
||||
final CSVFormat format = CSVFormat.newFormat(',').withQuoteChar('\'')
|
||||
.withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
|
||||
|
||||
final CSVParser parser = new CSVParser(code, format);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
|
@ -341,8 +341,8 @@ public class CSVParserTest {
|
|||
};
|
||||
|
||||
|
||||
final CSVFormat format = CSVFormat.newBuilder(',').withEscape('/')
|
||||
.withIgnoreEmptyLines(true).withRecordSeparator(CRLF).build();
|
||||
final CSVFormat format = CSVFormat.newFormat(',')
|
||||
.withRecordSeparator(CRLF).withEscape('/').withIgnoreEmptyLines(true);
|
||||
|
||||
final CSVParser parser = new CSVParser(code, format);
|
||||
final List<CSVRecord> records = parser.getRecords();
|
||||
|
@ -380,7 +380,7 @@ public class CSVParserTest {
|
|||
{"\n", " ", "#"},
|
||||
};
|
||||
|
||||
format = CSVFormat.newBuilder().withCommentStart('#').build();
|
||||
format = CSVFormat.DEFAULT.withCommentStart('#');
|
||||
parser = new CSVParser(code, format);
|
||||
records = parser.getRecords();
|
||||
|
||||
|
@ -481,7 +481,7 @@ public class CSVParserTest {
|
|||
public void testHeader() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
|
||||
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader().parse(in).iterator();
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertTrue(records.hasNext());
|
||||
|
@ -498,7 +498,7 @@ public class CSVParserTest {
|
|||
public void testHeaderComment() throws Exception {
|
||||
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
|
||||
|
||||
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withCommentStart('#').withHeader().parse(in).iterator();
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withCommentStart('#').withHeader().parse(in).iterator();
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertTrue(records.hasNext());
|
||||
|
@ -515,7 +515,7 @@ public class CSVParserTest {
|
|||
public void testProvidedHeader() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
|
||||
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").parse(in).iterator();
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator();
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
assertTrue(records.hasNext());
|
||||
|
@ -536,7 +536,7 @@ public class CSVParserTest {
|
|||
public void testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
|
||||
|
||||
final Iterator<CSVRecord> records = CSVFormat.newBuilder().withHeader("A", "B", "C").parse(in).iterator();
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator();
|
||||
|
||||
// header record
|
||||
assertTrue(records.hasNext());
|
||||
|
@ -582,7 +582,7 @@ public class CSVParserTest {
|
|||
|
||||
@Test
|
||||
public void testGetHeaderMap() throws Exception {
|
||||
final CSVParser parser = new CSVParser("a,b,c\n1,2,3\nx,y,z", CSVFormat.newBuilder().withHeader("A", "B", "C").build());
|
||||
final CSVParser parser = new CSVParser("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader("A", "B", "C"));
|
||||
final Map<String, Integer> headerMap = parser.getHeaderMap();
|
||||
final Iterator<String> columnNames = headerMap.keySet().iterator();
|
||||
// Headers are iterated in column order.
|
||||
|
@ -626,7 +626,7 @@ public class CSVParserTest {
|
|||
@Test
|
||||
public void testGetRecordWithMultiiLineValues() throws Exception {
|
||||
final CSVParser parser = new CSVParser("\"a\r\n1\",\"a\r\n2\"" + CRLF + "\"b\r\n1\",\"b\r\n2\"" + CRLF + "\"c\r\n1\",\"c\r\n2\"",
|
||||
CSVFormat.newBuilder().withRecordSeparator(CRLF).build());
|
||||
CSVFormat.DEFAULT.withRecordSeparator(CRLF));
|
||||
CSVRecord record;
|
||||
assertEquals(0, parser.getRecordNumber());
|
||||
assertEquals(0, parser.getCurrentLineNumber());
|
||||
|
@ -657,8 +657,14 @@ public class CSVParserTest {
|
|||
validateRecordNumbers(String.valueOf(CR));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testInvalidFormat() throws Exception {
|
||||
CSVFormat invalidFormat = CSVFormat.DEFAULT.withDelimiter(CR);
|
||||
new CSVParser((Reader) null, invalidFormat);
|
||||
}
|
||||
|
||||
private void validateRecordNumbers(final String lineSeparator) throws IOException {
|
||||
final CSVParser parser = new CSVParser("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.newBuilder().withRecordSeparator(lineSeparator).build());
|
||||
final CSVParser parser = new CSVParser("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
|
||||
CSVRecord record;
|
||||
assertEquals(0, parser.getRecordNumber());
|
||||
assertNotNull(record = parser.nextRecord());
|
||||
|
@ -675,7 +681,7 @@ public class CSVParserTest {
|
|||
}
|
||||
|
||||
private void validateLineNumbers(final String lineSeparator) throws IOException {
|
||||
final CSVParser parser = new CSVParser("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.newBuilder().withRecordSeparator(lineSeparator).build());
|
||||
final CSVParser parser = new CSVParser("a" + lineSeparator + "b" + lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator));
|
||||
assertEquals(0, parser.getCurrentLineNumber());
|
||||
assertNotNull(parser.nextRecord());
|
||||
assertEquals(1, parser.getCurrentLineNumber());
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.apache.commons.csv;
|
||||
|
||||
import static org.apache.commons.csv.Constants.CR;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
|
@ -231,7 +232,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testMultiLineComment() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withCommentStart('#').build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
|
||||
printer.printComment("This is a comment\non multiple lines");
|
||||
|
||||
assertEquals("# This is a comment" + recordSeparator + "# on multiple lines" + recordSeparator, sw.toString());
|
||||
|
@ -313,7 +314,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPrintCustomNullValues() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.toBuilder().withNullString("NULL").build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withNullString("NULL"));
|
||||
printer.printRecord("a", null, "b");
|
||||
assertEquals("a,NULL,b" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -322,7 +323,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testParseCustomNullValues() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVFormat format = CSVFormat.DEFAULT.toBuilder().withNullString("NULL").build();
|
||||
final CSVFormat format = CSVFormat.DEFAULT.withNullString("NULL");
|
||||
final CSVPrinter printer = new CSVPrinter(sw, format);
|
||||
printer.printRecord("a", null, "b");
|
||||
printer.close();
|
||||
|
@ -340,7 +341,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testQuoteAll() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuotePolicy(Quote.ALL).build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuotePolicy(Quote.ALL));
|
||||
printer.printRecord("a", "b\nc", "d");
|
||||
assertEquals("\"a\",\"b\nc\",\"d\"" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -349,7 +350,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testQuoteNonNumeric() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuotePolicy(Quote.NON_NUMERIC).build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuotePolicy(Quote.NON_NUMERIC));
|
||||
printer.printRecord("a", "b\nc", Integer.valueOf(1));
|
||||
assertEquals("\"a\",\"b\nc\",1" + recordSeparator, sw.toString());
|
||||
printer.close();
|
||||
|
@ -366,7 +367,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPlainQuoted() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuoteChar('\'').build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar('\''));
|
||||
printer.print("abc");
|
||||
assertEquals("abc", sw.toString());
|
||||
printer.close();
|
||||
|
@ -375,7 +376,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testSingleLineComment() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withCommentStart('#').build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
|
||||
printer.printComment("This is a comment");
|
||||
|
||||
assertEquals("# This is a comment" + recordSeparator, sw.toString());
|
||||
|
@ -385,7 +386,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testSingleQuoteQuoted() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuoteChar('\'').build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar('\''));
|
||||
printer.print("a'b'c");
|
||||
printer.print("xyz");
|
||||
assertEquals("'a''b''c',xyz", sw.toString());
|
||||
|
@ -395,7 +396,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testDelimeterQuoted() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuoteChar('\'').build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar('\''));
|
||||
printer.print("a,b,c");
|
||||
printer.print("xyz");
|
||||
assertEquals("'a,b,c',xyz", sw.toString());
|
||||
|
@ -405,7 +406,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testDelimeterQuoteNONE() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVFormat format = CSVFormat.newBuilder().withEscape('!').withQuotePolicy(Quote.NONE).build();
|
||||
final CSVFormat format = CSVFormat.DEFAULT.withEscape('!').withQuotePolicy(Quote.NONE);
|
||||
final CSVPrinter printer = new CSVPrinter(sw, format);
|
||||
printer.print("a,b,c");
|
||||
printer.print("xyz");
|
||||
|
@ -416,7 +417,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testEOLQuoted() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuoteChar('\'').build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar('\''));
|
||||
printer.print("a\rb\nc");
|
||||
printer.print("x\by\fz");
|
||||
assertEquals("'a\rb\nc',x\by\fz", sw.toString());
|
||||
|
@ -426,7 +427,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPlainEscaped() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuoteChar(null).withEscape('!').build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar(null).withEscape('!'));
|
||||
printer.print("abc");
|
||||
printer.print("xyz");
|
||||
assertEquals("abc,xyz", sw.toString());
|
||||
|
@ -436,7 +437,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testDelimiterEscaped() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuoteChar(null).withEscape('!').build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape('!').withQuoteChar(null));
|
||||
printer.print("a,b,c");
|
||||
printer.print("xyz");
|
||||
assertEquals("a!,b!,c,xyz", sw.toString());
|
||||
|
@ -446,7 +447,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testEOLEscaped() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuoteChar(null).withEscape('!').build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar(null).withEscape('!'));
|
||||
printer.print("a\rb\nc");
|
||||
printer.print("x\fy\bz");
|
||||
assertEquals("a!rb!nc,x\fy\bz", sw.toString());
|
||||
|
@ -456,7 +457,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testPlainPlain() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuoteChar(null).build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar(null));
|
||||
printer.print("abc");
|
||||
printer.print("xyz");
|
||||
assertEquals("abc,xyz", sw.toString());
|
||||
|
@ -466,7 +467,7 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testDelimiterPlain() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuoteChar(null).build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar(null));
|
||||
printer.print("a,b,c");
|
||||
printer.print("xyz");
|
||||
assertEquals("a,b,c,xyz", sw.toString());
|
||||
|
@ -476,11 +477,16 @@ public class CSVPrinterTest {
|
|||
@Test
|
||||
public void testEOLPlain() throws IOException {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.newBuilder().withQuoteChar(null).build());
|
||||
final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuoteChar(null));
|
||||
printer.print("a\rb\nc");
|
||||
printer.print("x\fy\bz");
|
||||
assertEquals("a\rb\nc,x\fy\bz", sw.toString());
|
||||
printer.close();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testInvalidFormat() throws Exception {
|
||||
CSVFormat invalidFormat = CSVFormat.DEFAULT.withDelimiter(CR);
|
||||
new CSVPrinter(null, invalidFormat);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ public class PerformanceTest {
|
|||
}
|
||||
|
||||
private long parse(final Reader in, final boolean traverseColumns) throws IOException {
|
||||
final CSVFormat format = CSVFormat.newBuilder().withIgnoreSurroundingSpaces(false).build();
|
||||
final CSVFormat format = CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false);
|
||||
long recordCount = 0;
|
||||
for (final CSVRecord record : format.parse(in)) {
|
||||
recordCount++;
|
||||
|
|
Loading…
Reference in New Issue