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:
Jacopo Cappellato 2011-02-01 08:46:00 +00:00
parent fe5bd51f8a
commit 1166ca605b
20 changed files with 2420 additions and 2246 deletions

File diff suppressed because it is too large Load Diff

View File

@ -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);
}
} }

View File

@ -20,7 +20,7 @@ import java.io.Serializable;
/** /**
* CSVStrategy * CSVStrategy
* *
* Represents the strategy for a CSV. * Represents the strategy for a CSV.
*/ */
public class CSVStrategy implements Cloneable, Serializable { public class CSVStrategy implements Cloneable, Serializable {
@ -41,47 +41,46 @@ public class CSVStrategy implements Cloneable, Serializable {
// an EOF signal (-1), and because \ufffe in UTF-16 would be // 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) {
this(delimiter, encapsulator, commentStart, true, false, true); this(delimiter, encapsulator, commentStart, true, false, true);
} }
/** /**
* 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 getEncapsulator() { return this.encapsulator; }
public void setCommentStart(char commentStart) { this.commentStart = commentStart; }
public char getCommentStart() { return this.commentStart; }
public boolean isCommentingDisabled() { return this.commentStart == COMMENTS_DISABLED; }
public void setEscape(char escape) { this.escape = escape; }
public char getEscape() { return this.escape; }
public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) {
this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;
} }
public boolean getIgnoreLeadingWhitespaces() { return this.ignoreLeadingWhitespaces; }
public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) { public char getDelimiter() {
this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces; return this.delimiter;
} }
public boolean getIgnoreTrailingWhitespaces() { return this.ignoreTrailingWhitespaces; }
public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) { public void setEncapsulator(char encapsulator) {
this.interpretUnicodeEscapes = interpretUnicodeEscapes; this.encapsulator = encapsulator;
} }
public boolean getUnicodeEscapeInterpretation() { return this.interpretUnicodeEscapes; }
public void setIgnoreEmptyLines(boolean ignoreEmptyLines) { this.ignoreEmptyLines = ignoreEmptyLines; } public char getEncapsulator() {
public boolean getIgnoreEmptyLines() { return this.ignoreEmptyLines; } return this.encapsulator;
}
public void setCommentStart(char commentStart) {
this.commentStart = commentStart;
}
public char getCommentStart() {
return this.commentStart;
}
public boolean isCommentingDisabled() {
return this.commentStart == COMMENTS_DISABLED;
}
public void setEscape(char escape) {
this.escape = escape;
}
public char getEscape() {
return this.escape;
}
public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) {
this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;
}
public boolean getIgnoreLeadingWhitespaces() {
return this.ignoreLeadingWhitespaces;
}
public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {
this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;
}
public boolean getIgnoreTrailingWhitespaces() {
return this.ignoreTrailingWhitespaces;
}
public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) {
this.interpretUnicodeEscapes = interpretUnicodeEscapes;
}
public boolean getUnicodeEscapeInterpretation() {
return this.interpretUnicodeEscapes;
}
public void setIgnoreEmptyLines(boolean ignoreEmptyLines) {
this.ignoreEmptyLines = ignoreEmptyLines;
}
public boolean getIgnoreEmptyLines() {
return this.ignoreEmptyLines;
}
public void setPrinterNewline(String newline) { 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
} }
} }
} }

View File

@ -30,14 +30,14 @@ public class CSVUtils {
/** /**
* <p><code>CSVUtils</code> instances should NOT be constructed in * <p><code>CSVUtils</code> instances should NOT be constructed in
* standard programming. * standard programming.
* *
* <p>This constructor is public to permit tools that require a JavaBean * <p>This constructor is public to permit tools that require a JavaBean
* instance to operate.</p> * instance to operate.</p>
*/ */
public CSVUtils() { public CSVUtils() {
} }
/** /**
* Converts an array of string values into a single CSV line. All * Converts an array of string values into a single CSV line. All
* <code>null</code> values are converted to the string <code>"null"</code>, * <code>null</code> values are converted to the string <code>"null"</code>,
@ -46,13 +46,13 @@ 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
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
CSVPrinter csvPrinter = new CSVPrinter(stringWriter, strategy); CSVPrinter csvPrinter = new CSVPrinter(stringWriter, strategy);
// check for null values an "null" as strings and convert them // check for null values an "null" as strings and convert them
// into the strings "null" and "\"null\"" // into the strings "null" and "\"null\""
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
@ -62,60 +62,60 @@ public class CSVUtils {
values[i] = "\"null\""; values[i] = "\"null\"";
} }
} }
// 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 * Parses the first line only according to the default {@link CSVStrategy}.
// (i.e. not "result = new String[][] {{""}};") *
result = EMPTY_DOUBLE_STRING_ARRAY; * Parsing empty string will be handled as valid records containing zero
* elements, so the following property holds: parseLine("").length == 0.
*
* @param s CSV String to be parsed.
* @return parsed String vector (which is never null)
* @throws IOException in case of error
*/
public static String[] parseLine(String s) throws IOException {
if (s == null) {
throw new IllegalArgumentException("Null argument not allowed.");
}
// uh,jh: make sure that parseLine("").length == 0
if (s.length() == 0) {
return EMPTY_STRING_ARRAY;
}
return (new CSVParser(new StringReader(s))).getLine();
} }
return result;
}
/**
* Parses the first line only according to the default {@link CSVStrategy}.
*
* Parsing empty string will be handled as valid records containing zero
* elements, so the following property holds: parseLine("").length == 0.
*
* @param s CSV String to be parsed.
* @return parsed String vector (which is never null)
* @throws IOException in case of error
*/
public static String[] parseLine(String s) throws IOException {
if (s == null) {
throw new IllegalArgumentException("Null argument not allowed.");
}
// uh,jh: make sure that parseLine("").length == 0
if (s.length() == 0) {
return EMPTY_STRING_ARRAY;
}
return (new CSVParser(new StringReader(s))).getLine();
}
} }

View File

@ -19,11 +19,11 @@
package org.apache.commons.csv; package org.apache.commons.csv;
/** /**
* A simple StringBuffer replacement that aims to * A simple StringBuffer replacement that aims to
* reduce copying as much as possible. The buffer * reduce copying as much as possible. The buffer
* grows as necessary. * grows as necessary.
* This class is not thread safe. * This class is not thread safe.
* *
* @author Ortwin Gl<EFBFBD>ck * @author Ortwin Gl<EFBFBD>ck
*/ */
public class CharBuffer { public class CharBuffer {
@ -31,21 +31,21 @@ public class CharBuffer {
private char[] c; private char[] c;
/** /**
* Actually used number of characters in the array. * Actually used number of characters in the array.
* It is also the index at which * It is also the index at which
* a new character will be inserted into <code>c</code>. * a new character will be inserted into <code>c</code>.
*/ */
private int length; private int length;
/** /**
* Creates a new CharBuffer with an initial capacity of 32 characters. * Creates a new CharBuffer with an initial capacity of 32 characters.
*/ */
public CharBuffer() { public CharBuffer() {
this(32); this(32);
} }
/** /**
* Creates a new CharBuffer with an initial capacity * Creates a new CharBuffer with an initial capacity
* of <code>length</code> characters. * of <code>length</code> characters.
*/ */
public CharBuffer(final int length) { public CharBuffer(final int length) {
@ -54,16 +54,17 @@ public class CharBuffer {
} }
this.c = new char[length]; this.c = new char[length];
} }
/** /**
* Empties the buffer. The capacity still remains the same, so no memory is freed. * Empties the buffer. The capacity still remains the same, so no memory is freed.
*/ */
public void clear() { public void clear() {
length = 0; length = 0;
} }
/** /**
* 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,16 +73,18 @@ 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;
} }
/** /**
* 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) {
@ -92,10 +95,11 @@ public class CharBuffer {
System.arraycopy(cb.c, 0, c, length, cb.length); System.arraycopy(cb.c, 0, c, length, cb.length);
length += cb.length; length += cb.length;
} }
/** /**
* 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) {
@ -104,10 +108,11 @@ public class CharBuffer {
} }
append(s.toCharArray()); append(s.toCharArray());
} }
/** /**
* 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) {
@ -118,10 +123,11 @@ public class CharBuffer {
sb.getChars(0, sb.length(), c, length); sb.getChars(0, sb.length(), c, length);
length += sb.length(); length += sb.length();
} }
/** /**
* 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) {
@ -132,10 +138,11 @@ public class CharBuffer {
System.arraycopy(data, 0, c, length, data.length); System.arraycopy(data, 0, c, length, data.length);
length += data.length; length += data.length;
} }
/** /**
* 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) {
@ -143,7 +150,7 @@ public class CharBuffer {
c[length] = data; c[length] = data;
length++; length++;
} }
/** /**
* Shrinks the capacity of the buffer to the current length if necessary. * Shrinks the capacity of the buffer to the current length if necessary.
* This method involves copying the data once! * This method involves copying the data once!
@ -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() {
@ -200,25 +209,27 @@ public class CharBuffer {
sb.append(c, 0, length); sb.append(c, 0, length);
return sb; return sb;
} }
/** /**
* 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() {
return new String(c, 0, length); return new String(c, 0, length);
} }
/** /**
* 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;

View File

@ -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;
/**
* the line counter
*/
private int lineCounter = 0;
private CharBuffer line = new CharBuffer();
/**
* Created extended buffered reader using default buffer-size
*/
public ExtendedBufferedReader(Reader r) {
super(r);
/* note uh: do not fetch the first char here,
* because this might block the method!
*/
} }
lastChar = lookaheadChar;
if (super.ready()) { /**
lookaheadChar = super.read(); * Create extended buffered reader using the given buffer-size
} else { */
lookaheadChar = UNDEFINED; public ExtendedBufferedReader(Reader r, int bufSize) {
super(r, bufSize);
/* note uh: do not fetch the first char here,
* because this might block the method!
*/
} }
if (lastChar == '\n') {
lineCounter++; /**
} * Reads the next char from the input stream.
return lastChar; *
} * @return the next char or END_OF_STREAM if end of stream has been reached.
*/
/** public int read() throws IOException {
* Returns the last read character again. // initialize the lookahead
* if (lookaheadChar == UNDEFINED) {
* @return the last read char or UNDEFINED lookaheadChar = super.read();
*/ }
public int readAgain() { lastChar = lookaheadChar;
return lastChar; if (super.ready()) {
} lookaheadChar = super.read();
/**
* Non-blocking reading of len chars into buffer buf starting
* at bufferposition off.
*
* performs an iteratative read on the underlying stream
* as long as the following conditions hold:
* - less than len chars have been read
* - end of stream has not been reached
* - next read is not blocking
*
* @return nof chars actually read or END_OF_STREAM
*/
public int read(char[] buf, int off, int len) throws IOException {
// do not claim if len == 0
if (len == 0) {
return 0;
}
// init lookahead, but do not block !!
if (lookaheadChar == UNDEFINED) {
if (ready()) {
lookaheadChar = super.read();
} else { } else {
return -1; lookaheadChar = UNDEFINED;
}
if (lastChar == '\n') {
lineCounter++;
}
return lastChar;
}
/**
* Returns the last read character again.
*
* @return the last read char or UNDEFINED
*/
public int readAgain() {
return lastChar;
}
/**
* Non-blocking reading of len chars into buffer buf starting
* at bufferposition off.
* <p/>
* performs an iterative read on the underlying stream
* as long as the following conditions hold:
* - less than len chars have been read
* - end of stream has not been reached
* - next read is not blocking
*
* @return nof chars actually read or END_OF_STREAM
*/
public int read(char[] buf, int off, int len) throws IOException {
// do not claim if len == 0
if (len == 0) {
return 0;
}
// init lookahead, but do not block !!
if (lookaheadChar == UNDEFINED) {
if (ready()) {
lookaheadChar = super.read();
} else {
return -1;
}
}
// 'first read of underlying stream'
if (lookaheadChar == -1) {
return -1;
}
// continue until the lookaheadChar would block
int cOff = off;
while (len > 0 && ready()) {
if (lookaheadChar == -1) {
// eof stream reached, do not continue
return cOff - off;
} else {
buf[cOff++] = (char) lookaheadChar;
if (lookaheadChar == '\n') {
lineCounter++;
}
lastChar = lookaheadChar;
lookaheadChar = super.read();
len--;
}
}
return cOff - off;
}
/**
* @return A String containing the contents of the line, not
* including any line-termination characters, or null
* if the end of the stream has been reached
*/
public String readLine() throws IOException {
if (lookaheadChar == UNDEFINED) {
lookaheadChar = super.read();
}
line.clear(); //reuse
// return null if end of stream has been reached
if (lookaheadChar == END_OF_STREAM) {
return null;
}
// do we have a line termination already
char laChar = (char) lookaheadChar;
if (laChar == '\n' || laChar == '\r') {
lastChar = lookaheadChar;
lookaheadChar = super.read();
// ignore '\r\n' as well
if ((char) lookaheadChar == '\n') {
lastChar = lookaheadChar;
lookaheadChar = super.read();
}
lineCounter++;
return line.toString();
}
// create the rest-of-line return and update the lookahead
line.append(laChar);
String restOfLine = super.readLine(); // TODO involves copying
lastChar = lookaheadChar;
lookaheadChar = super.read();
if (restOfLine != null) {
line.append(restOfLine);
}
lineCounter++;
return line.toString();
}
/**
* Unsupported
*/
public long skip(long n) throws IllegalArgumentException, IOException {
throw new UnsupportedOperationException("CSV has no reason to implement this");
}
/**
* Returns the next char in the stream without consuming it.
*
* Remember the next char read by read(..) will always be
* identical to lookAhead().
*
* @return the next char (without consuming it) or END_OF_STREAM
*/
public int lookAhead() throws IOException {
if (lookaheadChar == UNDEFINED) {
lookaheadChar = super.read();
}
return lookaheadChar;
}
/**
* Returns the nof line read
*
* @return the current-line-number (or -1)
*/
public int getLineNumber() {
if (lineCounter > -1) {
return lineCounter;
} else {
return -1;
} }
} }
// 'first read of underlying stream'
if (lookaheadChar == -1) {
return -1;
}
// continue until the lookaheadChar would block
int cOff = off;
while (len > 0 && ready()) {
if (lookaheadChar == -1) {
// eof stream reached, do not continue
return cOff - off;
} else {
buf[cOff++] = (char) lookaheadChar;
if (lookaheadChar == '\n') {
lineCounter++;
}
lastChar = lookaheadChar;
lookaheadChar = super.read();
len--;
}
}
return cOff - off;
}
/**
* @return A String containing the contents of the line, not
* including any line-termination characters, or null
* if the end of the stream has been reached
*/
public String readLine() throws IOException {
if (lookaheadChar == UNDEFINED) {
lookaheadChar = super.read();
}
line.clear(); //reuse
// return null if end of stream has been reached
if (lookaheadChar == END_OF_STREAM) {
return null;
}
// do we have a line termination already
char laChar = (char) lookaheadChar;
if (laChar == '\n' || laChar == '\r') {
lastChar = lookaheadChar;
lookaheadChar = super.read();
// ignore '\r\n' as well
if ((char) lookaheadChar == '\n') {
lastChar = lookaheadChar;
lookaheadChar = super.read();
}
lineCounter++;
return line.toString();
}
// create the rest-of-line return and update the lookahead
line.append(laChar);
String restOfLine = super.readLine(); // TODO involves copying
lastChar = lookaheadChar;
lookaheadChar = super.read();
if (restOfLine != null) {
line.append(restOfLine);
}
lineCounter++;
return line.toString();
}
/**
* Unsupported
*/
public long skip(long n) throws IllegalArgumentException, IOException {
throw new UnsupportedOperationException("CSV has no reason to implement this");
}
/**
* Returns the next char in the stream without consuming it.
*
* Remember the next char read by read(..) will always be
* identical to lookAhead().
*
* @return the next char (without consuming it) or END_OF_STREAM
*/
public int lookAhead() throws IOException {
if (lookaheadChar == UNDEFINED) {
lookaheadChar = super.read();
}
return lookaheadChar;
}
/**
* Returns the nof line read
*
* @return the current-line-number (or -1)
*/
public int getLineNumber() {
if (lineCounter > -1) {
return lineCounter;
} else {
return -1;
}
}
/** /**
* Unsupported * Unsupported
*/ */
public boolean markSupported() { public boolean markSupported() {
throw new UnsupportedOperationException("CSV has no reason to implement this"); throw new UnsupportedOperationException("CSV has no reason to implement this");
} }
} }

View File

@ -32,69 +32,100 @@ 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 **/
public static final int FILLNONE = 0;
/** Fill content the the left. Mainly usable together with fixedWidth **/
public static final int FILLLEFT = 1;
/** Fill content to the right. Mainly usable together with fixedWidth **/
public static final int FILLRIGHT = 2;
/** The fill pattern */
private int fill;
/** The fill char. Defaults to a space */
private char fillChar = ' ';
/** The seperator character. Defaults to , */
private char delimiter = ',';
/** The row separator. Defaults to \n */
private String rowDelimiter = "\n";
/** Should we ignore the delimiter. Defaults to false */
private boolean ignoreDelimiter = false;
/** the value delimiter. Defaults to " */
private char valueDelimiter = '"';
/** Should we ignore the value delimiter. Defaults to true */
private boolean ignoreValueDelimiter = true;
/** Specifies if we want to use a field header */
private boolean fieldHeader = false;
/** Specifies if the end of the line needs to be trimmed */
private boolean endTrimmed = false;
/** /**
* * Do no do any filling *
*/
public static final int FILLNONE = 0;
/**
* Fill content the the left. Mainly usable together with fixedWidth *
*/
public static final int FILLLEFT = 1;
/**
* Fill content to the right. Mainly usable together with fixedWidth *
*/
public static final int FILLRIGHT = 2;
/**
* The fill pattern
*/
private int fill;
/**
* The fill char. Defaults to a space
*/
private char fillChar = ' ';
/**
* The seperator character. Defaults to ,
*/
private char delimiter = ',';
/**
* The row separator. Defaults to \n
*/
private String rowDelimiter = "\n";
/**
* Should we ignore the delimiter. Defaults to false
*/
private boolean ignoreDelimiter = false;
/**
* the value delimiter. Defaults to "
*/
private char valueDelimiter = '"';
/**
* Should we ignore the value delimiter. Defaults to true
*/
private boolean ignoreValueDelimiter = true;
/**
* Specifies if we want to use a field header
*/
private boolean fieldHeader = false;
/**
* Specifies if the end of the line needs to be trimmed
*/
private boolean endTrimmed = false;
/**
*
*/ */
public CSVConfig() { public CSVConfig() {
super(); super();
} }
/** /**
* @return if the CSV file is fixedWidth * @return if the CSV file is fixedWidth
*/ */
public boolean isFixedWidth() { public boolean isFixedWidth() {
return fixedWidth; return fixedWidth;
} }
/** /**
* 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) {
this.fixedWidth = fixedWidth; this.fixedWidth = fixedWidth;
} }
public void addField(CSVField field) { public void addField(CSVField field) {
if (fields == null) { if (fields == null) {
fields = new ArrayList(); fields = new ArrayList();
} }
fields.add(field); fields.add(field);
} }
/** /**
* 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) {
@ -103,9 +134,10 @@ public class CSVConfig {
} }
fields = new ArrayList(Arrays.asList(csvFields)); fields = new ArrayList(Arrays.asList(csvFields));
} }
/** /**
* 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) {
@ -125,12 +157,12 @@ public class CSVConfig {
} }
return csvFields; return csvFields;
} }
public CSVField getField(String name) { public CSVField getField(String name) {
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) {
@ -209,7 +244,8 @@ public class CSVConfig {
} }
/** /**
* Specify if the writer should ignore the delimiter. * Specify if the writer should ignore the delimiter.
*
* @param ignoreDelimiter defaults to false. * @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) {
@ -240,7 +277,8 @@ public class CSVConfig {
} }
/** /**
* Specify if the writer should ignore the value delimiter. * Specify if the writer should ignore the value delimiter.
*
* @param ignoreValueDelimiter defaults to false. * @param ignoreValueDelimiter defaults to false.
*/ */
public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) { public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) {
@ -253,16 +291,19 @@ 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) {
this.fieldHeader = fieldHeader; this.fieldHeader = fieldHeader;
} }
/** /**
* 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,8 +319,9 @@ 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. *
* @return the guessed config. * @param inputStream the inputstream.
* @return the guessed config.
*/ */
public static CSVConfig guessConfig(InputStream inputStream) { public static CSVConfig guessConfig(InputStream inputStream) {
return null; return null;
@ -294,11 +336,12 @@ 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) {
this.endTrimmed = endTrimmed; this.endTrimmed = endTrimmed;
} }
} }

View File

@ -30,23 +30,27 @@ 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 */
protected CSVConfig config;
/** /**
* * The found config
*/
protected CSVConfig config;
/**
*
*/ */
public CSVConfigGuesser() { public CSVConfigGuesser() {
this.config = new CSVConfig(); this.config = new CSVConfig();
} }
/** /**
* @param in the inputstream to guess from * @param in the inputstream to guess from
*/ */
@ -54,23 +58,24 @@ public class CSVConfigGuesser {
this(); this();
setInputStream(in); setInputStream(in);
} }
public void setInputStream(InputStream in) { public void setInputStream(InputStream in) {
this.in = in; this.in = in;
} }
/** /**
* Allow override. * Allow override.
*
* @return the inputstream that was set. * @return the inputstream that was set.
*/ */
protected InputStream getInputStream() { protected InputStream getInputStream() {
return in; return in;
} }
/** /**
* Guess the config based on the first 10 (or less when less available) * Guess the config based on the first 10 (or less when less available)
* records of a CSV file. * records of a CSV file.
* *
* @return the guessed config. * @return the guessed config.
*/ */
public CSVConfig guess() { public CSVConfig guess() {
@ -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.
} }
} }
@ -107,15 +112,16 @@ public class CSVConfigGuesser {
config = null; config = null;
return conf; return conf;
} }
protected void analyseLines(String[] lines) { protected void analyseLines(String[] lines) {
guessFixedWidth(lines); guessFixedWidth(lines);
guessFieldSeperator(lines); guessFieldSeperator(lines);
} }
/** /**
* 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) {
@ -132,7 +138,7 @@ public class CSVConfigGuesser {
} }
} }
} }
protected void guessFieldSeperator(String[] lines) { protected void guessFieldSeperator(String[] lines) {
if (config.isFixedWidth()) { if (config.isFixedWidth()) {
@ -142,7 +148,7 @@ public class CSVConfigGuesser {
for (int i = 0; i < lines.length; i++) { for (int i = 0; i < lines.length; i++) {
} }
} }
protected void guessFixedWidthSeperator(String[] lines) { protected void guessFixedWidthSeperator(String[] lines) {
// keep track of the fieldlength // keep track of the fieldlength
int previousMatch = -1; int previousMatch = -1;
@ -156,21 +162,21 @@ public class CSVConfigGuesser {
if (last != lines[j].charAt(i)) { if (last != lines[j].charAt(i)) {
charMatches = false; charMatches = false;
break; break;
} }
} }
if (charMatches) { if (charMatches) {
if (previousMatch == -1) { if (previousMatch == -1) {
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,11 +185,12 @@ 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) {
this.hasFieldHeader = hasFieldHeader; this.hasFieldHeader = hasFieldHeader;
} }
} }

View File

@ -20,7 +20,6 @@ package org.apache.commons.csv.writer;
/** /**
*
* @author Martin van den Bemt * @author Martin van den Bemt
* @version $Id: $ * @version $Id: $
*/ */
@ -32,7 +31,7 @@ public class CSVField {
private boolean overrideFill; private boolean overrideFill;
/** /**
* *
*/ */
public CSVField() { public CSVField() {
} }
@ -59,9 +58,10 @@ public class CSVField {
public String getName() { public String getName() {
return name; return name;
} }
/** /**
* 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,16 +94,17 @@ 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) {
overrideFill = true; overrideFill = true;
this.fill = fill; this.fill = fill;
} }
/** /**
* Does this field override fill ? * Does this field override fill ?
* *
* @return * @return
*/ */
public boolean overrideFill() { public boolean overrideFill() {

View File

@ -31,16 +31,21 @@ import java.util.Map;
*/ */
public class CSVWriter { public class CSVWriter {
/** The CSV config **/
private CSVConfig config;
/** The writer **/
private Writer writer;
/** /**
* * The CSV config *
*/
private CSVConfig config;
/**
* The writer *
*/
private Writer writer;
/**
*
*/ */
public CSVWriter() { public CSVWriter() {
} }
public CSVWriter(CSVConfig config) { public CSVWriter(CSVConfig config) {
setConfig(config); setConfig(config);
} }
@ -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,11 +78,11 @@ 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();
} }
} }
protected String writeValue(CSVField field, String value) throws Exception { protected String writeValue(CSVField field, String value) throws Exception {
if (config.isFixedWidth()) { if (config.isFixedWidth()) {
if (value.length() < field.getSize()) { if (value.length() < field.getSize()) {
@ -106,11 +111,11 @@ 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;
} }
/** /**
* @return the CVSConfig or null if not present * @return the CVSConfig or null if not present
*/ */
@ -120,14 +125,16 @@ 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) {
this.config = config; this.config = config;
} }
/** /**
* 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

View File

@ -30,200 +30,219 @@ import junit.framework.TestSuite;
* CSVPrinterTest * CSVPrinterTest
*/ */
public class CSVPrinterTest extends TestCase { public class CSVPrinterTest extends TestCase {
String lineSeparator = "\n";
public void testPrinter1() throws IOException { String lineSeparator = "\n";
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
String[] line1 = {"a", "b"};
printer.println(line1);
assertEquals("a,b" + lineSeparator, sw.toString());
}
public void testPrinter2() 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", "b"}; String[] line1 = {"a", "b"};
printer.println(line1); printer.println(line1);
assertEquals("\"a,b\",b" + lineSeparator, sw.toString()); assertEquals("a,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);
}
} }

View File

@ -24,68 +24,68 @@ import junit.framework.TestCase;
* CSVStrategyTest * CSVStrategyTest
* *
* The test are organized in three different sections: * The test are organized in three different sections:
* The 'setter/getter' section, the lexer section and finally the strategy * The 'setter/getter' section, the lexer section and finally the strategy
* section. In case a test fails, you should follow a top-down approach for * section. In case a test fails, you should follow a top-down approach for
* fixing a potential bug (its likely that the strategy itself fails if the lexer * fixing a potential bug (its likely that the strategy itself fails if the lexer
* has problems...). * has problems...).
*/ */
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() {
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
// default settings
assertEquals(strategy.getDelimiter(), ',');
assertEquals(strategy.getEncapsulator(), '"');
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
assertEquals(true, strategy.getIgnoreEmptyLines());
// explicit csv settings
assertEquals(strategy.getDelimiter(), ',');
assertEquals(strategy.getEncapsulator(), '"');
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
assertEquals(true, strategy.getIgnoreEmptyLines());
}
public void testSetExcelStrategy() {
CSVStrategy strategy = CSVStrategy.EXCEL_STRATEGY;
assertEquals(strategy.getDelimiter(), ',');
assertEquals(strategy.getEncapsulator(), '"');
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
assertEquals(false, strategy.getIgnoreLeadingWhitespaces());
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
assertEquals(false, strategy.getIgnoreEmptyLines());
}
public void testSetCSVStrategy() {
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
// default settings
assertEquals(strategy.getDelimiter(), ',');
assertEquals(strategy.getEncapsulator(), '"');
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
assertEquals(true, strategy.getIgnoreEmptyLines());
// explicit csv settings
assertEquals(strategy.getDelimiter(), ',');
assertEquals(strategy.getEncapsulator(), '"');
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
assertEquals(true, strategy.getIgnoreLeadingWhitespaces());
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
assertEquals(true, strategy.getIgnoreEmptyLines());
}
public void testSetExcelStrategy() {
CSVStrategy strategy = CSVStrategy.EXCEL_STRATEGY;
assertEquals(strategy.getDelimiter(), ',');
assertEquals(strategy.getEncapsulator(), '"');
assertEquals(strategy.getCommentStart(), CSVStrategy.COMMENTS_DISABLED);
assertEquals(false, strategy.getIgnoreLeadingWhitespaces());
assertEquals(false, strategy.getUnicodeEscapeInterpretation());
assertEquals(false, strategy.getIgnoreEmptyLines());
}
} }

View File

@ -24,127 +24,127 @@ import junit.framework.TestCase;
* CSVUtilsTest * CSVUtilsTest
*/ */
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);
} }
} }

View File

@ -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,14 +30,14 @@ 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
} }
cb = new CharBuffer(128); cb = new CharBuffer(128);
assertEquals(0, cb.length()); assertEquals(0, cb.length());
} }
public void testAppendChar() { public void testAppendChar() {
CharBuffer cb = new CharBuffer(1); CharBuffer cb = new CharBuffer(1);
String expected = ""; String expected = "";
@ -49,59 +48,59 @@ public class CharBufferTest extends TestCase {
assertEquals(expected.length(), cb.length()); assertEquals(expected.length(), cb.length());
} }
} }
public void testAppendCharArray() { public void testAppendCharArray() {
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());
} }
} }
public void testAppendString() { public void testAppendString() {
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());
} }
} }
public void testAppendStringBuffer() { public void testAppendStringBuffer() {
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());
} }
} }
public void testAppendCharBuffer() { public void testAppendCharBuffer() {
CharBuffer cb = new CharBuffer(1); CharBuffer cb = new CharBuffer(1);
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());
} }
} }
public void testShrink() { public void testShrink() {
String data = "123456789012345678901234567890"; String data = "123456789012345678901234567890";
CharBuffer cb = new CharBuffer(data.length() + 100); CharBuffer cb = new CharBuffer(data.length() + 100);
assertEquals(data.length() + 100, cb.capacity()); assertEquals(data.length() + 100, cb.capacity());
cb.append(data); cb.append(data);
@ -112,24 +111,24 @@ public class CharBufferTest extends TestCase {
assertEquals(data.length(), cb.length()); assertEquals(data.length(), cb.length());
assertEquals(data, cb.toString()); assertEquals(data, cb.toString());
} }
//-- the following test cases have been adapted from the HttpComponents project //-- the following test cases have been adapted from the HttpComponents project
//-- written by Oleg Kalnichevski //-- written by Oleg Kalnichevski
public void testSimpleAppend() throws Exception { public void testSimpleAppend() throws Exception {
CharBuffer buffer = new CharBuffer(16); CharBuffer buffer = new CharBuffer(16);
assertEquals(16, buffer.capacity()); assertEquals(16, buffer.capacity());
assertEquals(0, buffer.length()); assertEquals(0, buffer.length());
char[] b1 = buffer.getCharacters(); char[] b1 = buffer.getCharacters();
assertNotNull(b1); assertNotNull(b1);
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());
char[] b2 = buffer.getCharacters(); char[] b2 = buffer.getCharacters();
assertNotNull(b2); assertNotNull(b2);
assertEquals(4, b2.length); assertEquals(4, b2.length);
@ -137,35 +136,35 @@ public class CharBufferTest extends TestCase {
assertEquals(tmp[i], b2[i]); assertEquals(tmp[i], b2[i]);
} }
assertEquals("1234", buffer.toString()); assertEquals("1234", buffer.toString());
buffer.clear(); buffer.clear();
assertEquals(16, buffer.capacity()); assertEquals(16, buffer.capacity());
assertEquals(0, buffer.length()); assertEquals(0, buffer.length());
} }
public void testAppendString2() throws Exception { public void testAppendString2() throws Exception {
CharBuffer buffer = new CharBuffer(8); CharBuffer buffer = new CharBuffer(8);
buffer.append("stuff"); buffer.append("stuff");
buffer.append(" and more stuff"); buffer.append(" and more stuff");
assertEquals("stuff and more stuff", buffer.toString()); assertEquals("stuff and more stuff", buffer.toString());
} }
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());
buffer.append((String)null);
assertEquals("", buffer.toString()); assertEquals("", buffer.toString());
buffer.append((CharBuffer)null); buffer.append((String) null);
assertEquals("", buffer.toString()); assertEquals("", buffer.toString());
buffer.append((char[])null); buffer.append((CharBuffer) null);
assertEquals("", buffer.toString());
buffer.append((char[]) null);
assertEquals("", buffer.toString()); assertEquals("", buffer.toString());
} }
public void testAppendCharArrayBuffer() throws Exception { public void testAppendCharArrayBuffer() throws Exception {
CharBuffer buffer1 = new CharBuffer(8); CharBuffer buffer1 = new CharBuffer(8);
buffer1.append(" and more stuff"); buffer1.append(" and more stuff");
@ -174,7 +173,7 @@ public class CharBufferTest extends TestCase {
buffer2.append(buffer1); buffer2.append(buffer1);
assertEquals("stuff and more stuff", buffer2.toString()); assertEquals("stuff and more stuff", buffer2.toString());
} }
public void testAppendSingleChar() throws Exception { public void testAppendSingleChar() throws Exception {
CharBuffer buffer = new CharBuffer(4); CharBuffer buffer = new CharBuffer(4);
buffer.append('1'); buffer.append('1');
@ -185,7 +184,7 @@ public class CharBufferTest extends TestCase {
buffer.append('6'); buffer.append('6');
assertEquals("123456", buffer.toString()); assertEquals("123456", buffer.toString());
} }
public void testProvideCapacity() throws Exception { public void testProvideCapacity() throws Exception {
CharBuffer buffer = new CharBuffer(4); CharBuffer buffer = new CharBuffer(4);
buffer.provideCapacity(2); buffer.provideCapacity(2);

View File

@ -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() {
ExtendedBufferedReader br = new ExtendedBufferedReader(new StringReader(""));
br = new ExtendedBufferedReader(new StringReader(""), 10);
}
public void testReadLookahead1() throws Exception {
assertEquals(ExtendedBufferedReader.END_OF_STREAM, getEBR("").read());
ExtendedBufferedReader br = getEBR("1\n2\r3\n");
assertEquals('1', br.lookAhead());
assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
assertEquals('1', br.read());
assertEquals('1', br.readAgain());
assertEquals(0, br.getLineNumber()); public void testConstructors() {
assertEquals('\n', br.lookAhead()); ExtendedBufferedReader br = new ExtendedBufferedReader(new StringReader(""));
assertEquals(0, br.getLineNumber()); br = new ExtendedBufferedReader(new StringReader(""), 10);
assertEquals('1', br.readAgain()); }
assertEquals('\n', br.read());
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber());
assertEquals('2', br.lookAhead());
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber());
assertEquals('2', br.read());
assertEquals('2', br.readAgain());
assertEquals('\r', br.lookAhead());
assertEquals('2', br.readAgain());
assertEquals('\r', br.read());
assertEquals('\r', br.readAgain());
assertEquals('3', br.lookAhead());
assertEquals('\r', br.readAgain());
assertEquals('3', br.read());
assertEquals('3', br.readAgain());
assertEquals('\n', br.lookAhead());
assertEquals(1, br.getLineNumber());
assertEquals('3', br.readAgain());
assertEquals('\n', br.read());
assertEquals(2, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals(2, br.getLineNumber());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
assertEquals('\n', br.readAgain());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
}
public void testReadLookahead2() throws Exception { public void testReadLookahead1() throws Exception {
char[] ref = new char[5];
char[] res = new char[5]; assertEquals(ExtendedBufferedReader.END_OF_STREAM, getEBR("").read());
ExtendedBufferedReader br = getEBR("1\n2\r3\n");
ExtendedBufferedReader br = getEBR(""); assertEquals('1', br.lookAhead());
assertEquals(0, br.read(res, 0, 0)); assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
assertTrue(Arrays.equals(res, ref)); assertEquals('1', br.read());
assertEquals('1', br.readAgain());
br = getEBR("abcdefg");
ref[0] = 'a'; assertEquals(0, br.getLineNumber());
ref[1] = 'b'; assertEquals('\n', br.lookAhead());
ref[2] = 'c'; assertEquals(0, br.getLineNumber());
assertEquals(3, br.read(res, 0, 3)); assertEquals('1', br.readAgain());
assertTrue(Arrays.equals(res, ref)); assertEquals('\n', br.read());
assertEquals('c', br.readAgain()); assertEquals(1, br.getLineNumber());
assertEquals('\n', br.readAgain());
assertEquals('d', br.lookAhead()); assertEquals(1, br.getLineNumber());
ref[4] = 'd';
assertEquals(1, br.read(res, 4, 1)); assertEquals('2', br.lookAhead());
assertTrue(Arrays.equals(res, ref)); assertEquals(1, br.getLineNumber());
assertEquals('d', br.readAgain()); assertEquals('\n', br.readAgain());
assertEquals(1, br.getLineNumber());
} assertEquals('2', br.read());
assertEquals('2', br.readAgain());
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getEBR(""); assertEquals('\r', br.lookAhead());
assertTrue(br.readLine() == null); assertEquals('2', br.readAgain());
assertEquals('\r', br.read());
br = getEBR("\n"); assertEquals('\r', br.readAgain());
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null); assertEquals('3', br.lookAhead());
assertEquals('\r', br.readAgain());
br = getEBR("foo\n\nhello"); assertEquals('3', br.read());
assertEquals(0, br.getLineNumber()); assertEquals('3', br.readAgain());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber()); assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals("")); assertEquals(1, br.getLineNumber());
assertEquals(2, br.getLineNumber()); assertEquals('3', br.readAgain());
assertTrue(br.readLine().equals("hello")); assertEquals('\n', br.read());
assertEquals(3, br.getLineNumber()); assertEquals(2, br.getLineNumber());
assertTrue(br.readLine() == null); assertEquals('\n', br.readAgain());
assertEquals(3, br.getLineNumber()); assertEquals(2, br.getLineNumber());
br = getEBR("foo\n\nhello"); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
assertEquals('f', br.read()); assertEquals('\n', br.readAgain());
assertEquals('o', br.lookAhead()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertTrue(br.readLine().equals("oo")); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
assertEquals(1, br.getLineNumber()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
assertEquals('\n', br.lookAhead()); assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber()); }
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null); public void testReadLookahead2() throws Exception {
assertEquals(3, br.getLineNumber()); char[] ref = new char[5];
char[] res = new char[5];
br = getEBR("foo\rbaar\r\nfoo"); ExtendedBufferedReader br = getEBR("");
assertTrue(br.readLine().equals("foo")); assertEquals(0, br.read(res, 0, 0));
assertEquals('b', br.lookAhead()); assertTrue(Arrays.equals(res, ref));
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead()); br = getEBR("abcdefg");
assertTrue(br.readLine().equals("foo")); ref[0] = 'a';
assertTrue(br.readLine() == null); ref[1] = 'b';
} ref[2] = 'c';
assertEquals(3, br.read(res, 0, 3));
private ExtendedBufferedReader getEBR(String s) { assertTrue(Arrays.equals(res, ref));
return new ExtendedBufferedReader(new StringReader(s)); assertEquals('c', br.readAgain());
}
assertEquals('d', br.lookAhead());
ref[4] = 'd';
assertEquals(1, br.read(res, 4, 1));
assertTrue(Arrays.equals(res, ref));
assertEquals('d', br.readAgain());
}
public void testReadLine() throws Exception {
ExtendedBufferedReader br = getEBR("");
assertTrue(br.readLine() == null);
br = getEBR("\n");
assertTrue(br.readLine().equals(""));
assertTrue(br.readLine() == null);
br = getEBR("foo\n\nhello");
assertEquals(0, br.getLineNumber());
assertTrue(br.readLine().equals("foo"));
assertEquals(1, br.getLineNumber());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertTrue(br.readLine().equals("hello"));
assertEquals(3, br.getLineNumber());
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\n\nhello");
assertEquals('f', br.read());
assertEquals('o', br.lookAhead());
assertTrue(br.readLine().equals("oo"));
assertEquals(1, br.getLineNumber());
assertEquals('\n', br.lookAhead());
assertTrue(br.readLine().equals(""));
assertEquals(2, br.getLineNumber());
assertEquals('h', br.lookAhead());
assertTrue(br.readLine().equals("hello"));
assertTrue(br.readLine() == null);
assertEquals(3, br.getLineNumber());
br = getEBR("foo\rbaar\r\nfoo");
assertTrue(br.readLine().equals("foo"));
assertEquals('b', br.lookAhead());
assertTrue(br.readLine().equals("baar"));
assertEquals('f', br.lookAhead());
assertTrue(br.readLine().equals("foo"));
assertTrue(br.readLine() == null);
}
private ExtendedBufferedReader getEBR(String s) {
return new ExtendedBufferedReader(new StringReader(s));
}
} }

View File

@ -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();

View File

@ -29,7 +29,7 @@ import junit.framework.TestCase;
* @version $Id: $ * @version $Id: $
*/ */
public class CSVConfigTest extends TestCase { public class CSVConfigTest extends TestCase {
public void testFixedWith() { public void testFixedWith() {
CSVConfig config = new CSVConfig(); CSVConfig config = new CSVConfig();
@ -37,13 +37,13 @@ public class CSVConfigTest extends TestCase {
config.setFixedWidth(true); config.setFixedWidth(true);
assertEquals(true, config.isFixedWidth()); assertEquals(true, config.isFixedWidth());
} }
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");
@ -53,7 +53,7 @@ public class CSVConfigTest extends TestCase {
assertEquals(null, config.getField("field11")); assertEquals(null, config.getField("field11"));
assertEquals(field, config.getField("field1")); assertEquals(field, config.getField("field1"));
} }
public void testFill() { public void testFill() {
CSVConfig config = new CSVConfig(); CSVConfig config = new CSVConfig();
assertEquals(CSVConfig.FILLNONE, config.getFill()); assertEquals(CSVConfig.FILLNONE, config.getFill());
@ -65,7 +65,7 @@ public class CSVConfigTest extends TestCase {
config.setFillChar('m'); config.setFillChar('m');
assertEquals('m', config.getFillChar()); assertEquals('m', config.getFillChar());
} }
public void testDelimiter() { public void testDelimiter() {
CSVConfig config = new CSVConfig(); CSVConfig config = new CSVConfig();
assertEquals(',', config.getDelimiter()); assertEquals(',', config.getDelimiter());
@ -75,7 +75,7 @@ public class CSVConfigTest extends TestCase {
config.setIgnoreDelimiter(true); config.setIgnoreDelimiter(true);
assertEquals(true, config.isDelimiterIgnored()); assertEquals(true, config.isDelimiterIgnored());
} }
public void testValueDelimiter() { public void testValueDelimiter() {
CSVConfig config = new CSVConfig(); CSVConfig config = new CSVConfig();
assertEquals('"', config.getValueDelimiter()); assertEquals('"', config.getValueDelimiter());
@ -85,14 +85,14 @@ public class CSVConfigTest extends TestCase {
config.setIgnoreValueDelimiter(false); config.setIgnoreValueDelimiter(false);
assertEquals(false, config.isValueDelimiterIgnored()); assertEquals(false, config.isValueDelimiterIgnored());
} }
public void testFieldHeader() { public void testFieldHeader() {
CSVConfig config = new CSVConfig(); CSVConfig config = new CSVConfig();
assertEquals(false, config.isFieldHeader()); assertEquals(false, config.isFieldHeader());
config.setFieldHeader(true); config.setFieldHeader(true);
assertEquals(true, config.isFieldHeader()); assertEquals(true, config.isFieldHeader());
} }
public void testTrimEnd() { public void testTrimEnd() {
CSVConfig config = new CSVConfig(); CSVConfig config = new CSVConfig();
assertEquals(false, config.isEndTrimmed()); assertEquals(false, config.isEndTrimmed());

View File

@ -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: $
*/ */
@ -41,7 +40,7 @@ public class CSVFieldTest extends TestCase {
assertEquals("name", field.getName()); assertEquals("name", field.getName());
assertEquals(10, field.getSize()); assertEquals(10, field.getSize());
} }
public void testFill() { public void testFill() {
CSVField field = new CSVField(); CSVField field = new CSVField();
assertEquals(CSVConfig.FILLNONE, field.getFill()); assertEquals(CSVConfig.FILLNONE, field.getFill());

View File

@ -26,17 +26,17 @@ import junit.framework.TestCase;
/** /**
* The testcase for the csv writer. * The testcase for the csv writer.
* *
* @author Martin van den Bemt * @author Martin van den Bemt
* @version $Id: $ * @version $Id: $
*/ */
public class CSVWriterTest extends TestCase { public class CSVWriterTest extends TestCase {
private Map map; private Map map;
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
map = new HashMap(); map = new HashMap();
map.put("field1", "12345"); map.put("field1", "12345");
map.put("field2", "1234"); map.put("field2", "1234");