No functional changes are contained in this commit: reformatted Java code to fix several formatting inconsistencies (between classes and within the same class); sorry for the big commit, but I have preferred to isolate into one commit all the formatting changes.
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1065950 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fe5bd51f8a
commit
1166ca605b
File diff suppressed because it is too large
Load Diff
|
@ -26,282 +26,289 @@ import java.io.Writer;
|
|||
*/
|
||||
public class CSVPrinter {
|
||||
|
||||
/** The place that the values get written. */
|
||||
protected final Writer out;
|
||||
protected final CSVStrategy strategy;
|
||||
/**
|
||||
* The place that the values get written.
|
||||
*/
|
||||
protected final Writer out;
|
||||
protected final CSVStrategy strategy;
|
||||
|
||||
/** True if we just began a new line. */
|
||||
protected boolean newLine = true;
|
||||
/**
|
||||
* True if we just began a new line.
|
||||
*/
|
||||
protected boolean newLine = true;
|
||||
|
||||
protected char[] buf = new char[0]; // temporary buffer
|
||||
protected char[] buf = new char[0]; // temporary buffer
|
||||
|
||||
/**
|
||||
* Create a printer that will print values to the given
|
||||
* stream following the CSVStrategy.
|
||||
*
|
||||
* Currently, only a pure encapsulation strategy or a pure escaping strategy
|
||||
* is supported. Hybrid strategies (encapsulation and escaping with a different character) are not supported.
|
||||
*
|
||||
* @param out stream to which to print.
|
||||
* @param strategy describes the CSV variation.
|
||||
*/
|
||||
public CSVPrinter(Writer out, CSVStrategy strategy) {
|
||||
this.out = out;
|
||||
this.strategy = strategy==null ? CSVStrategy.DEFAULT_STRATEGY : strategy;
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// printing implementation
|
||||
// ======================================================
|
||||
|
||||
/**
|
||||
* Output a blank line
|
||||
*/
|
||||
public void println() throws IOException {
|
||||
out.write(strategy.getPrinterNewline());
|
||||
newLine = true;
|
||||
}
|
||||
|
||||
public void flush() throws IOException {
|
||||
out.flush();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Print a single line of comma separated values.
|
||||
* The values will be quoted if needed. Quotes and
|
||||
* newLine characters will be escaped.
|
||||
*
|
||||
* @param values values to be outputted.
|
||||
*/
|
||||
public void println(String[] values) throws IOException {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
print(values[i]);
|
||||
}
|
||||
println();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Put a comment among the comma separated values.
|
||||
* Comments will always begin on a new line and occupy a
|
||||
* least one full line. The character specified to star
|
||||
* comments and a space will be inserted at the beginning of
|
||||
* each new line in the comment.
|
||||
*
|
||||
* @param comment the comment to output
|
||||
*/
|
||||
public void printlnComment(String comment) throws IOException {
|
||||
if(this.strategy.isCommentingDisabled()) {
|
||||
return;
|
||||
}
|
||||
if (!newLine) {
|
||||
println();
|
||||
}
|
||||
out.write(this.strategy.getCommentStart());
|
||||
out.write(' ');
|
||||
for (int i = 0; i < comment.length(); i++) {
|
||||
char c = comment.charAt(i);
|
||||
switch (c) {
|
||||
case '\r' :
|
||||
if (i + 1 < comment.length() && comment.charAt(i + 1) == '\n') {
|
||||
i++;
|
||||
}
|
||||
// break intentionally excluded.
|
||||
case '\n' :
|
||||
println();
|
||||
out.write(this.strategy.getCommentStart());
|
||||
out.write(' ');
|
||||
break;
|
||||
default :
|
||||
out.write(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
println();
|
||||
}
|
||||
|
||||
|
||||
public void print(char[] value, int offset, int len, boolean checkForEscape) throws IOException {
|
||||
if (!checkForEscape) {
|
||||
printSep();
|
||||
out.write(value, offset, len);
|
||||
return;
|
||||
/**
|
||||
* Create a printer that will print values to the given
|
||||
* stream following the CSVStrategy.
|
||||
* <p/>
|
||||
* Currently, only a pure encapsulation strategy or a pure escaping strategy
|
||||
* is supported. Hybrid strategies (encapsulation and escaping with a different character) are not supported.
|
||||
*
|
||||
* @param out stream to which to print.
|
||||
* @param strategy describes the CSV variation.
|
||||
*/
|
||||
public CSVPrinter(Writer out, CSVStrategy strategy) {
|
||||
this.out = out;
|
||||
this.strategy = strategy == null ? CSVStrategy.DEFAULT_STRATEGY : strategy;
|
||||
}
|
||||
|
||||
if (strategy.getEncapsulator() != CSVStrategy.ENCAPSULATOR_DISABLED) {
|
||||
printAndEncapsulate(value, offset, len);
|
||||
} else if (strategy.getEscape() != CSVStrategy.ESCAPE_DISABLED) {
|
||||
printAndEscape(value, offset, len);
|
||||
} else {
|
||||
printSep();
|
||||
out.write(value, offset, len);
|
||||
// ======================================================
|
||||
// printing implementation
|
||||
// ======================================================
|
||||
|
||||
/**
|
||||
* Output a blank line
|
||||
*/
|
||||
public void println() throws IOException {
|
||||
out.write(strategy.getPrinterNewline());
|
||||
newLine = true;
|
||||
}
|
||||
}
|
||||
|
||||
void printSep() throws IOException {
|
||||
if (newLine) {
|
||||
newLine = false;
|
||||
} else {
|
||||
out.write(this.strategy.getDelimiter());
|
||||
public void flush() throws IOException {
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
|
||||
void printAndEscape(char[] value, int offset, int len) throws IOException {
|
||||
int start = offset;
|
||||
int pos = offset;
|
||||
int end = offset + len;
|
||||
|
||||
printSep();
|
||||
|
||||
char delim = this.strategy.getDelimiter();
|
||||
char escape = this.strategy.getEscape();
|
||||
|
||||
while (pos < end) {
|
||||
char c = value[pos];
|
||||
if (c == '\r' || c=='\n' || c==delim || c==escape) {
|
||||
// write out segment up until this char
|
||||
int l = pos-start;
|
||||
if (l>0) {
|
||||
out.write(value, start, l);
|
||||
/**
|
||||
* Print a single line of comma separated values.
|
||||
* The values will be quoted if needed. Quotes and
|
||||
* newLine characters will be escaped.
|
||||
*
|
||||
* @param values values to be outputted.
|
||||
*/
|
||||
public void println(String[] values) throws IOException {
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
print(values[i]);
|
||||
}
|
||||
if (c=='\n') c='n';
|
||||
else if (c=='\r') c='r';
|
||||
|
||||
out.write(escape);
|
||||
out.write(c);
|
||||
|
||||
start = pos+1; // start on the current char after this one
|
||||
}
|
||||
|
||||
pos++;
|
||||
println();
|
||||
}
|
||||
|
||||
// write last segment
|
||||
int l = pos-start;
|
||||
if (l>0) {
|
||||
out.write(value, start, l);
|
||||
|
||||
/**
|
||||
* Put a comment among the comma separated values.
|
||||
* Comments will always begin on a new line and occupy a
|
||||
* least one full line. The character specified to star
|
||||
* comments and a space will be inserted at the beginning of
|
||||
* each new line in the comment.
|
||||
*
|
||||
* @param comment the comment to output
|
||||
*/
|
||||
public void printlnComment(String comment) throws IOException {
|
||||
if (this.strategy.isCommentingDisabled()) {
|
||||
return;
|
||||
}
|
||||
if (!newLine) {
|
||||
println();
|
||||
}
|
||||
out.write(this.strategy.getCommentStart());
|
||||
out.write(' ');
|
||||
for (int i = 0; i < comment.length(); i++) {
|
||||
char c = comment.charAt(i);
|
||||
switch (c) {
|
||||
case '\r':
|
||||
if (i + 1 < comment.length() && comment.charAt(i + 1) == '\n') {
|
||||
i++;
|
||||
}
|
||||
// break intentionally excluded.
|
||||
case '\n':
|
||||
println();
|
||||
out.write(this.strategy.getCommentStart());
|
||||
out.write(' ');
|
||||
break;
|
||||
default:
|
||||
out.write(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
println();
|
||||
}
|
||||
}
|
||||
|
||||
void printAndEncapsulate(char[] value, int offset, int len) throws IOException {
|
||||
boolean first = newLine; // is this the first value on this line?
|
||||
boolean quote = false;
|
||||
int start = offset;
|
||||
int pos = offset;
|
||||
int end = offset + len;
|
||||
|
||||
printSep();
|
||||
public void print(char[] value, int offset, int len, boolean checkForEscape) throws IOException {
|
||||
if (!checkForEscape) {
|
||||
printSep();
|
||||
out.write(value, offset, len);
|
||||
return;
|
||||
}
|
||||
|
||||
char delim = this.strategy.getDelimiter();
|
||||
char encapsulator = this.strategy.getEncapsulator();
|
||||
if (strategy.getEncapsulator() != CSVStrategy.ENCAPSULATOR_DISABLED) {
|
||||
printAndEncapsulate(value, offset, len);
|
||||
} else if (strategy.getEscape() != CSVStrategy.ESCAPE_DISABLED) {
|
||||
printAndEscape(value, offset, len);
|
||||
} else {
|
||||
printSep();
|
||||
out.write(value, offset, len);
|
||||
}
|
||||
}
|
||||
|
||||
if (len <= 0) {
|
||||
// always quote an empty token that is the first
|
||||
// on the line, as it may be the only thing on the
|
||||
// line. If it were not quoted in that case,
|
||||
// an empty line has no tokens.
|
||||
if (first) {
|
||||
quote = true;
|
||||
}
|
||||
} else {
|
||||
char c = value[pos];
|
||||
void printSep() throws IOException {
|
||||
if (newLine) {
|
||||
newLine = false;
|
||||
} else {
|
||||
out.write(this.strategy.getDelimiter());
|
||||
}
|
||||
}
|
||||
|
||||
void printAndEscape(char[] value, int offset, int len) throws IOException {
|
||||
int start = offset;
|
||||
int pos = offset;
|
||||
int end = offset + len;
|
||||
|
||||
printSep();
|
||||
|
||||
char delim = this.strategy.getDelimiter();
|
||||
char escape = this.strategy.getEscape();
|
||||
|
||||
// Hmmm, where did this rule come from?
|
||||
if (first
|
||||
&& (c < '0'
|
||||
|| (c > '9' && c < 'A')
|
||||
|| (c > 'Z' && c < 'a')
|
||||
|| (c > 'z'))) {
|
||||
quote = true;
|
||||
// } else if (c == ' ' || c == '\f' || c == '\t') {
|
||||
} else if (c <= '#') {
|
||||
// Some other chars at the start of a value caused the parser to fail, so for now
|
||||
// encapsulate if we start in anything less than '#'. We are being conservative
|
||||
// by including the default comment char too.
|
||||
quote = true;
|
||||
} else {
|
||||
while (pos < end) {
|
||||
c = value[pos];
|
||||
if (c=='\n' || c=='\r' || c==encapsulator || c==delim) {
|
||||
quote = true;
|
||||
break;
|
||||
}
|
||||
pos++;
|
||||
char c = value[pos];
|
||||
if (c == '\r' || c == '\n' || c == delim || c == escape) {
|
||||
// write out segment up until this char
|
||||
int l = pos - start;
|
||||
if (l > 0) {
|
||||
out.write(value, start, l);
|
||||
}
|
||||
if (c == '\n') {
|
||||
c = 'n';
|
||||
} else if (c == '\r') {
|
||||
c = 'r';
|
||||
}
|
||||
|
||||
out.write(escape);
|
||||
out.write(c);
|
||||
|
||||
start = pos + 1; // start on the current char after this one
|
||||
}
|
||||
|
||||
pos++;
|
||||
}
|
||||
|
||||
// write last segment
|
||||
int l = pos - start;
|
||||
if (l > 0) {
|
||||
out.write(value, start, l);
|
||||
}
|
||||
}
|
||||
|
||||
void printAndEncapsulate(char[] value, int offset, int len) throws IOException {
|
||||
boolean first = newLine; // is this the first value on this line?
|
||||
boolean quote = false;
|
||||
int start = offset;
|
||||
int pos = offset;
|
||||
int end = offset + len;
|
||||
|
||||
printSep();
|
||||
|
||||
char delim = this.strategy.getDelimiter();
|
||||
char encapsulator = this.strategy.getEncapsulator();
|
||||
|
||||
if (len <= 0) {
|
||||
// always quote an empty token that is the first
|
||||
// on the line, as it may be the only thing on the
|
||||
// line. If it were not quoted in that case,
|
||||
// an empty line has no tokens.
|
||||
if (first) {
|
||||
quote = true;
|
||||
}
|
||||
} else {
|
||||
char c = value[pos];
|
||||
|
||||
// Hmmm, where did this rule come from?
|
||||
if (first
|
||||
&& (c < '0'
|
||||
|| (c > '9' && c < 'A')
|
||||
|| (c > 'Z' && c < 'a')
|
||||
|| (c > 'z'))) {
|
||||
quote = true;
|
||||
// } else if (c == ' ' || c == '\f' || c == '\t') {
|
||||
} else if (c <= '#') {
|
||||
// Some other chars at the start of a value caused the parser to fail, so for now
|
||||
// encapsulate if we start in anything less than '#'. We are being conservative
|
||||
// by including the default comment char too.
|
||||
quote = true;
|
||||
} else {
|
||||
while (pos < end) {
|
||||
c = value[pos];
|
||||
if (c == '\n' || c == '\r' || c == encapsulator || c == delim) {
|
||||
quote = true;
|
||||
break;
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
|
||||
if (!quote) {
|
||||
pos = end - 1;
|
||||
c = value[pos];
|
||||
// if (c == ' ' || c == '\f' || c == '\t') {
|
||||
// Some other chars at the end caused the parser to fail, so for now
|
||||
// encapsulate if we end in anything less than ' '
|
||||
if (c <= ' ') {
|
||||
quote = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!quote) {
|
||||
pos = end-1;
|
||||
c = value[pos];
|
||||
// if (c == ' ' || c == '\f' || c == '\t') {
|
||||
// Some other chars at the end caused the parser to fail, so for now
|
||||
// encapsulate if we end in anything less than ' '
|
||||
if (c <= ' ') {
|
||||
quote = true;
|
||||
}
|
||||
// no encapsulation needed - write out the original value
|
||||
out.write(value, offset, len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// we hit something that needed encapsulation
|
||||
out.write(encapsulator);
|
||||
|
||||
// Pick up where we left off: pos should be positioned on the first character that caused
|
||||
// the need for encapsulation.
|
||||
while (pos < end) {
|
||||
char c = value[pos];
|
||||
if (c == encapsulator) {
|
||||
// write out the chunk up until this point
|
||||
|
||||
// add 1 to the length to write out the encapsulator also
|
||||
out.write(value, start, pos - start + 1);
|
||||
// put the next starting position on the encapsulator so we will
|
||||
// write it out again with the next string (effectively doubling it)
|
||||
start = pos;
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
|
||||
// write the last segment
|
||||
out.write(value, start, pos - start);
|
||||
out.write(encapsulator);
|
||||
}
|
||||
|
||||
if (!quote) {
|
||||
// no encapsulation needed - write out the original value
|
||||
out.write(value, offset, len);
|
||||
return;
|
||||
/**
|
||||
* Print the string as the next value on the line. The value
|
||||
* will be escaped or encapsulated as needed if checkForEscape==true
|
||||
*
|
||||
* @param value value to be outputted.
|
||||
*/
|
||||
public void print(String value, boolean checkForEscape) throws IOException {
|
||||
if (!checkForEscape) {
|
||||
// write directly from string
|
||||
printSep();
|
||||
out.write(value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (buf.length < value.length()) {
|
||||
buf = new char[value.length()];
|
||||
}
|
||||
|
||||
value.getChars(0, value.length(), buf, 0);
|
||||
print(buf, 0, value.length(), checkForEscape);
|
||||
}
|
||||
|
||||
// we hit something that needed encapsulation
|
||||
out.write(encapsulator);
|
||||
|
||||
// Pick up where we left off: pos should be positioned on the first character that caused
|
||||
// the need for encapsulation.
|
||||
while (pos<end) {
|
||||
char c = value[pos];
|
||||
if (c==encapsulator) {
|
||||
// write out the chunk up until this point
|
||||
|
||||
// add 1 to the length to write out the encapsulator also
|
||||
out.write(value, start, pos-start+1);
|
||||
// put the next starting position on the encapsulator so we will
|
||||
// write it out again with the next string (effectively doubling it)
|
||||
start = pos;
|
||||
}
|
||||
pos++;
|
||||
/**
|
||||
* Print the string as the next value on the line. The value
|
||||
* will be escaped or encapsulated as needed.
|
||||
*
|
||||
* @param value value to be outputted.
|
||||
*/
|
||||
public void print(String value) throws IOException {
|
||||
print(value, true);
|
||||
}
|
||||
|
||||
// write the last segment
|
||||
out.write(value, start, pos-start);
|
||||
out.write(encapsulator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the string as the next value on the line. The value
|
||||
* will be escaped or encapsulated as needed if checkForEscape==true
|
||||
*
|
||||
* @param value value to be outputted.
|
||||
*/
|
||||
public void print(String value, boolean checkForEscape) throws IOException {
|
||||
if (!checkForEscape) {
|
||||
// write directly from string
|
||||
printSep();
|
||||
out.write(value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (buf.length < value.length()) {
|
||||
buf = new char[value.length()];
|
||||
}
|
||||
|
||||
value.getChars(0, value.length(), buf, 0);
|
||||
print(buf, 0, value.length(), checkForEscape);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the string as the next value on the line. The value
|
||||
* will be escaped or encapsulated as needed.
|
||||
*
|
||||
* @param value value to be outputted.
|
||||
*/
|
||||
public void print(String value) throws IOException {
|
||||
print(value, true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ import java.io.Serializable;
|
|||
|
||||
/**
|
||||
* CSVStrategy
|
||||
*
|
||||
*
|
||||
* Represents the strategy for a CSV.
|
||||
*/
|
||||
public class CSVStrategy implements Cloneable, Serializable {
|
||||
|
@ -41,47 +41,46 @@ public class CSVStrategy implements Cloneable, Serializable {
|
|||
// an EOF signal (-1), and because \ufffe in UTF-16 would be
|
||||
// encoded as two chars (using surrogates) and thus there should never
|
||||
// be a collision with a real text char.
|
||||
public static char COMMENTS_DISABLED = (char)-2;
|
||||
public static char ESCAPE_DISABLED = (char)-2;
|
||||
public static char ENCAPSULATOR_DISABLED = (char)-2;
|
||||
public static char COMMENTS_DISABLED = (char) -2;
|
||||
public static char ESCAPE_DISABLED = (char) -2;
|
||||
public static char ENCAPSULATOR_DISABLED = (char) -2;
|
||||
|
||||
public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
|
||||
true, false, true);
|
||||
public static CSVStrategy EXCEL_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false,
|
||||
false, false, false);
|
||||
public static CSVStrategy TDF_STRATEGY = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
|
||||
true, false, true);
|
||||
public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
|
||||
true, false, true);
|
||||
public static CSVStrategy EXCEL_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false,
|
||||
false, false, false);
|
||||
public static CSVStrategy TDF_STRATEGY = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
|
||||
true, false, true);
|
||||
|
||||
|
||||
public CSVStrategy(char delimiter, char encapsulator, char commentStart) {
|
||||
this(delimiter, encapsulator, commentStart, true, false, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Customized CSV strategy setter.
|
||||
*
|
||||
* @param delimiter a Char used for value separation
|
||||
* @param encapsulator a Char used as value encapsulation marker
|
||||
* @param commentStart a Char used for comment identification
|
||||
* @param escape a Char used to escape special characters in values
|
||||
* @param ignoreLeadingWhitespace TRUE when leading whitespaces should be
|
||||
* ignored
|
||||
* @param delimiter a Char used for value separation
|
||||
* @param encapsulator a Char used as value encapsulation marker
|
||||
* @param commentStart a Char used for comment identification
|
||||
* @param escape a Char used to escape special characters in values
|
||||
* @param ignoreLeadingWhitespace TRUE when leading whitespaces should be
|
||||
* ignored
|
||||
* @param ignoreTrailingWhitespace TRUE when trailing whitespaces should be
|
||||
* ignored
|
||||
* @param interpretUnicodeEscapes TRUE when unicode escapes should be
|
||||
* interpreted
|
||||
* @param ignoreEmptyLines TRUE when the parser should skip emtpy lines
|
||||
* ignored
|
||||
* @param interpretUnicodeEscapes TRUE when unicode escapes should be
|
||||
* interpreted
|
||||
* @param ignoreEmptyLines TRUE when the parser should skip emtpy lines
|
||||
*/
|
||||
public CSVStrategy(
|
||||
char delimiter,
|
||||
char encapsulator,
|
||||
char commentStart,
|
||||
char escape,
|
||||
boolean ignoreLeadingWhitespace,
|
||||
boolean ignoreTrailingWhitespace,
|
||||
boolean interpretUnicodeEscapes,
|
||||
boolean ignoreEmptyLines)
|
||||
{
|
||||
char delimiter,
|
||||
char encapsulator,
|
||||
char commentStart,
|
||||
char escape,
|
||||
boolean ignoreLeadingWhitespace,
|
||||
boolean ignoreTrailingWhitespace,
|
||||
boolean interpretUnicodeEscapes,
|
||||
boolean ignoreEmptyLines) {
|
||||
setDelimiter(delimiter);
|
||||
setEncapsulator(encapsulator);
|
||||
setCommentStart(commentStart);
|
||||
|
@ -92,62 +91,101 @@ public class CSVStrategy implements Cloneable, Serializable {
|
|||
setIgnoreEmptyLines(ignoreEmptyLines);
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public CSVStrategy(
|
||||
char delimiter,
|
||||
char encapsulator,
|
||||
char commentStart,
|
||||
boolean ignoreLeadingWhitespace,
|
||||
boolean interpretUnicodeEscapes,
|
||||
boolean ignoreEmptyLines)
|
||||
{
|
||||
this(delimiter, encapsulator, commentStart, CSVStrategy.ESCAPE_DISABLED, ignoreLeadingWhitespace,
|
||||
true, interpretUnicodeEscapes, ignoreEmptyLines);
|
||||
char delimiter,
|
||||
char encapsulator,
|
||||
char commentStart,
|
||||
boolean ignoreLeadingWhitespace,
|
||||
boolean interpretUnicodeEscapes,
|
||||
boolean ignoreEmptyLines) {
|
||||
this(delimiter, encapsulator, commentStart, CSVStrategy.ESCAPE_DISABLED, ignoreLeadingWhitespace,
|
||||
true, interpretUnicodeEscapes, ignoreEmptyLines);
|
||||
}
|
||||
|
||||
public void setDelimiter(char delimiter) { this.delimiter = delimiter; }
|
||||
public char getDelimiter() { return this.delimiter; }
|
||||
|
||||
public void setEncapsulator(char encapsulator) { this.encapsulator = encapsulator; }
|
||||
public char getEncapsulator() { return this.encapsulator; }
|
||||
|
||||
public void setCommentStart(char commentStart) { this.commentStart = commentStart; }
|
||||
public char getCommentStart() { return this.commentStart; }
|
||||
public boolean isCommentingDisabled() { return this.commentStart == COMMENTS_DISABLED; }
|
||||
|
||||
public void setEscape(char escape) { this.escape = escape; }
|
||||
public char getEscape() { return this.escape; }
|
||||
|
||||
public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) {
|
||||
this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;
|
||||
public void setDelimiter(char delimiter) {
|
||||
this.delimiter = delimiter;
|
||||
}
|
||||
public boolean getIgnoreLeadingWhitespaces() { return this.ignoreLeadingWhitespaces; }
|
||||
|
||||
public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {
|
||||
this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;
|
||||
public char getDelimiter() {
|
||||
return this.delimiter;
|
||||
}
|
||||
public boolean getIgnoreTrailingWhitespaces() { return this.ignoreTrailingWhitespaces; }
|
||||
|
||||
public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) {
|
||||
this.interpretUnicodeEscapes = interpretUnicodeEscapes;
|
||||
public void setEncapsulator(char encapsulator) {
|
||||
this.encapsulator = encapsulator;
|
||||
}
|
||||
public boolean getUnicodeEscapeInterpretation() { return this.interpretUnicodeEscapes; }
|
||||
|
||||
public void setIgnoreEmptyLines(boolean ignoreEmptyLines) { this.ignoreEmptyLines = ignoreEmptyLines; }
|
||||
public boolean getIgnoreEmptyLines() { return this.ignoreEmptyLines; }
|
||||
public char getEncapsulator() {
|
||||
return this.encapsulator;
|
||||
}
|
||||
|
||||
public void setCommentStart(char commentStart) {
|
||||
this.commentStart = commentStart;
|
||||
}
|
||||
|
||||
public char getCommentStart() {
|
||||
return this.commentStart;
|
||||
}
|
||||
|
||||
public boolean isCommentingDisabled() {
|
||||
return this.commentStart == COMMENTS_DISABLED;
|
||||
}
|
||||
|
||||
public void setEscape(char escape) {
|
||||
this.escape = escape;
|
||||
}
|
||||
|
||||
public char getEscape() {
|
||||
return this.escape;
|
||||
}
|
||||
|
||||
public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) {
|
||||
this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;
|
||||
}
|
||||
|
||||
public boolean getIgnoreLeadingWhitespaces() {
|
||||
return this.ignoreLeadingWhitespaces;
|
||||
}
|
||||
|
||||
public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {
|
||||
this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;
|
||||
}
|
||||
|
||||
public boolean getIgnoreTrailingWhitespaces() {
|
||||
return this.ignoreTrailingWhitespaces;
|
||||
}
|
||||
|
||||
public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) {
|
||||
this.interpretUnicodeEscapes = interpretUnicodeEscapes;
|
||||
}
|
||||
|
||||
public boolean getUnicodeEscapeInterpretation() {
|
||||
return this.interpretUnicodeEscapes;
|
||||
}
|
||||
|
||||
public void setIgnoreEmptyLines(boolean ignoreEmptyLines) {
|
||||
this.ignoreEmptyLines = ignoreEmptyLines;
|
||||
}
|
||||
|
||||
public boolean getIgnoreEmptyLines() {
|
||||
return this.ignoreEmptyLines;
|
||||
}
|
||||
|
||||
public void setPrinterNewline(String newline) {
|
||||
this.printerNewline = newline;
|
||||
this.printerNewline = newline;
|
||||
}
|
||||
|
||||
public String getPrinterNewline() {
|
||||
return this.printerNewline;
|
||||
return this.printerNewline;
|
||||
}
|
||||
|
||||
public Object clone() {
|
||||
try {
|
||||
return super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new RuntimeException(e); // impossible
|
||||
}
|
||||
try {
|
||||
return super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new RuntimeException(e); // impossible
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,14 +30,14 @@ public class CSVUtils {
|
|||
|
||||
/**
|
||||
* <p><code>CSVUtils</code> instances should NOT be constructed in
|
||||
* standard programming.
|
||||
* standard programming.
|
||||
*
|
||||
* <p>This constructor is public to permit tools that require a JavaBean
|
||||
* instance to operate.</p>
|
||||
*/
|
||||
public CSVUtils() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts an array of string values into a single CSV line. All
|
||||
* <code>null</code> values are converted to the string <code>"null"</code>,
|
||||
|
@ -46,13 +46,13 @@ public class CSVUtils {
|
|||
*
|
||||
* @param values the value array
|
||||
* @return the CSV string, will be an empty string if the length of the
|
||||
* value array is 0
|
||||
* value array is 0
|
||||
*/
|
||||
public static String printLine(String[] values, CSVStrategy strategy) {
|
||||
// set up a CSVUtils
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
CSVPrinter csvPrinter = new CSVPrinter(stringWriter, strategy);
|
||||
|
||||
|
||||
// check for null values an "null" as strings and convert them
|
||||
// into the strings "null" and "\"null\""
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
|
@ -62,60 +62,60 @@ public class CSVUtils {
|
|||
values[i] = "\"null\"";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// convert to CSV
|
||||
try {
|
||||
csvPrinter.println(values);
|
||||
csvPrinter.println(values);
|
||||
} catch (IOException e) {
|
||||
// should not happen with StringWriter
|
||||
// should not happen with StringWriter
|
||||
}
|
||||
// as the resulting string has \r\n at the end, we will trim that away
|
||||
return stringWriter.toString().trim();
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// static parsers
|
||||
// ======================================================
|
||||
|
||||
/**
|
||||
* Parses the given String according to the default {@link CSVStrategy}.
|
||||
*
|
||||
* @param s CSV String to be parsed.
|
||||
* @return parsed String matrix (which is never null)
|
||||
* @throws IOException in case of error
|
||||
*/
|
||||
public static String[][] parse(String s) throws IOException {
|
||||
if (s == null) {
|
||||
throw new IllegalArgumentException("Null argument not allowed.");
|
||||
|
||||
// ======================================================
|
||||
// static parsers
|
||||
// ======================================================
|
||||
|
||||
/**
|
||||
* Parses the given String according to the default {@link CSVStrategy}.
|
||||
*
|
||||
* @param s CSV String to be parsed.
|
||||
* @return parsed String matrix (which is never null)
|
||||
* @throws IOException in case of error
|
||||
*/
|
||||
public static String[][] parse(String s) throws IOException {
|
||||
if (s == null) {
|
||||
throw new IllegalArgumentException("Null argument not allowed.");
|
||||
}
|
||||
String[][] result = (new CSVParser(new StringReader(s))).getAllValues();
|
||||
if (result == null) {
|
||||
// since CSVStrategy ignores empty lines an empty array is returned
|
||||
// (i.e. not "result = new String[][] {{""}};")
|
||||
result = EMPTY_DOUBLE_STRING_ARRAY;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
String[][] result = (new CSVParser(new StringReader(s))).getAllValues();
|
||||
if (result == null) {
|
||||
// since CSVStrategy ignores empty lines an empty array is returned
|
||||
// (i.e. not "result = new String[][] {{""}};")
|
||||
result = EMPTY_DOUBLE_STRING_ARRAY;
|
||||
|
||||
/**
|
||||
* Parses the first line only according to the default {@link CSVStrategy}.
|
||||
*
|
||||
* Parsing empty string will be handled as valid records containing zero
|
||||
* elements, so the following property holds: parseLine("").length == 0.
|
||||
*
|
||||
* @param s CSV String to be parsed.
|
||||
* @return parsed String vector (which is never null)
|
||||
* @throws IOException in case of error
|
||||
*/
|
||||
public static String[] parseLine(String s) throws IOException {
|
||||
if (s == null) {
|
||||
throw new IllegalArgumentException("Null argument not allowed.");
|
||||
}
|
||||
// uh,jh: make sure that parseLine("").length == 0
|
||||
if (s.length() == 0) {
|
||||
return EMPTY_STRING_ARRAY;
|
||||
}
|
||||
return (new CSVParser(new StringReader(s))).getLine();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the first line only according to the default {@link CSVStrategy}.
|
||||
*
|
||||
* Parsing empty string will be handled as valid records containing zero
|
||||
* elements, so the following property holds: parseLine("").length == 0.
|
||||
*
|
||||
* @param s CSV String to be parsed.
|
||||
* @return parsed String vector (which is never null)
|
||||
* @throws IOException in case of error
|
||||
*/
|
||||
public static String[] parseLine(String s) throws IOException {
|
||||
if (s == null) {
|
||||
throw new IllegalArgumentException("Null argument not allowed.");
|
||||
}
|
||||
// uh,jh: make sure that parseLine("").length == 0
|
||||
if (s.length() == 0) {
|
||||
return EMPTY_STRING_ARRAY;
|
||||
}
|
||||
return (new CSVParser(new StringReader(s))).getLine();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -19,11 +19,11 @@
|
|||
package org.apache.commons.csv;
|
||||
|
||||
/**
|
||||
* A simple StringBuffer replacement that aims to
|
||||
* A simple StringBuffer replacement that aims to
|
||||
* reduce copying as much as possible. The buffer
|
||||
* grows as necessary.
|
||||
* This class is not thread safe.
|
||||
*
|
||||
*
|
||||
* @author Ortwin Gl<EFBFBD>ck
|
||||
*/
|
||||
public class CharBuffer {
|
||||
|
@ -31,21 +31,21 @@ public class CharBuffer {
|
|||
private char[] c;
|
||||
|
||||
/**
|
||||
* Actually used number of characters in the array.
|
||||
* Actually used number of characters in the array.
|
||||
* It is also the index at which
|
||||
* a new character will be inserted into <code>c</code>.
|
||||
*/
|
||||
* a new character will be inserted into <code>c</code>.
|
||||
*/
|
||||
private int length;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new CharBuffer with an initial capacity of 32 characters.
|
||||
*/
|
||||
public CharBuffer() {
|
||||
this(32);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new CharBuffer with an initial capacity
|
||||
* Creates a new CharBuffer with an initial capacity
|
||||
* of <code>length</code> characters.
|
||||
*/
|
||||
public CharBuffer(final int length) {
|
||||
|
@ -54,16 +54,17 @@ public class CharBuffer {
|
|||
}
|
||||
this.c = new char[length];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Empties the buffer. The capacity still remains the same, so no memory is freed.
|
||||
*/
|
||||
public void clear() {
|
||||
length = 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of characters in the buffer.
|
||||
*
|
||||
* @return the number of characters
|
||||
*/
|
||||
public int length() {
|
||||
|
@ -72,16 +73,18 @@ public class CharBuffer {
|
|||
|
||||
/**
|
||||
* Returns the current capacity of the buffer.
|
||||
*
|
||||
* @return the maximum number of characters that can be stored in this buffer without
|
||||
* resizing it.
|
||||
* resizing it.
|
||||
*/
|
||||
public int capacity() {
|
||||
return c.length;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Appends the contents of <code>cb</code> to the end of this CharBuffer.
|
||||
*
|
||||
* @param cb the CharBuffer to append or null
|
||||
*/
|
||||
public void append(final CharBuffer cb) {
|
||||
|
@ -92,10 +95,11 @@ public class CharBuffer {
|
|||
System.arraycopy(cb.c, 0, c, length, cb.length);
|
||||
length += cb.length;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Appends <code>s</code> to the end of this CharBuffer.
|
||||
* This method involves copying the new data once!
|
||||
*
|
||||
* @param s the String to append or null
|
||||
*/
|
||||
public void append(final String s) {
|
||||
|
@ -104,10 +108,11 @@ public class CharBuffer {
|
|||
}
|
||||
append(s.toCharArray());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Appends <code>sb</code> to the end of this CharBuffer.
|
||||
* This method involves copying the new data once!
|
||||
*
|
||||
* @param sb the StringBuffer to append or null
|
||||
*/
|
||||
public void append(final StringBuffer sb) {
|
||||
|
@ -118,10 +123,11 @@ public class CharBuffer {
|
|||
sb.getChars(0, sb.length(), c, length);
|
||||
length += sb.length();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Appends <code>data</code> to the end of this CharBuffer.
|
||||
* This method involves copying the new data once!
|
||||
*
|
||||
* @param data the char[] to append or null
|
||||
*/
|
||||
public void append(final char[] data) {
|
||||
|
@ -132,10 +138,11 @@ public class CharBuffer {
|
|||
System.arraycopy(data, 0, c, length, data.length);
|
||||
length += data.length;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Appends a single character to the end of this CharBuffer.
|
||||
* This method involves copying the new data once!
|
||||
*
|
||||
* @param data the char to append
|
||||
*/
|
||||
public void append(final char data) {
|
||||
|
@ -143,7 +150,7 @@ public class CharBuffer {
|
|||
c[length] = data;
|
||||
length++;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Shrinks the capacity of the buffer to the current length if necessary.
|
||||
* This method involves copying the data once!
|
||||
|
@ -157,13 +164,13 @@ public class CharBuffer {
|
|||
c = newc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes trailing whitespace.
|
||||
*/
|
||||
/**
|
||||
* Removes trailing whitespace.
|
||||
*/
|
||||
public void trimTrailingWhitespace() {
|
||||
while (length>0 && Character.isWhitespace(c[length-1])) {
|
||||
length--;
|
||||
}
|
||||
while (length > 0 && Character.isWhitespace(c[length - 1])) {
|
||||
length--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -172,6 +179,7 @@ public class CharBuffer {
|
|||
* modifying it.
|
||||
* This method allows to avoid copying if the caller knows the exact capacity
|
||||
* before.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public char[] getCharacters() {
|
||||
|
@ -183,16 +191,17 @@ public class CharBuffer {
|
|||
return chars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the character at the specified position.
|
||||
*/
|
||||
/**
|
||||
* Returns the character at the specified position.
|
||||
*/
|
||||
public char charAt(int pos) {
|
||||
return c[pos];
|
||||
}
|
||||
return c[pos];
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the contents of the buffer into a StringBuffer.
|
||||
* This method involves copying the new data once!
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public StringBuffer toStringBuffer() {
|
||||
|
@ -200,25 +209,27 @@ public class CharBuffer {
|
|||
sb.append(c, 0, length);
|
||||
return sb;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts the contents of the buffer into a StringBuffer.
|
||||
* This method involves copying the new data once!
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toString() {
|
||||
return new String(c, 0, length);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Copies the data into a new array of at least <code>capacity</code> size.
|
||||
*
|
||||
* @param capacity
|
||||
*/
|
||||
public void provideCapacity(final int capacity) {
|
||||
if (c.length >= capacity) {
|
||||
return;
|
||||
}
|
||||
int newcapacity = ((capacity*3)>>1) + 1;
|
||||
int newcapacity = ((capacity * 3) >> 1) + 1;
|
||||
char[] newc = new char[newcapacity];
|
||||
System.arraycopy(c, 0, newc, 0, length);
|
||||
c = newc;
|
||||
|
|
|
@ -23,214 +23,223 @@ import java.io.Reader;
|
|||
/**
|
||||
* ExtendedBufferedReader
|
||||
*
|
||||
* A special reader decorater which supports more
|
||||
* A special reader decorator which supports more
|
||||
* sophisticated access to the underlying reader object.
|
||||
*
|
||||
*
|
||||
* In particular the reader supports a look-ahead option,
|
||||
* which allows you to see the next char returned by
|
||||
* next().
|
||||
*
|
||||
*/
|
||||
class ExtendedBufferedReader extends BufferedReader {
|
||||
class ExtendedBufferedReader extends BufferedReader {
|
||||
|
||||
|
||||
/** the end of stream symbol */
|
||||
public static final int END_OF_STREAM = -1;
|
||||
/** undefined state for the lookahead char */
|
||||
public static final int UNDEFINED = -2;
|
||||
|
||||
/** the lookahead chars */
|
||||
private int lookaheadChar = UNDEFINED;
|
||||
/** the last char returned */
|
||||
private int lastChar = UNDEFINED;
|
||||
/** the line counter */
|
||||
private int lineCounter = 0;
|
||||
private CharBuffer line = new CharBuffer();
|
||||
|
||||
/**
|
||||
* Created extended buffered reader using default buffer-size
|
||||
*
|
||||
*/
|
||||
public ExtendedBufferedReader(Reader r) {
|
||||
super(r);
|
||||
/* note uh: do not fetch the first char here,
|
||||
* because this might block the method!
|
||||
|
||||
/**
|
||||
* the end of stream symbol
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Create extended buffered reader using the given buffer-size
|
||||
*/
|
||||
public ExtendedBufferedReader(Reader r, int bufSize) {
|
||||
super(r, bufSize);
|
||||
/* note uh: do not fetch the first char here,
|
||||
* because this might block the method!
|
||||
public static final int END_OF_STREAM = -1;
|
||||
/**
|
||||
* undefined state for the lookahead char
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the next char from the input stream.
|
||||
* @return the next char or END_OF_STREAM if end of stream has been reached.
|
||||
*/
|
||||
public int read() throws IOException {
|
||||
// initalize the lookahead
|
||||
if (lookaheadChar == UNDEFINED) {
|
||||
lookaheadChar = super.read();
|
||||
public static final int UNDEFINED = -2;
|
||||
|
||||
/**
|
||||
* the lookahead chars
|
||||
*/
|
||||
private int lookaheadChar = UNDEFINED;
|
||||
/**
|
||||
* the last char returned
|
||||
*/
|
||||
private int lastChar = UNDEFINED;
|
||||
/**
|
||||
* the line counter
|
||||
*/
|
||||
private int lineCounter = 0;
|
||||
private CharBuffer line = new CharBuffer();
|
||||
|
||||
/**
|
||||
* Created extended buffered reader using default buffer-size
|
||||
*/
|
||||
public ExtendedBufferedReader(Reader r) {
|
||||
super(r);
|
||||
/* note uh: do not fetch the first char here,
|
||||
* because this might block the method!
|
||||
*/
|
||||
}
|
||||
lastChar = lookaheadChar;
|
||||
if (super.ready()) {
|
||||
lookaheadChar = super.read();
|
||||
} else {
|
||||
lookaheadChar = UNDEFINED;
|
||||
|
||||
/**
|
||||
* Create extended buffered reader using the given buffer-size
|
||||
*/
|
||||
public ExtendedBufferedReader(Reader r, int bufSize) {
|
||||
super(r, bufSize);
|
||||
/* note uh: do not fetch the first char here,
|
||||
* because this might block the method!
|
||||
*/
|
||||
}
|
||||
if (lastChar == '\n') {
|
||||
lineCounter++;
|
||||
}
|
||||
return lastChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last read character again.
|
||||
*
|
||||
* @return the last read char or UNDEFINED
|
||||
*/
|
||||
public int readAgain() {
|
||||
return lastChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Non-blocking reading of len chars into buffer buf starting
|
||||
* at bufferposition off.
|
||||
*
|
||||
* performs an iteratative read on the underlying stream
|
||||
* as long as the following conditions hold:
|
||||
* - less than len chars have been read
|
||||
* - end of stream has not been reached
|
||||
* - next read is not blocking
|
||||
*
|
||||
* @return nof chars actually read or END_OF_STREAM
|
||||
*/
|
||||
public int read(char[] buf, int off, int len) throws IOException {
|
||||
// do not claim if len == 0
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// init lookahead, but do not block !!
|
||||
if (lookaheadChar == UNDEFINED) {
|
||||
if (ready()) {
|
||||
lookaheadChar = super.read();
|
||||
|
||||
/**
|
||||
* Reads the next char from the input stream.
|
||||
*
|
||||
* @return the next char or END_OF_STREAM if end of stream has been reached.
|
||||
*/
|
||||
public int read() throws IOException {
|
||||
// initialize the lookahead
|
||||
if (lookaheadChar == UNDEFINED) {
|
||||
lookaheadChar = super.read();
|
||||
}
|
||||
lastChar = lookaheadChar;
|
||||
if (super.ready()) {
|
||||
lookaheadChar = super.read();
|
||||
} else {
|
||||
return -1;
|
||||
lookaheadChar = UNDEFINED;
|
||||
}
|
||||
if (lastChar == '\n') {
|
||||
lineCounter++;
|
||||
}
|
||||
return lastChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last read character again.
|
||||
*
|
||||
* @return the last read char or UNDEFINED
|
||||
*/
|
||||
public int readAgain() {
|
||||
return lastChar;
|
||||
}
|
||||
|
||||
/**
|
||||
* Non-blocking reading of len chars into buffer buf starting
|
||||
* at bufferposition off.
|
||||
* <p/>
|
||||
* performs an iterative read on the underlying stream
|
||||
* as long as the following conditions hold:
|
||||
* - less than len chars have been read
|
||||
* - end of stream has not been reached
|
||||
* - next read is not blocking
|
||||
*
|
||||
* @return nof chars actually read or END_OF_STREAM
|
||||
*/
|
||||
public int read(char[] buf, int off, int len) throws IOException {
|
||||
// do not claim if len == 0
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// init lookahead, but do not block !!
|
||||
if (lookaheadChar == UNDEFINED) {
|
||||
if (ready()) {
|
||||
lookaheadChar = super.read();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// 'first read of underlying stream'
|
||||
if (lookaheadChar == -1) {
|
||||
return -1;
|
||||
}
|
||||
// continue until the lookaheadChar would block
|
||||
int cOff = off;
|
||||
while (len > 0 && ready()) {
|
||||
if (lookaheadChar == -1) {
|
||||
// eof stream reached, do not continue
|
||||
return cOff - off;
|
||||
} else {
|
||||
buf[cOff++] = (char) lookaheadChar;
|
||||
if (lookaheadChar == '\n') {
|
||||
lineCounter++;
|
||||
}
|
||||
lastChar = lookaheadChar;
|
||||
lookaheadChar = super.read();
|
||||
len--;
|
||||
}
|
||||
}
|
||||
return cOff - off;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A String containing the contents of the line, not
|
||||
* including any line-termination characters, or null
|
||||
* if the end of the stream has been reached
|
||||
*/
|
||||
public String readLine() throws IOException {
|
||||
|
||||
if (lookaheadChar == UNDEFINED) {
|
||||
lookaheadChar = super.read();
|
||||
}
|
||||
|
||||
line.clear(); //reuse
|
||||
|
||||
// return null if end of stream has been reached
|
||||
if (lookaheadChar == END_OF_STREAM) {
|
||||
return null;
|
||||
}
|
||||
// do we have a line termination already
|
||||
char laChar = (char) lookaheadChar;
|
||||
if (laChar == '\n' || laChar == '\r') {
|
||||
lastChar = lookaheadChar;
|
||||
lookaheadChar = super.read();
|
||||
// ignore '\r\n' as well
|
||||
if ((char) lookaheadChar == '\n') {
|
||||
lastChar = lookaheadChar;
|
||||
lookaheadChar = super.read();
|
||||
}
|
||||
lineCounter++;
|
||||
return line.toString();
|
||||
}
|
||||
|
||||
// create the rest-of-line return and update the lookahead
|
||||
line.append(laChar);
|
||||
String restOfLine = super.readLine(); // TODO involves copying
|
||||
lastChar = lookaheadChar;
|
||||
lookaheadChar = super.read();
|
||||
if (restOfLine != null) {
|
||||
line.append(restOfLine);
|
||||
}
|
||||
lineCounter++;
|
||||
return line.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsupported
|
||||
*/
|
||||
public long skip(long n) throws IllegalArgumentException, IOException {
|
||||
throw new UnsupportedOperationException("CSV has no reason to implement this");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next char in the stream without consuming it.
|
||||
*
|
||||
* Remember the next char read by read(..) will always be
|
||||
* identical to lookAhead().
|
||||
*
|
||||
* @return the next char (without consuming it) or END_OF_STREAM
|
||||
*/
|
||||
public int lookAhead() throws IOException {
|
||||
if (lookaheadChar == UNDEFINED) {
|
||||
lookaheadChar = super.read();
|
||||
}
|
||||
return lookaheadChar;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the nof line read
|
||||
*
|
||||
* @return the current-line-number (or -1)
|
||||
*/
|
||||
public int getLineNumber() {
|
||||
if (lineCounter > -1) {
|
||||
return lineCounter;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// 'first read of underlying stream'
|
||||
if (lookaheadChar == -1) {
|
||||
return -1;
|
||||
}
|
||||
// continue until the lookaheadChar would block
|
||||
int cOff = off;
|
||||
while (len > 0 && ready()) {
|
||||
if (lookaheadChar == -1) {
|
||||
// eof stream reached, do not continue
|
||||
return cOff - off;
|
||||
} else {
|
||||
buf[cOff++] = (char) lookaheadChar;
|
||||
if (lookaheadChar == '\n') {
|
||||
lineCounter++;
|
||||
}
|
||||
lastChar = lookaheadChar;
|
||||
lookaheadChar = super.read();
|
||||
len--;
|
||||
}
|
||||
}
|
||||
return cOff - off;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A String containing the contents of the line, not
|
||||
* including any line-termination characters, or null
|
||||
* if the end of the stream has been reached
|
||||
*/
|
||||
public String readLine() throws IOException {
|
||||
|
||||
if (lookaheadChar == UNDEFINED) {
|
||||
lookaheadChar = super.read();
|
||||
}
|
||||
|
||||
line.clear(); //reuse
|
||||
|
||||
// return null if end of stream has been reached
|
||||
if (lookaheadChar == END_OF_STREAM) {
|
||||
return null;
|
||||
}
|
||||
// do we have a line termination already
|
||||
char laChar = (char) lookaheadChar;
|
||||
if (laChar == '\n' || laChar == '\r') {
|
||||
lastChar = lookaheadChar;
|
||||
lookaheadChar = super.read();
|
||||
// ignore '\r\n' as well
|
||||
if ((char) lookaheadChar == '\n') {
|
||||
lastChar = lookaheadChar;
|
||||
lookaheadChar = super.read();
|
||||
}
|
||||
lineCounter++;
|
||||
return line.toString();
|
||||
}
|
||||
|
||||
// create the rest-of-line return and update the lookahead
|
||||
line.append(laChar);
|
||||
String restOfLine = super.readLine(); // TODO involves copying
|
||||
lastChar = lookaheadChar;
|
||||
lookaheadChar = super.read();
|
||||
if (restOfLine != null) {
|
||||
line.append(restOfLine);
|
||||
}
|
||||
lineCounter++;
|
||||
return line.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsupported
|
||||
*/
|
||||
public long skip(long n) throws IllegalArgumentException, IOException {
|
||||
throw new UnsupportedOperationException("CSV has no reason to implement this");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next char in the stream without consuming it.
|
||||
*
|
||||
* Remember the next char read by read(..) will always be
|
||||
* identical to lookAhead().
|
||||
*
|
||||
* @return the next char (without consuming it) or END_OF_STREAM
|
||||
*/
|
||||
public int lookAhead() throws IOException {
|
||||
if (lookaheadChar == UNDEFINED) {
|
||||
lookaheadChar = super.read();
|
||||
}
|
||||
return lookaheadChar;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the nof line read
|
||||
*
|
||||
* @return the current-line-number (or -1)
|
||||
*/
|
||||
public int getLineNumber() {
|
||||
if (lineCounter > -1) {
|
||||
return lineCounter;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsupported
|
||||
*/
|
||||
public boolean markSupported() {
|
||||
throw new UnsupportedOperationException("CSV has no reason to implement this");
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsupported
|
||||
*/
|
||||
public boolean markSupported() {
|
||||
throw new UnsupportedOperationException("CSV has no reason to implement this");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,69 +32,100 @@ import java.util.List;
|
|||
*/
|
||||
public class CSVConfig {
|
||||
|
||||
/** specifies if it is a fixed width csv file **/
|
||||
/**
|
||||
* specifies if it is a fixed width csv file *
|
||||
*/
|
||||
private boolean fixedWidth;
|
||||
/** list of fields **/
|
||||
/**
|
||||
* list of fields *
|
||||
*/
|
||||
private List fields;
|
||||
|
||||
/** Do no do any filling **/
|
||||
public static final int FILLNONE = 0;
|
||||
/** Fill content the the left. Mainly usable together with fixedWidth **/
|
||||
public static final int FILLLEFT = 1;
|
||||
/** Fill content to the right. Mainly usable together with fixedWidth **/
|
||||
public static final int FILLRIGHT = 2;
|
||||
|
||||
/** The fill pattern */
|
||||
private int fill;
|
||||
/** The fill char. Defaults to a space */
|
||||
private char fillChar = ' ';
|
||||
/** The seperator character. Defaults to , */
|
||||
private char delimiter = ',';
|
||||
/** The row separator. Defaults to \n */
|
||||
private String rowDelimiter = "\n";
|
||||
/** Should we ignore the delimiter. Defaults to false */
|
||||
private boolean ignoreDelimiter = false;
|
||||
/** the value delimiter. Defaults to " */
|
||||
private char valueDelimiter = '"';
|
||||
/** Should we ignore the value delimiter. Defaults to true */
|
||||
private boolean ignoreValueDelimiter = true;
|
||||
/** Specifies if we want to use a field header */
|
||||
private boolean fieldHeader = false;
|
||||
/** Specifies if the end of the line needs to be trimmed */
|
||||
private boolean endTrimmed = false;
|
||||
/**
|
||||
*
|
||||
* Do no do any filling *
|
||||
*/
|
||||
public static final int FILLNONE = 0;
|
||||
/**
|
||||
* Fill content the the left. Mainly usable together with fixedWidth *
|
||||
*/
|
||||
public static final int FILLLEFT = 1;
|
||||
/**
|
||||
* Fill content to the right. Mainly usable together with fixedWidth *
|
||||
*/
|
||||
public static final int FILLRIGHT = 2;
|
||||
|
||||
/**
|
||||
* The fill pattern
|
||||
*/
|
||||
private int fill;
|
||||
/**
|
||||
* The fill char. Defaults to a space
|
||||
*/
|
||||
private char fillChar = ' ';
|
||||
/**
|
||||
* The seperator character. Defaults to ,
|
||||
*/
|
||||
private char delimiter = ',';
|
||||
/**
|
||||
* The row separator. Defaults to \n
|
||||
*/
|
||||
private String rowDelimiter = "\n";
|
||||
/**
|
||||
* Should we ignore the delimiter. Defaults to false
|
||||
*/
|
||||
private boolean ignoreDelimiter = false;
|
||||
/**
|
||||
* the value delimiter. Defaults to "
|
||||
*/
|
||||
private char valueDelimiter = '"';
|
||||
/**
|
||||
* Should we ignore the value delimiter. Defaults to true
|
||||
*/
|
||||
private boolean ignoreValueDelimiter = true;
|
||||
/**
|
||||
* Specifies if we want to use a field header
|
||||
*/
|
||||
private boolean fieldHeader = false;
|
||||
/**
|
||||
* Specifies if the end of the line needs to be trimmed
|
||||
*/
|
||||
private boolean endTrimmed = false;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CSVConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return if the CSV file is fixedWidth
|
||||
*/
|
||||
public boolean isFixedWidth() {
|
||||
return fixedWidth;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Specify if the CSV file is fixed width.
|
||||
* Defaults to false
|
||||
*
|
||||
* @param fixedWidth the fixedwidth
|
||||
*/
|
||||
public void setFixedWidth(boolean fixedWidth) {
|
||||
this.fixedWidth = fixedWidth;
|
||||
}
|
||||
|
||||
|
||||
public void addField(CSVField field) {
|
||||
if (fields == null) {
|
||||
fields = new ArrayList();
|
||||
}
|
||||
fields.add(field);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the fields that should be used by the writer.
|
||||
* This will overwrite currently added fields completely!
|
||||
*
|
||||
* @param csvFields the csvfields array. If null it will do nothing
|
||||
*/
|
||||
public void setFields(CSVField[] csvFields) {
|
||||
|
@ -103,9 +134,10 @@ public class CSVConfig {
|
|||
}
|
||||
fields = new ArrayList(Arrays.asList(csvFields));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the fields that should be used by the writer
|
||||
*
|
||||
* @param csvField a collection with fields. If null it will do nothing
|
||||
*/
|
||||
public void setFields(Collection csvField) {
|
||||
|
@ -125,12 +157,12 @@ public class CSVConfig {
|
|||
}
|
||||
return csvFields;
|
||||
}
|
||||
|
||||
|
||||
public CSVField getField(String name) {
|
||||
if (fields == null || name == null) {
|
||||
return null;
|
||||
}
|
||||
for(int i = 0; i < fields.size(); i++) {
|
||||
for (int i = 0; i < fields.size(); i++) {
|
||||
CSVField field = (CSVField) fields.get(i);
|
||||
if (name.equals(field.getName())) {
|
||||
return field;
|
||||
|
@ -149,6 +181,7 @@ public class CSVConfig {
|
|||
/**
|
||||
* Set the fill pattern. Defaults to {@link #FILLNONE}
|
||||
* <br/>Other options are : {@link #FILLLEFT} and {@link #FILLRIGHT}
|
||||
*
|
||||
* @param fill the fill pattern.
|
||||
*/
|
||||
public void setFill(int fill) {
|
||||
|
@ -156,7 +189,6 @@ public class CSVConfig {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the fillchar. Defaults to a space.
|
||||
*/
|
||||
public char getFillChar() {
|
||||
|
@ -165,6 +197,7 @@ public class CSVConfig {
|
|||
|
||||
/**
|
||||
* Set the fill char
|
||||
*
|
||||
* @param fillChar the fill char
|
||||
*/
|
||||
public void setFillChar(char fillChar) {
|
||||
|
@ -180,6 +213,7 @@ public class CSVConfig {
|
|||
|
||||
/**
|
||||
* Set the delimiter to use
|
||||
*
|
||||
* @param delimiter the delimiter character.
|
||||
*/
|
||||
public void setDelimiter(char delimiter) {
|
||||
|
@ -195,6 +229,7 @@ public class CSVConfig {
|
|||
|
||||
/**
|
||||
* Set the rowDelimiter to use
|
||||
*
|
||||
* @param rowDelimiter the row delimiter character.
|
||||
*/
|
||||
public void setRowDelimiter(String rowDelimiter) {
|
||||
|
@ -209,7 +244,8 @@ public class CSVConfig {
|
|||
}
|
||||
|
||||
/**
|
||||
* Specify if the writer should ignore the delimiter.
|
||||
* Specify if the writer should ignore the delimiter.
|
||||
*
|
||||
* @param ignoreDelimiter defaults to false.
|
||||
*/
|
||||
public void setIgnoreDelimiter(boolean ignoreDelimiter) {
|
||||
|
@ -225,6 +261,7 @@ public class CSVConfig {
|
|||
|
||||
/**
|
||||
* Set the value delimiter to use
|
||||
*
|
||||
* @param valueDelimiter the value delimiter character.
|
||||
*/
|
||||
public void setValueDelimiter(char valueDelimiter) {
|
||||
|
@ -240,7 +277,8 @@ public class CSVConfig {
|
|||
}
|
||||
|
||||
/**
|
||||
* Specify if the writer should ignore the value delimiter.
|
||||
* Specify if the writer should ignore the value delimiter.
|
||||
*
|
||||
* @param ignoreValueDelimiter defaults to false.
|
||||
*/
|
||||
public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) {
|
||||
|
@ -253,16 +291,19 @@ public class CSVConfig {
|
|||
public boolean isFieldHeader() {
|
||||
return fieldHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if you want to use a field header.
|
||||
*
|
||||
* @param fieldHeader true or false.
|
||||
*/
|
||||
public void setFieldHeader(boolean fieldHeader) {
|
||||
this.fieldHeader = fieldHeader;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* TODO..
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
public boolean equals(Object obj) {
|
||||
|
@ -278,8 +319,9 @@ public class CSVConfig {
|
|||
/**
|
||||
* Creates a config based on a stream. It tries to guess<br/>
|
||||
* NOTE : The stream will be closed.
|
||||
* @param inputStream the inputstream.
|
||||
* @return the guessed config.
|
||||
*
|
||||
* @param inputStream the inputstream.
|
||||
* @return the guessed config.
|
||||
*/
|
||||
public static CSVConfig guessConfig(InputStream inputStream) {
|
||||
return null;
|
||||
|
@ -294,11 +336,12 @@ public class CSVConfig {
|
|||
|
||||
/**
|
||||
* Specify if the end of the line needs to be trimmed. Defaults to false.
|
||||
*
|
||||
* @param endTrimmed
|
||||
*/
|
||||
public void setEndTrimmed(boolean endTrimmed) {
|
||||
this.endTrimmed = endTrimmed;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -30,23 +30,27 @@ import java.io.InputStreamReader;
|
|||
*/
|
||||
public class CSVConfigGuesser {
|
||||
|
||||
/** The stream to read */
|
||||
/**
|
||||
* The stream to read
|
||||
*/
|
||||
private InputStream in;
|
||||
/**
|
||||
/**
|
||||
* if the file has a field header (need this info, to be able to guess better)
|
||||
* Defaults to false
|
||||
*/
|
||||
private boolean hasFieldHeader = false;
|
||||
/** The found config */
|
||||
protected CSVConfig config;
|
||||
|
||||
/**
|
||||
*
|
||||
* The found config
|
||||
*/
|
||||
protected CSVConfig config;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CSVConfigGuesser() {
|
||||
this.config = new CSVConfig();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param in the inputstream to guess from
|
||||
*/
|
||||
|
@ -54,23 +58,24 @@ public class CSVConfigGuesser {
|
|||
this();
|
||||
setInputStream(in);
|
||||
}
|
||||
|
||||
|
||||
public void setInputStream(InputStream in) {
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Allow override.
|
||||
*
|
||||
* @return the inputstream that was set.
|
||||
*/
|
||||
protected InputStream getInputStream() {
|
||||
return in;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Guess the config based on the first 10 (or less when less available)
|
||||
* Guess the config based on the first 10 (or less when less available)
|
||||
* records of a CSV file.
|
||||
*
|
||||
*
|
||||
* @return the guessed config.
|
||||
*/
|
||||
public CSVConfig guess() {
|
||||
|
@ -80,7 +85,7 @@ public class CSVConfigGuesser {
|
|||
String[] lines = new String[10];
|
||||
String line = null;
|
||||
int counter = 0;
|
||||
while ( (line = bIn.readLine()) != null && counter <= 10) {
|
||||
while ((line = bIn.readLine()) != null && counter <= 10) {
|
||||
lines[counter] = line;
|
||||
counter++;
|
||||
}
|
||||
|
@ -91,13 +96,13 @@ public class CSVConfigGuesser {
|
|||
lines = newLines;
|
||||
}
|
||||
analyseLines(lines);
|
||||
} catch(Exception e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch(Exception e) {
|
||||
} catch (Exception e) {
|
||||
// ignore exception.
|
||||
}
|
||||
}
|
||||
|
@ -107,15 +112,16 @@ public class CSVConfigGuesser {
|
|||
config = null;
|
||||
return conf;
|
||||
}
|
||||
|
||||
|
||||
protected void analyseLines(String[] lines) {
|
||||
guessFixedWidth(lines);
|
||||
guessFieldSeperator(lines);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Guess if this file is fixedwidth.
|
||||
* Just basing the fact on all lines being of the same length
|
||||
*
|
||||
* @param lines
|
||||
*/
|
||||
protected void guessFixedWidth(String[] lines) {
|
||||
|
@ -132,7 +138,7 @@ public class CSVConfigGuesser {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void guessFieldSeperator(String[] lines) {
|
||||
if (config.isFixedWidth()) {
|
||||
|
@ -142,7 +148,7 @@ public class CSVConfigGuesser {
|
|||
for (int i = 0; i < lines.length; i++) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void guessFixedWidthSeperator(String[] lines) {
|
||||
// keep track of the fieldlength
|
||||
int previousMatch = -1;
|
||||
|
@ -156,21 +162,21 @@ public class CSVConfigGuesser {
|
|||
if (last != lines[j].charAt(i)) {
|
||||
charMatches = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (charMatches) {
|
||||
if (previousMatch == -1) {
|
||||
previousMatch = 0;
|
||||
}
|
||||
CSVField field = new CSVField();
|
||||
field.setName("field"+config.getFields().length+1);
|
||||
field.setSize((i-previousMatch));
|
||||
field.setName("field" + config.getFields().length + 1);
|
||||
field.setSize((i - previousMatch));
|
||||
config.addField(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return if the field uses a field header. Defaults to false.
|
||||
*/
|
||||
public boolean hasFieldHeader() {
|
||||
|
@ -179,11 +185,12 @@ public class CSVConfigGuesser {
|
|||
|
||||
/**
|
||||
* Specify if the CSV file has a field header
|
||||
*
|
||||
* @param hasFieldHeader true or false
|
||||
*/
|
||||
public void setHasFieldHeader(boolean hasFieldHeader) {
|
||||
this.hasFieldHeader = hasFieldHeader;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ package org.apache.commons.csv.writer;
|
|||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
|
@ -32,7 +31,7 @@ public class CSVField {
|
|||
private boolean overrideFill;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
public CSVField() {
|
||||
}
|
||||
|
@ -59,9 +58,10 @@ public class CSVField {
|
|||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the name of the field
|
||||
*
|
||||
* @param name the name
|
||||
*/
|
||||
public void setName(String name) {
|
||||
|
@ -69,7 +69,6 @@ public class CSVField {
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the size of the field
|
||||
*/
|
||||
public int getSize() {
|
||||
|
@ -79,6 +78,7 @@ public class CSVField {
|
|||
/**
|
||||
* Set the size of the field.
|
||||
* The size will be ignored when fixedwidth is set to false in the CSVConfig
|
||||
*
|
||||
* @param size the size of the field.
|
||||
*/
|
||||
public void setSize(int size) {
|
||||
|
@ -94,16 +94,17 @@ public class CSVField {
|
|||
|
||||
/**
|
||||
* Sets overrideFill to true.
|
||||
*
|
||||
* @param fill the file pattern
|
||||
*/
|
||||
public void setFill(int fill) {
|
||||
overrideFill = true;
|
||||
this.fill = fill;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Does this field override fill ?
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean overrideFill() {
|
||||
|
|
|
@ -31,16 +31,21 @@ import java.util.Map;
|
|||
*/
|
||||
public class CSVWriter {
|
||||
|
||||
/** The CSV config **/
|
||||
private CSVConfig config;
|
||||
/** The writer **/
|
||||
private Writer writer;
|
||||
/**
|
||||
*
|
||||
* The CSV config *
|
||||
*/
|
||||
private CSVConfig config;
|
||||
/**
|
||||
* The writer *
|
||||
*/
|
||||
private Writer writer;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CSVWriter() {
|
||||
}
|
||||
|
||||
|
||||
public CSVWriter(CSVConfig config) {
|
||||
setConfig(config);
|
||||
}
|
||||
|
@ -56,12 +61,12 @@ public class CSVWriter {
|
|||
value = writeValue(fields[i], value);
|
||||
sb.append(value);
|
||||
}
|
||||
if (!config.isDelimiterIgnored() && fields.length != (i+1)) {
|
||||
if (!config.isDelimiterIgnored() && fields.length != (i + 1)) {
|
||||
sb.append(config.getDelimiter());
|
||||
}
|
||||
}
|
||||
if (config.isEndTrimmed()) {
|
||||
for (int i = sb.length()-1; i >= 0; i--) {
|
||||
for (int i = sb.length() - 1; i >= 0; i--) {
|
||||
System.out.println("i : " + i);
|
||||
if (Character.isWhitespace(sb.charAt(i))) {
|
||||
sb.deleteCharAt(i);
|
||||
|
@ -73,11 +78,11 @@ public class CSVWriter {
|
|||
sb.append(config.getRowDelimiter());
|
||||
String line = sb.toString();
|
||||
writer.write(line);
|
||||
} catch(Exception e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected String writeValue(CSVField field, String value) throws Exception {
|
||||
if (config.isFixedWidth()) {
|
||||
if (value.length() < field.getSize()) {
|
||||
|
@ -106,11 +111,11 @@ public class CSVWriter {
|
|||
}
|
||||
if (!config.isValueDelimiterIgnored()) {
|
||||
// add the value delimiter..
|
||||
value = config.getValueDelimiter()+value+config.getValueDelimiter();
|
||||
value = config.getValueDelimiter() + value + config.getValueDelimiter();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return the CVSConfig or null if not present
|
||||
*/
|
||||
|
@ -120,14 +125,16 @@ public class CSVWriter {
|
|||
|
||||
/**
|
||||
* Set the CSVConfig
|
||||
*
|
||||
* @param config the CVSConfig
|
||||
*/
|
||||
public void setConfig(CSVConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the writer to write the CSV file to.
|
||||
*
|
||||
* @param writer the writer.
|
||||
*/
|
||||
public void setWriter(Writer writer) {
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -30,200 +30,219 @@ import junit.framework.TestSuite;
|
|||
* CSVPrinterTest
|
||||
*/
|
||||
public class CSVPrinterTest extends TestCase {
|
||||
|
||||
String lineSeparator = "\n";
|
||||
|
||||
public void testPrinter1() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a", "b"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,b" + lineSeparator, sw.toString());
|
||||
}
|
||||
String lineSeparator = "\n";
|
||||
|
||||
public void testPrinter2() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a,b", "b"};
|
||||
printer.println(line1);
|
||||
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testPrinter3() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a, b", "b "};
|
||||
printer.println(line1);
|
||||
assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testPrinter4() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a", "b\"c"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testPrinter5() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a", "b\nc"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testPrinter6() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a", "b\r\nc"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testPrinter7() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a", "b\\c"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,b\\c" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testExcelPrinter1() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY);
|
||||
String[] line1 = {"a", "b"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,b" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testExcelPrinter2() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY);
|
||||
String[] line1 = {"a,b", "b"};
|
||||
printer.println(line1);
|
||||
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void testRandom() throws Exception {
|
||||
int iter=10000;
|
||||
strategy = CSVStrategy.DEFAULT_STRATEGY;
|
||||
doRandom(iter);
|
||||
strategy = CSVStrategy.EXCEL_STRATEGY;
|
||||
doRandom(iter);
|
||||
|
||||
// Strategy for MySQL
|
||||
strategy = new CSVStrategy('\t', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED,'\\',false, false, false, false);
|
||||
doRandom(iter);
|
||||
}
|
||||
|
||||
Random r = new Random();
|
||||
CSVStrategy strategy;
|
||||
|
||||
public void doRandom(int iter) throws Exception {
|
||||
for (int i=0; i<iter; i++) {
|
||||
doOneRandom();
|
||||
}
|
||||
}
|
||||
|
||||
public void doOneRandom() throws Exception {
|
||||
int nLines = r.nextInt(4)+1;
|
||||
int nCol = r.nextInt(3)+1;
|
||||
// nLines=1;nCol=2;
|
||||
String[][] lines = new String[nLines][];
|
||||
for (int i=0; i<nLines; i++) {
|
||||
String[] line = new String[nCol];
|
||||
lines[i] = line;
|
||||
for (int j=0; j<nCol; j++) {
|
||||
line[j] = randStr();
|
||||
}
|
||||
public void testPrinter1() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a", "b"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,b" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, strategy);
|
||||
|
||||
for (int i=0; i<nLines; i++) {
|
||||
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
|
||||
printer.println(lines[i]);
|
||||
public void testPrinter2() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a,b", "b"};
|
||||
printer.println(line1);
|
||||
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
printer.flush();
|
||||
String result = sw.toString();
|
||||
// System.out.println("### :" + printable(result));
|
||||
|
||||
StringReader reader = new StringReader(result);
|
||||
|
||||
CSVParser parser = new CSVParser(reader, strategy);
|
||||
String[][] parseResult = parser.getAllValues();
|
||||
|
||||
if (!equals(lines, parseResult)) {
|
||||
System.out.println("Printer output :" + printable(result));
|
||||
assertTrue(false);
|
||||
public void testPrinter3() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a, b", "b "};
|
||||
printer.println(line1);
|
||||
assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean equals(String[][] a, String[][] b) {
|
||||
if (a.length != b.length) {
|
||||
return false;
|
||||
public void testPrinter4() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a", "b\"c"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
|
||||
}
|
||||
for (int i=0; i<a.length; i++) {
|
||||
String[] linea = a[i];
|
||||
String[] lineb = b[i];
|
||||
if (linea.length != lineb.length) {
|
||||
return false;
|
||||
}
|
||||
for (int j=0; j<linea.length; j++) {
|
||||
String aval = linea[j];
|
||||
String bval = lineb[j];
|
||||
if (!aval.equals(bval)) {
|
||||
System.out.println("expected :" + printable(aval));
|
||||
System.out.println("got :" + printable(bval));
|
||||
return false;
|
||||
|
||||
public void testPrinter5() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a", "b\nc"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testPrinter6() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a", "b\r\nc"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testPrinter7() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||
String[] line1 = {"a", "b\\c"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,b\\c" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testExcelPrinter1() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY);
|
||||
String[] line1 = {"a", "b"};
|
||||
printer.println(line1);
|
||||
assertEquals("a,b" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testExcelPrinter2() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY);
|
||||
String[] line1 = {"a,b", "b"};
|
||||
printer.println(line1);
|
||||
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
|
||||
public void testRandom() throws Exception {
|
||||
int iter = 10000;
|
||||
strategy = CSVStrategy.DEFAULT_STRATEGY;
|
||||
doRandom(iter);
|
||||
strategy = CSVStrategy.EXCEL_STRATEGY;
|
||||
doRandom(iter);
|
||||
|
||||
// Strategy for MySQL
|
||||
strategy = new CSVStrategy('\t', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '\\', false, false, false, false);
|
||||
doRandom(iter);
|
||||
}
|
||||
|
||||
Random r = new Random();
|
||||
CSVStrategy strategy;
|
||||
|
||||
public void doRandom(int iter) throws Exception {
|
||||
for (int i = 0; i < iter; i++) {
|
||||
doOneRandom();
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static String printable(String s) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i=0; i<s.length(); i++) {
|
||||
char ch = s.charAt(i);
|
||||
if (ch<=' ' || ch>=128) {
|
||||
sb.append("(" + (int)ch + ")");
|
||||
} else {
|
||||
sb.append(ch);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
public void doOneRandom() throws Exception {
|
||||
int nLines = r.nextInt(4) + 1;
|
||||
int nCol = r.nextInt(3) + 1;
|
||||
// nLines=1;nCol=2;
|
||||
String[][] lines = new String[nLines][];
|
||||
for (int i = 0; i < nLines; i++) {
|
||||
String[] line = new String[nCol];
|
||||
lines[i] = line;
|
||||
for (int j = 0; j < nCol; j++) {
|
||||
line[j] = randStr();
|
||||
}
|
||||
}
|
||||
|
||||
public String randStr() {
|
||||
int sz = r.nextInt(20);
|
||||
// sz = r.nextInt(3);
|
||||
char[] buf = new char[sz];
|
||||
for (int i=0; i<sz; i++) {
|
||||
// stick in special chars with greater frequency
|
||||
char ch;
|
||||
int what = r.nextInt(20);
|
||||
switch (what) {
|
||||
case 0: ch = '\r'; break;
|
||||
case 1: ch = '\n'; break;
|
||||
case 2: ch = '\t'; break;
|
||||
case 3: ch = '\f'; break;
|
||||
case 4: ch = ' '; break;
|
||||
case 5: ch = ','; break;
|
||||
case 6: ch = '"'; break;
|
||||
case 7: ch = '\''; break;
|
||||
case 8: ch = '\\'; break;
|
||||
default: ch = (char)r.nextInt(300); break;
|
||||
// default: ch = 'a'; break;
|
||||
}
|
||||
buf[i] = ch;
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, strategy);
|
||||
|
||||
for (int i = 0; i < nLines; i++) {
|
||||
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
|
||||
printer.println(lines[i]);
|
||||
}
|
||||
|
||||
printer.flush();
|
||||
String result = sw.toString();
|
||||
// System.out.println("### :" + printable(result));
|
||||
|
||||
StringReader reader = new StringReader(result);
|
||||
|
||||
CSVParser parser = new CSVParser(reader, strategy);
|
||||
String[][] parseResult = parser.getAllValues();
|
||||
|
||||
if (!equals(lines, parseResult)) {
|
||||
System.out.println("Printer output :" + printable(result));
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean equals(String[][] a, String[][] b) {
|
||||
if (a.length != b.length) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
String[] linea = a[i];
|
||||
String[] lineb = b[i];
|
||||
if (linea.length != lineb.length) {
|
||||
return false;
|
||||
}
|
||||
for (int j = 0; j < linea.length; j++) {
|
||||
String aval = linea[j];
|
||||
String bval = lineb[j];
|
||||
if (!aval.equals(bval)) {
|
||||
System.out.println("expected :" + printable(aval));
|
||||
System.out.println("got :" + printable(bval));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static String printable(String s) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char ch = s.charAt(i);
|
||||
if (ch <= ' ' || ch >= 128) {
|
||||
sb.append("(" + (int) ch + ")");
|
||||
} else {
|
||||
sb.append(ch);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String randStr() {
|
||||
int sz = r.nextInt(20);
|
||||
// sz = r.nextInt(3);
|
||||
char[] buf = new char[sz];
|
||||
for (int i = 0; i < sz; i++) {
|
||||
// stick in special chars with greater frequency
|
||||
char ch;
|
||||
int what = r.nextInt(20);
|
||||
switch (what) {
|
||||
case 0:
|
||||
ch = '\r';
|
||||
break;
|
||||
case 1:
|
||||
ch = '\n';
|
||||
break;
|
||||
case 2:
|
||||
ch = '\t';
|
||||
break;
|
||||
case 3:
|
||||
ch = '\f';
|
||||
break;
|
||||
case 4:
|
||||
ch = ' ';
|
||||
break;
|
||||
case 5:
|
||||
ch = ',';
|
||||
break;
|
||||
case 6:
|
||||
ch = '"';
|
||||
break;
|
||||
case 7:
|
||||
ch = '\'';
|
||||
break;
|
||||
case 8:
|
||||
ch = '\\';
|
||||
break;
|
||||
default:
|
||||
ch = (char) r.nextInt(300);
|
||||
break;
|
||||
// default: ch = 'a'; break;
|
||||
}
|
||||
buf[i] = ch;
|
||||
}
|
||||
return new String(buf);
|
||||
}
|
||||
return new String(buf);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,68 +24,68 @@ import junit.framework.TestCase;
|
|||
* CSVStrategyTest
|
||||
*
|
||||
* The test are organized in three different sections:
|
||||
* The 'setter/getter' section, the lexer section and finally the strategy
|
||||
* section. In case a test fails, you should follow a top-down approach for
|
||||
* The 'setter/getter' section, the lexer section and finally the strategy
|
||||
* section. In case a test fails, you should follow a top-down approach for
|
||||
* fixing a potential bug (its likely that the strategy itself fails if the lexer
|
||||
* has problems...).
|
||||
*/
|
||||
public class CSVStrategyTest extends TestCase {
|
||||
|
||||
// ======================================================
|
||||
// getters / setters
|
||||
// ======================================================
|
||||
public void testGetSetCommentStart() {
|
||||
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
|
||||
strategy.setCommentStart('#');
|
||||
assertEquals(strategy.getCommentStart(), '#');
|
||||
strategy.setCommentStart('!');
|
||||
assertEquals(strategy.getCommentStart(), '!');
|
||||
}
|
||||
// ======================================================
|
||||
// getters / setters
|
||||
// ======================================================
|
||||
public void testGetSetCommentStart() {
|
||||
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
|
||||
strategy.setCommentStart('#');
|
||||
assertEquals(strategy.getCommentStart(), '#');
|
||||
strategy.setCommentStart('!');
|
||||
assertEquals(strategy.getCommentStart(), '!');
|
||||
}
|
||||
|
||||
public void testGetSetEncapsulator() {
|
||||
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
|
||||
strategy.setEncapsulator('"');
|
||||
assertEquals(strategy.getEncapsulator(), '"');
|
||||
strategy.setEncapsulator('\'');
|
||||
assertEquals(strategy.getEncapsulator(), '\'');
|
||||
}
|
||||
public void testGetSetEncapsulator() {
|
||||
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
|
||||
strategy.setEncapsulator('"');
|
||||
assertEquals(strategy.getEncapsulator(), '"');
|
||||
strategy.setEncapsulator('\'');
|
||||
assertEquals(strategy.getEncapsulator(), '\'');
|
||||
}
|
||||
|
||||
public void testGetSetDelimiter() {
|
||||
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
|
||||
strategy.setDelimiter(';');
|
||||
assertEquals(strategy.getDelimiter(), ';');
|
||||
strategy.setDelimiter(',');
|
||||
assertEquals(strategy.getDelimiter(), ',');
|
||||
strategy.setDelimiter('\t');
|
||||
assertEquals(strategy.getDelimiter(), '\t');
|
||||
}
|
||||
public void testGetSetDelimiter() {
|
||||
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
|
||||
strategy.setDelimiter(';');
|
||||
assertEquals(strategy.getDelimiter(), ';');
|
||||
strategy.setDelimiter(',');
|
||||
assertEquals(strategy.getDelimiter(), ',');
|
||||
strategy.setDelimiter('\t');
|
||||
assertEquals(strategy.getDelimiter(), '\t');
|
||||
}
|
||||
|
||||
public void testSetCSVStrategy() {
|
||||
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
|
||||
// default settings
|
||||
assertEquals(strategy.getDelimiter(), ',');
|
||||
assertEquals(strategy.getEncapsulator(), '"');
|
||||
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
||||
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
|
||||
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
||||
assertEquals(true, strategy.getIgnoreEmptyLines());
|
||||
// explicit csv settings
|
||||
assertEquals(strategy.getDelimiter(), ',');
|
||||
assertEquals(strategy.getEncapsulator(), '"');
|
||||
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
||||
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
|
||||
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
||||
assertEquals(true, strategy.getIgnoreEmptyLines());
|
||||
}
|
||||
|
||||
public void testSetExcelStrategy() {
|
||||
CSVStrategy strategy = CSVStrategy.EXCEL_STRATEGY;
|
||||
assertEquals(strategy.getDelimiter(), ',');
|
||||
assertEquals(strategy.getEncapsulator(), '"');
|
||||
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
||||
assertEquals(false, strategy.getIgnoreLeadingWhitespaces());
|
||||
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
||||
assertEquals(false, strategy.getIgnoreEmptyLines());
|
||||
}
|
||||
|
||||
public void testSetCSVStrategy() {
|
||||
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
|
||||
// default settings
|
||||
assertEquals(strategy.getDelimiter(), ',');
|
||||
assertEquals(strategy.getEncapsulator(), '"');
|
||||
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
||||
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
|
||||
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
||||
assertEquals(true, strategy.getIgnoreEmptyLines());
|
||||
// explicit csv settings
|
||||
assertEquals(strategy.getDelimiter(), ',');
|
||||
assertEquals(strategy.getEncapsulator(), '"');
|
||||
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
||||
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
|
||||
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
||||
assertEquals(true, strategy.getIgnoreEmptyLines());
|
||||
}
|
||||
|
||||
public void testSetExcelStrategy() {
|
||||
CSVStrategy strategy = CSVStrategy.EXCEL_STRATEGY;
|
||||
assertEquals(strategy.getDelimiter(), ',');
|
||||
assertEquals(strategy.getEncapsulator(), '"');
|
||||
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
||||
assertEquals(false, strategy.getIgnoreLeadingWhitespaces());
|
||||
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
||||
assertEquals(false, strategy.getIgnoreEmptyLines());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,127 +24,127 @@ import junit.framework.TestCase;
|
|||
* CSVUtilsTest
|
||||
*/
|
||||
public class CSVUtilsTest extends TestCase {
|
||||
|
||||
// ======================================================
|
||||
// static parser tests
|
||||
// ======================================================
|
||||
public void testParse1() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc\ndef");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(1, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def", data[1][0]);
|
||||
|
||||
// ======================================================
|
||||
// static parser tests
|
||||
// ======================================================
|
||||
public void testParse1() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc\ndef");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(1, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def", data[1][0]);
|
||||
}
|
||||
|
||||
public void testParse2() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc,def,\"ghi,jkl\"\ndef");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(3, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def", data[0][1]);
|
||||
assertEquals("ghi,jkl", data[0][2]);
|
||||
assertEquals("def", data[1][0]);
|
||||
String[][] data = CSVUtils.parse("abc,def,\"ghi,jkl\"\ndef");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(3, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def", data[0][1]);
|
||||
assertEquals("ghi,jkl", data[0][2]);
|
||||
assertEquals("def", data[1][0]);
|
||||
}
|
||||
|
||||
public void testParse3() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc,\"def\nghi\"\njkl");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(2, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def\nghi", data[0][1]);
|
||||
assertEquals("jkl", data[1][0]);
|
||||
String[][] data = CSVUtils.parse("abc,\"def\nghi\"\njkl");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(2, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def\nghi", data[0][1]);
|
||||
assertEquals("jkl", data[1][0]);
|
||||
}
|
||||
|
||||
public void testParse4() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc,\"def\\\\nghi\"\njkl");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(2, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
// an escape char in quotes only escapes a delimiter, not itself
|
||||
assertEquals("def\\\\nghi", data[0][1]);
|
||||
assertEquals("jkl", data[1][0]);
|
||||
String[][] data = CSVUtils.parse("abc,\"def\\\\nghi\"\njkl");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(2, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
// an escape char in quotes only escapes a delimiter, not itself
|
||||
assertEquals("def\\\\nghi", data[0][1]);
|
||||
assertEquals("jkl", data[1][0]);
|
||||
}
|
||||
|
||||
public void testParse5() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc,def\\nghi\njkl");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(2, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def\\nghi", data[0][1]);
|
||||
assertEquals("jkl", data[1][0]);
|
||||
String[][] data = CSVUtils.parse("abc,def\\nghi\njkl");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(2, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def\\nghi", data[0][1]);
|
||||
assertEquals("jkl", data[1][0]);
|
||||
}
|
||||
|
||||
|
||||
public void testParse6() throws IOException {
|
||||
String[][] data = CSVUtils.parse("");
|
||||
// default strategy is CSV, which ignores empty lines
|
||||
assertEquals(0, data.length);
|
||||
String[][] data = CSVUtils.parse("");
|
||||
// default strategy is CSV, which ignores empty lines
|
||||
assertEquals(0, data.length);
|
||||
}
|
||||
|
||||
|
||||
public void testParse7() throws IOException {
|
||||
boolean io = false;
|
||||
try {
|
||||
CSVUtils.parse(null);
|
||||
} catch (IllegalArgumentException e) {
|
||||
io = true;
|
||||
}
|
||||
assertTrue(io);
|
||||
boolean io = false;
|
||||
try {
|
||||
CSVUtils.parse(null);
|
||||
} catch (IllegalArgumentException e) {
|
||||
io = true;
|
||||
}
|
||||
assertTrue(io);
|
||||
}
|
||||
|
||||
|
||||
public void testParseLine1() throws IOException {
|
||||
String[] data = CSVUtils.parseLine("abc,def,ghi");
|
||||
assertEquals(3, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def", data[1]);
|
||||
assertEquals("ghi", data[2]);
|
||||
String[] data = CSVUtils.parseLine("abc,def,ghi");
|
||||
assertEquals(3, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def", data[1]);
|
||||
assertEquals("ghi", data[2]);
|
||||
}
|
||||
|
||||
public void testParseLine2() throws IOException {
|
||||
String[] data = CSVUtils.parseLine("abc,def,ghi\n");
|
||||
assertEquals(3, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def", data[1]);
|
||||
assertEquals("ghi", data[2]);
|
||||
String[] data = CSVUtils.parseLine("abc,def,ghi\n");
|
||||
assertEquals(3, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def", data[1]);
|
||||
assertEquals("ghi", data[2]);
|
||||
}
|
||||
|
||||
public void testParseLine3() throws IOException {
|
||||
String[] data = CSVUtils.parseLine("abc,\"def,ghi\"");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def,ghi", data[1]);
|
||||
String[] data = CSVUtils.parseLine("abc,\"def,ghi\"");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def,ghi", data[1]);
|
||||
}
|
||||
|
||||
public void testParseLine4() throws IOException {
|
||||
String[] data = CSVUtils.parseLine("abc,\"def\nghi\"");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def\nghi", data[1]);
|
||||
String[] data = CSVUtils.parseLine("abc,\"def\nghi\"");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def\nghi", data[1]);
|
||||
}
|
||||
|
||||
|
||||
public void testParseLine5() throws IOException {
|
||||
String[] data = CSVUtils.parseLine("");
|
||||
assertEquals(0, data.length);
|
||||
// assertEquals("", data[0]);
|
||||
String[] data = CSVUtils.parseLine("");
|
||||
assertEquals(0, data.length);
|
||||
// assertEquals("", data[0]);
|
||||
}
|
||||
|
||||
|
||||
public void testParseLine6() throws IOException {
|
||||
boolean io = false;
|
||||
try {
|
||||
CSVUtils.parseLine(null);
|
||||
} catch (IllegalArgumentException e) {
|
||||
io = true;
|
||||
}
|
||||
assertTrue(io);
|
||||
boolean io = false;
|
||||
try {
|
||||
CSVUtils.parseLine(null);
|
||||
} catch (IllegalArgumentException e) {
|
||||
io = true;
|
||||
}
|
||||
assertTrue(io);
|
||||
}
|
||||
|
||||
|
||||
public void testParseLine7() throws IOException {
|
||||
String[] res = CSVUtils.parseLine("");
|
||||
assertNotNull(res);
|
||||
assertEquals(0, res.length);
|
||||
String[] res = CSVUtils.parseLine("");
|
||||
assertNotNull(res);
|
||||
assertEquals(0, res.length);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ package org.apache.commons.csv;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Ortwin Glück
|
||||
*/
|
||||
public class CharBufferTest extends TestCase {
|
||||
|
@ -31,14 +30,14 @@ public class CharBufferTest extends TestCase {
|
|||
try {
|
||||
cb = new CharBuffer(0);
|
||||
fail("Should not be possible");
|
||||
} catch(IllegalArgumentException e) {
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
|
||||
cb = new CharBuffer(128);
|
||||
assertEquals(0, cb.length());
|
||||
}
|
||||
|
||||
|
||||
public void testAppendChar() {
|
||||
CharBuffer cb = new CharBuffer(1);
|
||||
String expected = "";
|
||||
|
@ -49,59 +48,59 @@ public class CharBufferTest extends TestCase {
|
|||
assertEquals(expected.length(), cb.length());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testAppendCharArray() {
|
||||
CharBuffer cb = new CharBuffer(1);
|
||||
char[] abcd = "abcd".toCharArray();
|
||||
String expected = "";
|
||||
for (int i=0; i<10; i++) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
cb.append(abcd);
|
||||
expected += "abcd";
|
||||
assertEquals(expected, cb.toString());
|
||||
assertEquals(4*(i+1), cb.length());
|
||||
assertEquals(4 * (i + 1), cb.length());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testAppendString() {
|
||||
CharBuffer cb = new CharBuffer(1);
|
||||
String abcd = "abcd";
|
||||
String expected = "";
|
||||
for (int i=0; i<10; i++) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
cb.append(abcd);
|
||||
expected += abcd;
|
||||
assertEquals(expected, cb.toString());
|
||||
assertEquals(4*(i+1), cb.length());
|
||||
assertEquals(4 * (i + 1), cb.length());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testAppendStringBuffer() {
|
||||
CharBuffer cb = new CharBuffer(1);
|
||||
StringBuffer abcd = new StringBuffer("abcd");
|
||||
String expected = "";
|
||||
for (int i=0; i<10; i++) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
cb.append(abcd);
|
||||
expected += "abcd";
|
||||
assertEquals(expected, cb.toString());
|
||||
assertEquals(4*(i+1), cb.length());
|
||||
assertEquals(4 * (i + 1), cb.length());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testAppendCharBuffer() {
|
||||
CharBuffer cb = new CharBuffer(1);
|
||||
CharBuffer abcd = new CharBuffer(17);
|
||||
abcd.append("abcd");
|
||||
String expected = "";
|
||||
for (int i=0; i<10; i++) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
cb.append(abcd);
|
||||
expected += "abcd";
|
||||
assertEquals(expected, cb.toString());
|
||||
assertEquals(4*(i+1), cb.length());
|
||||
assertEquals(4 * (i + 1), cb.length());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testShrink() {
|
||||
String data = "123456789012345678901234567890";
|
||||
|
||||
|
||||
CharBuffer cb = new CharBuffer(data.length() + 100);
|
||||
assertEquals(data.length() + 100, cb.capacity());
|
||||
cb.append(data);
|
||||
|
@ -112,24 +111,24 @@ public class CharBufferTest extends TestCase {
|
|||
assertEquals(data.length(), cb.length());
|
||||
assertEquals(data, cb.toString());
|
||||
}
|
||||
|
||||
|
||||
//-- the following test cases have been adapted from the HttpComponents project
|
||||
//-- written by Oleg Kalnichevski
|
||||
|
||||
|
||||
public void testSimpleAppend() throws Exception {
|
||||
CharBuffer buffer = new CharBuffer(16);
|
||||
assertEquals(16, buffer.capacity());
|
||||
assertEquals(16, buffer.capacity());
|
||||
assertEquals(0, buffer.length());
|
||||
char[] b1 = buffer.getCharacters();
|
||||
assertNotNull(b1);
|
||||
assertEquals(0, b1.length);
|
||||
assertEquals(0, buffer.length());
|
||||
|
||||
char[] tmp = new char[] { '1', '2', '3', '4'};
|
||||
|
||||
char[] tmp = new char[]{'1', '2', '3', '4'};
|
||||
buffer.append(tmp);
|
||||
assertEquals(16, buffer.capacity());
|
||||
assertEquals(16, buffer.capacity());
|
||||
assertEquals(4, buffer.length());
|
||||
|
||||
|
||||
char[] b2 = buffer.getCharacters();
|
||||
assertNotNull(b2);
|
||||
assertEquals(4, b2.length);
|
||||
|
@ -137,35 +136,35 @@ public class CharBufferTest extends TestCase {
|
|||
assertEquals(tmp[i], b2[i]);
|
||||
}
|
||||
assertEquals("1234", buffer.toString());
|
||||
|
||||
|
||||
buffer.clear();
|
||||
assertEquals(16, buffer.capacity());
|
||||
assertEquals(16, buffer.capacity());
|
||||
assertEquals(0, buffer.length());
|
||||
}
|
||||
|
||||
|
||||
public void testAppendString2() throws Exception {
|
||||
CharBuffer buffer = new CharBuffer(8);
|
||||
buffer.append("stuff");
|
||||
buffer.append(" and more stuff");
|
||||
assertEquals("stuff and more stuff", buffer.toString());
|
||||
}
|
||||
|
||||
|
||||
public void testAppendNull() throws Exception {
|
||||
CharBuffer buffer = new CharBuffer(8);
|
||||
|
||||
buffer.append((StringBuffer)null);
|
||||
assertEquals("", buffer.toString());
|
||||
|
||||
buffer.append((String)null);
|
||||
|
||||
buffer.append((StringBuffer) null);
|
||||
assertEquals("", buffer.toString());
|
||||
|
||||
buffer.append((CharBuffer)null);
|
||||
buffer.append((String) null);
|
||||
assertEquals("", buffer.toString());
|
||||
|
||||
buffer.append((char[])null);
|
||||
buffer.append((CharBuffer) null);
|
||||
assertEquals("", buffer.toString());
|
||||
|
||||
buffer.append((char[]) null);
|
||||
assertEquals("", buffer.toString());
|
||||
}
|
||||
|
||||
|
||||
public void testAppendCharArrayBuffer() throws Exception {
|
||||
CharBuffer buffer1 = new CharBuffer(8);
|
||||
buffer1.append(" and more stuff");
|
||||
|
@ -174,7 +173,7 @@ public class CharBufferTest extends TestCase {
|
|||
buffer2.append(buffer1);
|
||||
assertEquals("stuff and more stuff", buffer2.toString());
|
||||
}
|
||||
|
||||
|
||||
public void testAppendSingleChar() throws Exception {
|
||||
CharBuffer buffer = new CharBuffer(4);
|
||||
buffer.append('1');
|
||||
|
@ -185,7 +184,7 @@ public class CharBufferTest extends TestCase {
|
|||
buffer.append('6');
|
||||
assertEquals("123456", buffer.toString());
|
||||
}
|
||||
|
||||
|
||||
public void testProvideCapacity() throws Exception {
|
||||
CharBuffer buffer = new CharBuffer(4);
|
||||
buffer.provideCapacity(2);
|
||||
|
|
|
@ -25,139 +25,138 @@ import junit.framework.TestSuite;
|
|||
|
||||
/**
|
||||
* ExtendedBufferedReaderTest
|
||||
*
|
||||
*/
|
||||
public class ExtendedBufferedReaderTest extends TestCase {
|
||||
|
||||
// ======================================================
|
||||
// the test cases
|
||||
// ======================================================
|
||||
|
||||
public void testConstructors() {
|
||||
ExtendedBufferedReader br = new ExtendedBufferedReader(new StringReader(""));
|
||||
br = new ExtendedBufferedReader(new StringReader(""), 10);
|
||||
}
|
||||
|
||||
public void testReadLookahead1() throws Exception {
|
||||
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, getEBR("").read());
|
||||
ExtendedBufferedReader br = getEBR("1\n2\r3\n");
|
||||
assertEquals('1', br.lookAhead());
|
||||
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
|
||||
assertEquals('1', br.read());
|
||||
assertEquals('1', br.readAgain());
|
||||
// ======================================================
|
||||
// the test cases
|
||||
// ======================================================
|
||||
|
||||
assertEquals(0, br.getLineNumber());
|
||||
assertEquals('\n', br.lookAhead());
|
||||
assertEquals(0, br.getLineNumber());
|
||||
assertEquals('1', br.readAgain());
|
||||
assertEquals('\n', br.read());
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertEquals('\n', br.readAgain());
|
||||
assertEquals(1, br.getLineNumber());
|
||||
|
||||
assertEquals('2', br.lookAhead());
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertEquals('\n', br.readAgain());
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertEquals('2', br.read());
|
||||
assertEquals('2', br.readAgain());
|
||||
|
||||
assertEquals('\r', br.lookAhead());
|
||||
assertEquals('2', br.readAgain());
|
||||
assertEquals('\r', br.read());
|
||||
assertEquals('\r', br.readAgain());
|
||||
|
||||
assertEquals('3', br.lookAhead());
|
||||
assertEquals('\r', br.readAgain());
|
||||
assertEquals('3', br.read());
|
||||
assertEquals('3', br.readAgain());
|
||||
|
||||
assertEquals('\n', br.lookAhead());
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertEquals('3', br.readAgain());
|
||||
assertEquals('\n', br.read());
|
||||
assertEquals(2, br.getLineNumber());
|
||||
assertEquals('\n', br.readAgain());
|
||||
assertEquals(2, br.getLineNumber());
|
||||
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
|
||||
assertEquals('\n', br.readAgain());
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
|
||||
|
||||
}
|
||||
|
||||
public void testConstructors() {
|
||||
ExtendedBufferedReader br = new ExtendedBufferedReader(new StringReader(""));
|
||||
br = new ExtendedBufferedReader(new StringReader(""), 10);
|
||||
}
|
||||
|
||||
public void testReadLookahead2() throws Exception {
|
||||
char[] ref = new char[5];
|
||||
char[] res = new char[5];
|
||||
|
||||
ExtendedBufferedReader br = getEBR("");
|
||||
assertEquals(0, br.read(res, 0, 0));
|
||||
assertTrue(Arrays.equals(res, ref));
|
||||
|
||||
br = getEBR("abcdefg");
|
||||
ref[0] = 'a';
|
||||
ref[1] = 'b';
|
||||
ref[2] = 'c';
|
||||
assertEquals(3, br.read(res, 0, 3));
|
||||
assertTrue(Arrays.equals(res, ref));
|
||||
assertEquals('c', br.readAgain());
|
||||
|
||||
assertEquals('d', br.lookAhead());
|
||||
ref[4] = 'd';
|
||||
assertEquals(1, br.read(res, 4, 1));
|
||||
assertTrue(Arrays.equals(res, ref));
|
||||
assertEquals('d', br.readAgain());
|
||||
|
||||
}
|
||||
|
||||
public void testReadLine() throws Exception {
|
||||
ExtendedBufferedReader br = getEBR("");
|
||||
assertTrue(br.readLine() == null);
|
||||
|
||||
br = getEBR("\n");
|
||||
assertTrue(br.readLine().equals(""));
|
||||
assertTrue(br.readLine() == null);
|
||||
|
||||
br = getEBR("foo\n\nhello");
|
||||
assertEquals(0, br.getLineNumber());
|
||||
assertTrue(br.readLine().equals("foo"));
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertTrue(br.readLine().equals(""));
|
||||
assertEquals(2, br.getLineNumber());
|
||||
assertTrue(br.readLine().equals("hello"));
|
||||
assertEquals(3, br.getLineNumber());
|
||||
assertTrue(br.readLine() == null);
|
||||
assertEquals(3, br.getLineNumber());
|
||||
|
||||
br = getEBR("foo\n\nhello");
|
||||
assertEquals('f', br.read());
|
||||
assertEquals('o', br.lookAhead());
|
||||
assertTrue(br.readLine().equals("oo"));
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertEquals('\n', br.lookAhead());
|
||||
assertTrue(br.readLine().equals(""));
|
||||
assertEquals(2, br.getLineNumber());
|
||||
assertEquals('h', br.lookAhead());
|
||||
assertTrue(br.readLine().equals("hello"));
|
||||
assertTrue(br.readLine() == null);
|
||||
assertEquals(3, br.getLineNumber());
|
||||
|
||||
|
||||
br = getEBR("foo\rbaar\r\nfoo");
|
||||
assertTrue(br.readLine().equals("foo"));
|
||||
assertEquals('b', br.lookAhead());
|
||||
assertTrue(br.readLine().equals("baar"));
|
||||
assertEquals('f', br.lookAhead());
|
||||
assertTrue(br.readLine().equals("foo"));
|
||||
assertTrue(br.readLine() == null);
|
||||
}
|
||||
|
||||
private ExtendedBufferedReader getEBR(String s) {
|
||||
return new ExtendedBufferedReader(new StringReader(s));
|
||||
}
|
||||
public void testReadLookahead1() throws Exception {
|
||||
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, getEBR("").read());
|
||||
ExtendedBufferedReader br = getEBR("1\n2\r3\n");
|
||||
assertEquals('1', br.lookAhead());
|
||||
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
|
||||
assertEquals('1', br.read());
|
||||
assertEquals('1', br.readAgain());
|
||||
|
||||
assertEquals(0, br.getLineNumber());
|
||||
assertEquals('\n', br.lookAhead());
|
||||
assertEquals(0, br.getLineNumber());
|
||||
assertEquals('1', br.readAgain());
|
||||
assertEquals('\n', br.read());
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertEquals('\n', br.readAgain());
|
||||
assertEquals(1, br.getLineNumber());
|
||||
|
||||
assertEquals('2', br.lookAhead());
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertEquals('\n', br.readAgain());
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertEquals('2', br.read());
|
||||
assertEquals('2', br.readAgain());
|
||||
|
||||
assertEquals('\r', br.lookAhead());
|
||||
assertEquals('2', br.readAgain());
|
||||
assertEquals('\r', br.read());
|
||||
assertEquals('\r', br.readAgain());
|
||||
|
||||
assertEquals('3', br.lookAhead());
|
||||
assertEquals('\r', br.readAgain());
|
||||
assertEquals('3', br.read());
|
||||
assertEquals('3', br.readAgain());
|
||||
|
||||
assertEquals('\n', br.lookAhead());
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertEquals('3', br.readAgain());
|
||||
assertEquals('\n', br.read());
|
||||
assertEquals(2, br.getLineNumber());
|
||||
assertEquals('\n', br.readAgain());
|
||||
assertEquals(2, br.getLineNumber());
|
||||
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
|
||||
assertEquals('\n', br.readAgain());
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
|
||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void testReadLookahead2() throws Exception {
|
||||
char[] ref = new char[5];
|
||||
char[] res = new char[5];
|
||||
|
||||
ExtendedBufferedReader br = getEBR("");
|
||||
assertEquals(0, br.read(res, 0, 0));
|
||||
assertTrue(Arrays.equals(res, ref));
|
||||
|
||||
br = getEBR("abcdefg");
|
||||
ref[0] = 'a';
|
||||
ref[1] = 'b';
|
||||
ref[2] = 'c';
|
||||
assertEquals(3, br.read(res, 0, 3));
|
||||
assertTrue(Arrays.equals(res, ref));
|
||||
assertEquals('c', br.readAgain());
|
||||
|
||||
assertEquals('d', br.lookAhead());
|
||||
ref[4] = 'd';
|
||||
assertEquals(1, br.read(res, 4, 1));
|
||||
assertTrue(Arrays.equals(res, ref));
|
||||
assertEquals('d', br.readAgain());
|
||||
|
||||
}
|
||||
|
||||
public void testReadLine() throws Exception {
|
||||
ExtendedBufferedReader br = getEBR("");
|
||||
assertTrue(br.readLine() == null);
|
||||
|
||||
br = getEBR("\n");
|
||||
assertTrue(br.readLine().equals(""));
|
||||
assertTrue(br.readLine() == null);
|
||||
|
||||
br = getEBR("foo\n\nhello");
|
||||
assertEquals(0, br.getLineNumber());
|
||||
assertTrue(br.readLine().equals("foo"));
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertTrue(br.readLine().equals(""));
|
||||
assertEquals(2, br.getLineNumber());
|
||||
assertTrue(br.readLine().equals("hello"));
|
||||
assertEquals(3, br.getLineNumber());
|
||||
assertTrue(br.readLine() == null);
|
||||
assertEquals(3, br.getLineNumber());
|
||||
|
||||
br = getEBR("foo\n\nhello");
|
||||
assertEquals('f', br.read());
|
||||
assertEquals('o', br.lookAhead());
|
||||
assertTrue(br.readLine().equals("oo"));
|
||||
assertEquals(1, br.getLineNumber());
|
||||
assertEquals('\n', br.lookAhead());
|
||||
assertTrue(br.readLine().equals(""));
|
||||
assertEquals(2, br.getLineNumber());
|
||||
assertEquals('h', br.lookAhead());
|
||||
assertTrue(br.readLine().equals("hello"));
|
||||
assertTrue(br.readLine() == null);
|
||||
assertEquals(3, br.getLineNumber());
|
||||
|
||||
|
||||
br = getEBR("foo\rbaar\r\nfoo");
|
||||
assertTrue(br.readLine().equals("foo"));
|
||||
assertEquals('b', br.lookAhead());
|
||||
assertTrue(br.readLine().equals("baar"));
|
||||
assertEquals('f', br.lookAhead());
|
||||
assertTrue(br.readLine().equals("foo"));
|
||||
assertTrue(br.readLine() == null);
|
||||
}
|
||||
|
||||
private ExtendedBufferedReader getEBR(String s) {
|
||||
return new ExtendedBufferedReader(new StringReader(s));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,10 +41,10 @@ public class CSVConfigGuesserTest extends TestCase {
|
|||
guesser.setHasFieldHeader(true);
|
||||
assertEquals(true, guesser.hasFieldHeader());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a format like
|
||||
* 1234 ; abcd ; 1234 ;
|
||||
*
|
||||
* 1234 ; abcd ; 1234 ;
|
||||
*/
|
||||
public void testConfigGuess1() {
|
||||
CSVConfig expected = new CSVConfig();
|
||||
|
@ -67,11 +67,11 @@ public class CSVConfigGuesserTest extends TestCase {
|
|||
assertEquals(expected.getFields().length, guessed.getFields().length);
|
||||
assertEquals(expected.getFields()[0].getSize(), guessed.getFields()[0].getSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a format like
|
||||
* 1234,123123,12312312,213123
|
||||
* 1,2,3,4
|
||||
*
|
||||
* 1234,123123,12312312,213123
|
||||
* 1,2,3,4
|
||||
*/
|
||||
public void testConfigGuess2() {
|
||||
CSVConfig expected = new CSVConfig();
|
||||
|
|
|
@ -29,7 +29,7 @@ import junit.framework.TestCase;
|
|||
* @version $Id: $
|
||||
*/
|
||||
public class CSVConfigTest extends TestCase {
|
||||
|
||||
|
||||
|
||||
public void testFixedWith() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
|
@ -37,13 +37,13 @@ public class CSVConfigTest extends TestCase {
|
|||
config.setFixedWidth(true);
|
||||
assertEquals(true, config.isFixedWidth());
|
||||
}
|
||||
|
||||
|
||||
public void testFields() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(0, config.getFields().length);
|
||||
config.setFields((CSVField[])null);
|
||||
config.setFields((CSVField[]) null);
|
||||
assertEquals(0, config.getFields().length);
|
||||
config.setFields((Collection)null);
|
||||
config.setFields((Collection) null);
|
||||
assertEquals(0, config.getFields().length);
|
||||
CSVField field = new CSVField();
|
||||
field.setName("field1");
|
||||
|
@ -53,7 +53,7 @@ public class CSVConfigTest extends TestCase {
|
|||
assertEquals(null, config.getField("field11"));
|
||||
assertEquals(field, config.getField("field1"));
|
||||
}
|
||||
|
||||
|
||||
public void testFill() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(CSVConfig.FILLNONE, config.getFill());
|
||||
|
@ -65,7 +65,7 @@ public class CSVConfigTest extends TestCase {
|
|||
config.setFillChar('m');
|
||||
assertEquals('m', config.getFillChar());
|
||||
}
|
||||
|
||||
|
||||
public void testDelimiter() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(',', config.getDelimiter());
|
||||
|
@ -75,7 +75,7 @@ public class CSVConfigTest extends TestCase {
|
|||
config.setIgnoreDelimiter(true);
|
||||
assertEquals(true, config.isDelimiterIgnored());
|
||||
}
|
||||
|
||||
|
||||
public void testValueDelimiter() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals('"', config.getValueDelimiter());
|
||||
|
@ -85,14 +85,14 @@ public class CSVConfigTest extends TestCase {
|
|||
config.setIgnoreValueDelimiter(false);
|
||||
assertEquals(false, config.isValueDelimiterIgnored());
|
||||
}
|
||||
|
||||
|
||||
public void testFieldHeader() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(false, config.isFieldHeader());
|
||||
config.setFieldHeader(true);
|
||||
assertEquals(true, config.isFieldHeader());
|
||||
}
|
||||
|
||||
|
||||
public void testTrimEnd() {
|
||||
CSVConfig config = new CSVConfig();
|
||||
assertEquals(false, config.isEndTrimmed());
|
||||
|
|
|
@ -21,7 +21,6 @@ package org.apache.commons.csv.writer;
|
|||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
|
@ -41,7 +40,7 @@ public class CSVFieldTest extends TestCase {
|
|||
assertEquals("name", field.getName());
|
||||
assertEquals(10, field.getSize());
|
||||
}
|
||||
|
||||
|
||||
public void testFill() {
|
||||
CSVField field = new CSVField();
|
||||
assertEquals(CSVConfig.FILLNONE, field.getFill());
|
||||
|
|
|
@ -26,17 +26,17 @@ import junit.framework.TestCase;
|
|||
|
||||
/**
|
||||
* The testcase for the csv writer.
|
||||
*
|
||||
*
|
||||
* @author Martin van den Bemt
|
||||
* @version $Id: $
|
||||
*/
|
||||
public class CSVWriterTest extends TestCase {
|
||||
|
||||
private Map map;
|
||||
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
|
||||
map = new HashMap();
|
||||
map.put("field1", "12345");
|
||||
map.put("field2", "1234");
|
||||
|
|
Loading…
Reference in New Issue