Remove trailing spaces.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1383577 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0638a37368
commit
bf06bed9b8
|
@ -33,7 +33,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/** According to RFC 4180, line breaks are delimited by CRLF */
|
/** According to RFC 4180, line breaks are delimited by CRLF */
|
||||||
public static final String CRLF = "\r\n";
|
public static final String CRLF = "\r\n";
|
||||||
|
|
||||||
private final char delimiter;
|
private final char delimiter;
|
||||||
private final char encapsulator;
|
private final char encapsulator;
|
||||||
private final char commentStart;
|
private final char commentStart;
|
||||||
|
@ -60,14 +60,14 @@ public class CSVFormat implements Serializable {
|
||||||
*/
|
*/
|
||||||
static final CSVFormat PRISTINE = new CSVFormat(DISABLED, DISABLED, DISABLED, DISABLED, false, false, null, null);
|
static final CSVFormat PRISTINE = new CSVFormat(DISABLED, DISABLED, DISABLED, DISABLED, false, false, null, null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Standard comma separated format, as for {@link #RFC4180} but allowing blank lines.
|
* Standard comma separated format, as for {@link #RFC4180} but allowing blank lines.
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>withDelimiter(',')</li>
|
* <li>withDelimiter(',')</li>
|
||||||
* <li>withEncapsulator('"')</li>
|
* <li>withEncapsulator('"')</li>
|
||||||
* <li>withEmptyLinesIgnored(true)</li>
|
* <li>withEmptyLinesIgnored(true)</li>
|
||||||
* <li>withLineSeparator(CRLF)</li>
|
* <li>withLineSeparator(CRLF)</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
public static final CSVFormat DEFAULT =
|
public static final CSVFormat DEFAULT =
|
||||||
PRISTINE.
|
PRISTINE.
|
||||||
|
@ -83,7 +83,7 @@ public class CSVFormat implements Serializable {
|
||||||
* <li>withEncapsulator('"')</li>
|
* <li>withEncapsulator('"')</li>
|
||||||
* <li>withLineSeparator(CRLF)</li>
|
* <li>withLineSeparator(CRLF)</li>
|
||||||
* <li></li>
|
* <li></li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
public static final CSVFormat RFC4180 =
|
public static final CSVFormat RFC4180 =
|
||||||
PRISTINE.
|
PRISTINE.
|
||||||
|
@ -99,7 +99,7 @@ public class CSVFormat implements Serializable {
|
||||||
* <p/>
|
* <p/>
|
||||||
* For example for parsing or generating a CSV file on a French system
|
* For example for parsing or generating a CSV file on a French system
|
||||||
* the following format will be used:
|
* the following format will be used:
|
||||||
*
|
*
|
||||||
* <pre>CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');</pre>
|
* <pre>CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');</pre>
|
||||||
*/
|
*/
|
||||||
public static final CSVFormat EXCEL =
|
public static final CSVFormat EXCEL =
|
||||||
|
@ -122,7 +122,7 @@ public class CSVFormat implements Serializable {
|
||||||
* <tt>LOAD DATA INFILE</tt> operations. This is a tab-delimited
|
* <tt>LOAD DATA INFILE</tt> operations. This is a tab-delimited
|
||||||
* format with a LF character as the line separator. Values are not quoted
|
* format with a LF character as the line separator. Values are not quoted
|
||||||
* and special characters are escaped with '\'.
|
* and special characters are escaped with '\'.
|
||||||
*
|
*
|
||||||
* @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html">http://dev.mysql.com/doc/refman/5.1/en/load-data.html</a>
|
* @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html">http://dev.mysql.com/doc/refman/5.1/en/load-data.html</a>
|
||||||
*/
|
*/
|
||||||
public static final CSVFormat MYSQL =
|
public static final CSVFormat MYSQL =
|
||||||
|
@ -168,9 +168,9 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the given character is a line break character.
|
* Returns true if the given character is a line break character.
|
||||||
*
|
*
|
||||||
* @param c the character to check
|
* @param c the character to check
|
||||||
*
|
*
|
||||||
* @return true if <code>c</code> is a line break character
|
* @return true if <code>c</code> is a line break character
|
||||||
*/
|
*/
|
||||||
private static boolean isLineBreak(char c) {
|
private static boolean isLineBreak(char c) {
|
||||||
|
@ -184,19 +184,19 @@ public class CSVFormat implements Serializable {
|
||||||
if (delimiter == encapsulator) {
|
if (delimiter == encapsulator) {
|
||||||
throw new IllegalArgumentException("The encapsulator character and the delimiter cannot be the same (\"" + encapsulator + "\")");
|
throw new IllegalArgumentException("The encapsulator character and the delimiter cannot be the same (\"" + encapsulator + "\")");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (delimiter == escape) {
|
if (delimiter == escape) {
|
||||||
throw new IllegalArgumentException("The escape character and the delimiter cannot be the same (\"" + escape + "\")");
|
throw new IllegalArgumentException("The escape character and the delimiter cannot be the same (\"" + escape + "\")");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (delimiter == commentStart) {
|
if (delimiter == commentStart) {
|
||||||
throw new IllegalArgumentException("The comment start character and the delimiter cannot be the same (\"" + commentStart + "\")");
|
throw new IllegalArgumentException("The comment start character and the delimiter cannot be the same (\"" + commentStart + "\")");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (encapsulator != DISABLED && encapsulator == commentStart) {
|
if (encapsulator != DISABLED && encapsulator == commentStart) {
|
||||||
throw new IllegalArgumentException("The comment start character and the encapsulator cannot be the same (\"" + commentStart + "\")");
|
throw new IllegalArgumentException("The comment start character and the encapsulator cannot be the same (\"" + commentStart + "\")");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (escape != DISABLED && escape == commentStart) {
|
if (escape != DISABLED && escape == commentStart) {
|
||||||
throw new IllegalArgumentException("The comment start and the escape character cannot be the same (\"" + commentStart + "\")");
|
throw new IllegalArgumentException("The comment start and the escape character cannot be the same (\"" + commentStart + "\")");
|
||||||
}
|
}
|
||||||
|
@ -204,7 +204,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the character delimiting the values (typically ';', ',' or '\t').
|
* Returns the character delimiting the values (typically ';', ',' or '\t').
|
||||||
*
|
*
|
||||||
* @return the delimiter character
|
* @return the delimiter character
|
||||||
*/
|
*/
|
||||||
public char getDelimiter() {
|
public char getDelimiter() {
|
||||||
|
@ -213,7 +213,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a copy of this format using the specified delimiter character.
|
* Returns a copy of this format using the specified delimiter character.
|
||||||
*
|
*
|
||||||
* @param delimiter the delimiter character
|
* @param delimiter the delimiter character
|
||||||
* @return A copy of this format using the specified delimiter character
|
* @return A copy of this format using the specified delimiter character
|
||||||
* @throws IllegalArgumentException thrown if the specified character is a line break
|
* @throws IllegalArgumentException thrown if the specified character is a line break
|
||||||
|
@ -228,7 +228,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the character used to encapsulate values containing special characters.
|
* Returns the character used to encapsulate values containing special characters.
|
||||||
*
|
*
|
||||||
* @return the encapsulator character
|
* @return the encapsulator character
|
||||||
*/
|
*/
|
||||||
public char getEncapsulator() {
|
public char getEncapsulator() {
|
||||||
|
@ -237,7 +237,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a copy of this format using the specified encapsulator character.
|
* Returns a copy of this format using the specified encapsulator character.
|
||||||
*
|
*
|
||||||
* @param encapsulator the encapsulator character
|
* @param encapsulator the encapsulator character
|
||||||
* @return A copy of this format using the specified encapsulator character
|
* @return A copy of this format using the specified encapsulator character
|
||||||
* @throws IllegalArgumentException thrown if the specified character is a line break
|
* @throws IllegalArgumentException thrown if the specified character is a line break
|
||||||
|
@ -246,13 +246,13 @@ public class CSVFormat implements Serializable {
|
||||||
if (isLineBreak(encapsulator)) {
|
if (isLineBreak(encapsulator)) {
|
||||||
throw new IllegalArgumentException("The encapsulator cannot be a line break");
|
throw new IllegalArgumentException("The encapsulator cannot be a line break");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CSVFormat(delimiter, encapsulator, commentStart, escape, surroundingSpacesIgnored, emptyLinesIgnored, lineSeparator, header);
|
return new CSVFormat(delimiter, encapsulator, commentStart, escape, surroundingSpacesIgnored, emptyLinesIgnored, lineSeparator, header);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether an encapsulator has been defined.
|
* Returns whether an encapsulator has been defined.
|
||||||
*
|
*
|
||||||
* @return {@code true} if an encapsulator is defined
|
* @return {@code true} if an encapsulator is defined
|
||||||
*/
|
*/
|
||||||
public boolean isEncapsulating() {
|
public boolean isEncapsulating() {
|
||||||
|
@ -261,7 +261,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the character marking the start of a line comment.
|
* Returns the character marking the start of a line comment.
|
||||||
*
|
*
|
||||||
* @return the comment start marker.
|
* @return the comment start marker.
|
||||||
*/
|
*/
|
||||||
public char getCommentStart() {
|
public char getCommentStart() {
|
||||||
|
@ -270,10 +270,10 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a copy of this format using the specified character as the comment start marker.
|
* Returns a copy of this format using the specified character as the comment start marker.
|
||||||
*
|
*
|
||||||
* Note that the comment introducer character is only recognised
|
* Note that the comment introducer character is only recognised
|
||||||
* at the start of a line.
|
* at the start of a line.
|
||||||
*
|
*
|
||||||
* @param commentStart the comment start marker
|
* @param commentStart the comment start marker
|
||||||
* @return A copy of this format using the specified character as the comment start marker
|
* @return A copy of this format using the specified character as the comment start marker
|
||||||
* @throws IllegalArgumentException thrown if the specified character is a line break
|
* @throws IllegalArgumentException thrown if the specified character is a line break
|
||||||
|
@ -282,16 +282,16 @@ public class CSVFormat implements Serializable {
|
||||||
if (isLineBreak(commentStart)) {
|
if (isLineBreak(commentStart)) {
|
||||||
throw new IllegalArgumentException("The comment start character cannot be a line break");
|
throw new IllegalArgumentException("The comment start character cannot be a line break");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CSVFormat(delimiter, encapsulator, commentStart, escape, surroundingSpacesIgnored, emptyLinesIgnored, lineSeparator, header);
|
return new CSVFormat(delimiter, encapsulator, commentStart, escape, surroundingSpacesIgnored, emptyLinesIgnored, lineSeparator, header);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specifies whether comments are supported by this format.
|
* Specifies whether comments are supported by this format.
|
||||||
*
|
*
|
||||||
* Note that the comment introducer character is only recognised
|
* Note that the comment introducer character is only recognised
|
||||||
* at the start of a line.
|
* at the start of a line.
|
||||||
*
|
*
|
||||||
* @return <tt>true</tt> is comments are supported, <tt>false</tt> otherwise
|
* @return <tt>true</tt> is comments are supported, <tt>false</tt> otherwise
|
||||||
*/
|
*/
|
||||||
public boolean isCommentingEnabled() {
|
public boolean isCommentingEnabled() {
|
||||||
|
@ -300,7 +300,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the escape character.
|
* Returns the escape character.
|
||||||
*
|
*
|
||||||
* @return the escape character
|
* @return the escape character
|
||||||
*/
|
*/
|
||||||
public char getEscape() {
|
public char getEscape() {
|
||||||
|
@ -309,7 +309,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a copy of this format using the specified escape character.
|
* Returns a copy of this format using the specified escape character.
|
||||||
*
|
*
|
||||||
* @param escape the escape character
|
* @param escape the escape character
|
||||||
* @return A copy of this format using the specified escape character
|
* @return A copy of this format using the specified escape character
|
||||||
* @throws IllegalArgumentException thrown if the specified character is a line break
|
* @throws IllegalArgumentException thrown if the specified character is a line break
|
||||||
|
@ -318,13 +318,13 @@ public class CSVFormat implements Serializable {
|
||||||
if (isLineBreak(escape)) {
|
if (isLineBreak(escape)) {
|
||||||
throw new IllegalArgumentException("The escape character cannot be a line break");
|
throw new IllegalArgumentException("The escape character cannot be a line break");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CSVFormat(delimiter, encapsulator, commentStart, escape, surroundingSpacesIgnored, emptyLinesIgnored, lineSeparator, header);
|
return new CSVFormat(delimiter, encapsulator, commentStart, escape, surroundingSpacesIgnored, emptyLinesIgnored, lineSeparator, header);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether escape are being processed.
|
* Returns whether escape are being processed.
|
||||||
*
|
*
|
||||||
* @return {@code true} if escapes are processed
|
* @return {@code true} if escapes are processed
|
||||||
*/
|
*/
|
||||||
public boolean isEscaping() {
|
public boolean isEscaping() {
|
||||||
|
@ -333,7 +333,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specifies whether spaces around values are ignored when parsing input.
|
* Specifies whether spaces around values are ignored when parsing input.
|
||||||
*
|
*
|
||||||
* @return <tt>true</tt> if spaces around values are ignored, <tt>false</tt> if they are treated as part of the value.
|
* @return <tt>true</tt> if spaces around values are ignored, <tt>false</tt> if they are treated as part of the value.
|
||||||
*/
|
*/
|
||||||
public boolean isSurroundingSpacesIgnored() {
|
public boolean isSurroundingSpacesIgnored() {
|
||||||
|
@ -353,7 +353,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specifies whether empty lines between records are ignored when parsing input.
|
* Specifies whether empty lines between records are ignored when parsing input.
|
||||||
*
|
*
|
||||||
* @return <tt>true</tt> if empty lines between records are ignored, <tt>false</tt> if they are turned into empty records.
|
* @return <tt>true</tt> if empty lines between records are ignored, <tt>false</tt> if they are turned into empty records.
|
||||||
*/
|
*/
|
||||||
public boolean isEmptyLinesIgnored() {
|
public boolean isEmptyLinesIgnored() {
|
||||||
|
@ -373,7 +373,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the line separator delimiting output records.
|
* Returns the line separator delimiting output records.
|
||||||
*
|
*
|
||||||
* @return the line separator
|
* @return the line separator
|
||||||
*/
|
*/
|
||||||
public String getLineSeparator() {
|
public String getLineSeparator() {
|
||||||
|
@ -382,9 +382,9 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a copy of this format using the specified output line separator.
|
* Returns a copy of this format using the specified output line separator.
|
||||||
*
|
*
|
||||||
* @param lineSeparator the line separator to be used for output.
|
* @param lineSeparator the line separator to be used for output.
|
||||||
*
|
*
|
||||||
* @return A copy of this format using the specified output line separator
|
* @return A copy of this format using the specified output line separator
|
||||||
*/
|
*/
|
||||||
public CSVFormat withLineSeparator(String lineSeparator) {
|
public CSVFormat withLineSeparator(String lineSeparator) {
|
||||||
|
@ -415,7 +415,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the specified content.
|
* Parses the specified content.
|
||||||
*
|
*
|
||||||
* @param in the input stream
|
* @param in the input stream
|
||||||
*/
|
*/
|
||||||
public Iterable<CSVRecord> parse(Reader in) throws IOException {
|
public Iterable<CSVRecord> parse(Reader in) throws IOException {
|
||||||
|
@ -424,7 +424,7 @@ public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format the specified values.
|
* Format the specified values.
|
||||||
*
|
*
|
||||||
* @param values the values to format
|
* @param values the values to format
|
||||||
*/
|
*/
|
||||||
public String format(String... values) {
|
public String format(String... values) {
|
||||||
|
@ -435,7 +435,7 @@ public class CSVFormat implements Serializable {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// should not happen
|
// should not happen
|
||||||
throw new IllegalStateException(e);
|
throw new IllegalStateException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -448,19 +448,19 @@ public class CSVFormat implements Serializable {
|
||||||
}
|
}
|
||||||
if (isEncapsulating()) {
|
if (isEncapsulating()) {
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
sb.append("Encapsulator=<").append(encapsulator).append('>');
|
sb.append("Encapsulator=<").append(encapsulator).append('>');
|
||||||
}
|
}
|
||||||
if (isCommentingEnabled()) {
|
if (isCommentingEnabled()) {
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
sb.append("CommentStart=<").append(commentStart).append('>');
|
sb.append("CommentStart=<").append(commentStart).append('>');
|
||||||
}
|
}
|
||||||
if (isEmptyLinesIgnored()) {
|
if (isEmptyLinesIgnored()) {
|
||||||
sb.append(" EmptyLines:ignored");
|
sb.append(" EmptyLines:ignored");
|
||||||
}
|
}
|
||||||
if (isSurroundingSpacesIgnored()) {
|
if (isSurroundingSpacesIgnored()) {
|
||||||
sb.append(" SurroundingSpaces:ignored");
|
sb.append(" SurroundingSpaces:ignored");
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ class CSVLexer extends Lexer {
|
||||||
public CSVLexer(CSVFormat format, ExtendedBufferedReader in) {
|
public CSVLexer(CSVFormat format, ExtendedBufferedReader in) {
|
||||||
super(format, in);
|
super(format, in);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the next token.
|
* Returns the next token.
|
||||||
* <p/>
|
* <p/>
|
||||||
|
@ -92,7 +92,7 @@ class CSVLexer extends Lexer {
|
||||||
eol = isEndOfLine(c);
|
eol = isEndOfLine(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ok, start of token reached: encapsulated, or token
|
// ok, start of token reached: encapsulated, or token
|
||||||
if (isDelimiter(c)) {
|
if (isDelimiter(c)) {
|
||||||
// empty token return TOKEN("")
|
// empty token return TOKEN("")
|
||||||
|
@ -183,7 +183,7 @@ class CSVLexer extends Lexer {
|
||||||
*
|
*
|
||||||
* @param tkn the current token
|
* @param tkn the current token
|
||||||
* @return a valid token object
|
* @return a valid token object
|
||||||
* @throws IOException on invalid state:
|
* @throws IOException on invalid state:
|
||||||
* EOF before closing encapsulator or invalid character before delimiter or EOL
|
* EOF before closing encapsulator or invalid character before delimiter or EOL
|
||||||
*/
|
*/
|
||||||
private Token encapsulatedTokenLexer(Token tkn) throws IOException {
|
private Token encapsulatedTokenLexer(Token tkn) throws IOException {
|
||||||
|
@ -192,7 +192,7 @@ class CSVLexer extends Lexer {
|
||||||
int c;
|
int c;
|
||||||
while (true) {
|
while (true) {
|
||||||
c = in.read();
|
c = in.read();
|
||||||
|
|
||||||
if (isEscape(c)) {
|
if (isEscape(c)) {
|
||||||
tkn.content.append((char) readEscape());
|
tkn.content.append((char) readEscape());
|
||||||
} else if (isEncapsulator(c)) {
|
} else if (isEncapsulator(c)) {
|
||||||
|
|
|
@ -66,7 +66,7 @@ public class CSVParser implements Iterable<CSVRecord> {
|
||||||
private final Map<String, Integer> headerMapping;
|
private final Map<String, Integer> headerMapping;
|
||||||
|
|
||||||
// the following objects are shared to reduce garbage
|
// the following objects are shared to reduce garbage
|
||||||
|
|
||||||
/** A record buffer for getRecord(). Grows as necessary and is reused. */
|
/** A record buffer for getRecord(). Grows as necessary and is reused. */
|
||||||
private final List<String> record = new ArrayList<String>();
|
private final List<String> record = new ArrayList<String>();
|
||||||
private final Token reusableToken = new Token();
|
private final Token reusableToken = new Token();
|
||||||
|
@ -90,9 +90,9 @@ public class CSVParser implements Iterable<CSVRecord> {
|
||||||
*/
|
*/
|
||||||
public CSVParser(Reader input, CSVFormat format) throws IOException {
|
public CSVParser(Reader input, CSVFormat format) throws IOException {
|
||||||
format.validate();
|
format.validate();
|
||||||
|
|
||||||
this.lexer = new CSVLexer(format, new ExtendedBufferedReader(input));
|
this.lexer = new CSVLexer(format, new ExtendedBufferedReader(input));
|
||||||
|
|
||||||
this.headerMapping = initializeHeader(format);
|
this.headerMapping = initializeHeader(format);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,9 +166,9 @@ public class CSVParser implements Iterable<CSVRecord> {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (reusableToken.type == TOKEN);
|
} while (reusableToken.type == TOKEN);
|
||||||
|
|
||||||
if (!record.isEmpty()) {
|
if (!record.isEmpty()) {
|
||||||
result = new CSVRecord(record.toArray(new String[record.size()]), headerMapping,
|
result = new CSVRecord(record.toArray(new String[record.size()]), headerMapping,
|
||||||
sb == null ? null : sb.toString());
|
sb == null ? null : sb.toString());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -210,12 +210,12 @@ public class CSVParser implements Iterable<CSVRecord> {
|
||||||
public Iterator<CSVRecord> iterator() {
|
public Iterator<CSVRecord> iterator() {
|
||||||
return new Iterator<CSVRecord>() {
|
return new Iterator<CSVRecord>() {
|
||||||
private CSVRecord current;
|
private CSVRecord current;
|
||||||
|
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
if (current == null) {
|
if (current == null) {
|
||||||
current = getNextRecord();
|
current = getNextRecord();
|
||||||
}
|
}
|
||||||
|
|
||||||
return current != null;
|
return current != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,10 +230,10 @@ public class CSVParser implements Iterable<CSVRecord> {
|
||||||
throw new NoSuchElementException("No more CSV records available");
|
throw new NoSuchElementException("No more CSV records available");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CSVRecord getNextRecord() {
|
private CSVRecord getNextRecord() {
|
||||||
try {
|
try {
|
||||||
return getRecord();
|
return getRecord();
|
||||||
|
|
|
@ -62,7 +62,7 @@ public class CSVPrinter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flush the underlying stream.
|
* Flush the underlying stream.
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
|
@ -127,7 +127,7 @@ public class CSVPrinter {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void print(CharSequence value, int offset, int len) throws IOException {
|
private void print(CharSequence value, int offset, int len) throws IOException {
|
||||||
if (format.isEncapsulating()) {
|
if (format.isEncapsulating()) {
|
||||||
printAndEncapsulate(value, offset, len);
|
printAndEncapsulate(value, offset, len);
|
||||||
} else if (format.isEscaping()) {
|
} else if (format.isEscaping()) {
|
||||||
|
@ -284,7 +284,7 @@ public class CSVPrinter {
|
||||||
// null values are considered empty
|
// null values are considered empty
|
||||||
value = "";
|
value = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!checkForEscape) {
|
if (!checkForEscape) {
|
||||||
// write directly from string
|
// write directly from string
|
||||||
printSep();
|
printSep();
|
||||||
|
|
|
@ -26,11 +26,11 @@ import java.util.Map;
|
||||||
* A CSV record
|
* A CSV record
|
||||||
*/
|
*/
|
||||||
public class CSVRecord implements Serializable, Iterable<String> {
|
public class CSVRecord implements Serializable, Iterable<String> {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||||
|
|
||||||
/** The values of the record */
|
/** The values of the record */
|
||||||
private final String[] values;
|
private final String[] values;
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,7 @@ class ExtendedBufferedReader extends BufferedReader {
|
||||||
* character has been read then this will return {@link #UNDEFINED}. If the
|
* character has been read then this will return {@link #UNDEFINED}. If the
|
||||||
* end of the stream was reached on the last read then this will return
|
* end of the stream was reached on the last read then this will return
|
||||||
* {@link #END_OF_STREAM}.
|
* {@link #END_OF_STREAM}.
|
||||||
*
|
*
|
||||||
* @return the last character that was read
|
* @return the last character that was read
|
||||||
*/
|
*/
|
||||||
int readAgain() {
|
int readAgain() {
|
||||||
|
@ -83,16 +83,16 @@ class ExtendedBufferedReader extends BufferedReader {
|
||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int len = super.read(buf, offset, length);
|
int len = super.read(buf, offset, length);
|
||||||
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
|
|
||||||
for (int i = offset; i < offset + len; i++) {
|
for (int i = offset; i < offset + len; i++) {
|
||||||
char ch = buf[i];
|
char ch = buf[i];
|
||||||
if (ch == LF) {
|
if (ch == LF) {
|
||||||
if (CR != (i > 0 ? buf[i-1]: lastChar)) {
|
if (CR != (i > 0 ? buf[i-1]: lastChar)) {
|
||||||
lineCounter++;
|
lineCounter++;
|
||||||
}
|
}
|
||||||
} else if (ch == CR) {
|
} else if (ch == CR) {
|
||||||
lineCounter++;
|
lineCounter++;
|
||||||
|
@ -104,7 +104,7 @@ class ExtendedBufferedReader extends BufferedReader {
|
||||||
} else if (len == -1) {
|
} else if (len == -1) {
|
||||||
lastChar = END_OF_STREAM;
|
lastChar = END_OF_STREAM;
|
||||||
}
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,9 +115,9 @@ class ExtendedBufferedReader extends BufferedReader {
|
||||||
* <p>
|
* <p>
|
||||||
* Increments {@link #lineCounter}
|
* Increments {@link #lineCounter}
|
||||||
* <p>
|
* <p>
|
||||||
* Sets {@link #lastChar} to {@link #END_OF_STREAM} at EOF,
|
* Sets {@link #lastChar} to {@link #END_OF_STREAM} at EOF,
|
||||||
* otherwise to LF
|
* otherwise to LF
|
||||||
*
|
*
|
||||||
* @return the line that was read, or null if reached EOF.
|
* @return the line that was read, or null if reached EOF.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@ -137,9 +137,9 @@ class ExtendedBufferedReader extends BufferedReader {
|
||||||
/**
|
/**
|
||||||
* Returns the next character in the current reader without consuming it. So
|
* Returns the next character in the current reader without consuming it. So
|
||||||
* the next call to {@link #read()} will still return this value.
|
* the next call to {@link #read()} will still return this value.
|
||||||
*
|
*
|
||||||
* @return the next character
|
* @return the next character
|
||||||
*
|
*
|
||||||
* @throws IOException if there is an error in reading
|
* @throws IOException if there is an error in reading
|
||||||
*/
|
*/
|
||||||
int lookAhead() throws IOException {
|
int lookAhead() throws IOException {
|
||||||
|
|
|
@ -27,17 +27,17 @@ abstract class Lexer {
|
||||||
private final boolean isEncapsulating;
|
private final boolean isEncapsulating;
|
||||||
private final boolean isEscaping;
|
private final boolean isEscaping;
|
||||||
private final boolean isCommentEnabled;
|
private final boolean isCommentEnabled;
|
||||||
|
|
||||||
private final char delimiter;
|
private final char delimiter;
|
||||||
private final char escape;
|
private final char escape;
|
||||||
private final char encapsulator;
|
private final char encapsulator;
|
||||||
private final char commmentStart;
|
private final char commmentStart;
|
||||||
|
|
||||||
final boolean surroundingSpacesIgnored;
|
final boolean surroundingSpacesIgnored;
|
||||||
final boolean emptyLinesIgnored;
|
final boolean emptyLinesIgnored;
|
||||||
|
|
||||||
final CSVFormat format;
|
final CSVFormat format;
|
||||||
|
|
||||||
/** The input stream */
|
/** The input stream */
|
||||||
final ExtendedBufferedReader in;
|
final ExtendedBufferedReader in;
|
||||||
|
|
||||||
|
@ -116,7 +116,7 @@ abstract class Lexer {
|
||||||
/**
|
/**
|
||||||
* Checks if the current character represents the start of a line:
|
* Checks if the current character represents the start of a line:
|
||||||
* a CR, LF or is at the start of the file.
|
* a CR, LF or is at the start of the file.
|
||||||
*
|
*
|
||||||
* @param c
|
* @param c
|
||||||
* @return true if the character is at the start of a line.
|
* @return true if the character is at the start of a line.
|
||||||
*/
|
*/
|
||||||
|
@ -131,7 +131,7 @@ abstract class Lexer {
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract Token nextToken(Token reusableToken) throws IOException;
|
abstract Token nextToken(Token reusableToken) throws IOException;
|
||||||
|
|
||||||
boolean isDelimiter(int c) {
|
boolean isDelimiter(int c) {
|
||||||
return c == delimiter;
|
return c == delimiter;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,30 +28,30 @@ class Token {
|
||||||
|
|
||||||
/** length of the initial token (content-)buffer */
|
/** length of the initial token (content-)buffer */
|
||||||
private static final int INITIAL_TOKEN_LENGTH = 50;
|
private static final int INITIAL_TOKEN_LENGTH = 50;
|
||||||
|
|
||||||
enum Type {
|
enum Type {
|
||||||
/** Token has no valid content, i.e. is in its initialized state. */
|
/** Token has no valid content, i.e. is in its initialized state. */
|
||||||
INVALID,
|
INVALID,
|
||||||
|
|
||||||
/** Token with content, at beginning or in the middle of a line. */
|
/** Token with content, at beginning or in the middle of a line. */
|
||||||
TOKEN,
|
TOKEN,
|
||||||
|
|
||||||
/** Token (which can have content) when end of file is reached. */
|
/** Token (which can have content) when end of file is reached. */
|
||||||
EOF,
|
EOF,
|
||||||
|
|
||||||
/** Token with content when end of a line is reached. */
|
/** Token with content when end of a line is reached. */
|
||||||
EORECORD,
|
EORECORD,
|
||||||
|
|
||||||
/** Token is a comment line */
|
/** Token is a comment line */
|
||||||
COMMENT
|
COMMENT
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Token type */
|
/** Token type */
|
||||||
Token.Type type = INVALID;
|
Token.Type type = INVALID;
|
||||||
|
|
||||||
/** The content buffer. */
|
/** The content buffer. */
|
||||||
StringBuilder content = new StringBuilder(INITIAL_TOKEN_LENGTH);
|
StringBuilder content = new StringBuilder(INITIAL_TOKEN_LENGTH);
|
||||||
|
|
||||||
/** Token ready flag: indicates a valid token with content (ready for the parser). */
|
/** Token ready flag: indicates a valid token with content (ready for the parser). */
|
||||||
boolean isReady;
|
boolean isReady;
|
||||||
|
|
||||||
|
|
|
@ -17,11 +17,11 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apache Commons CSV Format Support.
|
* Apache Commons CSV Format Support.
|
||||||
*
|
*
|
||||||
* <p>CSV (or its dialects) are widely used as interfaces to legacy systems or
|
* <p>CSV (or its dialects) are widely used as interfaces to legacy systems or
|
||||||
* manual data-imports. Basically CSV stands for "Comma Separated Values" but
|
* manual data-imports. Basically CSV stands for "Comma Separated Values" but
|
||||||
* this simple abbreviation leads to more confusion than definitions.</p>
|
* this simple abbreviation leads to more confusion than definitions.</p>
|
||||||
*
|
*
|
||||||
* <p>Common to all file dialects is its basic structure: The CSV data-format
|
* <p>Common to all file dialects is its basic structure: The CSV data-format
|
||||||
* is record oriented, whereas each record starts on a new textual line. A
|
* is record oriented, whereas each record starts on a new textual line. A
|
||||||
* record is build of a list of values. Keep in mind that not all records
|
* record is build of a list of values. Keep in mind that not all records
|
||||||
|
@ -30,41 +30,41 @@
|
||||||
* csv := records*
|
* csv := records*
|
||||||
* record := values*
|
* record := values*
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* <p>The following list contains the csv aspects the Commons CSV parser supports:</p>
|
* <p>The following list contains the csv aspects the Commons CSV parser supports:</p>
|
||||||
* <dl>
|
* <dl>
|
||||||
* <dt>Separators (for lines)</dt>
|
* <dt>Separators (for lines)</dt>
|
||||||
* <dd>The record separators are hardcoded and cannot be changed. The must be '\r', '\n' or '\r\n'.</dd>
|
* <dd>The record separators are hardcoded and cannot be changed. The must be '\r', '\n' or '\r\n'.</dd>
|
||||||
*
|
*
|
||||||
* <dt>Delimiter (for values)</dt>
|
* <dt>Delimiter (for values)</dt>
|
||||||
* <dd>The delimiter for values is freely configurable (default ',').</dd>
|
* <dd>The delimiter for values is freely configurable (default ',').</dd>
|
||||||
*
|
*
|
||||||
* <dt>Comments</dt>
|
* <dt>Comments</dt>
|
||||||
* <dd>Some CSV-dialects support a simple comment syntax. A comment is a record
|
* <dd>Some CSV-dialects support a simple comment syntax. A comment is a record
|
||||||
* which must start with a designated character (the commentStarter). A record
|
* which must start with a designated character (the commentStarter). A record
|
||||||
* of this kind is treated as comment and gets removed from the input (default none)</dd>
|
* of this kind is treated as comment and gets removed from the input (default none)</dd>
|
||||||
*
|
*
|
||||||
* <dt>Encapsulator</dt>
|
* <dt>Encapsulator</dt>
|
||||||
* <dd>Two encapsulator characters (default '"') are used to enclose -> complex values.</dd>
|
* <dd>Two encapsulator characters (default '"') are used to enclose -> complex values.</dd>
|
||||||
*
|
*
|
||||||
* <dt>Simple values</dt>
|
* <dt>Simple values</dt>
|
||||||
* <dd>A simple value consist of all characters (except the delimiter) until
|
* <dd>A simple value consist of all characters (except the delimiter) until
|
||||||
* (but not including) the next delimiter or a record-terminator. Optionally
|
* (but not including) the next delimiter or a record-terminator. Optionally
|
||||||
* all surrounding whitespaces of a simple value can be ignored (default: true).</dd>
|
* all surrounding whitespaces of a simple value can be ignored (default: true).</dd>
|
||||||
*
|
*
|
||||||
* <dt>Complex values</dt>
|
* <dt>Complex values</dt>
|
||||||
* <dd>Complex values are encapsulated within a pair of the defined encapsulator characters.
|
* <dd>Complex values are encapsulated within a pair of the defined encapsulator characters.
|
||||||
* The encapsulator itself must be escaped or doubled when used inside complex values.
|
* The encapsulator itself must be escaped or doubled when used inside complex values.
|
||||||
* Complex values preserve all kind of formatting (including newlines -> multiline-values)</dd>
|
* Complex values preserve all kind of formatting (including newlines -> multiline-values)</dd>
|
||||||
*
|
*
|
||||||
* <dt>Empty line skipping</dt>
|
* <dt>Empty line skipping</dt>
|
||||||
* <dd>Optionally empty lines in CSV files can be skipped.
|
* <dd>Optionally empty lines in CSV files can be skipped.
|
||||||
* Otherwise, empty lines will return a record with a single empty value.</dd>
|
* Otherwise, empty lines will return a record with a single empty value.</dd>
|
||||||
* </dl>
|
* </dl>
|
||||||
*
|
*
|
||||||
* <p>In addition to individually defined dialects, two predefined dialects (strict-csv, and excel-csv)
|
* <p>In addition to individually defined dialects, two predefined dialects (strict-csv, and excel-csv)
|
||||||
* can be set directly.</p> <!-- TODO fix -->
|
* can be set directly.</p> <!-- TODO fix -->
|
||||||
*
|
*
|
||||||
* <p>Example usage:</p>
|
* <p>Example usage:</p>
|
||||||
* <blockquote><pre>
|
* <blockquote><pre>
|
||||||
* Reader in = new StringReader("a,b,c");
|
* Reader in = new StringReader("a,b,c");
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -45,7 +45,7 @@ import org.junit.runners.Parameterized.Parameters;
|
||||||
public class CSVFileParserTest {
|
public class CSVFileParserTest {
|
||||||
|
|
||||||
private static final File BASE = new File("src/test/resources/CSVFileParser");
|
private static final File BASE = new File("src/test/resources/CSVFileParser");
|
||||||
|
|
||||||
private final BufferedReader testData;
|
private final BufferedReader testData;
|
||||||
private final String testName;
|
private final String testName;
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ public class CSVFileParserTest {
|
||||||
public static Collection<Object[]> generateData()
|
public static Collection<Object[]> generateData()
|
||||||
{
|
{
|
||||||
List<Object[]> list = new ArrayList<Object[]>();
|
List<Object[]> list = new ArrayList<Object[]>();
|
||||||
|
|
||||||
final FilenameFilter filenameFilter = new FilenameFilter() {
|
final FilenameFilter filenameFilter = new FilenameFilter() {
|
||||||
public boolean accept(File dir, String name) {
|
public boolean accept(File dir, String name) {
|
||||||
return name.startsWith("test") && name.endsWith(".txt");
|
return name.startsWith("test") && name.endsWith(".txt");
|
||||||
|
@ -79,7 +79,7 @@ public class CSVFileParserTest {
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCSVFile() throws Exception {
|
public void testCSVFile() throws Exception {
|
||||||
String line = readTestData();
|
String line = readTestData();
|
||||||
|
@ -107,7 +107,7 @@ public class CSVFileParserTest {
|
||||||
}
|
}
|
||||||
line = readTestData(); // get string version of format
|
line = readTestData(); // get string version of format
|
||||||
assertEquals(testName+" Expected format ", line, fmt.toString());
|
assertEquals(testName+" Expected format ", line, fmt.toString());
|
||||||
|
|
||||||
// Now parse the file and compare against the expected results
|
// Now parse the file and compare against the expected results
|
||||||
for(CSVRecord rec : fmt.parse(csvFile)) {
|
for(CSVRecord rec : fmt.parse(csvFile)) {
|
||||||
String parsed = rec.toString();
|
String parsed = rec.toString();
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -31,7 +31,7 @@ public class CSVFormatTest {
|
||||||
@Test
|
@Test
|
||||||
public void testImmutalibity() {
|
public void testImmutalibity() {
|
||||||
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null);
|
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null);
|
||||||
|
|
||||||
format.withDelimiter('?');
|
format.withDelimiter('?');
|
||||||
format.withEncapsulator('?');
|
format.withEncapsulator('?');
|
||||||
format.withCommentStart('?');
|
format.withCommentStart('?');
|
||||||
|
@ -39,13 +39,13 @@ public class CSVFormatTest {
|
||||||
format.withEscape('?');
|
format.withEscape('?');
|
||||||
format.withSurroundingSpacesIgnored(false);
|
format.withSurroundingSpacesIgnored(false);
|
||||||
format.withEmptyLinesIgnored(false);
|
format.withEmptyLinesIgnored(false);
|
||||||
|
|
||||||
assertEquals('!', format.getDelimiter());
|
assertEquals('!', format.getDelimiter());
|
||||||
assertEquals('!', format.getEncapsulator());
|
assertEquals('!', format.getEncapsulator());
|
||||||
assertEquals('!', format.getCommentStart());
|
assertEquals('!', format.getCommentStart());
|
||||||
assertEquals('!', format.getEscape());
|
assertEquals('!', format.getEscape());
|
||||||
assertEquals(CSVFormat.CRLF, format.getLineSeparator());
|
assertEquals(CSVFormat.CRLF, format.getLineSeparator());
|
||||||
|
|
||||||
assertTrue(format.isSurroundingSpacesIgnored());
|
assertTrue(format.isSurroundingSpacesIgnored());
|
||||||
assertTrue(format.isEmptyLinesIgnored());
|
assertTrue(format.isEmptyLinesIgnored());
|
||||||
}
|
}
|
||||||
|
@ -53,13 +53,13 @@ public class CSVFormatTest {
|
||||||
@Test
|
@Test
|
||||||
public void testMutators() {
|
public void testMutators() {
|
||||||
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null);
|
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null);
|
||||||
|
|
||||||
assertEquals('?', format.withDelimiter('?').getDelimiter());
|
assertEquals('?', format.withDelimiter('?').getDelimiter());
|
||||||
assertEquals('?', format.withEncapsulator('?').getEncapsulator());
|
assertEquals('?', format.withEncapsulator('?').getEncapsulator());
|
||||||
assertEquals('?', format.withCommentStart('?').getCommentStart());
|
assertEquals('?', format.withCommentStart('?').getCommentStart());
|
||||||
assertEquals("?", format.withLineSeparator("?").getLineSeparator());
|
assertEquals("?", format.withLineSeparator("?").getLineSeparator());
|
||||||
assertEquals('?', format.withEscape('?').getEscape());
|
assertEquals('?', format.withEscape('?').getEscape());
|
||||||
|
|
||||||
assertFalse(format.withSurroundingSpacesIgnored(false).isSurroundingSpacesIgnored());
|
assertFalse(format.withSurroundingSpacesIgnored(false).isSurroundingSpacesIgnored());
|
||||||
assertFalse(format.withEmptyLinesIgnored(false).isEmptyLinesIgnored());
|
assertFalse(format.withEmptyLinesIgnored(false).isEmptyLinesIgnored());
|
||||||
}
|
}
|
||||||
|
@ -67,77 +67,77 @@ public class CSVFormatTest {
|
||||||
@Test
|
@Test
|
||||||
public void testFormat() {
|
public void testFormat() {
|
||||||
CSVFormat format = CSVFormat.DEFAULT;
|
CSVFormat format = CSVFormat.DEFAULT;
|
||||||
|
|
||||||
assertEquals("", format.format());
|
assertEquals("", format.format());
|
||||||
assertEquals("a,b,c", format.format("a", "b", "c"));
|
assertEquals("a,b,c", format.format("a", "b", "c"));
|
||||||
assertEquals("\"x,y\",z", format.format("x,y", "z"));
|
assertEquals("\"x,y\",z", format.format("x,y", "z"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testValidation() {
|
public void testValidation() {
|
||||||
CSVFormat format = CSVFormat.DEFAULT;
|
CSVFormat format = CSVFormat.DEFAULT;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format.withDelimiter('\n');
|
format.withDelimiter('\n');
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format.withEscape('\r');
|
format.withEscape('\r');
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format.withEncapsulator('\n');
|
format.withEncapsulator('\n');
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format.withCommentStart('\r');
|
format.withCommentStart('\r');
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format.withDelimiter('!').withEscape('!').validate();
|
format.withDelimiter('!').withEscape('!').validate();
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format.withDelimiter('!').withCommentStart('!').validate();
|
format.withDelimiter('!').withCommentStart('!').validate();
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format.withEncapsulator('!').withCommentStart('!').validate();
|
format.withEncapsulator('!').withCommentStart('!').validate();
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
||||||
format.withEncapsulator(CSVFormat.DISABLED).withCommentStart(CSVFormat.DISABLED).validate();
|
format.withEncapsulator(CSVFormat.DISABLED).withCommentStart(CSVFormat.DISABLED).validate();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format.withEscape('!').withCommentStart('!').validate();
|
format.withEscape('!').withCommentStart('!').validate();
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
||||||
format.withEscape(CSVFormat.DISABLED).withCommentStart(CSVFormat.DISABLED).validate();
|
format.withEscape(CSVFormat.DISABLED).withCommentStart(CSVFormat.DISABLED).validate();
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
format.withEncapsulator('!').withDelimiter('!').validate();
|
format.withEncapsulator('!').withDelimiter('!').validate();
|
||||||
fail();
|
fail();
|
||||||
|
@ -150,15 +150,15 @@ public class CSVFormatTest {
|
||||||
@Test
|
@Test
|
||||||
public void testSerialization() throws Exception {
|
public void testSerialization() throws Exception {
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|
||||||
ObjectOutputStream oos = new ObjectOutputStream(out);
|
ObjectOutputStream oos = new ObjectOutputStream(out);
|
||||||
oos.writeObject(CSVFormat.DEFAULT);
|
oos.writeObject(CSVFormat.DEFAULT);
|
||||||
oos.flush();
|
oos.flush();
|
||||||
oos.close();
|
oos.close();
|
||||||
|
|
||||||
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
|
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
|
||||||
CSVFormat format = (CSVFormat) in.readObject();
|
CSVFormat format = (CSVFormat) in.readObject();
|
||||||
|
|
||||||
assertNotNull(format);
|
assertNotNull(format);
|
||||||
assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter());
|
assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter());
|
||||||
assertEquals("encapsulator", CSVFormat.DEFAULT.getEncapsulator(), format.getEncapsulator());
|
assertEquals("encapsulator", CSVFormat.DEFAULT.getEncapsulator(), format.getEncapsulator());
|
||||||
|
@ -168,4 +168,4 @@ public class CSVFormatTest {
|
||||||
assertEquals("trim", CSVFormat.DEFAULT.isSurroundingSpacesIgnored(), format.isSurroundingSpacesIgnored());
|
assertEquals("trim", CSVFormat.DEFAULT.isSurroundingSpacesIgnored(), format.isSurroundingSpacesIgnored());
|
||||||
assertEquals("empty lines", CSVFormat.DEFAULT.isEmptyLinesIgnored(), format.isEmptyLinesIgnored());
|
assertEquals("empty lines", CSVFormat.DEFAULT.isEmptyLinesIgnored(), format.isEmptyLinesIgnored());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -24,12 +24,12 @@ import static org.apache.commons.csv.Token.Type.*;
|
||||||
class CSVLexer1 extends Lexer {
|
class CSVLexer1 extends Lexer {
|
||||||
|
|
||||||
private final StringBuilder wsBuf = new StringBuilder();
|
private final StringBuilder wsBuf = new StringBuilder();
|
||||||
|
|
||||||
// ctor needs to be public so can be called dynamically by PerformanceTest class
|
// ctor needs to be public so can be called dynamically by PerformanceTest class
|
||||||
public CSVLexer1(CSVFormat format, ExtendedBufferedReader in) {
|
public CSVLexer1(CSVFormat format, ExtendedBufferedReader in) {
|
||||||
super(format, in);
|
super(format, in);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the next token.
|
* Returns the next token.
|
||||||
* <p/>
|
* <p/>
|
||||||
|
@ -89,7 +89,7 @@ class CSVLexer1 extends Lexer {
|
||||||
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 == format.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)
|
||||||
|
@ -194,7 +194,7 @@ class CSVLexer1 extends Lexer {
|
||||||
// assert c == delimiter;
|
// assert c == delimiter;
|
||||||
while (true) {
|
while (true) {
|
||||||
c = in.read();
|
c = in.read();
|
||||||
|
|
||||||
if (c == format.getEscape()) {
|
if (c == format.getEscape()) {
|
||||||
tkn.content.append((char) readEscape());
|
tkn.content.append((char) readEscape());
|
||||||
} else if (c == format.getEncapsulator()) {
|
} else if (c == format.getEncapsulator()) {
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -27,7 +27,7 @@ class CSVLexer1306663 extends Lexer {
|
||||||
public CSVLexer1306663(CSVFormat format, ExtendedBufferedReader in) {
|
public CSVLexer1306663(CSVFormat format, ExtendedBufferedReader in) {
|
||||||
super(format, in);
|
super(format, in);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the next token.
|
* Returns the next token.
|
||||||
* <p/>
|
* <p/>
|
||||||
|
@ -92,7 +92,7 @@ class CSVLexer1306663 extends Lexer {
|
||||||
eol = isEndOfLine(c);
|
eol = isEndOfLine(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ok, start of token reached: encapsulated, or token
|
// ok, start of token reached: encapsulated, or token
|
||||||
if (isDelimiter(c)) {
|
if (isDelimiter(c)) {
|
||||||
// empty token return TOKEN("")
|
// empty token return TOKEN("")
|
||||||
|
@ -184,7 +184,7 @@ class CSVLexer1306663 extends Lexer {
|
||||||
int c;
|
int c;
|
||||||
while (true) {
|
while (true) {
|
||||||
c = in.read();
|
c = in.read();
|
||||||
|
|
||||||
if (isEscape(c)) {
|
if (isEscape(c)) {
|
||||||
tkn.content.append((char) readEscape());
|
tkn.content.append((char) readEscape());
|
||||||
} else if (isEncapsulator(c)) {
|
} else if (isEncapsulator(c)) {
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -27,7 +27,7 @@ class CSVLexer1306667 extends Lexer {
|
||||||
public CSVLexer1306667(CSVFormat format, ExtendedBufferedReader in) {
|
public CSVLexer1306667(CSVFormat format, ExtendedBufferedReader in) {
|
||||||
super(format, in);
|
super(format, in);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the next token.
|
* Returns the next token.
|
||||||
* <p/>
|
* <p/>
|
||||||
|
@ -92,7 +92,7 @@ class CSVLexer1306667 extends Lexer {
|
||||||
eol = isEndOfLine(c);
|
eol = isEndOfLine(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ok, start of token reached: encapsulated, or token
|
// ok, start of token reached: encapsulated, or token
|
||||||
if (isDelimiter(c)) {
|
if (isDelimiter(c)) {
|
||||||
// empty token return TOKEN("")
|
// empty token return TOKEN("")
|
||||||
|
@ -184,7 +184,7 @@ class CSVLexer1306667 extends Lexer {
|
||||||
int c;
|
int c;
|
||||||
while (true) {
|
while (true) {
|
||||||
c = in.read();
|
c = in.read();
|
||||||
|
|
||||||
if (isEscape(c)) {
|
if (isEscape(c)) {
|
||||||
tkn.content.append((char) readEscape());
|
tkn.content.append((char) readEscape());
|
||||||
} else if (isEncapsulator(c)) {
|
} else if (isEncapsulator(c)) {
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -25,18 +25,18 @@ import static org.apache.commons.csv.Token.Type.*;
|
||||||
* Experimental Lexer using enums to keep track of state and character type.
|
* Experimental Lexer using enums to keep track of state and character type.
|
||||||
* Unfortunately it is twice as slow.
|
* Unfortunately it is twice as slow.
|
||||||
* For reference purpose only.
|
* For reference purpose only.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class CSVLexer3 extends Lexer {
|
class CSVLexer3 extends Lexer {
|
||||||
|
|
||||||
private final char escape;
|
private final char escape;
|
||||||
|
|
||||||
// ctor needs to be public so can be called dynamically by PerformanceTest class
|
// ctor needs to be public so can be called dynamically by PerformanceTest class
|
||||||
public CSVLexer3(CSVFormat format, ExtendedBufferedReader in) {
|
public CSVLexer3(CSVFormat format, ExtendedBufferedReader in) {
|
||||||
super(format, in);
|
super(format, in);
|
||||||
this.escape = format.getEscape();
|
this.escape = format.getEscape();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Classify the character types
|
* Classify the character types
|
||||||
*/
|
*/
|
||||||
|
@ -130,7 +130,7 @@ class CSVLexer3 extends Lexer {
|
||||||
case WHITESPACE:
|
case WHITESPACE:
|
||||||
if (!surroundingSpacesIgnored){
|
if (!surroundingSpacesIgnored){
|
||||||
tkn.content.append((char) intch);
|
tkn.content.append((char) intch);
|
||||||
state = State.PLAIN;
|
state = State.PLAIN;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -165,7 +165,7 @@ class CSVLexer3 extends Lexer {
|
||||||
break;
|
break;
|
||||||
case EOFCHAR:
|
case EOFCHAR:
|
||||||
throw new IOException("(line " + getLineNumber() + ") unexpected EOF in quoted string");
|
throw new IOException("(line " + getLineNumber() + ") unexpected EOF in quoted string");
|
||||||
default:
|
default:
|
||||||
tkn.content.append((char) intch);
|
tkn.content.append((char) intch);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -209,7 +209,7 @@ class CSVLexer3 extends Lexer {
|
||||||
break;
|
break;
|
||||||
case COMMENT_START: // TODO should comment be escaped?
|
case COMMENT_START: // TODO should comment be escaped?
|
||||||
case ENCAP: // TODO is this correct?
|
case ENCAP: // TODO is this correct?
|
||||||
case OTHER: // TODO may need to escape further
|
case OTHER: // TODO may need to escape further
|
||||||
case WHITESPACE:
|
case WHITESPACE:
|
||||||
tkn.content.append(escape);
|
tkn.content.append(escape);
|
||||||
tkn.content.append((char) intch);
|
tkn.content.append((char) intch);
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -26,7 +26,7 @@ import static org.apache.commons.csv.Token.Type.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class CSVLexerTest {
|
public class CSVLexerTest {
|
||||||
|
|
||||||
private Lexer getLexer(String input, CSVFormat format) {
|
private Lexer getLexer(String input, CSVFormat format) {
|
||||||
return new CSVLexer(format, new ExtendedBufferedReader(new StringReader(input)));
|
return new CSVLexer(format, new ExtendedBufferedReader(new StringReader(input)));
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ public class CSVLexerTest {
|
||||||
assertEquals("Token type", expectedType, token.type);
|
assertEquals("Token type", expectedType, token.type);
|
||||||
assertEquals("Token content", expectedContent, token.content.toString());
|
assertEquals("Token content", expectedContent, token.content.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Single line (without comment)
|
// Single line (without comment)
|
||||||
@Test
|
@Test
|
||||||
public void testNextToken1() throws IOException {
|
public void testNextToken1() throws IOException {
|
||||||
|
@ -56,7 +56,7 @@ public class CSVLexerTest {
|
||||||
// multiline including comments (and empty lines)
|
// multiline including comments (and empty lines)
|
||||||
@Test
|
@Test
|
||||||
public void testNextToken2() throws IOException {
|
public void testNextToken2() throws IOException {
|
||||||
final String code =
|
final String code =
|
||||||
"1,2,3,\n"+ // 1
|
"1,2,3,\n"+ // 1
|
||||||
"\n"+
|
"\n"+
|
||||||
"\n"+
|
"\n"+
|
||||||
|
@ -74,7 +74,7 @@ public class CSVLexerTest {
|
||||||
"# Final comment\n"; // 7
|
"# Final comment\n"; // 7
|
||||||
CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#');
|
CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#');
|
||||||
assertTrue("Should ignore empty lines", format.isEmptyLinesIgnored());
|
assertTrue("Should ignore empty lines", format.isEmptyLinesIgnored());
|
||||||
|
|
||||||
Lexer parser = getLexer(code, format);
|
Lexer parser = getLexer(code, format);
|
||||||
|
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ public class CSVLexerTest {
|
||||||
// multiline including comments (and empty lines)
|
// multiline including comments (and empty lines)
|
||||||
@Test
|
@Test
|
||||||
public void testNextToken2EmptyLines() throws IOException {
|
public void testNextToken2EmptyLines() throws IOException {
|
||||||
final String code =
|
final String code =
|
||||||
"1,2,3,\n"+ // 1
|
"1,2,3,\n"+ // 1
|
||||||
"\n"+ // 1b
|
"\n"+ // 1b
|
||||||
"\n"+ // 1c
|
"\n"+ // 1c
|
||||||
|
@ -117,7 +117,7 @@ public class CSVLexerTest {
|
||||||
"# Final comment\n"; // 7
|
"# Final comment\n"; // 7
|
||||||
CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#').withEmptyLinesIgnored(false);
|
CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#').withEmptyLinesIgnored(false);
|
||||||
assertFalse("Should not ignore empty lines", format.isEmptyLinesIgnored());
|
assertFalse("Should not ignore empty lines", format.isEmptyLinesIgnored());
|
||||||
|
|
||||||
Lexer parser = getLexer(code, format);
|
Lexer parser = getLexer(code, format);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -60,7 +60,7 @@ public class CSVParserTest {
|
||||||
for (String[] re : RESULT) {
|
for (String[] re : RESULT) {
|
||||||
assertArrayEquals(re, parser.getRecord().values());
|
assertArrayEquals(re, parser.getRecord().values());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertNull(parser.getRecord());
|
assertNull(parser.getRecord());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +132,7 @@ public class CSVParserTest {
|
||||||
{""}, // Excel format does not ignore empty lines
|
{""}, // Excel format does not ignore empty lines
|
||||||
{"world", ""}
|
{"world", ""}
|
||||||
};
|
};
|
||||||
|
|
||||||
for (String code : codes) {
|
for (String code : codes) {
|
||||||
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
|
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
|
||||||
List<CSVRecord> records = parser.getRecords();
|
List<CSVRecord> records = parser.getRecords();
|
||||||
|
@ -369,7 +369,7 @@ public class CSVParserTest {
|
||||||
format = CSVFormat.DEFAULT.withCommentStart('#');
|
format = CSVFormat.DEFAULT.withCommentStart('#');
|
||||||
parser = new CSVParser(code, format);
|
parser = new CSVParser(code, format);
|
||||||
records = parser.getRecords();
|
records = parser.getRecords();
|
||||||
|
|
||||||
Utils.compare("Failed to parse with comments", res_comments, records);
|
Utils.compare("Failed to parse with comments", res_comments, records);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -410,13 +410,13 @@ public class CSVParserTest {
|
||||||
@Test
|
@Test
|
||||||
public void testForEach() throws Exception {
|
public void testForEach() throws Exception {
|
||||||
List<CSVRecord> records = new ArrayList<CSVRecord>();
|
List<CSVRecord> records = new ArrayList<CSVRecord>();
|
||||||
|
|
||||||
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||||
|
|
||||||
for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
|
for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
|
||||||
records.add(record);
|
records.add(record);
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(3, records.size());
|
assertEquals(3, records.size());
|
||||||
assertArrayEquals(new String[]{"a", "b", "c"}, records.get(0).values());
|
assertArrayEquals(new String[]{"a", "b", "c"}, records.get(0).values());
|
||||||
assertArrayEquals(new String[]{"1", "2", "3"}, records.get(1).values());
|
assertArrayEquals(new String[]{"1", "2", "3"}, records.get(1).values());
|
||||||
|
@ -426,9 +426,9 @@ public class CSVParserTest {
|
||||||
@Test
|
@Test
|
||||||
public void testIterator() throws Exception {
|
public void testIterator() throws Exception {
|
||||||
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||||
|
|
||||||
Iterator<CSVRecord> iterator = CSVFormat.DEFAULT.parse(in).iterator();
|
Iterator<CSVRecord> iterator = CSVFormat.DEFAULT.parse(in).iterator();
|
||||||
|
|
||||||
assertTrue(iterator.hasNext());
|
assertTrue(iterator.hasNext());
|
||||||
try {
|
try {
|
||||||
iterator.remove();
|
iterator.remove();
|
||||||
|
@ -442,7 +442,7 @@ public class CSVParserTest {
|
||||||
assertTrue(iterator.hasNext());
|
assertTrue(iterator.hasNext());
|
||||||
assertArrayEquals(new String[]{"x", "y", "z"}, iterator.next().values());
|
assertArrayEquals(new String[]{"x", "y", "z"}, iterator.next().values());
|
||||||
assertFalse(iterator.hasNext());
|
assertFalse(iterator.hasNext());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
iterator.next();
|
iterator.next();
|
||||||
fail("NoSuchElementException expected");
|
fail("NoSuchElementException expected");
|
||||||
|
@ -450,13 +450,13 @@ public class CSVParserTest {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHeader() throws Exception {
|
public void testHeader() throws Exception {
|
||||||
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||||
|
|
||||||
Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
|
Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
assertTrue(records.hasNext());
|
assertTrue(records.hasNext());
|
||||||
CSVRecord record = records.next();
|
CSVRecord record = records.next();
|
||||||
|
@ -464,7 +464,7 @@ public class CSVParserTest {
|
||||||
assertEquals(record.get(1), record.get("b"));
|
assertEquals(record.get(1), record.get("b"));
|
||||||
assertEquals(record.get(2), record.get("c"));
|
assertEquals(record.get(2), record.get("c"));
|
||||||
}
|
}
|
||||||
|
|
||||||
assertFalse(records.hasNext());
|
assertFalse(records.hasNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -473,7 +473,7 @@ public class CSVParserTest {
|
||||||
Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
|
Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
|
||||||
|
|
||||||
Iterator<CSVRecord> records = CSVFormat.DEFAULT.withCommentStart('#').withHeader().parse(in).iterator();
|
Iterator<CSVRecord> records = CSVFormat.DEFAULT.withCommentStart('#').withHeader().parse(in).iterator();
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
assertTrue(records.hasNext());
|
assertTrue(records.hasNext());
|
||||||
CSVRecord record = records.next();
|
CSVRecord record = records.next();
|
||||||
|
@ -481,7 +481,7 @@ public class CSVParserTest {
|
||||||
assertEquals(record.get(1), record.get("b"));
|
assertEquals(record.get(1), record.get("b"));
|
||||||
assertEquals(record.get(2), record.get("c"));
|
assertEquals(record.get(2), record.get("c"));
|
||||||
}
|
}
|
||||||
|
|
||||||
assertFalse(records.hasNext());
|
assertFalse(records.hasNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -505,7 +505,7 @@ public class CSVParserTest {
|
||||||
@Test
|
@Test
|
||||||
public void testGetLineNumberWithLF() throws Exception {
|
public void testGetLineNumberWithLF() throws Exception {
|
||||||
CSVParser parser = new CSVParser("a\nb\nc", CSVFormat.DEFAULT.withLineSeparator("\n"));
|
CSVParser parser = new CSVParser("a\nb\nc", CSVFormat.DEFAULT.withLineSeparator("\n"));
|
||||||
|
|
||||||
assertEquals(0, parser.getLineNumber());
|
assertEquals(0, parser.getLineNumber());
|
||||||
assertNotNull(parser.getRecord());
|
assertNotNull(parser.getRecord());
|
||||||
assertEquals(1, parser.getLineNumber());
|
assertEquals(1, parser.getLineNumber());
|
||||||
|
@ -519,7 +519,7 @@ public class CSVParserTest {
|
||||||
@Test
|
@Test
|
||||||
public void testGetLineNumberWithCRLF() throws Exception {
|
public void testGetLineNumberWithCRLF() throws Exception {
|
||||||
CSVParser parser = new CSVParser("a\r\nb\r\nc", CSVFormat.DEFAULT.withLineSeparator(CSVFormat.CRLF));
|
CSVParser parser = new CSVParser("a\r\nb\r\nc", CSVFormat.DEFAULT.withLineSeparator(CSVFormat.CRLF));
|
||||||
|
|
||||||
assertEquals(0, parser.getLineNumber());
|
assertEquals(0, parser.getLineNumber());
|
||||||
assertNotNull(parser.getRecord());
|
assertNotNull(parser.getRecord());
|
||||||
assertEquals(1, parser.getLineNumber());
|
assertEquals(1, parser.getLineNumber());
|
||||||
|
@ -533,7 +533,7 @@ public class CSVParserTest {
|
||||||
@Test
|
@Test
|
||||||
public void testGetLineNumberWithCR() throws Exception {
|
public void testGetLineNumberWithCR() throws Exception {
|
||||||
CSVParser parser = new CSVParser("a\rb\rc", CSVFormat.DEFAULT.withLineSeparator("\r"));
|
CSVParser parser = new CSVParser("a\rb\rc", CSVFormat.DEFAULT.withLineSeparator("\r"));
|
||||||
|
|
||||||
assertEquals(0, parser.getLineNumber());
|
assertEquals(0, parser.getLineNumber());
|
||||||
assertNotNull(parser.getRecord());
|
assertNotNull(parser.getRecord());
|
||||||
assertEquals(1, parser.getLineNumber());
|
assertEquals(1, parser.getLineNumber());
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -115,7 +115,7 @@ public class CSVPrinterTest {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||||
printer.printComment("This is a comment");
|
printer.printComment("This is a comment");
|
||||||
|
|
||||||
assertEquals("", sw.toString());
|
assertEquals("", sw.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ public class CSVPrinterTest {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
|
||||||
printer.printComment("This is a comment");
|
printer.printComment("This is a comment");
|
||||||
|
|
||||||
assertEquals("# This is a comment" + lineSeparator, sw.toString());
|
assertEquals("# This is a comment" + lineSeparator, sw.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ public class CSVPrinterTest {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
|
||||||
printer.printComment("This is a comment\non multiple lines");
|
printer.printComment("This is a comment\non multiple lines");
|
||||||
|
|
||||||
assertEquals("# This is a comment" + lineSeparator + "# on multiple lines" + lineSeparator, sw.toString());
|
assertEquals("# This is a comment" + lineSeparator + "# on multiple lines" + lineSeparator, sw.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,7 +153,7 @@ public class CSVPrinterTest {
|
||||||
|
|
||||||
public void doOneRandom(CSVFormat format) throws Exception {
|
public void doOneRandom(CSVFormat format) throws Exception {
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
|
|
||||||
int nLines = r.nextInt(4) + 1;
|
int nLines = r.nextInt(4) + 1;
|
||||||
int nCol = r.nextInt(3) + 1;
|
int nCol = r.nextInt(3) + 1;
|
||||||
// nLines=1;nCol=2;
|
// nLines=1;nCol=2;
|
||||||
|
@ -199,7 +199,7 @@ public class CSVPrinterTest {
|
||||||
|
|
||||||
public String randStr() {
|
public String randStr() {
|
||||||
Random r = new Random();
|
Random r = new Random();
|
||||||
|
|
||||||
int sz = r.nextInt(20);
|
int sz = r.nextInt(20);
|
||||||
// sz = r.nextInt(3);
|
// sz = r.nextInt(3);
|
||||||
char[] buf = new char[sz];
|
char[] buf = new char[sz];
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -90,7 +90,7 @@ public class ExtendedBufferedReaderTest {
|
||||||
public void testReadLookahead2() throws Exception {
|
public void testReadLookahead2() throws Exception {
|
||||||
char[] ref = new char[5];
|
char[] ref = new char[5];
|
||||||
char[] res = new char[5];
|
char[] res = new char[5];
|
||||||
|
|
||||||
ExtendedBufferedReader br = getBufferedReader("abcdefg");
|
ExtendedBufferedReader br = getBufferedReader("abcdefg");
|
||||||
ref[0] = 'a';
|
ref[0] = 'a';
|
||||||
ref[1] = 'b';
|
ref[1] = 'b';
|
||||||
|
@ -151,7 +151,7 @@ public class ExtendedBufferedReaderTest {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Test to illustrate https://issues.apache.org/jira/browse/CSV-75
|
* Test to illustrate https://issues.apache.org/jira/browse/CSV-75
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testReadChar() throws Exception {
|
public void testReadChar() throws Exception {
|
||||||
|
@ -160,7 +160,7 @@ public class ExtendedBufferedReaderTest {
|
||||||
// EOL eol EOL EOL eol eol EOL+CR EOL
|
// EOL eol EOL EOL eol eol EOL+CR EOL
|
||||||
final int EOLeolct=9;
|
final int EOLeolct=9;
|
||||||
ExtendedBufferedReader br;
|
ExtendedBufferedReader br;
|
||||||
|
|
||||||
br = getBufferedReader(test);
|
br = getBufferedReader(test);
|
||||||
assertEquals(0, br.getLineNumber());
|
assertEquals(0, br.getLineNumber());
|
||||||
while(br.readLine()!=null) {}
|
while(br.readLine()!=null) {}
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.apache.commons.csv;
|
package org.apache.commons.csv;
|
||||||
|
@ -29,10 +29,10 @@ public class Utils {
|
||||||
|
|
||||||
private Utils() {
|
private Utils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the two 2d arrays have identical contents.
|
* Checks if the two 2d arrays have identical contents.
|
||||||
*
|
*
|
||||||
* @param message the message to be displayed
|
* @param message the message to be displayed
|
||||||
* @param expected the 2d array of expected results
|
* @param expected the 2d array of expected results
|
||||||
* @param actual the 2d array of actual results
|
* @param actual the 2d array of actual results
|
||||||
|
@ -43,10 +43,10 @@ public class Utils {
|
||||||
Assert.assertArrayEquals(message+" (entry "+i+")",expected[i], actual[i]);
|
Assert.assertArrayEquals(message+" (entry "+i+")",expected[i], actual[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the 2d array has the same contents as the list of records.
|
* Checks if the 2d array has the same contents as the list of records.
|
||||||
*
|
*
|
||||||
* @param message the message to be displayed
|
* @param message the message to be displayed
|
||||||
* @param expected the 2d array of expected results
|
* @param expected the 2d array of expected results
|
||||||
* @param actual the List of {@link CSVRecord} entries, each containing an array of values
|
* @param actual the List of {@link CSVRecord} entries, each containing an array of values
|
||||||
|
|
|
@ -5,9 +5,9 @@
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* 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 not use this file except in compliance with
|
||||||
* the License. You may obtain a copy of the License at
|
* the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -28,7 +28,7 @@ import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests performance.
|
* Tests performance.
|
||||||
*
|
*
|
||||||
* Only enable for your own development.
|
* Only enable for your own development.
|
||||||
*/
|
*/
|
||||||
public class PerformanceTest {
|
public class PerformanceTest {
|
||||||
|
|
Loading…
Reference in New Issue