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

View File

@ -51,20 +51,32 @@ import java.util.ArrayList;
*/
public class CSVParser {
/** length of the initial token (content-)buffer */
/**
* length of the initial token (content-)buffer
*/
private static final int INITIAL_TOKEN_LENGTH = 50;
// the token types
/** Token has no valid content, i.e. is in its initialized state. */
/**
* Token has no valid content, i.e. is in its initialized state.
*/
protected static final int TT_INVALID = -1;
/** Token with content, at beginning or in the middle of a line. */
/**
* Token with content, at beginning or in the middle of a line.
*/
protected static final int TT_TOKEN = 0;
/** Token (which can have content) when end of file is reached. */
/**
* Token (which can have content) when end of file is reached.
*/
protected static final int TT_EOF = 1;
/** Token with content when end of a line is reached. */
/**
* Token with content when end of a line is reached.
*/
protected static final int TT_EORECORD = 2;
/** Immutable empty String array. */
/**
* Immutable empty String array.
*/
private static final String[] EMPTY_STRING_ARRAY = new String[0];
// the input stream
@ -73,7 +85,9 @@ public class CSVParser {
private final CSVStrategy strategy;
// the following objects are shared to reduce garbage
/** A record buffer for getLine(). Grows as necessary and is reused. */
/**
* A record buffer for getLine(). Grows as necessary and is reused.
*/
private final ArrayList record = new ArrayList();
private final Token reusableToken = new Token();
private final CharBuffer wsBuf = new CharBuffer();
@ -82,15 +96,21 @@ public class CSVParser {
/**
* Token is an internal token representation.
*
* <p/>
* It is used as contract between the lexer and the parser.
*/
static class Token {
/** Token type, see TT_xxx constants. */
/**
* Token type, see TT_xxx constants.
*/
int type = TT_INVALID;
/** The content buffer. */
/**
* The content buffer.
*/
CharBuffer content = new CharBuffer(INITIAL_TOKEN_LENGTH);
/** Token ready flag: indicates a valid token with content (ready for the parser). */
/**
* Token ready flag: indicates a valid token with content (ready for the parser).
*/
boolean isReady;
Token reset() {
@ -121,18 +141,18 @@ public class CSVParser {
* @param input a Reader containing "csv-formatted" input
*/
public CSVParser(Reader input) {
this(input, (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone());
this(input, (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone());
}
/**
* Customized value delimiter parser.
*
* <p/>
* The parser follows the default {@link CSVStrategy}
* except for the delimiter setting.
*
* @param input a Reader based on "csv-formatted" input
* @param delimiter a Char used for value separation
* @deprecated use {@link #CSVParser(Reader,CSVStrategy)}.
* @deprecated use {@link #CSVParser(Reader, CSVStrategy)}.
*/
public CSVParser(Reader input, char delimiter) {
this(input, delimiter, '"', CSVStrategy.COMMENTS_DISABLED);
@ -140,7 +160,7 @@ public class CSVParser {
/**
* Customized csv parser.
*
* <p/>
* The parser parses according to the given CSV dialect settings.
* Leading whitespaces are truncated, unicode escapes are
* not interpreted and empty lines are ignored.
@ -149,7 +169,7 @@ public class CSVParser {
* @param delimiter a Char used for value separation
* @param encapsulator a Char used as value encapsulation marker
* @param commentStart a Char used for comment identification
* @deprecated use {@link #CSVParser(Reader,CSVStrategy)}.
* @deprecated use {@link #CSVParser(Reader, CSVStrategy)}.
*/
public CSVParser(Reader input, char delimiter, char encapsulator, char commentStart) {
this(input, new CSVStrategy(delimiter, encapsulator, commentStart));
@ -174,7 +194,7 @@ public class CSVParser {
* Parses the CSV according to the given strategy
* and returns the content as an array of records
* (whereas records are arrays of single values).
* <p>
* <p/>
* The returned content starts at the current parse-position in
* the stream.
*
@ -270,7 +290,7 @@ public class CSVParser {
/**
* Returns the current line number in the input stream.
*
* <p/>
* ATTENTION: in case your csv has multiline-values the returned
* number does not correspond to the record-number
*
@ -293,7 +313,7 @@ public class CSVParser {
/**
* Returns the next token.
*
* <p/>
* A token corresponds to a term, a record change or an
* end-of-file indicator.
*
@ -386,7 +406,7 @@ public class CSVParser {
/**
* A simple token lexer
*
* <p/>
* Simple token are tokens which are not surrounded by encapsulators.
* A simple token might contain escaped delimiters (as \, or \;). The
* token is finished when one of the following conditions become true:
@ -399,11 +419,10 @@ public class CSVParser {
* @param tkn the current token
* @param c the current character
* @return the filled token
*
* @throws IOException on stream access error
*/
private Token simpleTokenLexer(Token tkn, int c) throws IOException {
for (;;) {
for (; ;) {
if (isEndOfLine(c)) {
// end of record
tkn.type = TT_EORECORD;
@ -423,7 +442,7 @@ public class CSVParser {
// interpret unicode escaped chars (like \u0070 -> p)
tkn.content.append((char) unicodeEscapeLexer(c));
} else if (c == strategy.getEscape()) {
tkn.content.append((char)readEscape(c));
tkn.content.append((char) readEscape(c));
} else {
tkn.content.append((char) c);
}
@ -441,7 +460,7 @@ public class CSVParser {
/**
* An encapsulated token lexer
*
* <p/>
* Encapsulated tokens are surrounded by the given encapsulating-string.
* The encapsulator itself might be included in the token using a
* doubling syntax (as "", '') or using escaping (as in \", \').
@ -457,13 +476,13 @@ public class CSVParser {
int startLineNumber = getLineNumber();
// ignore the given delimiter
// assert c == delimiter;
for (;;) {
for (; ;) {
c = in.read();
if (c == '\\' && strategy.getUnicodeEscapeInterpretation() && in.lookAhead()=='u') {
if (c == '\\' && strategy.getUnicodeEscapeInterpretation() && in.lookAhead() == 'u') {
tkn.content.append((char) unicodeEscapeLexer(c));
} else if (c == strategy.getEscape()) {
tkn.content.append((char)readEscape(c));
tkn.content.append((char) readEscape(c));
} else if (c == strategy.getEncapsulator()) {
if (in.lookAhead() == strategy.getEncapsulator()) {
// double or escaped encapsulator -> add single encapsulator to token
@ -471,7 +490,7 @@ public class CSVParser {
tkn.content.append((char) c);
} else {
// token finish mark (encapsulator) reached: ignore whitespace till delimiter
for (;;) {
for (; ;) {
c = in.read();
if (c == strategy.getDelimiter()) {
tkn.type = TT_TOKEN;
@ -511,9 +530,10 @@ public class CSVParser {
/**
* Decodes Unicode escapes.
*
* <p/>
* Interpretation of "\\uXXXX" escape sequences
* where XXXX is a hex-number.
*
* @param c current char which is discarded because it's the "\\" of "\\uXXXX"
* @return the decoded character
* @throws IOException on wrong unicode escape sequence or read error
@ -545,12 +565,23 @@ public class CSVParser {
c = in.read();
int out;
switch (c) {
case 'r': out='\r'; break;
case 'n': out='\n'; break;
case 't': out='\t'; break;
case 'b': out='\b'; break;
case 'f': out='\f'; break;
default : out=c;
case 'r':
out = '\r';
break;
case 'n':
out = '\n';
break;
case 't':
out = '\t';
break;
case 'b':
out = '\b';
break;
case 'f':
out = '\f';
break;
default:
out = c;
}
return out;
}

View File

@ -26,11 +26,15 @@ import java.io.Writer;
*/
public class CSVPrinter {
/** The place that the values get written. */
/**
* The place that the values get written.
*/
protected final Writer out;
protected final CSVStrategy strategy;
/** True if we just began a new line. */
/**
* True if we just began a new line.
*/
protected boolean newLine = true;
protected char[] buf = new char[0]; // temporary buffer
@ -38,7 +42,7 @@ public class CSVPrinter {
/**
* Create a printer that will print values to the given
* stream following the CSVStrategy.
*
* <p/>
* Currently, only a pure encapsulation strategy or a pure escaping strategy
* is supported. Hybrid strategies (encapsulation and escaping with a different character) are not supported.
*
@ -47,7 +51,7 @@ public class CSVPrinter {
*/
public CSVPrinter(Writer out, CSVStrategy strategy) {
this.out = out;
this.strategy = strategy==null ? CSVStrategy.DEFAULT_STRATEGY : strategy;
this.strategy = strategy == null ? CSVStrategy.DEFAULT_STRATEGY : strategy;
}
// ======================================================
@ -92,7 +96,7 @@ public class CSVPrinter {
* @param comment the comment to output
*/
public void printlnComment(String comment) throws IOException {
if(this.strategy.isCommentingDisabled()) {
if (this.strategy.isCommentingDisabled()) {
return;
}
if (!newLine) {
@ -103,17 +107,17 @@ public class CSVPrinter {
for (int i = 0; i < comment.length(); i++) {
char c = comment.charAt(i);
switch (c) {
case '\r' :
case '\r':
if (i + 1 < comment.length() && comment.charAt(i + 1) == '\n') {
i++;
}
// break intentionally excluded.
case '\n' :
case '\n':
println();
out.write(this.strategy.getCommentStart());
out.write(' ');
break;
default :
default:
out.write(c);
break;
}
@ -159,27 +163,30 @@ public class CSVPrinter {
while (pos < end) {
char c = value[pos];
if (c == '\r' || c=='\n' || c==delim || c==escape) {
if (c == '\r' || c == '\n' || c == delim || c == escape) {
// write out segment up until this char
int l = pos-start;
if (l>0) {
int l = pos - start;
if (l > 0) {
out.write(value, start, l);
}
if (c=='\n') c='n';
else if (c=='\r') c='r';
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
start = pos + 1; // start on the current char after this one
}
pos++;
}
// write last segment
int l = pos-start;
if (l>0) {
int l = pos - start;
if (l > 0) {
out.write(value, start, l);
}
}
@ -223,7 +230,7 @@ public class CSVPrinter {
} else {
while (pos < end) {
c = value[pos];
if (c=='\n' || c=='\r' || c==encapsulator || c==delim) {
if (c == '\n' || c == '\r' || c == encapsulator || c == delim) {
quote = true;
break;
}
@ -231,7 +238,7 @@ public class CSVPrinter {
}
if (!quote) {
pos = end-1;
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
@ -254,13 +261,13 @@ public class CSVPrinter {
// Pick up where we left off: pos should be positioned on the first character that caused
// the need for encapsulation.
while (pos<end) {
while (pos < end) {
char c = value[pos];
if (c==encapsulator) {
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);
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;
@ -269,7 +276,7 @@ public class CSVPrinter {
}
// write the last segment
out.write(value, start, pos-start);
out.write(value, start, pos - start);
out.write(encapsulator);
}

View File

@ -41,9 +41,9 @@ public class CSVStrategy implements Cloneable, Serializable {
// an EOF signal (-1), and because \ufffe in UTF-16 would be
// encoded as two chars (using surrogates) and thus there should never
// be a collision with a real text char.
public static char COMMENTS_DISABLED = (char)-2;
public static char ESCAPE_DISABLED = (char)-2;
public static char ENCAPSULATOR_DISABLED = (char)-2;
public static char COMMENTS_DISABLED = (char) -2;
public static char ESCAPE_DISABLED = (char) -2;
public static char ENCAPSULATOR_DISABLED = (char) -2;
public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
true, false, true);
@ -80,8 +80,7 @@ public class CSVStrategy implements Cloneable, Serializable {
boolean ignoreLeadingWhitespace,
boolean ignoreTrailingWhitespace,
boolean interpretUnicodeEscapes,
boolean ignoreEmptyLines)
{
boolean ignoreEmptyLines) {
setDelimiter(delimiter);
setEncapsulator(encapsulator);
setCommentStart(commentStart);
@ -92,53 +91,92 @@ public class CSVStrategy implements Cloneable, Serializable {
setIgnoreEmptyLines(ignoreEmptyLines);
}
/** @deprecated */
/**
* @deprecated
*/
public CSVStrategy(
char delimiter,
char encapsulator,
char commentStart,
boolean ignoreLeadingWhitespace,
boolean interpretUnicodeEscapes,
boolean ignoreEmptyLines)
{
boolean ignoreEmptyLines) {
this(delimiter, encapsulator, commentStart, CSVStrategy.ESCAPE_DISABLED, ignoreLeadingWhitespace,
true, interpretUnicodeEscapes, ignoreEmptyLines);
}
public void setDelimiter(char delimiter) { this.delimiter = delimiter; }
public char getDelimiter() { return this.delimiter; }
public void setDelimiter(char delimiter) {
this.delimiter = delimiter;
}
public void setEncapsulator(char encapsulator) { this.encapsulator = encapsulator; }
public char getEncapsulator() { return this.encapsulator; }
public char getDelimiter() {
return this.delimiter;
}
public void setCommentStart(char commentStart) { this.commentStart = commentStart; }
public char getCommentStart() { return this.commentStart; }
public boolean isCommentingDisabled() { return this.commentStart == COMMENTS_DISABLED; }
public void setEncapsulator(char encapsulator) {
this.encapsulator = encapsulator;
}
public void setEscape(char escape) { this.escape = escape; }
public char getEscape() { return this.escape; }
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 boolean getIgnoreLeadingWhitespaces() {
return this.ignoreLeadingWhitespaces;
}
public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {
this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;
}
public boolean getIgnoreTrailingWhitespaces() { return this.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 boolean getUnicodeEscapeInterpretation() {
return this.interpretUnicodeEscapes;
}
public void setIgnoreEmptyLines(boolean ignoreEmptyLines) {
this.ignoreEmptyLines = ignoreEmptyLines;
}
public boolean getIgnoreEmptyLines() {
return this.ignoreEmptyLines;
}
public void setPrinterNewline(String newline) {
this.printerNewline = newline;
}
public String getPrinterNewline() {
return this.printerNewline;
}

View File

@ -64,6 +64,7 @@ public class CharBuffer {
/**
* Returns the number of characters in the buffer.
*
* @return the number of characters
*/
public int length() {
@ -72,6 +73,7 @@ public class CharBuffer {
/**
* Returns the current capacity of the buffer.
*
* @return the maximum number of characters that can be stored in this buffer without
* resizing it.
*/
@ -82,6 +84,7 @@ public class CharBuffer {
/**
* Appends the contents of <code>cb</code> to the end of this CharBuffer.
*
* @param cb the CharBuffer to append or null
*/
public void append(final CharBuffer cb) {
@ -96,6 +99,7 @@ public class CharBuffer {
/**
* Appends <code>s</code> to the end of this CharBuffer.
* This method involves copying the new data once!
*
* @param s the String to append or null
*/
public void append(final String s) {
@ -108,6 +112,7 @@ public class CharBuffer {
/**
* Appends <code>sb</code> to the end of this CharBuffer.
* This method involves copying the new data once!
*
* @param sb the StringBuffer to append or null
*/
public void append(final StringBuffer sb) {
@ -122,6 +127,7 @@ public class CharBuffer {
/**
* Appends <code>data</code> to the end of this CharBuffer.
* This method involves copying the new data once!
*
* @param data the char[] to append or null
*/
public void append(final char[] data) {
@ -136,6 +142,7 @@ public class CharBuffer {
/**
* Appends a single character to the end of this CharBuffer.
* This method involves copying the new data once!
*
* @param data the char to append
*/
public void append(final char data) {
@ -161,7 +168,7 @@ public class CharBuffer {
* Removes trailing whitespace.
*/
public void trimTrailingWhitespace() {
while (length>0 && Character.isWhitespace(c[length-1])) {
while (length > 0 && Character.isWhitespace(c[length - 1])) {
length--;
}
}
@ -172,6 +179,7 @@ public class CharBuffer {
* modifying it.
* This method allows to avoid copying if the caller knows the exact capacity
* before.
*
* @return
*/
public char[] getCharacters() {
@ -193,6 +201,7 @@ public class CharBuffer {
/**
* Converts the contents of the buffer into a StringBuffer.
* This method involves copying the new data once!
*
* @return
*/
public StringBuffer toStringBuffer() {
@ -204,6 +213,7 @@ public class CharBuffer {
/**
* Converts the contents of the buffer into a StringBuffer.
* This method involves copying the new data once!
*
* @return
*/
public String toString() {
@ -212,13 +222,14 @@ public class CharBuffer {
/**
* Copies the data into a new array of at least <code>capacity</code> size.
*
* @param capacity
*/
public void provideCapacity(final int capacity) {
if (c.length >= capacity) {
return;
}
int newcapacity = ((capacity*3)>>1) + 1;
int newcapacity = ((capacity * 3) >> 1) + 1;
char[] newc = new char[newcapacity];
System.arraycopy(c, 0, newc, 0, length);
c = newc;

View File

@ -23,33 +23,41 @@ import java.io.Reader;
/**
* ExtendedBufferedReader
*
* A special reader decorater which supports more
* A special reader decorator which supports more
* sophisticated access to the underlying reader object.
*
* In particular the reader supports a look-ahead option,
* which allows you to see the next char returned by
* next().
*
*/
class ExtendedBufferedReader extends BufferedReader {
/** the end of stream symbol */
/**
* the end of stream symbol
*/
public static final int END_OF_STREAM = -1;
/** undefined state for the lookahead char */
/**
* undefined state for the lookahead char
*/
public static final int UNDEFINED = -2;
/** the lookahead chars */
/**
* the lookahead chars
*/
private int lookaheadChar = UNDEFINED;
/** the last char returned */
/**
* the last char returned
*/
private int lastChar = UNDEFINED;
/** the line counter */
/**
* 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);
@ -70,10 +78,11 @@ class ExtendedBufferedReader extends BufferedReader {
/**
* Reads the next char from the input stream.
*
* @return the next char or END_OF_STREAM if end of stream has been reached.
*/
public int read() throws IOException {
// initalize the lookahead
// initialize the lookahead
if (lookaheadChar == UNDEFINED) {
lookaheadChar = super.read();
}
@ -101,8 +110,8 @@ class ExtendedBufferedReader extends BufferedReader {
/**
* Non-blocking reading of len chars into buffer buf starting
* at bufferposition off.
*
* performs an iteratative read on the underlying stream
* <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

View File

@ -32,36 +32,65 @@ import java.util.List;
*/
public class CSVConfig {
/** specifies if it is a fixed width csv file **/
/**
* specifies if it is a fixed width csv file *
*/
private boolean fixedWidth;
/** list of fields **/
/**
* list of fields *
*/
private List fields;
/** Do no do any filling **/
/**
* Do no do any filling *
*/
public static final int FILLNONE = 0;
/** Fill content the the left. Mainly usable together with fixedWidth **/
/**
* Fill content the the left. Mainly usable together with fixedWidth *
*/
public static final int FILLLEFT = 1;
/** Fill content to the right. Mainly usable together with fixedWidth **/
/**
* Fill content to the right. Mainly usable together with fixedWidth *
*/
public static final int FILLRIGHT = 2;
/** The fill pattern */
/**
* The fill pattern
*/
private int fill;
/** The fill char. Defaults to a space */
/**
* The fill char. Defaults to a space
*/
private char fillChar = ' ';
/** The seperator character. Defaults to , */
/**
* The seperator character. Defaults to ,
*/
private char delimiter = ',';
/** The row separator. Defaults to \n */
/**
* The row separator. Defaults to \n
*/
private String rowDelimiter = "\n";
/** Should we ignore the delimiter. Defaults to false */
/**
* Should we ignore the delimiter. Defaults to false
*/
private boolean ignoreDelimiter = false;
/** the value delimiter. Defaults to " */
/**
* the value delimiter. Defaults to "
*/
private char valueDelimiter = '"';
/** Should we ignore the value delimiter. Defaults to true */
/**
* Should we ignore the value delimiter. Defaults to true
*/
private boolean ignoreValueDelimiter = true;
/** Specifies if we want to use a field header */
/**
* Specifies if we want to use a field header
*/
private boolean fieldHeader = false;
/** Specifies if the end of the line needs to be trimmed */
/**
* Specifies if the end of the line needs to be trimmed
*/
private boolean endTrimmed = false;
/**
*
*/
@ -79,6 +108,7 @@ public class CSVConfig {
/**
* Specify if the CSV file is fixed width.
* Defaults to false
*
* @param fixedWidth the fixedwidth
*/
public void setFixedWidth(boolean fixedWidth) {
@ -95,6 +125,7 @@ public class CSVConfig {
/**
* Set the fields that should be used by the writer.
* This will overwrite currently added fields completely!
*
* @param csvFields the csvfields array. If null it will do nothing
*/
public void setFields(CSVField[] csvFields) {
@ -106,6 +137,7 @@ public class CSVConfig {
/**
* Set the fields that should be used by the writer
*
* @param csvField a collection with fields. If null it will do nothing
*/
public void setFields(Collection csvField) {
@ -130,7 +162,7 @@ public class CSVConfig {
if (fields == null || name == null) {
return null;
}
for(int i = 0; i < fields.size(); i++) {
for (int i = 0; i < fields.size(); i++) {
CSVField field = (CSVField) fields.get(i);
if (name.equals(field.getName())) {
return field;
@ -149,6 +181,7 @@ public class CSVConfig {
/**
* Set the fill pattern. Defaults to {@link #FILLNONE}
* <br/>Other options are : {@link #FILLLEFT} and {@link #FILLRIGHT}
*
* @param fill the fill pattern.
*/
public void setFill(int fill) {
@ -156,7 +189,6 @@ public class CSVConfig {
}
/**
*
* @return the fillchar. Defaults to a space.
*/
public char getFillChar() {
@ -165,6 +197,7 @@ public class CSVConfig {
/**
* Set the fill char
*
* @param fillChar the fill char
*/
public void setFillChar(char fillChar) {
@ -180,6 +213,7 @@ public class CSVConfig {
/**
* Set the delimiter to use
*
* @param delimiter the delimiter character.
*/
public void setDelimiter(char delimiter) {
@ -195,6 +229,7 @@ public class CSVConfig {
/**
* Set the rowDelimiter to use
*
* @param rowDelimiter the row delimiter character.
*/
public void setRowDelimiter(String rowDelimiter) {
@ -210,6 +245,7 @@ public class CSVConfig {
/**
* Specify if the writer should ignore the delimiter.
*
* @param ignoreDelimiter defaults to false.
*/
public void setIgnoreDelimiter(boolean ignoreDelimiter) {
@ -225,6 +261,7 @@ public class CSVConfig {
/**
* Set the value delimiter to use
*
* @param valueDelimiter the value delimiter character.
*/
public void setValueDelimiter(char valueDelimiter) {
@ -241,6 +278,7 @@ public class CSVConfig {
/**
* Specify if the writer should ignore the value delimiter.
*
* @param ignoreValueDelimiter defaults to false.
*/
public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) {
@ -253,8 +291,10 @@ public class CSVConfig {
public boolean isFieldHeader() {
return fieldHeader;
}
/**
* Specify if you want to use a field header.
*
* @param fieldHeader true or false.
*/
public void setFieldHeader(boolean fieldHeader) {
@ -263,6 +303,7 @@ public class CSVConfig {
/**
* TODO..
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
@ -278,6 +319,7 @@ public class CSVConfig {
/**
* Creates a config based on a stream. It tries to guess<br/>
* NOTE : The stream will be closed.
*
* @param inputStream the inputstream.
* @return the guessed config.
*/
@ -294,6 +336,7 @@ public class CSVConfig {
/**
* Specify if the end of the line needs to be trimmed. Defaults to false.
*
* @param endTrimmed
*/
public void setEndTrimmed(boolean endTrimmed) {

View File

@ -30,14 +30,18 @@ import java.io.InputStreamReader;
*/
public class CSVConfigGuesser {
/** The stream to read */
/**
* The stream to read
*/
private InputStream in;
/**
* if the file has a field header (need this info, to be able to guess better)
* Defaults to false
*/
private boolean hasFieldHeader = false;
/** The found config */
/**
* The found config
*/
protected CSVConfig config;
/**
@ -61,6 +65,7 @@ public class CSVConfigGuesser {
/**
* Allow override.
*
* @return the inputstream that was set.
*/
protected InputStream getInputStream() {
@ -80,7 +85,7 @@ public class CSVConfigGuesser {
String[] lines = new String[10];
String line = null;
int counter = 0;
while ( (line = bIn.readLine()) != null && counter <= 10) {
while ((line = bIn.readLine()) != null && counter <= 10) {
lines[counter] = line;
counter++;
}
@ -91,13 +96,13 @@ public class CSVConfigGuesser {
lines = newLines;
}
analyseLines(lines);
} catch(Exception e) {
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch(Exception e) {
} catch (Exception e) {
// ignore exception.
}
}
@ -116,6 +121,7 @@ public class CSVConfigGuesser {
/**
* Guess if this file is fixedwidth.
* Just basing the fact on all lines being of the same length
*
* @param lines
*/
protected void guessFixedWidth(String[] lines) {
@ -163,14 +169,14 @@ public class CSVConfigGuesser {
previousMatch = 0;
}
CSVField field = new CSVField();
field.setName("field"+config.getFields().length+1);
field.setSize((i-previousMatch));
field.setName("field" + config.getFields().length + 1);
field.setSize((i - previousMatch));
config.addField(field);
}
}
}
/**
*
* @return if the field uses a field header. Defaults to false.
*/
public boolean hasFieldHeader() {
@ -179,6 +185,7 @@ public class CSVConfigGuesser {
/**
* Specify if the CSV file has a field header
*
* @param hasFieldHeader true or false
*/
public void setHasFieldHeader(boolean hasFieldHeader) {

View File

@ -20,7 +20,6 @@ package org.apache.commons.csv.writer;
/**
*
* @author Martin van den Bemt
* @version $Id: $
*/
@ -62,6 +61,7 @@ public class CSVField {
/**
* Set the name of the field
*
* @param name the name
*/
public void setName(String name) {
@ -69,7 +69,6 @@ public class CSVField {
}
/**
*
* @return the size of the field
*/
public int getSize() {
@ -79,6 +78,7 @@ public class CSVField {
/**
* Set the size of the field.
* The size will be ignored when fixedwidth is set to false in the CSVConfig
*
* @param size the size of the field.
*/
public void setSize(int size) {
@ -94,6 +94,7 @@ public class CSVField {
/**
* Sets overrideFill to true.
*
* @param fill the file pattern
*/
public void setFill(int fill) {

View File

@ -31,10 +31,15 @@ import java.util.Map;
*/
public class CSVWriter {
/** The CSV config **/
/**
* The CSV config *
*/
private CSVConfig config;
/** The writer **/
/**
* The writer *
*/
private Writer writer;
/**
*
*/
@ -56,12 +61,12 @@ public class CSVWriter {
value = writeValue(fields[i], value);
sb.append(value);
}
if (!config.isDelimiterIgnored() && fields.length != (i+1)) {
if (!config.isDelimiterIgnored() && fields.length != (i + 1)) {
sb.append(config.getDelimiter());
}
}
if (config.isEndTrimmed()) {
for (int i = sb.length()-1; i >= 0; i--) {
for (int i = sb.length() - 1; i >= 0; i--) {
System.out.println("i : " + i);
if (Character.isWhitespace(sb.charAt(i))) {
sb.deleteCharAt(i);
@ -73,7 +78,7 @@ public class CSVWriter {
sb.append(config.getRowDelimiter());
String line = sb.toString();
writer.write(line);
} catch(Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
@ -106,7 +111,7 @@ public class CSVWriter {
}
if (!config.isValueDelimiterIgnored()) {
// add the value delimiter..
value = config.getValueDelimiter()+value+config.getValueDelimiter();
value = config.getValueDelimiter() + value + config.getValueDelimiter();
}
return value;
}
@ -120,6 +125,7 @@ public class CSVWriter {
/**
* Set the CSVConfig
*
* @param config the CVSConfig
*/
public void setConfig(CSVConfig config) {
@ -128,6 +134,7 @@ public class CSVWriter {
/**
* Set the writer to write the CSV file to.
*
* @param writer the writer.
*/
public void setWriter(Writer writer) {

View File

@ -40,6 +40,7 @@ public class CSVParserTest extends TestCase {
class TestCSVParser extends CSVParser {
/**
* Test parser to investigate the type of the internal Token.
*
* @param in a Reader
*/
TestCSVParser(Reader in) {
@ -49,9 +50,11 @@ public class CSVParserTest extends TestCase {
TestCSVParser(Reader in, CSVStrategy strategy) {
super(in, strategy);
}
/**
* Calls super.nextToken() and prints out a String representation of token
* type and content.
*
* @return String representation of token type and content
* @throws IOException like {@link CSVParser#nextToken()}
*/
@ -91,7 +94,7 @@ public class CSVParserTest extends TestCase {
*
*/
String code = "1,2,3,\na,b x,c\n#foo\n\nd,e,\n\n";
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
// strategy.setIgnoreEmptyLines(false);
strategy.setCommentStart('#');
@ -120,7 +123,7 @@ public class CSVParserTest extends TestCase {
* \,,
*/
String code = "a,\\,,b\n\\,,";
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
strategy.setCommentStart('#');
TestCSVParser parser = new TestCSVParser(new StringReader(code), strategy);
@ -206,6 +209,7 @@ public class CSVParserTest extends TestCase {
{"foo baar", "b", ""},
{"foo\n,,\n\",,\n\"", "d", "e"}
};
public void testGetLine() throws IOException {
CSVParser parser = new CSVParser(new StringReader(code));
String[] tmp = null;
@ -395,15 +399,15 @@ public class CSVParserTest extends TestCase {
+ "a\\,b\n"
+ "\"a\\\\,b\"";
String[][] res = {
{ "one", "two", "three" },
{ "on\\\"e", "two" },
{ "on\"e", "two" },
{ "one", "tw\"o" },
{ "one", "t\\,wo" }, // backslash in quotes only escapes a delimiter (",")
{ "one", "two", "th,ree" },
{ "a\\\\" }, // backslash in quotes only escapes a delimiter (",")
{ "a\\", "b" }, // a backslash must be returnd
{ "a\\\\,b" } // backslash in quotes only escapes a delimiter (",")
{"one", "two", "three"},
{"on\\\"e", "two"},
{"on\"e", "two"},
{"one", "tw\"o"},
{"one", "t\\,wo"}, // backslash in quotes only escapes a delimiter (",")
{"one", "two", "th,ree"},
{"a\\\\"}, // backslash in quotes only escapes a delimiter (",")
{"a\\", "b"}, // a backslash must be returnd
{"a\\\\,b"} // backslash in quotes only escapes a delimiter (",")
};
CSVParser parser = new CSVParser(new StringReader(code));
String[][] tmp = parser.getAllValues();
@ -433,20 +437,20 @@ public class CSVParserTest extends TestCase {
+ "9, /\n \n" // escaped newline
+ "";
String[][] res = {
{ "one", "two", "three" }, // 0
{ "", "" }, // 1
{ "'", "'" }, // 2
{ "'", "'" }, // 3
{ "'", "'" }, // 4
{ ",", "," }, // 5
{ "/", "/" }, // 6
{ "/", "/" }, // 7
{ " 8 ", " \"quoted \"\" \" / string\" " },
{ "9", " \n " },
{"one", "two", "three"}, // 0
{"", ""}, // 1
{"'", "'"}, // 2
{"'", "'"}, // 3
{"'", "'"}, // 4
{",", ","}, // 5
{"/", "/"}, // 6
{"/", "/"}, // 7
{" 8 ", " \"quoted \"\" \" / string\" "},
{"9", " \n "},
};
CSVStrategy strategy = new CSVStrategy(',','\'',CSVStrategy.COMMENTS_DISABLED,'/',false,false,true,true);
CSVStrategy strategy = new CSVStrategy(',', '\'', CSVStrategy.COMMENTS_DISABLED, '/', false, false, true, true);
CSVParser parser = new CSVParser(new StringReader(code), strategy);
String[][] tmp = parser.getAllValues();
@ -468,13 +472,13 @@ public class CSVParserTest extends TestCase {
+ " // , /, , /,\n" // 3)
+ "";
String[][] res = {
{ " ", " ", " " }, // 1
{ " \t ", " ", " " }, // 2
{ " / ", " , ", " ," }, //3
{" ", " ", " "}, // 1
{" \t ", " ", " "}, // 2
{" / ", " , ", " ,"}, //3
};
CSVStrategy strategy = new CSVStrategy(',',CSVStrategy.ENCAPSULATOR_DISABLED,CSVStrategy.COMMENTS_DISABLED,'/',false,false,true,true);
CSVStrategy strategy = new CSVStrategy(',', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '/', false, false, true, true);
CSVParser parser = new CSVParser(new StringReader(code), strategy);
String[][] tmp = parser.getAllValues();
@ -495,9 +499,9 @@ public class CSVParserTest extends TestCase {
+ "\"\",#\n" // 2)
;
String[][] res = {
{ "a", "b" },
{ "\n", " " },
{ "", "#" },
{"a", "b"},
{"\n", " "},
{"", "#"},
};
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
@ -512,12 +516,12 @@ public class CSVParserTest extends TestCase {
}
String[][] res_comments = {
{ "a", "b" },
{ "\n", " " },
{ ""},
{"a", "b"},
{"\n", " "},
{""},
};
strategy = new CSVStrategy(',','"','#');
strategy = new CSVStrategy(',', '"', '#');
parser = new CSVParser(new StringReader(code), strategy);
tmp = parser.getAllValues();

View File

@ -106,16 +106,15 @@ public class CSVPrinterTest extends TestCase {
}
public void testRandom() throws Exception {
int iter=10000;
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);
strategy = new CSVStrategy('\t', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '\\', false, false, false, false);
doRandom(iter);
}
@ -123,20 +122,20 @@ public class CSVPrinterTest extends TestCase {
CSVStrategy strategy;
public void doRandom(int iter) throws Exception {
for (int i=0; i<iter; i++) {
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;
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++) {
for (int i = 0; i < nLines; i++) {
String[] line = new String[nCol];
lines[i] = line;
for (int j=0; j<nCol; j++) {
for (int j = 0; j < nCol; j++) {
line[j] = randStr();
}
}
@ -144,7 +143,7 @@ public class CSVPrinterTest extends TestCase {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, strategy);
for (int i=0; i<nLines; i++) {
for (int i = 0; i < nLines; i++) {
// for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
printer.println(lines[i]);
}
@ -168,13 +167,13 @@ public class CSVPrinterTest extends TestCase {
if (a.length != b.length) {
return false;
}
for (int i=0; i<a.length; i++) {
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++) {
for (int j = 0; j < linea.length; j++) {
String aval = linea[j];
String bval = lineb[j];
if (!aval.equals(bval)) {
@ -189,10 +188,10 @@ public class CSVPrinterTest extends TestCase {
public static String printable(String s) {
StringBuffer sb = new StringBuffer();
for (int i=0; i<s.length(); i++) {
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch<=' ' || ch>=128) {
sb.append("(" + (int)ch + ")");
if (ch <= ' ' || ch >= 128) {
sb.append("(" + (int) ch + ")");
} else {
sb.append(ch);
}
@ -204,21 +203,41 @@ public class CSVPrinterTest extends TestCase {
int sz = r.nextInt(20);
// sz = r.nextInt(3);
char[] buf = new char[sz];
for (int i=0; i<sz; i++) {
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;
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;

View File

@ -35,7 +35,7 @@ public class CSVStrategyTest extends TestCase {
// getters / setters
// ======================================================
public void testGetSetCommentStart() {
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
strategy.setCommentStart('#');
assertEquals(strategy.getCommentStart(), '#');
strategy.setCommentStart('!');
@ -43,7 +43,7 @@ public class CSVStrategyTest extends TestCase {
}
public void testGetSetEncapsulator() {
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
strategy.setEncapsulator('"');
assertEquals(strategy.getEncapsulator(), '"');
strategy.setEncapsulator('\'');
@ -51,7 +51,7 @@ public class CSVStrategyTest extends TestCase {
}
public void testGetSetDelimiter() {
CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
strategy.setDelimiter(';');
assertEquals(strategy.getDelimiter(), ';');
strategy.setDelimiter(',');

View File

@ -21,7 +21,6 @@ package org.apache.commons.csv;
import junit.framework.TestCase;
/**
*
* @author Ortwin Glück
*/
public class CharBufferTest extends TestCase {
@ -31,7 +30,7 @@ public class CharBufferTest extends TestCase {
try {
cb = new CharBuffer(0);
fail("Should not be possible");
} catch(IllegalArgumentException e) {
} catch (IllegalArgumentException e) {
// expected
}
@ -54,11 +53,11 @@ public class CharBufferTest extends TestCase {
CharBuffer cb = new CharBuffer(1);
char[] abcd = "abcd".toCharArray();
String expected = "";
for (int i=0; i<10; i++) {
for (int i = 0; i < 10; i++) {
cb.append(abcd);
expected += "abcd";
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
assertEquals(4 * (i + 1), cb.length());
}
}
@ -66,11 +65,11 @@ public class CharBufferTest extends TestCase {
CharBuffer cb = new CharBuffer(1);
String abcd = "abcd";
String expected = "";
for (int i=0; i<10; i++) {
for (int i = 0; i < 10; i++) {
cb.append(abcd);
expected += abcd;
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
assertEquals(4 * (i + 1), cb.length());
}
}
@ -78,11 +77,11 @@ public class CharBufferTest extends TestCase {
CharBuffer cb = new CharBuffer(1);
StringBuffer abcd = new StringBuffer("abcd");
String expected = "";
for (int i=0; i<10; i++) {
for (int i = 0; i < 10; i++) {
cb.append(abcd);
expected += "abcd";
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
assertEquals(4 * (i + 1), cb.length());
}
}
@ -91,11 +90,11 @@ public class CharBufferTest extends TestCase {
CharBuffer abcd = new CharBuffer(17);
abcd.append("abcd");
String expected = "";
for (int i=0; i<10; i++) {
for (int i = 0; i < 10; i++) {
cb.append(abcd);
expected += "abcd";
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
assertEquals(4 * (i + 1), cb.length());
}
}
@ -125,7 +124,7 @@ public class CharBufferTest extends TestCase {
assertEquals(0, b1.length);
assertEquals(0, buffer.length());
char[] tmp = new char[] { '1', '2', '3', '4'};
char[] tmp = new char[]{'1', '2', '3', '4'};
buffer.append(tmp);
assertEquals(16, buffer.capacity());
assertEquals(4, buffer.length());
@ -153,16 +152,16 @@ public class CharBufferTest extends TestCase {
public void testAppendNull() throws Exception {
CharBuffer buffer = new CharBuffer(8);
buffer.append((StringBuffer)null);
buffer.append((StringBuffer) null);
assertEquals("", buffer.toString());
buffer.append((String)null);
buffer.append((String) null);
assertEquals("", buffer.toString());
buffer.append((CharBuffer)null);
buffer.append((CharBuffer) null);
assertEquals("", buffer.toString());
buffer.append((char[])null);
buffer.append((char[]) null);
assertEquals("", buffer.toString());
}

View File

@ -25,7 +25,6 @@ import junit.framework.TestSuite;
/**
* ExtendedBufferedReaderTest
*
*/
public class ExtendedBufferedReaderTest extends TestCase {

View File

@ -41,10 +41,10 @@ public class CSVConfigGuesserTest extends TestCase {
guesser.setHasFieldHeader(true);
assertEquals(true, guesser.hasFieldHeader());
}
/**
* Test a format like
* 1234 ; abcd ; 1234 ;
*
*/
public void testConfigGuess1() {
CSVConfig expected = new CSVConfig();
@ -67,11 +67,11 @@ public class CSVConfigGuesserTest extends TestCase {
assertEquals(expected.getFields().length, guessed.getFields().length);
assertEquals(expected.getFields()[0].getSize(), guessed.getFields()[0].getSize());
}
/**
* Test a format like
* 1234,123123,12312312,213123
* 1,2,3,4
*
*/
public void testConfigGuess2() {
CSVConfig expected = new CSVConfig();

View File

@ -41,9 +41,9 @@ public class CSVConfigTest extends TestCase {
public void testFields() {
CSVConfig config = new CSVConfig();
assertEquals(0, config.getFields().length);
config.setFields((CSVField[])null);
config.setFields((CSVField[]) null);
assertEquals(0, config.getFields().length);
config.setFields((Collection)null);
config.setFields((Collection) null);
assertEquals(0, config.getFields().length);
CSVField field = new CSVField();
field.setName("field1");

View File

@ -21,7 +21,6 @@ package org.apache.commons.csv.writer;
import junit.framework.TestCase;
/**
*
* @author Martin van den Bemt
* @version $Id: $
*/