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 {
|
public class CSVPrinter {
|
||||||
|
|
||||||
/** The place that the values get written. */
|
/**
|
||||||
protected final Writer out;
|
* The place that the values get written.
|
||||||
protected final CSVStrategy strategy;
|
*/
|
||||||
|
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
|
* Create a printer that will print values to the given
|
||||||
* stream following the CSVStrategy.
|
* stream following the CSVStrategy.
|
||||||
*
|
* <p/>
|
||||||
* Currently, only a pure encapsulation strategy or a pure escaping strategy
|
* 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.
|
* is supported. Hybrid strategies (encapsulation and escaping with a different character) are not supported.
|
||||||
*
|
*
|
||||||
* @param out stream to which to print.
|
* @param out stream to which to print.
|
||||||
* @param strategy describes the CSV variation.
|
* @param strategy describes the CSV variation.
|
||||||
*/
|
*/
|
||||||
public CSVPrinter(Writer out, CSVStrategy strategy) {
|
public CSVPrinter(Writer out, CSVStrategy strategy) {
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.strategy = strategy==null ? CSVStrategy.DEFAULT_STRATEGY : strategy;
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strategy.getEncapsulator() != CSVStrategy.ENCAPSULATOR_DISABLED) {
|
// ======================================================
|
||||||
printAndEncapsulate(value, offset, len);
|
// printing implementation
|
||||||
} else if (strategy.getEscape() != CSVStrategy.ESCAPE_DISABLED) {
|
// ======================================================
|
||||||
printAndEscape(value, offset, len);
|
|
||||||
} else {
|
/**
|
||||||
printSep();
|
* Output a blank line
|
||||||
out.write(value, offset, len);
|
*/
|
||||||
|
public void println() throws IOException {
|
||||||
|
out.write(strategy.getPrinterNewline());
|
||||||
|
newLine = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void printSep() throws IOException {
|
public void flush() throws IOException {
|
||||||
if (newLine) {
|
out.flush();
|
||||||
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();
|
/**
|
||||||
|
* Print a single line of comma separated values.
|
||||||
char delim = this.strategy.getDelimiter();
|
* The values will be quoted if needed. Quotes and
|
||||||
char escape = this.strategy.getEscape();
|
* newLine characters will be escaped.
|
||||||
|
*
|
||||||
while (pos < end) {
|
* @param values values to be outputted.
|
||||||
char c = value[pos];
|
*/
|
||||||
if (c == '\r' || c=='\n' || c==delim || c==escape) {
|
public void println(String[] values) throws IOException {
|
||||||
// write out segment up until this char
|
for (int i = 0; i < values.length; i++) {
|
||||||
int l = pos-start;
|
print(values[i]);
|
||||||
if (l>0) {
|
|
||||||
out.write(value, start, l);
|
|
||||||
}
|
}
|
||||||
if (c=='\n') c='n';
|
println();
|
||||||
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) {
|
* Put a comment among the comma separated values.
|
||||||
out.write(value, start, l);
|
* 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();
|
if (strategy.getEncapsulator() != CSVStrategy.ENCAPSULATOR_DISABLED) {
|
||||||
char encapsulator = this.strategy.getEncapsulator();
|
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) {
|
void printSep() throws IOException {
|
||||||
// always quote an empty token that is the first
|
if (newLine) {
|
||||||
// on the line, as it may be the only thing on the
|
newLine = false;
|
||||||
// line. If it were not quoted in that case,
|
} else {
|
||||||
// an empty line has no tokens.
|
out.write(this.strategy.getDelimiter());
|
||||||
if (first) {
|
}
|
||||||
quote = true;
|
}
|
||||||
}
|
|
||||||
} else {
|
void printAndEscape(char[] value, int offset, int len) throws IOException {
|
||||||
char c = value[pos];
|
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) {
|
while (pos < end) {
|
||||||
c = value[pos];
|
char c = value[pos];
|
||||||
if (c=='\n' || c=='\r' || c==encapsulator || c==delim) {
|
if (c == '\r' || c == '\n' || c == delim || c == escape) {
|
||||||
quote = true;
|
// write out segment up until this char
|
||||||
break;
|
int l = pos - start;
|
||||||
}
|
if (l > 0) {
|
||||||
pos++;
|
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) {
|
if (!quote) {
|
||||||
pos = end-1;
|
// no encapsulation needed - write out the original value
|
||||||
c = value[pos];
|
out.write(value, offset, len);
|
||||||
// if (c == ' ' || c == '\f' || c == '\t') {
|
return;
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// 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
|
* Print the string as the next value on the line. The value
|
||||||
out.write(value, offset, len);
|
* will be escaped or encapsulated as needed if checkForEscape==true
|
||||||
return;
|
*
|
||||||
|
* @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);
|
* Print the string as the next value on the line. The value
|
||||||
|
* will be escaped or encapsulated as needed.
|
||||||
// Pick up where we left off: pos should be positioned on the first character that caused
|
*
|
||||||
// the need for encapsulation.
|
* @param value value to be outputted.
|
||||||
while (pos<end) {
|
*/
|
||||||
char c = value[pos];
|
public void print(String value) throws IOException {
|
||||||
if (c==encapsulator) {
|
print(value, true);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,16 +41,16 @@ public class CSVStrategy implements Cloneable, Serializable {
|
||||||
// an EOF signal (-1), and because \ufffe in UTF-16 would be
|
// an EOF signal (-1), and because \ufffe in UTF-16 would be
|
||||||
// encoded as two chars (using surrogates) and thus there should never
|
// encoded as two chars (using surrogates) and thus there should never
|
||||||
// be a collision with a real text char.
|
// be a collision with a real text char.
|
||||||
public static char COMMENTS_DISABLED = (char)-2;
|
public static char COMMENTS_DISABLED = (char) -2;
|
||||||
public static char ESCAPE_DISABLED = (char)-2;
|
public static char ESCAPE_DISABLED = (char) -2;
|
||||||
public static char ENCAPSULATOR_DISABLED = (char)-2;
|
public static char ENCAPSULATOR_DISABLED = (char) -2;
|
||||||
|
|
||||||
public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
|
public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
|
||||||
true, false, true);
|
true, false, true);
|
||||||
public static CSVStrategy EXCEL_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false,
|
public static CSVStrategy EXCEL_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false,
|
||||||
false, false, false);
|
false, false, false);
|
||||||
public static CSVStrategy TDF_STRATEGY = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
|
public static CSVStrategy TDF_STRATEGY = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
|
||||||
true, false, true);
|
true, false, true);
|
||||||
|
|
||||||
|
|
||||||
public CSVStrategy(char delimiter, char encapsulator, char commentStart) {
|
public CSVStrategy(char delimiter, char encapsulator, char commentStart) {
|
||||||
|
@ -60,28 +60,27 @@ public class CSVStrategy implements Cloneable, Serializable {
|
||||||
/**
|
/**
|
||||||
* Customized CSV strategy setter.
|
* Customized CSV strategy setter.
|
||||||
*
|
*
|
||||||
* @param delimiter a Char used for value separation
|
* @param delimiter a Char used for value separation
|
||||||
* @param encapsulator a Char used as value encapsulation marker
|
* @param encapsulator a Char used as value encapsulation marker
|
||||||
* @param commentStart a Char used for comment identification
|
* @param commentStart a Char used for comment identification
|
||||||
* @param escape a Char used to escape special characters in values
|
* @param escape a Char used to escape special characters in values
|
||||||
* @param ignoreLeadingWhitespace TRUE when leading whitespaces should be
|
* @param ignoreLeadingWhitespace TRUE when leading whitespaces should be
|
||||||
* ignored
|
* ignored
|
||||||
* @param ignoreTrailingWhitespace TRUE when trailing whitespaces should be
|
* @param ignoreTrailingWhitespace TRUE when trailing whitespaces should be
|
||||||
* ignored
|
* ignored
|
||||||
* @param interpretUnicodeEscapes TRUE when unicode escapes should be
|
* @param interpretUnicodeEscapes TRUE when unicode escapes should be
|
||||||
* interpreted
|
* interpreted
|
||||||
* @param ignoreEmptyLines TRUE when the parser should skip emtpy lines
|
* @param ignoreEmptyLines TRUE when the parser should skip emtpy lines
|
||||||
*/
|
*/
|
||||||
public CSVStrategy(
|
public CSVStrategy(
|
||||||
char delimiter,
|
char delimiter,
|
||||||
char encapsulator,
|
char encapsulator,
|
||||||
char commentStart,
|
char commentStart,
|
||||||
char escape,
|
char escape,
|
||||||
boolean ignoreLeadingWhitespace,
|
boolean ignoreLeadingWhitespace,
|
||||||
boolean ignoreTrailingWhitespace,
|
boolean ignoreTrailingWhitespace,
|
||||||
boolean interpretUnicodeEscapes,
|
boolean interpretUnicodeEscapes,
|
||||||
boolean ignoreEmptyLines)
|
boolean ignoreEmptyLines) {
|
||||||
{
|
|
||||||
setDelimiter(delimiter);
|
setDelimiter(delimiter);
|
||||||
setEncapsulator(encapsulator);
|
setEncapsulator(encapsulator);
|
||||||
setCommentStart(commentStart);
|
setCommentStart(commentStart);
|
||||||
|
@ -92,62 +91,101 @@ public class CSVStrategy implements Cloneable, Serializable {
|
||||||
setIgnoreEmptyLines(ignoreEmptyLines);
|
setIgnoreEmptyLines(ignoreEmptyLines);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @deprecated */
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
public CSVStrategy(
|
public CSVStrategy(
|
||||||
char delimiter,
|
char delimiter,
|
||||||
char encapsulator,
|
char encapsulator,
|
||||||
char commentStart,
|
char commentStart,
|
||||||
boolean ignoreLeadingWhitespace,
|
boolean ignoreLeadingWhitespace,
|
||||||
boolean interpretUnicodeEscapes,
|
boolean interpretUnicodeEscapes,
|
||||||
boolean ignoreEmptyLines)
|
boolean ignoreEmptyLines) {
|
||||||
{
|
|
||||||
this(delimiter, encapsulator, commentStart, CSVStrategy.ESCAPE_DISABLED, ignoreLeadingWhitespace,
|
this(delimiter, encapsulator, commentStart, CSVStrategy.ESCAPE_DISABLED, ignoreLeadingWhitespace,
|
||||||
true, interpretUnicodeEscapes, ignoreEmptyLines);
|
true, interpretUnicodeEscapes, ignoreEmptyLines);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDelimiter(char delimiter) { this.delimiter = delimiter; }
|
public void setDelimiter(char delimiter) {
|
||||||
public char getDelimiter() { return this.delimiter; }
|
this.delimiter = delimiter;
|
||||||
|
}
|
||||||
|
|
||||||
public void setEncapsulator(char encapsulator) { this.encapsulator = encapsulator; }
|
public char getDelimiter() {
|
||||||
public char getEncapsulator() { return this.encapsulator; }
|
return this.delimiter;
|
||||||
|
}
|
||||||
|
|
||||||
public void setCommentStart(char commentStart) { this.commentStart = commentStart; }
|
public void setEncapsulator(char encapsulator) {
|
||||||
public char getCommentStart() { return this.commentStart; }
|
this.encapsulator = encapsulator;
|
||||||
public boolean isCommentingDisabled() { return this.commentStart == COMMENTS_DISABLED; }
|
}
|
||||||
|
|
||||||
public void setEscape(char escape) { this.escape = escape; }
|
public char getEncapsulator() {
|
||||||
public char getEscape() { return this.escape; }
|
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) {
|
public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) {
|
||||||
this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;
|
this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;
|
||||||
}
|
}
|
||||||
public boolean getIgnoreLeadingWhitespaces() { return this.ignoreLeadingWhitespaces; }
|
|
||||||
|
public boolean getIgnoreLeadingWhitespaces() {
|
||||||
|
return this.ignoreLeadingWhitespaces;
|
||||||
|
}
|
||||||
|
|
||||||
public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {
|
public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {
|
||||||
this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;
|
this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;
|
||||||
}
|
}
|
||||||
public boolean getIgnoreTrailingWhitespaces() { return this.ignoreTrailingWhitespaces; }
|
|
||||||
|
public boolean getIgnoreTrailingWhitespaces() {
|
||||||
|
return this.ignoreTrailingWhitespaces;
|
||||||
|
}
|
||||||
|
|
||||||
public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) {
|
public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) {
|
||||||
this.interpretUnicodeEscapes = interpretUnicodeEscapes;
|
this.interpretUnicodeEscapes = interpretUnicodeEscapes;
|
||||||
}
|
}
|
||||||
public boolean getUnicodeEscapeInterpretation() { return this.interpretUnicodeEscapes; }
|
|
||||||
|
|
||||||
public void setIgnoreEmptyLines(boolean ignoreEmptyLines) { this.ignoreEmptyLines = ignoreEmptyLines; }
|
public boolean getUnicodeEscapeInterpretation() {
|
||||||
public boolean getIgnoreEmptyLines() { return this.ignoreEmptyLines; }
|
return this.interpretUnicodeEscapes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIgnoreEmptyLines(boolean ignoreEmptyLines) {
|
||||||
|
this.ignoreEmptyLines = ignoreEmptyLines;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getIgnoreEmptyLines() {
|
||||||
|
return this.ignoreEmptyLines;
|
||||||
|
}
|
||||||
|
|
||||||
public void setPrinterNewline(String newline) {
|
public void setPrinterNewline(String newline) {
|
||||||
this.printerNewline = newline;
|
this.printerNewline = newline;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPrinterNewline() {
|
public String getPrinterNewline() {
|
||||||
return this.printerNewline;
|
return this.printerNewline;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object clone() {
|
public Object clone() {
|
||||||
try {
|
try {
|
||||||
return super.clone();
|
return super.clone();
|
||||||
} catch (CloneNotSupportedException e) {
|
} catch (CloneNotSupportedException e) {
|
||||||
throw new RuntimeException(e); // impossible
|
throw new RuntimeException(e); // impossible
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ public class CSVUtils {
|
||||||
*
|
*
|
||||||
* @param values the value array
|
* @param values the value array
|
||||||
* @return the CSV string, will be an empty string if the length of the
|
* @return the CSV string, will be an empty string if the length of the
|
||||||
* value array is 0
|
* value array is 0
|
||||||
*/
|
*/
|
||||||
public static String printLine(String[] values, CSVStrategy strategy) {
|
public static String printLine(String[] values, CSVStrategy strategy) {
|
||||||
// set up a CSVUtils
|
// set up a CSVUtils
|
||||||
|
@ -65,57 +65,57 @@ public class CSVUtils {
|
||||||
|
|
||||||
// convert to CSV
|
// convert to CSV
|
||||||
try {
|
try {
|
||||||
csvPrinter.println(values);
|
csvPrinter.println(values);
|
||||||
} catch (IOException e) {
|
} 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
|
// as the resulting string has \r\n at the end, we will trim that away
|
||||||
return stringWriter.toString().trim();
|
return stringWriter.toString().trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
// static parsers
|
// static parsers
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the given String according to the default {@link CSVStrategy}.
|
* Parses the given String according to the default {@link CSVStrategy}.
|
||||||
*
|
*
|
||||||
* @param s CSV String to be parsed.
|
* @param s CSV String to be parsed.
|
||||||
* @return parsed String matrix (which is never null)
|
* @return parsed String matrix (which is never null)
|
||||||
* @throws IOException in case of error
|
* @throws IOException in case of error
|
||||||
*/
|
*/
|
||||||
public static String[][] parse(String s) throws IOException {
|
public static String[][] parse(String s) throws IOException {
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
throw new IllegalArgumentException("Null argument not allowed.");
|
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;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the first line only according to the default {@link CSVStrategy}.
|
* Parses the first line only according to the default {@link CSVStrategy}.
|
||||||
*
|
*
|
||||||
* Parsing empty string will be handled as valid records containing zero
|
* Parsing empty string will be handled as valid records containing zero
|
||||||
* elements, so the following property holds: parseLine("").length == 0.
|
* elements, so the following property holds: parseLine("").length == 0.
|
||||||
*
|
*
|
||||||
* @param s CSV String to be parsed.
|
* @param s CSV String to be parsed.
|
||||||
* @return parsed String vector (which is never null)
|
* @return parsed String vector (which is never null)
|
||||||
* @throws IOException in case of error
|
* @throws IOException in case of error
|
||||||
*/
|
*/
|
||||||
public static String[] parseLine(String s) throws IOException {
|
public static String[] parseLine(String s) throws IOException {
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
throw new IllegalArgumentException("Null argument not allowed.");
|
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();
|
||||||
}
|
}
|
||||||
// uh,jh: make sure that parseLine("").length == 0
|
|
||||||
if (s.length() == 0) {
|
|
||||||
return EMPTY_STRING_ARRAY;
|
|
||||||
}
|
|
||||||
return (new CSVParser(new StringReader(s))).getLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,6 +64,7 @@ public class CharBuffer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of characters in the buffer.
|
* Returns the number of characters in the buffer.
|
||||||
|
*
|
||||||
* @return the number of characters
|
* @return the number of characters
|
||||||
*/
|
*/
|
||||||
public int length() {
|
public int length() {
|
||||||
|
@ -72,8 +73,9 @@ public class CharBuffer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current capacity of the buffer.
|
* Returns the current capacity of the buffer.
|
||||||
|
*
|
||||||
* @return the maximum number of characters that can be stored in this buffer without
|
* @return the maximum number of characters that can be stored in this buffer without
|
||||||
* resizing it.
|
* resizing it.
|
||||||
*/
|
*/
|
||||||
public int capacity() {
|
public int capacity() {
|
||||||
return c.length;
|
return c.length;
|
||||||
|
@ -82,6 +84,7 @@ public class CharBuffer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends the contents of <code>cb</code> to the end of this CharBuffer.
|
* Appends the contents of <code>cb</code> to the end of this CharBuffer.
|
||||||
|
*
|
||||||
* @param cb the CharBuffer to append or null
|
* @param cb the CharBuffer to append or null
|
||||||
*/
|
*/
|
||||||
public void append(final CharBuffer cb) {
|
public void append(final CharBuffer cb) {
|
||||||
|
@ -96,6 +99,7 @@ public class CharBuffer {
|
||||||
/**
|
/**
|
||||||
* Appends <code>s</code> to the end of this CharBuffer.
|
* Appends <code>s</code> to the end of this CharBuffer.
|
||||||
* This method involves copying the new data once!
|
* This method involves copying the new data once!
|
||||||
|
*
|
||||||
* @param s the String to append or null
|
* @param s the String to append or null
|
||||||
*/
|
*/
|
||||||
public void append(final String s) {
|
public void append(final String s) {
|
||||||
|
@ -108,6 +112,7 @@ public class CharBuffer {
|
||||||
/**
|
/**
|
||||||
* Appends <code>sb</code> to the end of this CharBuffer.
|
* Appends <code>sb</code> to the end of this CharBuffer.
|
||||||
* This method involves copying the new data once!
|
* This method involves copying the new data once!
|
||||||
|
*
|
||||||
* @param sb the StringBuffer to append or null
|
* @param sb the StringBuffer to append or null
|
||||||
*/
|
*/
|
||||||
public void append(final StringBuffer sb) {
|
public void append(final StringBuffer sb) {
|
||||||
|
@ -122,6 +127,7 @@ public class CharBuffer {
|
||||||
/**
|
/**
|
||||||
* Appends <code>data</code> to the end of this CharBuffer.
|
* Appends <code>data</code> to the end of this CharBuffer.
|
||||||
* This method involves copying the new data once!
|
* This method involves copying the new data once!
|
||||||
|
*
|
||||||
* @param data the char[] to append or null
|
* @param data the char[] to append or null
|
||||||
*/
|
*/
|
||||||
public void append(final char[] data) {
|
public void append(final char[] data) {
|
||||||
|
@ -136,6 +142,7 @@ public class CharBuffer {
|
||||||
/**
|
/**
|
||||||
* Appends a single character to the end of this CharBuffer.
|
* Appends a single character to the end of this CharBuffer.
|
||||||
* This method involves copying the new data once!
|
* This method involves copying the new data once!
|
||||||
|
*
|
||||||
* @param data the char to append
|
* @param data the char to append
|
||||||
*/
|
*/
|
||||||
public void append(final char data) {
|
public void append(final char data) {
|
||||||
|
@ -157,13 +164,13 @@ public class CharBuffer {
|
||||||
c = newc;
|
c = newc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes trailing whitespace.
|
* Removes trailing whitespace.
|
||||||
*/
|
*/
|
||||||
public void trimTrailingWhitespace() {
|
public void trimTrailingWhitespace() {
|
||||||
while (length>0 && Character.isWhitespace(c[length-1])) {
|
while (length > 0 && Character.isWhitespace(c[length - 1])) {
|
||||||
length--;
|
length--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -172,6 +179,7 @@ public class CharBuffer {
|
||||||
* modifying it.
|
* modifying it.
|
||||||
* This method allows to avoid copying if the caller knows the exact capacity
|
* This method allows to avoid copying if the caller knows the exact capacity
|
||||||
* before.
|
* before.
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public char[] getCharacters() {
|
public char[] getCharacters() {
|
||||||
|
@ -183,16 +191,17 @@ public class CharBuffer {
|
||||||
return chars;
|
return chars;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the character at the specified position.
|
* Returns the character at the specified position.
|
||||||
*/
|
*/
|
||||||
public char charAt(int pos) {
|
public char charAt(int pos) {
|
||||||
return c[pos];
|
return c[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the contents of the buffer into a StringBuffer.
|
* Converts the contents of the buffer into a StringBuffer.
|
||||||
* This method involves copying the new data once!
|
* This method involves copying the new data once!
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public StringBuffer toStringBuffer() {
|
public StringBuffer toStringBuffer() {
|
||||||
|
@ -204,6 +213,7 @@ public class CharBuffer {
|
||||||
/**
|
/**
|
||||||
* Converts the contents of the buffer into a StringBuffer.
|
* Converts the contents of the buffer into a StringBuffer.
|
||||||
* This method involves copying the new data once!
|
* This method involves copying the new data once!
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -212,13 +222,14 @@ public class CharBuffer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies the data into a new array of at least <code>capacity</code> size.
|
* Copies the data into a new array of at least <code>capacity</code> size.
|
||||||
|
*
|
||||||
* @param capacity
|
* @param capacity
|
||||||
*/
|
*/
|
||||||
public void provideCapacity(final int capacity) {
|
public void provideCapacity(final int capacity) {
|
||||||
if (c.length >= capacity) {
|
if (c.length >= capacity) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int newcapacity = ((capacity*3)>>1) + 1;
|
int newcapacity = ((capacity * 3) >> 1) + 1;
|
||||||
char[] newc = new char[newcapacity];
|
char[] newc = new char[newcapacity];
|
||||||
System.arraycopy(c, 0, newc, 0, length);
|
System.arraycopy(c, 0, newc, 0, length);
|
||||||
c = newc;
|
c = newc;
|
||||||
|
|
|
@ -23,214 +23,223 @@ import java.io.Reader;
|
||||||
/**
|
/**
|
||||||
* ExtendedBufferedReader
|
* ExtendedBufferedReader
|
||||||
*
|
*
|
||||||
* A special reader decorater which supports more
|
* A special reader decorator which supports more
|
||||||
* sophisticated access to the underlying reader object.
|
* sophisticated access to the underlying reader object.
|
||||||
*
|
*
|
||||||
* In particular the reader supports a look-ahead option,
|
* In particular the reader supports a look-ahead option,
|
||||||
* which allows you to see the next char returned by
|
* which allows you to see the next char returned by
|
||||||
* next().
|
* next().
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
class ExtendedBufferedReader extends BufferedReader {
|
class ExtendedBufferedReader extends BufferedReader {
|
||||||
|
|
||||||
|
|
||||||
/** the end of stream symbol */
|
/**
|
||||||
public static final int END_OF_STREAM = -1;
|
* the end of stream symbol
|
||||||
/** 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!
|
|
||||||
*/
|
*/
|
||||||
}
|
public static final int END_OF_STREAM = -1;
|
||||||
|
/**
|
||||||
/**
|
* undefined state for the lookahead char
|
||||||
* 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 UNDEFINED = -2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads the next char from the input stream.
|
* the lookahead chars
|
||||||
* @return the next char or END_OF_STREAM if end of stream has been reached.
|
*/
|
||||||
*/
|
private int lookaheadChar = UNDEFINED;
|
||||||
public int read() throws IOException {
|
/**
|
||||||
// initalize the lookahead
|
* the last char returned
|
||||||
if (lookaheadChar == UNDEFINED) {
|
*/
|
||||||
lookaheadChar = super.read();
|
private int lastChar = UNDEFINED;
|
||||||
}
|
/**
|
||||||
lastChar = lookaheadChar;
|
* the line counter
|
||||||
if (super.ready()) {
|
*/
|
||||||
lookaheadChar = super.read();
|
private int lineCounter = 0;
|
||||||
} else {
|
private CharBuffer line = new CharBuffer();
|
||||||
lookaheadChar = UNDEFINED;
|
|
||||||
}
|
|
||||||
if (lastChar == '\n') {
|
|
||||||
lineCounter++;
|
|
||||||
}
|
|
||||||
return lastChar;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the last read character again.
|
* Created extended buffered reader using default buffer-size
|
||||||
*
|
*/
|
||||||
* @return the last read char or UNDEFINED
|
public ExtendedBufferedReader(Reader r) {
|
||||||
*/
|
super(r);
|
||||||
public int readAgain() {
|
/* note uh: do not fetch the first char here,
|
||||||
return lastChar;
|
* because this might block the method!
|
||||||
}
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* 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) {
|
* Create extended buffered reader using the given buffer-size
|
||||||
if (ready()) {
|
*/
|
||||||
lookaheadChar = super.read();
|
public ExtendedBufferedReader(Reader r, int bufSize) {
|
||||||
|
super(r, bufSize);
|
||||||
|
/* note uh: do not fetch the first char here,
|
||||||
|
* because this might block the method!
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
} else {
|
||||||
return -1;
|
lookaheadChar = UNDEFINED;
|
||||||
}
|
}
|
||||||
|
if (lastChar == '\n') {
|
||||||
|
lineCounter++;
|
||||||
|
}
|
||||||
|
return lastChar;
|
||||||
}
|
}
|
||||||
// 'first read of underlying stream'
|
|
||||||
if (lookaheadChar == -1) {
|
/**
|
||||||
return -1;
|
* Returns the last read character again.
|
||||||
|
*
|
||||||
|
* @return the last read char or UNDEFINED
|
||||||
|
*/
|
||||||
|
public int readAgain() {
|
||||||
|
return lastChar;
|
||||||
}
|
}
|
||||||
// continue until the lookaheadChar would block
|
|
||||||
int cOff = off;
|
/**
|
||||||
while (len > 0 && ready()) {
|
* Non-blocking reading of len chars into buffer buf starting
|
||||||
if (lookaheadChar == -1) {
|
* at bufferposition off.
|
||||||
// eof stream reached, do not continue
|
* <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 cOff - off;
|
||||||
} else {
|
}
|
||||||
buf[cOff++] = (char) lookaheadChar;
|
|
||||||
if (lookaheadChar == '\n') {
|
/**
|
||||||
lineCounter++;
|
* @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;
|
lastChar = lookaheadChar;
|
||||||
lookaheadChar = super.read();
|
lookaheadChar = super.read();
|
||||||
len--;
|
if (restOfLine != null) {
|
||||||
}
|
line.append(restOfLine);
|
||||||
}
|
}
|
||||||
return cOff - off;
|
lineCounter++;
|
||||||
}
|
return line.toString();
|
||||||
|
|
||||||
/**
|
|
||||||
* @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
|
/**
|
||||||
|
* Unsupported
|
||||||
// return null if end of stream has been reached
|
*/
|
||||||
if (lookaheadChar == END_OF_STREAM) {
|
public long skip(long n) throws IllegalArgumentException, IOException {
|
||||||
return null;
|
throw new UnsupportedOperationException("CSV has no reason to implement this");
|
||||||
}
|
|
||||||
// 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);
|
* Returns the next char in the stream without consuming it.
|
||||||
String restOfLine = super.readLine(); // TODO involves copying
|
*
|
||||||
lastChar = lookaheadChar;
|
* Remember the next char read by read(..) will always be
|
||||||
lookaheadChar = super.read();
|
* identical to lookAhead().
|
||||||
if (restOfLine != null) {
|
*
|
||||||
line.append(restOfLine);
|
* @return the next char (without consuming it) or END_OF_STREAM
|
||||||
|
*/
|
||||||
|
public int lookAhead() throws IOException {
|
||||||
|
if (lookaheadChar == UNDEFINED) {
|
||||||
|
lookaheadChar = super.read();
|
||||||
|
}
|
||||||
|
return lookaheadChar;
|
||||||
}
|
}
|
||||||
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.
|
* Returns the nof line read
|
||||||
*
|
*
|
||||||
* Remember the next char read by read(..) will always be
|
* @return the current-line-number (or -1)
|
||||||
* identical to lookAhead().
|
*/
|
||||||
*
|
public int getLineNumber() {
|
||||||
* @return the next char (without consuming it) or END_OF_STREAM
|
if (lineCounter > -1) {
|
||||||
*/
|
return lineCounter;
|
||||||
public int lookAhead() throws IOException {
|
} else {
|
||||||
if (lookaheadChar == UNDEFINED) {
|
return -1;
|
||||||
lookaheadChar = super.read();
|
}
|
||||||
}
|
}
|
||||||
return lookaheadChar;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* Unsupported
|
||||||
* Returns the nof line read
|
*/
|
||||||
*
|
public boolean markSupported() {
|
||||||
* @return the current-line-number (or -1)
|
throw new UnsupportedOperationException("CSV has no reason to implement this");
|
||||||
*/
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,36 +32,65 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public class CSVConfig {
|
public class CSVConfig {
|
||||||
|
|
||||||
/** specifies if it is a fixed width csv file **/
|
/**
|
||||||
|
* specifies if it is a fixed width csv file *
|
||||||
|
*/
|
||||||
private boolean fixedWidth;
|
private boolean fixedWidth;
|
||||||
/** list of fields **/
|
/**
|
||||||
|
* list of fields *
|
||||||
|
*/
|
||||||
private List fields;
|
private List fields;
|
||||||
|
|
||||||
/** Do no do any filling **/
|
/**
|
||||||
|
* Do no do any filling *
|
||||||
|
*/
|
||||||
public static final int FILLNONE = 0;
|
public static final int FILLNONE = 0;
|
||||||
/** Fill content the the left. Mainly usable together with fixedWidth **/
|
/**
|
||||||
|
* Fill content the the left. Mainly usable together with fixedWidth *
|
||||||
|
*/
|
||||||
public static final int FILLLEFT = 1;
|
public static final int FILLLEFT = 1;
|
||||||
/** Fill content to the right. Mainly usable together with fixedWidth **/
|
/**
|
||||||
|
* Fill content to the right. Mainly usable together with fixedWidth *
|
||||||
|
*/
|
||||||
public static final int FILLRIGHT = 2;
|
public static final int FILLRIGHT = 2;
|
||||||
|
|
||||||
/** The fill pattern */
|
/**
|
||||||
|
* The fill pattern
|
||||||
|
*/
|
||||||
private int fill;
|
private int fill;
|
||||||
/** The fill char. Defaults to a space */
|
/**
|
||||||
|
* The fill char. Defaults to a space
|
||||||
|
*/
|
||||||
private char fillChar = ' ';
|
private char fillChar = ' ';
|
||||||
/** The seperator character. Defaults to , */
|
/**
|
||||||
|
* The seperator character. Defaults to ,
|
||||||
|
*/
|
||||||
private char delimiter = ',';
|
private char delimiter = ',';
|
||||||
/** The row separator. Defaults to \n */
|
/**
|
||||||
|
* The row separator. Defaults to \n
|
||||||
|
*/
|
||||||
private String rowDelimiter = "\n";
|
private String rowDelimiter = "\n";
|
||||||
/** Should we ignore the delimiter. Defaults to false */
|
/**
|
||||||
|
* Should we ignore the delimiter. Defaults to false
|
||||||
|
*/
|
||||||
private boolean ignoreDelimiter = false;
|
private boolean ignoreDelimiter = false;
|
||||||
/** the value delimiter. Defaults to " */
|
/**
|
||||||
|
* the value delimiter. Defaults to "
|
||||||
|
*/
|
||||||
private char valueDelimiter = '"';
|
private char valueDelimiter = '"';
|
||||||
/** Should we ignore the value delimiter. Defaults to true */
|
/**
|
||||||
|
* Should we ignore the value delimiter. Defaults to true
|
||||||
|
*/
|
||||||
private boolean ignoreValueDelimiter = true;
|
private boolean ignoreValueDelimiter = true;
|
||||||
/** Specifies if we want to use a field header */
|
/**
|
||||||
|
* Specifies if we want to use a field header
|
||||||
|
*/
|
||||||
private boolean fieldHeader = false;
|
private boolean fieldHeader = false;
|
||||||
/** Specifies if the end of the line needs to be trimmed */
|
/**
|
||||||
|
* Specifies if the end of the line needs to be trimmed
|
||||||
|
*/
|
||||||
private boolean endTrimmed = false;
|
private boolean endTrimmed = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -79,6 +108,7 @@ public class CSVConfig {
|
||||||
/**
|
/**
|
||||||
* Specify if the CSV file is fixed width.
|
* Specify if the CSV file is fixed width.
|
||||||
* Defaults to false
|
* Defaults to false
|
||||||
|
*
|
||||||
* @param fixedWidth the fixedwidth
|
* @param fixedWidth the fixedwidth
|
||||||
*/
|
*/
|
||||||
public void setFixedWidth(boolean fixedWidth) {
|
public void setFixedWidth(boolean fixedWidth) {
|
||||||
|
@ -95,6 +125,7 @@ public class CSVConfig {
|
||||||
/**
|
/**
|
||||||
* Set the fields that should be used by the writer.
|
* Set the fields that should be used by the writer.
|
||||||
* This will overwrite currently added fields completely!
|
* This will overwrite currently added fields completely!
|
||||||
|
*
|
||||||
* @param csvFields the csvfields array. If null it will do nothing
|
* @param csvFields the csvfields array. If null it will do nothing
|
||||||
*/
|
*/
|
||||||
public void setFields(CSVField[] csvFields) {
|
public void setFields(CSVField[] csvFields) {
|
||||||
|
@ -106,6 +137,7 @@ public class CSVConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the fields that should be used by the writer
|
* Set the fields that should be used by the writer
|
||||||
|
*
|
||||||
* @param csvField a collection with fields. If null it will do nothing
|
* @param csvField a collection with fields. If null it will do nothing
|
||||||
*/
|
*/
|
||||||
public void setFields(Collection csvField) {
|
public void setFields(Collection csvField) {
|
||||||
|
@ -130,7 +162,7 @@ public class CSVConfig {
|
||||||
if (fields == null || name == null) {
|
if (fields == null || name == null) {
|
||||||
return 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);
|
CSVField field = (CSVField) fields.get(i);
|
||||||
if (name.equals(field.getName())) {
|
if (name.equals(field.getName())) {
|
||||||
return field;
|
return field;
|
||||||
|
@ -149,6 +181,7 @@ public class CSVConfig {
|
||||||
/**
|
/**
|
||||||
* Set the fill pattern. Defaults to {@link #FILLNONE}
|
* Set the fill pattern. Defaults to {@link #FILLNONE}
|
||||||
* <br/>Other options are : {@link #FILLLEFT} and {@link #FILLRIGHT}
|
* <br/>Other options are : {@link #FILLLEFT} and {@link #FILLRIGHT}
|
||||||
|
*
|
||||||
* @param fill the fill pattern.
|
* @param fill the fill pattern.
|
||||||
*/
|
*/
|
||||||
public void setFill(int fill) {
|
public void setFill(int fill) {
|
||||||
|
@ -156,7 +189,6 @@ public class CSVConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return the fillchar. Defaults to a space.
|
* @return the fillchar. Defaults to a space.
|
||||||
*/
|
*/
|
||||||
public char getFillChar() {
|
public char getFillChar() {
|
||||||
|
@ -165,6 +197,7 @@ public class CSVConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the fill char
|
* Set the fill char
|
||||||
|
*
|
||||||
* @param fillChar the fill char
|
* @param fillChar the fill char
|
||||||
*/
|
*/
|
||||||
public void setFillChar(char fillChar) {
|
public void setFillChar(char fillChar) {
|
||||||
|
@ -180,6 +213,7 @@ public class CSVConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the delimiter to use
|
* Set the delimiter to use
|
||||||
|
*
|
||||||
* @param delimiter the delimiter character.
|
* @param delimiter the delimiter character.
|
||||||
*/
|
*/
|
||||||
public void setDelimiter(char delimiter) {
|
public void setDelimiter(char delimiter) {
|
||||||
|
@ -195,6 +229,7 @@ public class CSVConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the rowDelimiter to use
|
* Set the rowDelimiter to use
|
||||||
|
*
|
||||||
* @param rowDelimiter the row delimiter character.
|
* @param rowDelimiter the row delimiter character.
|
||||||
*/
|
*/
|
||||||
public void setRowDelimiter(String rowDelimiter) {
|
public void setRowDelimiter(String rowDelimiter) {
|
||||||
|
@ -210,6 +245,7 @@ public class CSVConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify if the writer should ignore the delimiter.
|
* Specify if the writer should ignore the delimiter.
|
||||||
|
*
|
||||||
* @param ignoreDelimiter defaults to false.
|
* @param ignoreDelimiter defaults to false.
|
||||||
*/
|
*/
|
||||||
public void setIgnoreDelimiter(boolean ignoreDelimiter) {
|
public void setIgnoreDelimiter(boolean ignoreDelimiter) {
|
||||||
|
@ -225,6 +261,7 @@ public class CSVConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the value delimiter to use
|
* Set the value delimiter to use
|
||||||
|
*
|
||||||
* @param valueDelimiter the value delimiter character.
|
* @param valueDelimiter the value delimiter character.
|
||||||
*/
|
*/
|
||||||
public void setValueDelimiter(char valueDelimiter) {
|
public void setValueDelimiter(char valueDelimiter) {
|
||||||
|
@ -241,6 +278,7 @@ 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.
|
* @param ignoreValueDelimiter defaults to false.
|
||||||
*/
|
*/
|
||||||
public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) {
|
public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) {
|
||||||
|
@ -253,8 +291,10 @@ public class CSVConfig {
|
||||||
public boolean isFieldHeader() {
|
public boolean isFieldHeader() {
|
||||||
return fieldHeader;
|
return fieldHeader;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify if you want to use a field header.
|
* Specify if you want to use a field header.
|
||||||
|
*
|
||||||
* @param fieldHeader true or false.
|
* @param fieldHeader true or false.
|
||||||
*/
|
*/
|
||||||
public void setFieldHeader(boolean fieldHeader) {
|
public void setFieldHeader(boolean fieldHeader) {
|
||||||
|
@ -263,6 +303,7 @@ public class CSVConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO..
|
* TODO..
|
||||||
|
*
|
||||||
* @see java.lang.Object#equals(java.lang.Object)
|
* @see java.lang.Object#equals(java.lang.Object)
|
||||||
*/
|
*/
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
|
@ -278,6 +319,7 @@ public class CSVConfig {
|
||||||
/**
|
/**
|
||||||
* Creates a config based on a stream. It tries to guess<br/>
|
* Creates a config based on a stream. It tries to guess<br/>
|
||||||
* NOTE : The stream will be closed.
|
* NOTE : The stream will be closed.
|
||||||
|
*
|
||||||
* @param inputStream the inputstream.
|
* @param inputStream the inputstream.
|
||||||
* @return the guessed config.
|
* @return the guessed config.
|
||||||
*/
|
*/
|
||||||
|
@ -294,6 +336,7 @@ public class CSVConfig {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify if the end of the line needs to be trimmed. Defaults to false.
|
* Specify if the end of the line needs to be trimmed. Defaults to false.
|
||||||
|
*
|
||||||
* @param endTrimmed
|
* @param endTrimmed
|
||||||
*/
|
*/
|
||||||
public void setEndTrimmed(boolean endTrimmed) {
|
public void setEndTrimmed(boolean endTrimmed) {
|
||||||
|
|
|
@ -30,14 +30,18 @@ import java.io.InputStreamReader;
|
||||||
*/
|
*/
|
||||||
public class CSVConfigGuesser {
|
public class CSVConfigGuesser {
|
||||||
|
|
||||||
/** The stream to read */
|
/**
|
||||||
|
* The stream to read
|
||||||
|
*/
|
||||||
private InputStream in;
|
private InputStream in;
|
||||||
/**
|
/**
|
||||||
* if the file has a field header (need this info, to be able to guess better)
|
* if the file has a field header (need this info, to be able to guess better)
|
||||||
* Defaults to false
|
* Defaults to false
|
||||||
*/
|
*/
|
||||||
private boolean hasFieldHeader = false;
|
private boolean hasFieldHeader = false;
|
||||||
/** The found config */
|
/**
|
||||||
|
* The found config
|
||||||
|
*/
|
||||||
protected CSVConfig config;
|
protected CSVConfig config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,6 +65,7 @@ public class CSVConfigGuesser {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allow override.
|
* Allow override.
|
||||||
|
*
|
||||||
* @return the inputstream that was set.
|
* @return the inputstream that was set.
|
||||||
*/
|
*/
|
||||||
protected InputStream getInputStream() {
|
protected InputStream getInputStream() {
|
||||||
|
@ -80,7 +85,7 @@ public class CSVConfigGuesser {
|
||||||
String[] lines = new String[10];
|
String[] lines = new String[10];
|
||||||
String line = null;
|
String line = null;
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
while ( (line = bIn.readLine()) != null && counter <= 10) {
|
while ((line = bIn.readLine()) != null && counter <= 10) {
|
||||||
lines[counter] = line;
|
lines[counter] = line;
|
||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
@ -91,13 +96,13 @@ public class CSVConfigGuesser {
|
||||||
lines = newLines;
|
lines = newLines;
|
||||||
}
|
}
|
||||||
analyseLines(lines);
|
analyseLines(lines);
|
||||||
} catch(Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
if (in != null) {
|
if (in != null) {
|
||||||
try {
|
try {
|
||||||
in.close();
|
in.close();
|
||||||
} catch(Exception e) {
|
} catch (Exception e) {
|
||||||
// ignore exception.
|
// ignore exception.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,6 +121,7 @@ public class CSVConfigGuesser {
|
||||||
/**
|
/**
|
||||||
* Guess if this file is fixedwidth.
|
* Guess if this file is fixedwidth.
|
||||||
* Just basing the fact on all lines being of the same length
|
* Just basing the fact on all lines being of the same length
|
||||||
|
*
|
||||||
* @param lines
|
* @param lines
|
||||||
*/
|
*/
|
||||||
protected void guessFixedWidth(String[] lines) {
|
protected void guessFixedWidth(String[] lines) {
|
||||||
|
@ -163,14 +169,14 @@ public class CSVConfigGuesser {
|
||||||
previousMatch = 0;
|
previousMatch = 0;
|
||||||
}
|
}
|
||||||
CSVField field = new CSVField();
|
CSVField field = new CSVField();
|
||||||
field.setName("field"+config.getFields().length+1);
|
field.setName("field" + config.getFields().length + 1);
|
||||||
field.setSize((i-previousMatch));
|
field.setSize((i - previousMatch));
|
||||||
config.addField(field);
|
config.addField(field);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return if the field uses a field header. Defaults to false.
|
* @return if the field uses a field header. Defaults to false.
|
||||||
*/
|
*/
|
||||||
public boolean hasFieldHeader() {
|
public boolean hasFieldHeader() {
|
||||||
|
@ -179,6 +185,7 @@ public class CSVConfigGuesser {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify if the CSV file has a field header
|
* Specify if the CSV file has a field header
|
||||||
|
*
|
||||||
* @param hasFieldHeader true or false
|
* @param hasFieldHeader true or false
|
||||||
*/
|
*/
|
||||||
public void setHasFieldHeader(boolean hasFieldHeader) {
|
public void setHasFieldHeader(boolean hasFieldHeader) {
|
||||||
|
|
|
@ -20,7 +20,6 @@ package org.apache.commons.csv.writer;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Martin van den Bemt
|
* @author Martin van den Bemt
|
||||||
* @version $Id: $
|
* @version $Id: $
|
||||||
*/
|
*/
|
||||||
|
@ -62,6 +61,7 @@ public class CSVField {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the name of the field
|
* Set the name of the field
|
||||||
|
*
|
||||||
* @param name the name
|
* @param name the name
|
||||||
*/
|
*/
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
|
@ -69,7 +69,6 @@ public class CSVField {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @return the size of the field
|
* @return the size of the field
|
||||||
*/
|
*/
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
|
@ -79,6 +78,7 @@ public class CSVField {
|
||||||
/**
|
/**
|
||||||
* Set the size of the field.
|
* Set the size of the field.
|
||||||
* The size will be ignored when fixedwidth is set to false in the CSVConfig
|
* The size will be ignored when fixedwidth is set to false in the CSVConfig
|
||||||
|
*
|
||||||
* @param size the size of the field.
|
* @param size the size of the field.
|
||||||
*/
|
*/
|
||||||
public void setSize(int size) {
|
public void setSize(int size) {
|
||||||
|
@ -94,6 +94,7 @@ public class CSVField {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets overrideFill to true.
|
* Sets overrideFill to true.
|
||||||
|
*
|
||||||
* @param fill the file pattern
|
* @param fill the file pattern
|
||||||
*/
|
*/
|
||||||
public void setFill(int fill) {
|
public void setFill(int fill) {
|
||||||
|
|
|
@ -31,10 +31,15 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public class CSVWriter {
|
public class CSVWriter {
|
||||||
|
|
||||||
/** The CSV config **/
|
/**
|
||||||
|
* The CSV config *
|
||||||
|
*/
|
||||||
private CSVConfig config;
|
private CSVConfig config;
|
||||||
/** The writer **/
|
/**
|
||||||
|
* The writer *
|
||||||
|
*/
|
||||||
private Writer writer;
|
private Writer writer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -56,12 +61,12 @@ public class CSVWriter {
|
||||||
value = writeValue(fields[i], value);
|
value = writeValue(fields[i], value);
|
||||||
sb.append(value);
|
sb.append(value);
|
||||||
}
|
}
|
||||||
if (!config.isDelimiterIgnored() && fields.length != (i+1)) {
|
if (!config.isDelimiterIgnored() && fields.length != (i + 1)) {
|
||||||
sb.append(config.getDelimiter());
|
sb.append(config.getDelimiter());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (config.isEndTrimmed()) {
|
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);
|
System.out.println("i : " + i);
|
||||||
if (Character.isWhitespace(sb.charAt(i))) {
|
if (Character.isWhitespace(sb.charAt(i))) {
|
||||||
sb.deleteCharAt(i);
|
sb.deleteCharAt(i);
|
||||||
|
@ -73,7 +78,7 @@ public class CSVWriter {
|
||||||
sb.append(config.getRowDelimiter());
|
sb.append(config.getRowDelimiter());
|
||||||
String line = sb.toString();
|
String line = sb.toString();
|
||||||
writer.write(line);
|
writer.write(line);
|
||||||
} catch(Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,7 +111,7 @@ public class CSVWriter {
|
||||||
}
|
}
|
||||||
if (!config.isValueDelimiterIgnored()) {
|
if (!config.isValueDelimiterIgnored()) {
|
||||||
// add the value delimiter..
|
// add the value delimiter..
|
||||||
value = config.getValueDelimiter()+value+config.getValueDelimiter();
|
value = config.getValueDelimiter() + value + config.getValueDelimiter();
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
@ -120,6 +125,7 @@ public class CSVWriter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the CSVConfig
|
* Set the CSVConfig
|
||||||
|
*
|
||||||
* @param config the CVSConfig
|
* @param config the CVSConfig
|
||||||
*/
|
*/
|
||||||
public void setConfig(CSVConfig config) {
|
public void setConfig(CSVConfig config) {
|
||||||
|
@ -128,6 +134,7 @@ public class CSVWriter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the writer to write the CSV file to.
|
* Set the writer to write the CSV file to.
|
||||||
|
*
|
||||||
* @param writer the writer.
|
* @param writer the writer.
|
||||||
*/
|
*/
|
||||||
public void setWriter(Writer writer) {
|
public void setWriter(Writer writer) {
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -31,199 +31,218 @@ import junit.framework.TestSuite;
|
||||||
*/
|
*/
|
||||||
public class CSVPrinterTest extends TestCase {
|
public class CSVPrinterTest extends TestCase {
|
||||||
|
|
||||||
String lineSeparator = "\n";
|
String lineSeparator = "\n";
|
||||||
|
|
||||||
public void testPrinter1() throws IOException {
|
public void testPrinter1() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||||
String[] line1 = {"a", "b"};
|
String[] line1 = {"a", "b"};
|
||||||
printer.println(line1);
|
printer.println(line1);
|
||||||
assertEquals("a,b" + lineSeparator, sw.toString());
|
assertEquals("a,b" + lineSeparator, sw.toString());
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StringWriter sw = new StringWriter();
|
public void testPrinter2() throws IOException {
|
||||||
CSVPrinter printer = new CSVPrinter(sw, strategy);
|
StringWriter sw = new StringWriter();
|
||||||
|
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||||
for (int i=0; i<nLines; i++) {
|
String[] line1 = {"a,b", "b"};
|
||||||
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
|
printer.println(line1);
|
||||||
printer.println(lines[i]);
|
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
printer.flush();
|
public void testPrinter3() throws IOException {
|
||||||
String result = sw.toString();
|
StringWriter sw = new StringWriter();
|
||||||
// System.out.println("### :" + printable(result));
|
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||||
|
String[] line1 = {"a, b", "b "};
|
||||||
StringReader reader = new StringReader(result);
|
printer.println(line1);
|
||||||
|
assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
|
||||||
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) {
|
public void testPrinter4() throws IOException {
|
||||||
if (a.length != b.length) {
|
StringWriter sw = new StringWriter();
|
||||||
return false;
|
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];
|
public void testPrinter5() throws IOException {
|
||||||
String[] lineb = b[i];
|
StringWriter sw = new StringWriter();
|
||||||
if (linea.length != lineb.length) {
|
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||||
return false;
|
String[] line1 = {"a", "b\nc"};
|
||||||
}
|
printer.println(line1);
|
||||||
for (int j=0; j<linea.length; j++) {
|
assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
|
||||||
String aval = linea[j];
|
}
|
||||||
String bval = lineb[j];
|
|
||||||
if (!aval.equals(bval)) {
|
public void testPrinter6() throws IOException {
|
||||||
System.out.println("expected :" + printable(aval));
|
StringWriter sw = new StringWriter();
|
||||||
System.out.println("got :" + printable(bval));
|
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
|
||||||
return false;
|
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) {
|
public void doOneRandom() throws Exception {
|
||||||
StringBuffer sb = new StringBuffer();
|
int nLines = r.nextInt(4) + 1;
|
||||||
for (int i=0; i<s.length(); i++) {
|
int nCol = r.nextInt(3) + 1;
|
||||||
char ch = s.charAt(i);
|
// nLines=1;nCol=2;
|
||||||
if (ch<=' ' || ch>=128) {
|
String[][] lines = new String[nLines][];
|
||||||
sb.append("(" + (int)ch + ")");
|
for (int i = 0; i < nLines; i++) {
|
||||||
} else {
|
String[] line = new String[nCol];
|
||||||
sb.append(ch);
|
lines[i] = line;
|
||||||
}
|
for (int j = 0; j < nCol; j++) {
|
||||||
}
|
line[j] = randStr();
|
||||||
return sb.toString();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String randStr() {
|
StringWriter sw = new StringWriter();
|
||||||
int sz = r.nextInt(20);
|
CSVPrinter printer = new CSVPrinter(sw, strategy);
|
||||||
// sz = r.nextInt(3);
|
|
||||||
char[] buf = new char[sz];
|
for (int i = 0; i < nLines; i++) {
|
||||||
for (int i=0; i<sz; i++) {
|
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
|
||||||
// stick in special chars with greater frequency
|
printer.println(lines[i]);
|
||||||
char ch;
|
}
|
||||||
int what = r.nextInt(20);
|
|
||||||
switch (what) {
|
printer.flush();
|
||||||
case 0: ch = '\r'; break;
|
String result = sw.toString();
|
||||||
case 1: ch = '\n'; break;
|
// System.out.println("### :" + printable(result));
|
||||||
case 2: ch = '\t'; break;
|
|
||||||
case 3: ch = '\f'; break;
|
StringReader reader = new StringReader(result);
|
||||||
case 4: ch = ' '; break;
|
|
||||||
case 5: ch = ','; break;
|
CSVParser parser = new CSVParser(reader, strategy);
|
||||||
case 6: ch = '"'; break;
|
String[][] parseResult = parser.getAllValues();
|
||||||
case 7: ch = '\''; break;
|
|
||||||
case 8: ch = '\\'; break;
|
if (!equals(lines, parseResult)) {
|
||||||
default: ch = (char)r.nextInt(300); break;
|
System.out.println("Printer output :" + printable(result));
|
||||||
// default: ch = 'a'; break;
|
assertTrue(false);
|
||||||
}
|
}
|
||||||
buf[i] = ch;
|
}
|
||||||
|
|
||||||
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,61 +31,61 @@ import junit.framework.TestCase;
|
||||||
*/
|
*/
|
||||||
public class CSVStrategyTest extends TestCase {
|
public class CSVStrategyTest extends TestCase {
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
// getters / setters
|
// getters / setters
|
||||||
// ======================================================
|
// ======================================================
|
||||||
public void testGetSetCommentStart() {
|
public void testGetSetCommentStart() {
|
||||||
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
|
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
|
||||||
strategy.setCommentStart('#');
|
strategy.setCommentStart('#');
|
||||||
assertEquals(strategy.getCommentStart(), '#');
|
assertEquals(strategy.getCommentStart(), '#');
|
||||||
strategy.setCommentStart('!');
|
strategy.setCommentStart('!');
|
||||||
assertEquals(strategy.getCommentStart(), '!');
|
assertEquals(strategy.getCommentStart(), '!');
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSetEncapsulator() {
|
public void testGetSetEncapsulator() {
|
||||||
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
|
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
|
||||||
strategy.setEncapsulator('"');
|
strategy.setEncapsulator('"');
|
||||||
assertEquals(strategy.getEncapsulator(), '"');
|
assertEquals(strategy.getEncapsulator(), '"');
|
||||||
strategy.setEncapsulator('\'');
|
strategy.setEncapsulator('\'');
|
||||||
assertEquals(strategy.getEncapsulator(), '\'');
|
assertEquals(strategy.getEncapsulator(), '\'');
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSetDelimiter() {
|
public void testGetSetDelimiter() {
|
||||||
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
|
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
|
||||||
strategy.setDelimiter(';');
|
strategy.setDelimiter(';');
|
||||||
assertEquals(strategy.getDelimiter(), ';');
|
assertEquals(strategy.getDelimiter(), ';');
|
||||||
strategy.setDelimiter(',');
|
strategy.setDelimiter(',');
|
||||||
assertEquals(strategy.getDelimiter(), ',');
|
assertEquals(strategy.getDelimiter(), ',');
|
||||||
strategy.setDelimiter('\t');
|
strategy.setDelimiter('\t');
|
||||||
assertEquals(strategy.getDelimiter(), '\t');
|
assertEquals(strategy.getDelimiter(), '\t');
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetCSVStrategy() {
|
public void testSetCSVStrategy() {
|
||||||
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
|
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
|
||||||
// default settings
|
// default settings
|
||||||
assertEquals(strategy.getDelimiter(), ',');
|
assertEquals(strategy.getDelimiter(), ',');
|
||||||
assertEquals(strategy.getEncapsulator(), '"');
|
assertEquals(strategy.getEncapsulator(), '"');
|
||||||
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
||||||
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
|
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
|
||||||
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
||||||
assertEquals(true, strategy.getIgnoreEmptyLines());
|
assertEquals(true, strategy.getIgnoreEmptyLines());
|
||||||
// explicit csv settings
|
// explicit csv settings
|
||||||
assertEquals(strategy.getDelimiter(), ',');
|
assertEquals(strategy.getDelimiter(), ',');
|
||||||
assertEquals(strategy.getEncapsulator(), '"');
|
assertEquals(strategy.getEncapsulator(), '"');
|
||||||
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
||||||
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
|
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
|
||||||
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
||||||
assertEquals(true, strategy.getIgnoreEmptyLines());
|
assertEquals(true, strategy.getIgnoreEmptyLines());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetExcelStrategy() {
|
public void testSetExcelStrategy() {
|
||||||
CSVStrategy strategy = CSVStrategy.EXCEL_STRATEGY;
|
CSVStrategy strategy = CSVStrategy.EXCEL_STRATEGY;
|
||||||
assertEquals(strategy.getDelimiter(), ',');
|
assertEquals(strategy.getDelimiter(), ',');
|
||||||
assertEquals(strategy.getEncapsulator(), '"');
|
assertEquals(strategy.getEncapsulator(), '"');
|
||||||
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
|
||||||
assertEquals(false, strategy.getIgnoreLeadingWhitespaces());
|
assertEquals(false, strategy.getIgnoreLeadingWhitespaces());
|
||||||
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
|
||||||
assertEquals(false, strategy.getIgnoreEmptyLines());
|
assertEquals(false, strategy.getIgnoreEmptyLines());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,126 +25,126 @@ import junit.framework.TestCase;
|
||||||
*/
|
*/
|
||||||
public class CSVUtilsTest extends TestCase {
|
public class CSVUtilsTest extends TestCase {
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
// static parser tests
|
// static parser tests
|
||||||
// ======================================================
|
// ======================================================
|
||||||
public void testParse1() throws IOException {
|
public void testParse1() throws IOException {
|
||||||
String[][] data = CSVUtils.parse("abc\ndef");
|
String[][] data = CSVUtils.parse("abc\ndef");
|
||||||
assertEquals(2, data.length);
|
assertEquals(2, data.length);
|
||||||
assertEquals(1, data[0].length);
|
assertEquals(1, data[0].length);
|
||||||
assertEquals(1, data[1].length);
|
assertEquals(1, data[1].length);
|
||||||
assertEquals("abc", data[0][0]);
|
assertEquals("abc", data[0][0]);
|
||||||
assertEquals("def", data[1][0]);
|
assertEquals("def", data[1][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParse2() throws IOException {
|
public void testParse2() throws IOException {
|
||||||
String[][] data = CSVUtils.parse("abc,def,\"ghi,jkl\"\ndef");
|
String[][] data = CSVUtils.parse("abc,def,\"ghi,jkl\"\ndef");
|
||||||
assertEquals(2, data.length);
|
assertEquals(2, data.length);
|
||||||
assertEquals(3, data[0].length);
|
assertEquals(3, data[0].length);
|
||||||
assertEquals(1, data[1].length);
|
assertEquals(1, data[1].length);
|
||||||
assertEquals("abc", data[0][0]);
|
assertEquals("abc", data[0][0]);
|
||||||
assertEquals("def", data[0][1]);
|
assertEquals("def", data[0][1]);
|
||||||
assertEquals("ghi,jkl", data[0][2]);
|
assertEquals("ghi,jkl", data[0][2]);
|
||||||
assertEquals("def", data[1][0]);
|
assertEquals("def", data[1][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParse3() throws IOException {
|
public void testParse3() throws IOException {
|
||||||
String[][] data = CSVUtils.parse("abc,\"def\nghi\"\njkl");
|
String[][] data = CSVUtils.parse("abc,\"def\nghi\"\njkl");
|
||||||
assertEquals(2, data.length);
|
assertEquals(2, data.length);
|
||||||
assertEquals(2, data[0].length);
|
assertEquals(2, data[0].length);
|
||||||
assertEquals(1, data[1].length);
|
assertEquals(1, data[1].length);
|
||||||
assertEquals("abc", data[0][0]);
|
assertEquals("abc", data[0][0]);
|
||||||
assertEquals("def\nghi", data[0][1]);
|
assertEquals("def\nghi", data[0][1]);
|
||||||
assertEquals("jkl", data[1][0]);
|
assertEquals("jkl", data[1][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParse4() throws IOException {
|
public void testParse4() throws IOException {
|
||||||
String[][] data = CSVUtils.parse("abc,\"def\\\\nghi\"\njkl");
|
String[][] data = CSVUtils.parse("abc,\"def\\\\nghi\"\njkl");
|
||||||
assertEquals(2, data.length);
|
assertEquals(2, data.length);
|
||||||
assertEquals(2, data[0].length);
|
assertEquals(2, data[0].length);
|
||||||
assertEquals(1, data[1].length);
|
assertEquals(1, data[1].length);
|
||||||
assertEquals("abc", data[0][0]);
|
assertEquals("abc", data[0][0]);
|
||||||
// an escape char in quotes only escapes a delimiter, not itself
|
// an escape char in quotes only escapes a delimiter, not itself
|
||||||
assertEquals("def\\\\nghi", data[0][1]);
|
assertEquals("def\\\\nghi", data[0][1]);
|
||||||
assertEquals("jkl", data[1][0]);
|
assertEquals("jkl", data[1][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParse5() throws IOException {
|
public void testParse5() throws IOException {
|
||||||
String[][] data = CSVUtils.parse("abc,def\\nghi\njkl");
|
String[][] data = CSVUtils.parse("abc,def\\nghi\njkl");
|
||||||
assertEquals(2, data.length);
|
assertEquals(2, data.length);
|
||||||
assertEquals(2, data[0].length);
|
assertEquals(2, data[0].length);
|
||||||
assertEquals(1, data[1].length);
|
assertEquals(1, data[1].length);
|
||||||
assertEquals("abc", data[0][0]);
|
assertEquals("abc", data[0][0]);
|
||||||
assertEquals("def\\nghi", data[0][1]);
|
assertEquals("def\\nghi", data[0][1]);
|
||||||
assertEquals("jkl", data[1][0]);
|
assertEquals("jkl", data[1][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParse6() throws IOException {
|
public void testParse6() throws IOException {
|
||||||
String[][] data = CSVUtils.parse("");
|
String[][] data = CSVUtils.parse("");
|
||||||
// default strategy is CSV, which ignores empty lines
|
// default strategy is CSV, which ignores empty lines
|
||||||
assertEquals(0, data.length);
|
assertEquals(0, data.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParse7() throws IOException {
|
public void testParse7() throws IOException {
|
||||||
boolean io = false;
|
boolean io = false;
|
||||||
try {
|
try {
|
||||||
CSVUtils.parse(null);
|
CSVUtils.parse(null);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
io = true;
|
io = true;
|
||||||
}
|
}
|
||||||
assertTrue(io);
|
assertTrue(io);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParseLine1() throws IOException {
|
public void testParseLine1() throws IOException {
|
||||||
String[] data = CSVUtils.parseLine("abc,def,ghi");
|
String[] data = CSVUtils.parseLine("abc,def,ghi");
|
||||||
assertEquals(3, data.length);
|
assertEquals(3, data.length);
|
||||||
assertEquals("abc", data[0]);
|
assertEquals("abc", data[0]);
|
||||||
assertEquals("def", data[1]);
|
assertEquals("def", data[1]);
|
||||||
assertEquals("ghi", data[2]);
|
assertEquals("ghi", data[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParseLine2() throws IOException {
|
public void testParseLine2() throws IOException {
|
||||||
String[] data = CSVUtils.parseLine("abc,def,ghi\n");
|
String[] data = CSVUtils.parseLine("abc,def,ghi\n");
|
||||||
assertEquals(3, data.length);
|
assertEquals(3, data.length);
|
||||||
assertEquals("abc", data[0]);
|
assertEquals("abc", data[0]);
|
||||||
assertEquals("def", data[1]);
|
assertEquals("def", data[1]);
|
||||||
assertEquals("ghi", data[2]);
|
assertEquals("ghi", data[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParseLine3() throws IOException {
|
public void testParseLine3() throws IOException {
|
||||||
String[] data = CSVUtils.parseLine("abc,\"def,ghi\"");
|
String[] data = CSVUtils.parseLine("abc,\"def,ghi\"");
|
||||||
assertEquals(2, data.length);
|
assertEquals(2, data.length);
|
||||||
assertEquals("abc", data[0]);
|
assertEquals("abc", data[0]);
|
||||||
assertEquals("def,ghi", data[1]);
|
assertEquals("def,ghi", data[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParseLine4() throws IOException {
|
public void testParseLine4() throws IOException {
|
||||||
String[] data = CSVUtils.parseLine("abc,\"def\nghi\"");
|
String[] data = CSVUtils.parseLine("abc,\"def\nghi\"");
|
||||||
assertEquals(2, data.length);
|
assertEquals(2, data.length);
|
||||||
assertEquals("abc", data[0]);
|
assertEquals("abc", data[0]);
|
||||||
assertEquals("def\nghi", data[1]);
|
assertEquals("def\nghi", data[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParseLine5() throws IOException {
|
public void testParseLine5() throws IOException {
|
||||||
String[] data = CSVUtils.parseLine("");
|
String[] data = CSVUtils.parseLine("");
|
||||||
assertEquals(0, data.length);
|
assertEquals(0, data.length);
|
||||||
// assertEquals("", data[0]);
|
// assertEquals("", data[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParseLine6() throws IOException {
|
public void testParseLine6() throws IOException {
|
||||||
boolean io = false;
|
boolean io = false;
|
||||||
try {
|
try {
|
||||||
CSVUtils.parseLine(null);
|
CSVUtils.parseLine(null);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
io = true;
|
io = true;
|
||||||
}
|
}
|
||||||
assertTrue(io);
|
assertTrue(io);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testParseLine7() throws IOException {
|
public void testParseLine7() throws IOException {
|
||||||
String[] res = CSVUtils.parseLine("");
|
String[] res = CSVUtils.parseLine("");
|
||||||
assertNotNull(res);
|
assertNotNull(res);
|
||||||
assertEquals(0, res.length);
|
assertEquals(0, res.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ package org.apache.commons.csv;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Ortwin Glück
|
* @author Ortwin Glück
|
||||||
*/
|
*/
|
||||||
public class CharBufferTest extends TestCase {
|
public class CharBufferTest extends TestCase {
|
||||||
|
@ -31,7 +30,7 @@ public class CharBufferTest extends TestCase {
|
||||||
try {
|
try {
|
||||||
cb = new CharBuffer(0);
|
cb = new CharBuffer(0);
|
||||||
fail("Should not be possible");
|
fail("Should not be possible");
|
||||||
} catch(IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,11 +53,11 @@ public class CharBufferTest extends TestCase {
|
||||||
CharBuffer cb = new CharBuffer(1);
|
CharBuffer cb = new CharBuffer(1);
|
||||||
char[] abcd = "abcd".toCharArray();
|
char[] abcd = "abcd".toCharArray();
|
||||||
String expected = "";
|
String expected = "";
|
||||||
for (int i=0; i<10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
cb.append(abcd);
|
cb.append(abcd);
|
||||||
expected += "abcd";
|
expected += "abcd";
|
||||||
assertEquals(expected, cb.toString());
|
assertEquals(expected, cb.toString());
|
||||||
assertEquals(4*(i+1), cb.length());
|
assertEquals(4 * (i + 1), cb.length());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,11 +65,11 @@ public class CharBufferTest extends TestCase {
|
||||||
CharBuffer cb = new CharBuffer(1);
|
CharBuffer cb = new CharBuffer(1);
|
||||||
String abcd = "abcd";
|
String abcd = "abcd";
|
||||||
String expected = "";
|
String expected = "";
|
||||||
for (int i=0; i<10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
cb.append(abcd);
|
cb.append(abcd);
|
||||||
expected += abcd;
|
expected += abcd;
|
||||||
assertEquals(expected, cb.toString());
|
assertEquals(expected, cb.toString());
|
||||||
assertEquals(4*(i+1), cb.length());
|
assertEquals(4 * (i + 1), cb.length());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,11 +77,11 @@ public class CharBufferTest extends TestCase {
|
||||||
CharBuffer cb = new CharBuffer(1);
|
CharBuffer cb = new CharBuffer(1);
|
||||||
StringBuffer abcd = new StringBuffer("abcd");
|
StringBuffer abcd = new StringBuffer("abcd");
|
||||||
String expected = "";
|
String expected = "";
|
||||||
for (int i=0; i<10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
cb.append(abcd);
|
cb.append(abcd);
|
||||||
expected += "abcd";
|
expected += "abcd";
|
||||||
assertEquals(expected, cb.toString());
|
assertEquals(expected, cb.toString());
|
||||||
assertEquals(4*(i+1), cb.length());
|
assertEquals(4 * (i + 1), cb.length());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,11 +90,11 @@ public class CharBufferTest extends TestCase {
|
||||||
CharBuffer abcd = new CharBuffer(17);
|
CharBuffer abcd = new CharBuffer(17);
|
||||||
abcd.append("abcd");
|
abcd.append("abcd");
|
||||||
String expected = "";
|
String expected = "";
|
||||||
for (int i=0; i<10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
cb.append(abcd);
|
cb.append(abcd);
|
||||||
expected += "abcd";
|
expected += "abcd";
|
||||||
assertEquals(expected, cb.toString());
|
assertEquals(expected, cb.toString());
|
||||||
assertEquals(4*(i+1), cb.length());
|
assertEquals(4 * (i + 1), cb.length());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,7 +124,7 @@ public class CharBufferTest extends TestCase {
|
||||||
assertEquals(0, b1.length);
|
assertEquals(0, b1.length);
|
||||||
assertEquals(0, buffer.length());
|
assertEquals(0, buffer.length());
|
||||||
|
|
||||||
char[] tmp = new char[] { '1', '2', '3', '4'};
|
char[] tmp = new char[]{'1', '2', '3', '4'};
|
||||||
buffer.append(tmp);
|
buffer.append(tmp);
|
||||||
assertEquals(16, buffer.capacity());
|
assertEquals(16, buffer.capacity());
|
||||||
assertEquals(4, buffer.length());
|
assertEquals(4, buffer.length());
|
||||||
|
@ -153,16 +152,16 @@ public class CharBufferTest extends TestCase {
|
||||||
public void testAppendNull() throws Exception {
|
public void testAppendNull() throws Exception {
|
||||||
CharBuffer buffer = new CharBuffer(8);
|
CharBuffer buffer = new CharBuffer(8);
|
||||||
|
|
||||||
buffer.append((StringBuffer)null);
|
buffer.append((StringBuffer) null);
|
||||||
assertEquals("", buffer.toString());
|
assertEquals("", buffer.toString());
|
||||||
|
|
||||||
buffer.append((String)null);
|
buffer.append((String) null);
|
||||||
assertEquals("", buffer.toString());
|
assertEquals("", buffer.toString());
|
||||||
|
|
||||||
buffer.append((CharBuffer)null);
|
buffer.append((CharBuffer) null);
|
||||||
assertEquals("", buffer.toString());
|
assertEquals("", buffer.toString());
|
||||||
|
|
||||||
buffer.append((char[])null);
|
buffer.append((char[]) null);
|
||||||
assertEquals("", buffer.toString());
|
assertEquals("", buffer.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,139 +25,138 @@ import junit.framework.TestSuite;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ExtendedBufferedReaderTest
|
* ExtendedBufferedReaderTest
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ExtendedBufferedReaderTest extends TestCase {
|
public class ExtendedBufferedReaderTest extends TestCase {
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
// the test cases
|
// the test cases
|
||||||
// ======================================================
|
// ======================================================
|
||||||
|
|
||||||
public void testConstructors() {
|
public void testConstructors() {
|
||||||
ExtendedBufferedReader br = new ExtendedBufferedReader(new StringReader(""));
|
ExtendedBufferedReader br = new ExtendedBufferedReader(new StringReader(""));
|
||||||
br = new ExtendedBufferedReader(new StringReader(""), 10);
|
br = new ExtendedBufferedReader(new StringReader(""), 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReadLookahead1() throws Exception {
|
public void testReadLookahead1() throws Exception {
|
||||||
|
|
||||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, getEBR("").read());
|
assertEquals(ExtendedBufferedReader.END_OF_STREAM, getEBR("").read());
|
||||||
ExtendedBufferedReader br = getEBR("1\n2\r3\n");
|
ExtendedBufferedReader br = getEBR("1\n2\r3\n");
|
||||||
assertEquals('1', br.lookAhead());
|
assertEquals('1', br.lookAhead());
|
||||||
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
|
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
|
||||||
assertEquals('1', br.read());
|
assertEquals('1', br.read());
|
||||||
assertEquals('1', br.readAgain());
|
assertEquals('1', br.readAgain());
|
||||||
|
|
||||||
assertEquals(0, br.getLineNumber());
|
assertEquals(0, br.getLineNumber());
|
||||||
assertEquals('\n', br.lookAhead());
|
assertEquals('\n', br.lookAhead());
|
||||||
assertEquals(0, br.getLineNumber());
|
assertEquals(0, br.getLineNumber());
|
||||||
assertEquals('1', br.readAgain());
|
assertEquals('1', br.readAgain());
|
||||||
assertEquals('\n', br.read());
|
assertEquals('\n', br.read());
|
||||||
assertEquals(1, br.getLineNumber());
|
assertEquals(1, br.getLineNumber());
|
||||||
assertEquals('\n', br.readAgain());
|
assertEquals('\n', br.readAgain());
|
||||||
assertEquals(1, br.getLineNumber());
|
assertEquals(1, br.getLineNumber());
|
||||||
|
|
||||||
assertEquals('2', br.lookAhead());
|
assertEquals('2', br.lookAhead());
|
||||||
assertEquals(1, br.getLineNumber());
|
assertEquals(1, br.getLineNumber());
|
||||||
assertEquals('\n', br.readAgain());
|
assertEquals('\n', br.readAgain());
|
||||||
assertEquals(1, br.getLineNumber());
|
assertEquals(1, br.getLineNumber());
|
||||||
assertEquals('2', br.read());
|
assertEquals('2', br.read());
|
||||||
assertEquals('2', br.readAgain());
|
assertEquals('2', br.readAgain());
|
||||||
|
|
||||||
assertEquals('\r', br.lookAhead());
|
assertEquals('\r', br.lookAhead());
|
||||||
assertEquals('2', br.readAgain());
|
assertEquals('2', br.readAgain());
|
||||||
assertEquals('\r', br.read());
|
assertEquals('\r', br.read());
|
||||||
assertEquals('\r', br.readAgain());
|
assertEquals('\r', br.readAgain());
|
||||||
|
|
||||||
assertEquals('3', br.lookAhead());
|
assertEquals('3', br.lookAhead());
|
||||||
assertEquals('\r', br.readAgain());
|
assertEquals('\r', br.readAgain());
|
||||||
assertEquals('3', br.read());
|
assertEquals('3', br.read());
|
||||||
assertEquals('3', br.readAgain());
|
assertEquals('3', br.readAgain());
|
||||||
|
|
||||||
assertEquals('\n', br.lookAhead());
|
assertEquals('\n', br.lookAhead());
|
||||||
assertEquals(1, br.getLineNumber());
|
assertEquals(1, br.getLineNumber());
|
||||||
assertEquals('3', br.readAgain());
|
assertEquals('3', br.readAgain());
|
||||||
assertEquals('\n', br.read());
|
assertEquals('\n', br.read());
|
||||||
assertEquals(2, br.getLineNumber());
|
assertEquals(2, br.getLineNumber());
|
||||||
assertEquals('\n', br.readAgain());
|
assertEquals('\n', br.readAgain());
|
||||||
assertEquals(2, br.getLineNumber());
|
assertEquals(2, br.getLineNumber());
|
||||||
|
|
||||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
|
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
|
||||||
assertEquals('\n', br.readAgain());
|
assertEquals('\n', br.readAgain());
|
||||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
|
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
|
||||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
|
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
|
||||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
|
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
|
||||||
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
|
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testReadLookahead2() throws Exception {
|
public void testReadLookahead2() throws Exception {
|
||||||
char[] ref = new char[5];
|
char[] ref = new char[5];
|
||||||
char[] res = new char[5];
|
char[] res = new char[5];
|
||||||
|
|
||||||
ExtendedBufferedReader br = getEBR("");
|
ExtendedBufferedReader br = getEBR("");
|
||||||
assertEquals(0, br.read(res, 0, 0));
|
assertEquals(0, br.read(res, 0, 0));
|
||||||
assertTrue(Arrays.equals(res, ref));
|
assertTrue(Arrays.equals(res, ref));
|
||||||
|
|
||||||
br = getEBR("abcdefg");
|
br = getEBR("abcdefg");
|
||||||
ref[0] = 'a';
|
ref[0] = 'a';
|
||||||
ref[1] = 'b';
|
ref[1] = 'b';
|
||||||
ref[2] = 'c';
|
ref[2] = 'c';
|
||||||
assertEquals(3, br.read(res, 0, 3));
|
assertEquals(3, br.read(res, 0, 3));
|
||||||
assertTrue(Arrays.equals(res, ref));
|
assertTrue(Arrays.equals(res, ref));
|
||||||
assertEquals('c', br.readAgain());
|
assertEquals('c', br.readAgain());
|
||||||
|
|
||||||
assertEquals('d', br.lookAhead());
|
assertEquals('d', br.lookAhead());
|
||||||
ref[4] = 'd';
|
ref[4] = 'd';
|
||||||
assertEquals(1, br.read(res, 4, 1));
|
assertEquals(1, br.read(res, 4, 1));
|
||||||
assertTrue(Arrays.equals(res, ref));
|
assertTrue(Arrays.equals(res, ref));
|
||||||
assertEquals('d', br.readAgain());
|
assertEquals('d', br.readAgain());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReadLine() throws Exception {
|
public void testReadLine() throws Exception {
|
||||||
ExtendedBufferedReader br = getEBR("");
|
ExtendedBufferedReader br = getEBR("");
|
||||||
assertTrue(br.readLine() == null);
|
assertTrue(br.readLine() == null);
|
||||||
|
|
||||||
br = getEBR("\n");
|
br = getEBR("\n");
|
||||||
assertTrue(br.readLine().equals(""));
|
assertTrue(br.readLine().equals(""));
|
||||||
assertTrue(br.readLine() == null);
|
assertTrue(br.readLine() == null);
|
||||||
|
|
||||||
br = getEBR("foo\n\nhello");
|
br = getEBR("foo\n\nhello");
|
||||||
assertEquals(0, br.getLineNumber());
|
assertEquals(0, br.getLineNumber());
|
||||||
assertTrue(br.readLine().equals("foo"));
|
assertTrue(br.readLine().equals("foo"));
|
||||||
assertEquals(1, br.getLineNumber());
|
assertEquals(1, br.getLineNumber());
|
||||||
assertTrue(br.readLine().equals(""));
|
assertTrue(br.readLine().equals(""));
|
||||||
assertEquals(2, br.getLineNumber());
|
assertEquals(2, br.getLineNumber());
|
||||||
assertTrue(br.readLine().equals("hello"));
|
assertTrue(br.readLine().equals("hello"));
|
||||||
assertEquals(3, br.getLineNumber());
|
assertEquals(3, br.getLineNumber());
|
||||||
assertTrue(br.readLine() == null);
|
assertTrue(br.readLine() == null);
|
||||||
assertEquals(3, br.getLineNumber());
|
assertEquals(3, br.getLineNumber());
|
||||||
|
|
||||||
br = getEBR("foo\n\nhello");
|
br = getEBR("foo\n\nhello");
|
||||||
assertEquals('f', br.read());
|
assertEquals('f', br.read());
|
||||||
assertEquals('o', br.lookAhead());
|
assertEquals('o', br.lookAhead());
|
||||||
assertTrue(br.readLine().equals("oo"));
|
assertTrue(br.readLine().equals("oo"));
|
||||||
assertEquals(1, br.getLineNumber());
|
assertEquals(1, br.getLineNumber());
|
||||||
assertEquals('\n', br.lookAhead());
|
assertEquals('\n', br.lookAhead());
|
||||||
assertTrue(br.readLine().equals(""));
|
assertTrue(br.readLine().equals(""));
|
||||||
assertEquals(2, br.getLineNumber());
|
assertEquals(2, br.getLineNumber());
|
||||||
assertEquals('h', br.lookAhead());
|
assertEquals('h', br.lookAhead());
|
||||||
assertTrue(br.readLine().equals("hello"));
|
assertTrue(br.readLine().equals("hello"));
|
||||||
assertTrue(br.readLine() == null);
|
assertTrue(br.readLine() == null);
|
||||||
assertEquals(3, br.getLineNumber());
|
assertEquals(3, br.getLineNumber());
|
||||||
|
|
||||||
|
|
||||||
br = getEBR("foo\rbaar\r\nfoo");
|
br = getEBR("foo\rbaar\r\nfoo");
|
||||||
assertTrue(br.readLine().equals("foo"));
|
assertTrue(br.readLine().equals("foo"));
|
||||||
assertEquals('b', br.lookAhead());
|
assertEquals('b', br.lookAhead());
|
||||||
assertTrue(br.readLine().equals("baar"));
|
assertTrue(br.readLine().equals("baar"));
|
||||||
assertEquals('f', br.lookAhead());
|
assertEquals('f', br.lookAhead());
|
||||||
assertTrue(br.readLine().equals("foo"));
|
assertTrue(br.readLine().equals("foo"));
|
||||||
assertTrue(br.readLine() == null);
|
assertTrue(br.readLine() == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ExtendedBufferedReader getEBR(String s) {
|
private ExtendedBufferedReader getEBR(String s) {
|
||||||
return new ExtendedBufferedReader(new StringReader(s));
|
return new ExtendedBufferedReader(new StringReader(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,10 +41,10 @@ public class CSVConfigGuesserTest extends TestCase {
|
||||||
guesser.setHasFieldHeader(true);
|
guesser.setHasFieldHeader(true);
|
||||||
assertEquals(true, guesser.hasFieldHeader());
|
assertEquals(true, guesser.hasFieldHeader());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test a format like
|
* Test a format like
|
||||||
* 1234 ; abcd ; 1234 ;
|
* 1234 ; abcd ; 1234 ;
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public void testConfigGuess1() {
|
public void testConfigGuess1() {
|
||||||
CSVConfig expected = new CSVConfig();
|
CSVConfig expected = new CSVConfig();
|
||||||
|
@ -67,11 +67,11 @@ public class CSVConfigGuesserTest extends TestCase {
|
||||||
assertEquals(expected.getFields().length, guessed.getFields().length);
|
assertEquals(expected.getFields().length, guessed.getFields().length);
|
||||||
assertEquals(expected.getFields()[0].getSize(), guessed.getFields()[0].getSize());
|
assertEquals(expected.getFields()[0].getSize(), guessed.getFields()[0].getSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test a format like
|
* Test a format like
|
||||||
* 1234,123123,12312312,213123
|
* 1234,123123,12312312,213123
|
||||||
* 1,2,3,4
|
* 1,2,3,4
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public void testConfigGuess2() {
|
public void testConfigGuess2() {
|
||||||
CSVConfig expected = new CSVConfig();
|
CSVConfig expected = new CSVConfig();
|
||||||
|
|
|
@ -41,9 +41,9 @@ public class CSVConfigTest extends TestCase {
|
||||||
public void testFields() {
|
public void testFields() {
|
||||||
CSVConfig config = new CSVConfig();
|
CSVConfig config = new CSVConfig();
|
||||||
assertEquals(0, config.getFields().length);
|
assertEquals(0, config.getFields().length);
|
||||||
config.setFields((CSVField[])null);
|
config.setFields((CSVField[]) null);
|
||||||
assertEquals(0, config.getFields().length);
|
assertEquals(0, config.getFields().length);
|
||||||
config.setFields((Collection)null);
|
config.setFields((Collection) null);
|
||||||
assertEquals(0, config.getFields().length);
|
assertEquals(0, config.getFields().length);
|
||||||
CSVField field = new CSVField();
|
CSVField field = new CSVField();
|
||||||
field.setName("field1");
|
field.setName("field1");
|
||||||
|
|
|
@ -21,7 +21,6 @@ package org.apache.commons.csv.writer;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Martin van den Bemt
|
* @author Martin van den Bemt
|
||||||
* @version $Id: $
|
* @version $Id: $
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue