You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ja...@apache.org on 2011/02/01 09:46:01 UTC
svn commit: r1065950 [2/3] - in /commons/sandbox/csv/trunk/src:
java/org/apache/commons/csv/ java/org/apache/commons/csv/writer/
test/org/apache/commons/csv/ test/org/apache/commons/csv/writer/
Modified: commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/ExtendedBufferedReader.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/ExtendedBufferedReader.java?rev=1065950&r1=1065949&r2=1065950&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/ExtendedBufferedReader.java (original)
+++ commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/ExtendedBufferedReader.java Tue Feb 1 08:46:00 2011
@@ -23,214 +23,223 @@ import java.io.Reader;
/**
* ExtendedBufferedReader
*
- * A special reader decorater which supports more
+ * A special reader decorator which supports more
* sophisticated access to the underlying reader object.
- *
+ *
* In particular the reader supports a look-ahead option,
* which allows you to see the next char returned by
* next().
- *
*/
-class ExtendedBufferedReader extends BufferedReader {
+class ExtendedBufferedReader extends BufferedReader {
+
+
+ /**
+ * the end of stream symbol
+ */
+ public static final int END_OF_STREAM = -1;
+ /**
+ * undefined state for the lookahead char
+ */
+ public static final int UNDEFINED = -2;
+
+ /**
+ * the lookahead chars
+ */
+ private int lookaheadChar = UNDEFINED;
+ /**
+ * the last char returned
+ */
+ private int lastChar = UNDEFINED;
+ /**
+ * the line counter
+ */
+ private int lineCounter = 0;
+ private CharBuffer line = new CharBuffer();
+
+ /**
+ * Created extended buffered reader using default buffer-size
+ */
+ public ExtendedBufferedReader(Reader r) {
+ super(r);
+ /* note uh: do not fetch the first char here,
+ * because this might block the method!
+ */
+ }
+
+ /**
+ * 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!
+ */
+ }
-
- /** the end of stream symbol */
- public static final int END_OF_STREAM = -1;
- /** undefined state for the lookahead char */
- public static final int UNDEFINED = -2;
-
- /** the lookahead chars */
- private int lookaheadChar = UNDEFINED;
- /** the last char returned */
- private int lastChar = UNDEFINED;
- /** the line counter */
- private int lineCounter = 0;
- private CharBuffer line = new CharBuffer();
-
- /**
- * Created extended buffered reader using default buffer-size
- *
- */
- public ExtendedBufferedReader(Reader r) {
- super(r);
- /* note uh: do not fetch the first char here,
- * because this might block the method!
- */
- }
-
- /**
- * 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!
- */
- }
-
- /**
- * Reads the next char from the input stream.
- * @return the next char or END_OF_STREAM if end of stream has been reached.
- */
- public int read() throws IOException {
- // initalize the lookahead
- if (lookaheadChar == UNDEFINED) {
- lookaheadChar = super.read();
- }
- lastChar = lookaheadChar;
- if (super.ready()) {
- lookaheadChar = super.read();
- } else {
- 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.
- *
- * performs an iteratative read on the underlying stream
- * as long as the following conditions hold:
- * - less than len chars have been read
- * - end of stream has not been reached
- * - next read is not blocking
- *
- * @return nof chars actually read or END_OF_STREAM
- */
- public int read(char[] buf, int off, int len) throws IOException {
- // do not claim if len == 0
- if (len == 0) {
- return 0;
- }
-
- // init lookahead, but do not block !!
- if (lookaheadChar == UNDEFINED) {
- if (ready()) {
- lookaheadChar = super.read();
+ /**
+ * Reads the next char from the input stream.
+ *
+ * @return the next char or END_OF_STREAM if end of stream has been reached.
+ */
+ public int read() throws IOException {
+ // initialize the lookahead
+ if (lookaheadChar == UNDEFINED) {
+ lookaheadChar = super.read();
+ }
+ lastChar = lookaheadChar;
+ if (super.ready()) {
+ lookaheadChar = super.read();
} else {
- return -1;
+ lookaheadChar = UNDEFINED;
+ }
+ if (lastChar == '\n') {
+ lineCounter++;
}
+ return lastChar;
}
- // '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
+
+ /**
+ * 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;
- } 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') {
+
+ /**
+ * @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();
- }
- lineCounter++;
- return line.toString();
- }
-
- // create the rest-of-line return and update the lookahead
- line.append(laChar);
- String restOfLine = super.readLine(); // TODO involves copying
- lastChar = lookaheadChar;
- lookaheadChar = super.read();
- if (restOfLine != null) {
- line.append(restOfLine);
- }
- lineCounter++;
- return line.toString();
- }
-
- /**
- * Unsupported
- */
- public long skip(long n) throws IllegalArgumentException, IOException {
- throw new UnsupportedOperationException("CSV has no reason to implement this");
- }
-
- /**
- * Returns the next char in the stream without consuming it.
- *
- * Remember the next char read by read(..) will always be
- * identical to lookAhead().
- *
- * @return the next char (without consuming it) or END_OF_STREAM
- */
- public int lookAhead() throws IOException {
- if (lookaheadChar == UNDEFINED) {
- lookaheadChar = super.read();
- }
- return lookaheadChar;
- }
-
-
- /**
- * Returns the nof line read
- *
- * @return the current-line-number (or -1)
- */
- public int getLineNumber() {
- if (lineCounter > -1) {
- return lineCounter;
- } else {
- return -1;
- }
- }
-
- /**
- * Unsupported
- */
- public boolean markSupported() {
- throw new UnsupportedOperationException("CSV has no reason to implement this");
- }
-
+ if (restOfLine != null) {
+ line.append(restOfLine);
+ }
+ lineCounter++;
+ return line.toString();
+ }
+
+ /**
+ * Unsupported
+ */
+ public long skip(long n) throws IllegalArgumentException, IOException {
+ throw new UnsupportedOperationException("CSV has no reason to implement this");
+ }
+
+ /**
+ * Returns the next char in the stream without consuming it.
+ *
+ * Remember the next char read by read(..) will always be
+ * identical to lookAhead().
+ *
+ * @return the next char (without consuming it) or END_OF_STREAM
+ */
+ public int lookAhead() throws IOException {
+ if (lookaheadChar == UNDEFINED) {
+ lookaheadChar = super.read();
+ }
+ return lookaheadChar;
+ }
+
+
+ /**
+ * Returns the nof line read
+ *
+ * @return the current-line-number (or -1)
+ */
+ public int getLineNumber() {
+ if (lineCounter > -1) {
+ return lineCounter;
+ } else {
+ return -1;
+ }
+ }
+
+ /**
+ * Unsupported
+ */
+ public boolean markSupported() {
+ throw new UnsupportedOperationException("CSV has no reason to implement this");
+ }
+
}
Modified: commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVConfig.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVConfig.java?rev=1065950&r1=1065949&r2=1065950&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVConfig.java (original)
+++ commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVConfig.java Tue Feb 1 08:46:00 2011
@@ -32,69 +32,100 @@ import java.util.List;
*/
public class CSVConfig {
- /** specifies if it is a fixed width csv file **/
+ /**
+ * specifies if it is a fixed width csv file *
+ */
private boolean fixedWidth;
- /** list of fields **/
+ /**
+ * list of fields *
+ */
private List fields;
- /** Do no do any filling **/
+ /**
+ * 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;
+
/**
- *
+ *
*/
public CSVConfig() {
super();
}
-
+
/**
* @return if the CSV file is fixedWidth
*/
public boolean isFixedWidth() {
return fixedWidth;
}
-
+
/**
* Specify if the CSV file is fixed width.
* Defaults to false
+ *
* @param fixedWidth the fixedwidth
*/
public void setFixedWidth(boolean fixedWidth) {
this.fixedWidth = fixedWidth;
}
-
+
public void addField(CSVField field) {
if (fields == null) {
fields = new ArrayList();
}
fields.add(field);
}
-
+
/**
* Set the fields that should be used by the writer.
* This will overwrite currently added fields completely!
+ *
* @param csvFields the csvfields array. If null it will do nothing
*/
public void setFields(CSVField[] csvFields) {
@@ -103,9 +134,10 @@ public class CSVConfig {
}
fields = new ArrayList(Arrays.asList(csvFields));
}
-
+
/**
* Set the fields that should be used by the writer
+ *
* @param csvField a collection with fields. If null it will do nothing
*/
public void setFields(Collection csvField) {
@@ -125,12 +157,12 @@ public class CSVConfig {
}
return csvFields;
}
-
+
public CSVField getField(String name) {
if (fields == null || name == null) {
return null;
}
- for(int i = 0; i < fields.size(); i++) {
+ for (int i = 0; i < fields.size(); i++) {
CSVField field = (CSVField) fields.get(i);
if (name.equals(field.getName())) {
return field;
@@ -149,6 +181,7 @@ public class CSVConfig {
/**
* Set the fill pattern. Defaults to {@link #FILLNONE}
* <br/>Other options are : {@link #FILLLEFT} and {@link #FILLRIGHT}
+ *
* @param fill the fill pattern.
*/
public void setFill(int fill) {
@@ -156,7 +189,6 @@ public class CSVConfig {
}
/**
- *
* @return the fillchar. Defaults to a space.
*/
public char getFillChar() {
@@ -165,6 +197,7 @@ public class CSVConfig {
/**
* Set the fill char
+ *
* @param fillChar the fill char
*/
public void setFillChar(char fillChar) {
@@ -180,6 +213,7 @@ public class CSVConfig {
/**
* Set the delimiter to use
+ *
* @param delimiter the delimiter character.
*/
public void setDelimiter(char delimiter) {
@@ -195,6 +229,7 @@ public class CSVConfig {
/**
* Set the rowDelimiter to use
+ *
* @param rowDelimiter the row delimiter character.
*/
public void setRowDelimiter(String rowDelimiter) {
@@ -209,7 +244,8 @@ public class CSVConfig {
}
/**
- * Specify if the writer should ignore the delimiter.
+ * Specify if the writer should ignore the delimiter.
+ *
* @param ignoreDelimiter defaults to false.
*/
public void setIgnoreDelimiter(boolean ignoreDelimiter) {
@@ -225,6 +261,7 @@ public class CSVConfig {
/**
* Set the value delimiter to use
+ *
* @param valueDelimiter the value delimiter character.
*/
public void setValueDelimiter(char valueDelimiter) {
@@ -240,7 +277,8 @@ public class CSVConfig {
}
/**
- * Specify if the writer should ignore the value delimiter.
+ * Specify if the writer should ignore the value delimiter.
+ *
* @param ignoreValueDelimiter defaults to false.
*/
public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) {
@@ -253,16 +291,19 @@ public class CSVConfig {
public boolean isFieldHeader() {
return fieldHeader;
}
+
/**
* Specify if you want to use a field header.
+ *
* @param fieldHeader true or false.
*/
public void setFieldHeader(boolean fieldHeader) {
this.fieldHeader = fieldHeader;
}
-
+
/**
* TODO..
+ *
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
@@ -278,8 +319,9 @@ public class CSVConfig {
/**
* Creates a config based on a stream. It tries to guess<br/>
* NOTE : The stream will be closed.
- * @param inputStream the inputstream.
- * @return the guessed config.
+ *
+ * @param inputStream the inputstream.
+ * @return the guessed config.
*/
public static CSVConfig guessConfig(InputStream inputStream) {
return null;
@@ -294,11 +336,12 @@ public class CSVConfig {
/**
* Specify if the end of the line needs to be trimmed. Defaults to false.
+ *
* @param endTrimmed
*/
public void setEndTrimmed(boolean endTrimmed) {
this.endTrimmed = endTrimmed;
}
-
+
}
Modified: commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVConfigGuesser.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVConfigGuesser.java?rev=1065950&r1=1065949&r2=1065950&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVConfigGuesser.java (original)
+++ commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVConfigGuesser.java Tue Feb 1 08:46:00 2011
@@ -30,23 +30,27 @@ import java.io.InputStreamReader;
*/
public class CSVConfigGuesser {
- /** The stream to read */
+ /**
+ * The stream to read
+ */
private InputStream in;
- /**
+ /**
* if the file has a field header (need this info, to be able to guess better)
* Defaults to false
*/
private boolean hasFieldHeader = false;
- /** The found config */
+ /**
+ * The found config
+ */
protected CSVConfig config;
-
+
/**
- *
+ *
*/
public CSVConfigGuesser() {
this.config = new CSVConfig();
}
-
+
/**
* @param in the inputstream to guess from
*/
@@ -54,23 +58,24 @@ public class CSVConfigGuesser {
this();
setInputStream(in);
}
-
+
public void setInputStream(InputStream in) {
this.in = in;
}
-
+
/**
* Allow override.
+ *
* @return the inputstream that was set.
*/
protected InputStream getInputStream() {
return in;
}
-
+
/**
- * Guess the config based on the first 10 (or less when less available)
+ * Guess the config based on the first 10 (or less when less available)
* records of a CSV file.
- *
+ *
* @return the guessed config.
*/
public CSVConfig guess() {
@@ -80,7 +85,7 @@ public class CSVConfigGuesser {
String[] lines = new String[10];
String line = null;
int counter = 0;
- while ( (line = bIn.readLine()) != null && counter <= 10) {
+ while ((line = bIn.readLine()) != null && counter <= 10) {
lines[counter] = line;
counter++;
}
@@ -91,13 +96,13 @@ public class CSVConfigGuesser {
lines = newLines;
}
analyseLines(lines);
- } catch(Exception e) {
+ } catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
- } catch(Exception e) {
+ } catch (Exception e) {
// ignore exception.
}
}
@@ -107,15 +112,16 @@ public class CSVConfigGuesser {
config = null;
return conf;
}
-
+
protected void analyseLines(String[] lines) {
guessFixedWidth(lines);
guessFieldSeperator(lines);
}
-
+
/**
* Guess if this file is fixedwidth.
* Just basing the fact on all lines being of the same length
+ *
* @param lines
*/
protected void guessFixedWidth(String[] lines) {
@@ -132,7 +138,7 @@ public class CSVConfigGuesser {
}
}
}
-
+
protected void guessFieldSeperator(String[] lines) {
if (config.isFixedWidth()) {
@@ -142,7 +148,7 @@ public class CSVConfigGuesser {
for (int i = 0; i < lines.length; i++) {
}
}
-
+
protected void guessFixedWidthSeperator(String[] lines) {
// keep track of the fieldlength
int previousMatch = -1;
@@ -156,21 +162,21 @@ public class CSVConfigGuesser {
if (last != lines[j].charAt(i)) {
charMatches = false;
break;
- }
+ }
}
if (charMatches) {
if (previousMatch == -1) {
previousMatch = 0;
}
CSVField field = new CSVField();
- field.setName("field"+config.getFields().length+1);
- field.setSize((i-previousMatch));
+ field.setName("field" + config.getFields().length + 1);
+ field.setSize((i - previousMatch));
config.addField(field);
}
}
}
+
/**
- *
* @return if the field uses a field header. Defaults to false.
*/
public boolean hasFieldHeader() {
@@ -179,11 +185,12 @@ public class CSVConfigGuesser {
/**
* Specify if the CSV file has a field header
+ *
* @param hasFieldHeader true or false
*/
public void setHasFieldHeader(boolean hasFieldHeader) {
this.hasFieldHeader = hasFieldHeader;
}
-
-
+
+
}
Modified: commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVField.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVField.java?rev=1065950&r1=1065949&r2=1065950&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVField.java (original)
+++ commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVField.java Tue Feb 1 08:46:00 2011
@@ -20,7 +20,6 @@ package org.apache.commons.csv.writer;
/**
- *
* @author Martin van den Bemt
* @version $Id: $
*/
@@ -32,7 +31,7 @@ public class CSVField {
private boolean overrideFill;
/**
- *
+ *
*/
public CSVField() {
}
@@ -59,9 +58,10 @@ public class CSVField {
public String getName() {
return name;
}
-
+
/**
* Set the name of the field
+ *
* @param name the name
*/
public void setName(String name) {
@@ -69,7 +69,6 @@ public class CSVField {
}
/**
- *
* @return the size of the field
*/
public int getSize() {
@@ -79,6 +78,7 @@ public class CSVField {
/**
* Set the size of the field.
* The size will be ignored when fixedwidth is set to false in the CSVConfig
+ *
* @param size the size of the field.
*/
public void setSize(int size) {
@@ -94,16 +94,17 @@ public class CSVField {
/**
* Sets overrideFill to true.
+ *
* @param fill the file pattern
*/
public void setFill(int fill) {
overrideFill = true;
this.fill = fill;
}
-
+
/**
* Does this field override fill ?
- *
+ *
* @return
*/
public boolean overrideFill() {
Modified: commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVWriter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVWriter.java?rev=1065950&r1=1065949&r2=1065950&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVWriter.java (original)
+++ commons/sandbox/csv/trunk/src/java/org/apache/commons/csv/writer/CSVWriter.java Tue Feb 1 08:46:00 2011
@@ -31,16 +31,21 @@ import java.util.Map;
*/
public class CSVWriter {
- /** The CSV config **/
+ /**
+ * The CSV config *
+ */
private CSVConfig config;
- /** The writer **/
+ /**
+ * The writer *
+ */
private Writer writer;
+
/**
- *
+ *
*/
public CSVWriter() {
}
-
+
public CSVWriter(CSVConfig config) {
setConfig(config);
}
@@ -56,12 +61,12 @@ public class CSVWriter {
value = writeValue(fields[i], value);
sb.append(value);
}
- if (!config.isDelimiterIgnored() && fields.length != (i+1)) {
+ if (!config.isDelimiterIgnored() && fields.length != (i + 1)) {
sb.append(config.getDelimiter());
}
}
if (config.isEndTrimmed()) {
- for (int i = sb.length()-1; i >= 0; i--) {
+ for (int i = sb.length() - 1; i >= 0; i--) {
System.out.println("i : " + i);
if (Character.isWhitespace(sb.charAt(i))) {
sb.deleteCharAt(i);
@@ -73,11 +78,11 @@ public class CSVWriter {
sb.append(config.getRowDelimiter());
String line = sb.toString();
writer.write(line);
- } catch(Exception e) {
+ } catch (Exception e) {
e.printStackTrace();
}
}
-
+
protected String writeValue(CSVField field, String value) throws Exception {
if (config.isFixedWidth()) {
if (value.length() < field.getSize()) {
@@ -106,11 +111,11 @@ public class CSVWriter {
}
if (!config.isValueDelimiterIgnored()) {
// add the value delimiter..
- value = config.getValueDelimiter()+value+config.getValueDelimiter();
+ value = config.getValueDelimiter() + value + config.getValueDelimiter();
}
return value;
}
-
+
/**
* @return the CVSConfig or null if not present
*/
@@ -120,14 +125,16 @@ public class CSVWriter {
/**
* Set the CSVConfig
+ *
* @param config the CVSConfig
*/
public void setConfig(CSVConfig config) {
this.config = config;
}
-
+
/**
* Set the writer to write the CSV file to.
+ *
* @param writer the writer.
*/
public void setWriter(Writer writer) {
Modified: commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java?rev=1065950&r1=1065949&r2=1065950&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java (original)
+++ commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVParserTest.java Tue Feb 1 08:46:00 2011
@@ -27,569 +27,573 @@ import junit.framework.TestCase;
* CSVParserTest
*
* The test are organized in three different sections:
- * The 'setter/getter' section, the lexer section and finally the parser
- * section. In case a test fails, you should follow a top-down approach for
+ * The 'setter/getter' section, the lexer section and finally the parser
+ * section. In case a test fails, you should follow a top-down approach for
* fixing a potential bug (its likely that the parser itself fails if the lexer
* has problems...).
*/
public class CSVParserTest extends TestCase {
-
- /**
- * TestCSVParser.
- */
- class TestCSVParser extends CSVParser {
+
/**
- * Test parser to investigate the type of the internal Token.
- * @param in a Reader
+ * TestCSVParser.
*/
- TestCSVParser(Reader in) {
- super(in);
- }
+ class TestCSVParser extends CSVParser {
+ /**
+ * Test parser to investigate the type of the internal Token.
+ *
+ * @param in a Reader
+ */
+ TestCSVParser(Reader in) {
+ super(in);
+ }
- TestCSVParser(Reader in, CSVStrategy strategy) {
- super(in, strategy);
+ 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()}
+ */
+ public String testNextToken() throws IOException {
+ Token t = super.nextToken();
+ return Integer.toString(t.type) + ";" + t.content + ";";
+ }
}
- /**
- * 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()}
- */
- public String testNextToken() throws IOException {
- Token t = super.nextToken();
- return Integer.toString(t.type) + ";" + t.content + ";";
- }
- }
-
- // ======================================================
- // lexer tests
- // ======================================================
-
- // Single line (without comment)
- public void testNextToken1() throws IOException {
- String code = "abc,def, hijk, lmnop, qrst,uv ,wxy ,z , ,";
- TestCSVParser parser = new TestCSVParser(new StringReader(code));
- assertEquals(CSVParser.TT_TOKEN + ";abc;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";def;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";hijk;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";lmnop;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";qrst;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";uv;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";wxy;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";z;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
- assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());
- }
-
- // multiline including comments (and empty lines)
- public void testNextToken2() throws IOException {
- /* file: 1,2,3,
- * a,b x,c
- *
- * # this is a comment
- * d,e,
- *
- */
- String code = "1,2,3,\na,b x,c\n#foo\n\nd,e,\n\n";
- CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
- // strategy.setIgnoreEmptyLines(false);
- strategy.setCommentStart('#');
-
- TestCSVParser parser = new TestCSVParser(new StringReader(code), strategy);
-
-
- assertEquals(CSVParser.TT_TOKEN + ";1;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";2;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";3;", parser.testNextToken());
- assertEquals(CSVParser.TT_EORECORD + ";;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";b x;", parser.testNextToken());
- assertEquals(CSVParser.TT_EORECORD + ";c;", parser.testNextToken());
- assertEquals(CSVParser.TT_EORECORD + ";;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";d;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";e;", parser.testNextToken());
- assertEquals(CSVParser.TT_EORECORD + ";;", parser.testNextToken());
- assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());
- assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());
-
- }
-
- // simple token with escaping
- public void testNextToken3() throws IOException {
- /* file: a,\,,b
- * \,,
- */
- String code = "a,\\,,b\n\\,,";
- CSVStrategy strategy = (CSVStrategy)CSVStrategy.DEFAULT_STRATEGY.clone();
- strategy.setCommentStart('#');
- TestCSVParser parser = new TestCSVParser(new StringReader(code), strategy);
-
- assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
- // an unquoted single backslash is not an escape char
- assertEquals(CSVParser.TT_TOKEN + ";\\;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
- assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
- // an unquoted single backslash is not an escape char
- assertEquals(CSVParser.TT_TOKEN + ";\\;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
- assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());
- }
-
- // encapsulator tokenizer (sinle line)
- public void testNextToken4() throws IOException {
- /* file: a,"foo",b
- * a, " foo",b
- * a,"foo " ,b // whitespace after closing encapsulator
- * a, " foo " ,b
- */
- String code =
- "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na, \" foo \" ,b";
- TestCSVParser parser = new TestCSVParser(new StringReader(code));
- assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";foo;", parser.testNextToken());
- assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + "; foo;", parser.testNextToken());
- assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";foo ;", parser.testNextToken());
- assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + "; foo ;", parser.testNextToken());
+
+ // ======================================================
+ // lexer tests
+ // ======================================================
+
+ // Single line (without comment)
+ public void testNextToken1() throws IOException {
+ String code = "abc,def, hijk, lmnop, qrst,uv ,wxy ,z , ,";
+ TestCSVParser parser = new TestCSVParser(new StringReader(code));
+ assertEquals(CSVParser.TT_TOKEN + ";abc;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";def;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";hijk;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";lmnop;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";qrst;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";uv;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";wxy;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";z;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());
+ }
+
+ // multiline including comments (and empty lines)
+ public void testNextToken2() throws IOException {
+ /* file: 1,2,3,
+ * a,b x,c
+ *
+ * # this is a comment
+ * d,e,
+ *
+ */
+ String code = "1,2,3,\na,b x,c\n#foo\n\nd,e,\n\n";
+ CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
+ // strategy.setIgnoreEmptyLines(false);
+ strategy.setCommentStart('#');
+
+ TestCSVParser parser = new TestCSVParser(new StringReader(code), strategy);
+
+
+ assertEquals(CSVParser.TT_TOKEN + ";1;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";2;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";3;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EORECORD + ";;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";b x;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EORECORD + ";c;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EORECORD + ";;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";d;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";e;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EORECORD + ";;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());
+
+ }
+
+ // simple token with escaping
+ public void testNextToken3() throws IOException {
+ /* file: a,\,,b
+ * \,,
+ */
+ String code = "a,\\,,b\n\\,,";
+ CSVStrategy strategy = (CSVStrategy) CSVStrategy.DEFAULT_STRATEGY.clone();
+ strategy.setCommentStart('#');
+ TestCSVParser parser = new TestCSVParser(new StringReader(code), strategy);
+
+ assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
+ // an unquoted single backslash is not an escape char
+ assertEquals(CSVParser.TT_TOKEN + ";\\;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
+ // an unquoted single backslash is not an escape char
+ assertEquals(CSVParser.TT_TOKEN + ";\\;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EOF + ";;", parser.testNextToken());
+ }
+
+ // encapsulator tokenizer (sinle line)
+ public void testNextToken4() throws IOException {
+ /* file: a,"foo",b
+ * a, " foo",b
+ * a,"foo " ,b // whitespace after closing encapsulator
+ * a, " foo " ,b
+ */
+ String code =
+ "a,\"foo\",b\na, \" foo\",b\na,\"foo \" ,b\na, \" foo \" ,b";
+ TestCSVParser parser = new TestCSVParser(new StringReader(code));
+ assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";foo;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + "; foo;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";foo ;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + "; foo ;", parser.testNextToken());
// assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
- assertEquals(CSVParser.TT_EOF + ";b;", parser.testNextToken());
- }
-
- // encapsulator tokenizer (multi line, delimiter in string)
- public void testNextToken5() throws IOException {
- String code =
- "a,\"foo\n\",b\n\"foo\n baar ,,,\"\n\"\n\t \n\"";
- TestCSVParser parser = new TestCSVParser(new StringReader(code));
- assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";foo\n;", parser.testNextToken());
- assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
- assertEquals(CSVParser.TT_EORECORD + ";foo\n baar ,,,;",
- parser.testNextToken());
- assertEquals(CSVParser.TT_EOF + ";\n\t \n;", parser.testNextToken());
-
- }
-
- // change delimiters, comment, encapsulater
- public void testNextToken6() throws IOException {
- /* file: a;'b and \' more
- * '
- * !comment;;;;
- * ;;
- */
- String code = "a;'b and '' more\n'\n!comment;;;;\n;;";
- TestCSVParser parser = new TestCSVParser(new StringReader(code), new CSVStrategy(';', '\'', '!'));
- assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
- assertEquals(
- CSVParser.TT_EORECORD + ";b and ' more\n;",
- parser.testNextToken());
- }
-
-
- // ======================================================
- // parser tests
- // ======================================================
-
- String code =
- "a,b,c,d\n"
- + " a , b , 1 2 \n"
- + "\"foo baar\", b,\n"
- // + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n";
- + " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping
- String[][] res = {
- {"a", "b", "c", "d"},
- {"a", "b", "1 2"},
- {"foo baar", "b", ""},
- {"foo\n,,\n\",,\n\"", "d", "e"}
- };
- public void testGetLine() throws IOException {
- CSVParser parser = new CSVParser(new StringReader(code));
- String[] tmp = null;
- for (int i = 0; i < res.length; i++) {
- tmp = parser.getLine();
- assertTrue(Arrays.equals(res[i], tmp));
- }
- tmp = parser.getLine();
- assertTrue(tmp == null);
- }
-
- public void testNextValue() throws IOException {
- CSVParser parser = new CSVParser(new StringReader(code));
- String tmp = null;
- for (int i = 0; i < res.length; i++) {
- for (int j = 0; j < res[i].length; j++) {
- tmp = parser.nextValue();
- assertEquals(res[i][j], tmp);
- }
+ assertEquals(CSVParser.TT_EOF + ";b;", parser.testNextToken());
}
- tmp = parser.nextValue();
- assertTrue(tmp == null);
- }
-
- public void testGetAllValues() throws IOException {
- CSVParser parser = new CSVParser(new StringReader(code));
- String[][] tmp = parser.getAllValues();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
- for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
- }
- }
-
- public void testExcelStrategy1() throws IOException {
- String code =
- "value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
- + "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
- String[][] res = {
- {"value1", "value2", "value3", "value4"},
- {"a", "b", "c", "d"},
- {" x", "", "", ""},
- {""},
- {"\"hello\"", " \"world\"", "abc\ndef", ""}
- };
- CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY);
- String[][] tmp = parser.getAllValues();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
- for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
- }
- }
-
- public void testExcelStrategy2() throws Exception {
- String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
+
+ // encapsulator tokenizer (multi line, delimiter in string)
+ public void testNextToken5() throws IOException {
+ String code =
+ "a,\"foo\n\",b\n\"foo\n baar ,,,\"\n\"\n\t \n\"";
+ TestCSVParser parser = new TestCSVParser(new StringReader(code));
+ assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";foo\n;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EORECORD + ";b;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EORECORD + ";foo\n baar ,,,;",
+ parser.testNextToken());
+ assertEquals(CSVParser.TT_EOF + ";\n\t \n;", parser.testNextToken());
+
+ }
+
+ // change delimiters, comment, encapsulater
+ public void testNextToken6() throws IOException {
+ /* file: a;'b and \' more
+ * '
+ * !comment;;;;
+ * ;;
+ */
+ String code = "a;'b and '' more\n'\n!comment;;;;\n;;";
+ TestCSVParser parser = new TestCSVParser(new StringReader(code), new CSVStrategy(';', '\'', '!'));
+ assertEquals(CSVParser.TT_TOKEN + ";a;", parser.testNextToken());
+ assertEquals(
+ CSVParser.TT_EORECORD + ";b and ' more\n;",
+ parser.testNextToken());
+ }
+
+
+ // ======================================================
+ // parser tests
+ // ======================================================
+
+ String code =
+ "a,b,c,d\n"
+ + " a , b , 1 2 \n"
+ + "\"foo baar\", b,\n"
+ // + " \"foo\n,,\n\"\",,\n\\\"\",d,e\n";
+ + " \"foo\n,,\n\"\",,\n\"\"\",d,e\n"; // changed to use standard CSV escaping
String[][] res = {
- {"foo", "baar"},
- {""},
- {"hello", ""},
- {""},
- {"world", ""}
+ {"a", "b", "c", "d"},
+ {"a", "b", "1 2"},
+ {"foo baar", "b", ""},
+ {"foo\n,,\n\",,\n\"", "d", "e"}
};
- CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY);
- String[][] tmp = parser.getAllValues();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
- for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
- }
- }
-
- public void testEndOfFileBehaviourExcel() throws Exception {
- String[] codes = {
- "hello,\r\n\r\nworld,\r\n",
- "hello,\r\n\r\nworld,",
- "hello,\r\n\r\nworld,\"\"\r\n",
- "hello,\r\n\r\nworld,\"\"",
- "hello,\r\n\r\nworld,\n",
- "hello,\r\n\r\nworld,",
- "hello,\r\n\r\nworld,\"\"\n",
- "hello,\r\n\r\nworld,\"\""
+
+ public void testGetLine() throws IOException {
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String[] tmp = null;
+ for (int i = 0; i < res.length; i++) {
+ tmp = parser.getLine();
+ assertTrue(Arrays.equals(res[i], tmp));
+ }
+ tmp = parser.getLine();
+ assertTrue(tmp == null);
+ }
+
+ public void testNextValue() throws IOException {
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String tmp = null;
+ for (int i = 0; i < res.length; i++) {
+ for (int j = 0; j < res[i].length; j++) {
+ tmp = parser.nextValue();
+ assertEquals(res[i][j], tmp);
+ }
+ }
+ tmp = parser.nextValue();
+ assertTrue(tmp == null);
+ }
+
+ public void testGetAllValues() throws IOException {
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String[][] tmp = parser.getAllValues();
+ assertEquals(res.length, tmp.length);
+ assertTrue(tmp.length > 0);
+ for (int i = 0; i < res.length; i++) {
+ assertTrue(Arrays.equals(res[i], tmp[i]));
+ }
+ }
+
+ public void testExcelStrategy1() throws IOException {
+ String code =
+ "value1,value2,value3,value4\r\na,b,c,d\r\n x,,,"
+ + "\r\n\r\n\"\"\"hello\"\"\",\" \"\"world\"\"\",\"abc\ndef\",\r\n";
+ String[][] res = {
+ {"value1", "value2", "value3", "value4"},
+ {"a", "b", "c", "d"},
+ {" x", "", "", ""},
+ {""},
+ {"\"hello\"", " \"world\"", "abc\ndef", ""}
};
- String[][] res = {
- {"hello", ""},
- {""}, // ExcelStrategy does not ignore empty lines
- {"world", ""}
- };
- String code;
- for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
- code = codes[codeIndex];
- CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY);
- String[][] tmp = parser.getAllValues();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
- for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
- }
- }
- }
-
- public void testEndOfFileBehaviorCSV() throws Exception {
- String[] codes = {
- "hello,\r\n\r\nworld,\r\n",
- "hello,\r\n\r\nworld,",
- "hello,\r\n\r\nworld,\"\"\r\n",
- "hello,\r\n\r\nworld,\"\"",
- "hello,\r\n\r\nworld,\n",
- "hello,\r\n\r\nworld,",
- "hello,\r\n\r\nworld,\"\"\n",
- "hello,\r\n\r\nworld,\"\""
+ CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY);
+ String[][] tmp = parser.getAllValues();
+ assertEquals(res.length, tmp.length);
+ assertTrue(tmp.length > 0);
+ for (int i = 0; i < res.length; i++) {
+ assertTrue(Arrays.equals(res[i], tmp[i]));
+ }
+ }
+
+ public void testExcelStrategy2() throws Exception {
+ String code = "foo,baar\r\n\r\nhello,\r\n\r\nworld,\r\n";
+ String[][] res = {
+ {"foo", "baar"},
+ {""},
+ {"hello", ""},
+ {""},
+ {"world", ""}
};
- String[][] res = {
- {"hello", ""}, // CSV Strategy ignores empty lines
- {"world", ""}
- };
- String code;
- for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
- code = codes[codeIndex];
- CSVParser parser = new CSVParser(new StringReader(code));
- String[][] tmp = parser.getAllValues();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
- for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
- }
- }
- }
-
- public void testEmptyLineBehaviourExcel() throws Exception {
- String[] codes = {
- "hello,\r\n\r\n\r\n",
- "hello,\n\n\n",
- "hello,\"\"\r\n\r\n\r\n",
- "hello,\"\"\n\n\n"
+ CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY);
+ String[][] tmp = parser.getAllValues();
+ assertEquals(res.length, tmp.length);
+ assertTrue(tmp.length > 0);
+ for (int i = 0; i < res.length; i++) {
+ assertTrue(Arrays.equals(res[i], tmp[i]));
+ }
+ }
+
+ public void testEndOfFileBehaviourExcel() throws Exception {
+ String[] codes = {
+ "hello,\r\n\r\nworld,\r\n",
+ "hello,\r\n\r\nworld,",
+ "hello,\r\n\r\nworld,\"\"\r\n",
+ "hello,\r\n\r\nworld,\"\"",
+ "hello,\r\n\r\nworld,\n",
+ "hello,\r\n\r\nworld,",
+ "hello,\r\n\r\nworld,\"\"\n",
+ "hello,\r\n\r\nworld,\"\""
};
- String[][] res = {
- {"hello", ""},
- {""}, // ExcelStrategy does not ignore empty lines
- {""}
- };
- String code;
- for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
- code = codes[codeIndex];
- CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY);
- String[][] tmp = parser.getAllValues();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
- for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
- }
- }
- }
-
- public void testEmptyLineBehaviourCSV() throws Exception {
- String[] codes = {
- "hello,\r\n\r\n\r\n",
- "hello,\n\n\n",
- "hello,\"\"\r\n\r\n\r\n",
- "hello,\"\"\n\n\n"
+ String[][] res = {
+ {"hello", ""},
+ {""}, // ExcelStrategy does not ignore empty lines
+ {"world", ""}
};
- String[][] res = {
- {"hello", ""} // CSV Strategy ignores empty lines
- };
- String code;
- for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
- code = codes[codeIndex];
- CSVParser parser = new CSVParser(new StringReader(code));
- String[][] tmp = parser.getAllValues();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
- for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
- }
- }
- }
-
- public void OLDtestBackslashEscaping() throws IOException {
- String code =
- "one,two,three\n"
- + "on\\\"e,two\n"
- + "on\"e,two\n"
- + "one,\"tw\\\"o\"\n"
- + "one,\"t\\,wo\"\n"
- + "one,two,\"th,ree\"\n"
- + "\"a\\\\\"\n"
- + "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 (",")
- };
- CSVParser parser = new CSVParser(new StringReader(code));
- String[][] tmp = parser.getAllValues();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
- for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
- }
- }
-
- public void testBackslashEscaping() throws IOException {
-
- // To avoid confusion over the need for escaping chars in java code,
- // We will test with a forward slash as the escape char, and a single
- // quote as the encapsulator.
+ String code;
+ for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
+ code = codes[codeIndex];
+ CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY);
+ String[][] tmp = parser.getAllValues();
+ assertEquals(res.length, tmp.length);
+ assertTrue(tmp.length > 0);
+ for (int i = 0; i < res.length; i++) {
+ assertTrue(Arrays.equals(res[i], tmp[i]));
+ }
+ }
+ }
- String code =
- "one,two,three\n" // 0
- + "'',''\n" // 1) empty encapsulators
- + "/',/'\n" // 2) single encapsulators
- + "'/'','/''\n" // 3) single encapsulators encapsulated via escape
- + "'''',''''\n" // 4) single encapsulators encapsulated via doubling
- + "/,,/,\n" // 5) separator escaped
- + "//,//\n" // 6) escape escaped
- + "'//','//'\n" // 7) escape escaped in encapsulation
- + " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
- + "9, /\n \n" // escaped newline
- + "";
- String[][] res = {
- { "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);
-
- CSVParser parser = new CSVParser(new StringReader(code), strategy);
- String[][] tmp = parser.getAllValues();
- assertTrue(tmp.length > 0);
- for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
- }
- }
-
- public void testBackslashEscaping2() throws IOException {
-
- // To avoid confusion over the need for escaping chars in java code,
- // We will test with a forward slash as the escape char, and a single
- // quote as the encapsulator.
-
- String code = ""
- + " , , \n" // 1)
- + " \t , , \n" // 2)
- + " // , /, , /,\n" // 3)
- + "";
- String[][] res = {
- { " ", " ", " " }, // 1
- { " \t ", " ", " " }, // 2
- { " / ", " , ", " ," }, //3
- };
+ public void testEndOfFileBehaviorCSV() throws Exception {
+ String[] codes = {
+ "hello,\r\n\r\nworld,\r\n",
+ "hello,\r\n\r\nworld,",
+ "hello,\r\n\r\nworld,\"\"\r\n",
+ "hello,\r\n\r\nworld,\"\"",
+ "hello,\r\n\r\nworld,\n",
+ "hello,\r\n\r\nworld,",
+ "hello,\r\n\r\nworld,\"\"\n",
+ "hello,\r\n\r\nworld,\"\""
+ };
+ String[][] res = {
+ {"hello", ""}, // CSV Strategy ignores empty lines
+ {"world", ""}
+ };
+ String code;
+ for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
+ code = codes[codeIndex];
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String[][] tmp = parser.getAllValues();
+ assertEquals(res.length, tmp.length);
+ assertTrue(tmp.length > 0);
+ for (int i = 0; i < res.length; i++) {
+ assertTrue(Arrays.equals(res[i], tmp[i]));
+ }
+ }
+ }
+ public void testEmptyLineBehaviourExcel() throws Exception {
+ String[] codes = {
+ "hello,\r\n\r\n\r\n",
+ "hello,\n\n\n",
+ "hello,\"\"\r\n\r\n\r\n",
+ "hello,\"\"\n\n\n"
+ };
+ String[][] res = {
+ {"hello", ""},
+ {""}, // ExcelStrategy does not ignore empty lines
+ {""}
+ };
+ String code;
+ for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
+ code = codes[codeIndex];
+ CSVParser parser = new CSVParser(new StringReader(code), CSVStrategy.EXCEL_STRATEGY);
+ String[][] tmp = parser.getAllValues();
+ assertEquals(res.length, tmp.length);
+ assertTrue(tmp.length > 0);
+ for (int i = 0; i < res.length; i++) {
+ assertTrue(Arrays.equals(res[i], tmp[i]));
+ }
+ }
+ }
+
+ public void testEmptyLineBehaviourCSV() throws Exception {
+ String[] codes = {
+ "hello,\r\n\r\n\r\n",
+ "hello,\n\n\n",
+ "hello,\"\"\r\n\r\n\r\n",
+ "hello,\"\"\n\n\n"
+ };
+ String[][] res = {
+ {"hello", ""} // CSV Strategy ignores empty lines
+ };
+ String code;
+ for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
+ code = codes[codeIndex];
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String[][] tmp = parser.getAllValues();
+ assertEquals(res.length, tmp.length);
+ assertTrue(tmp.length > 0);
+ for (int i = 0; i < res.length; i++) {
+ assertTrue(Arrays.equals(res[i], tmp[i]));
+ }
+ }
+ }
- CSVStrategy strategy = new CSVStrategy(',',CSVStrategy.ENCAPSULATOR_DISABLED,CSVStrategy.COMMENTS_DISABLED,'/',false,false,true,true);
+ public void OLDtestBackslashEscaping() throws IOException {
+ String code =
+ "one,two,three\n"
+ + "on\\\"e,two\n"
+ + "on\"e,two\n"
+ + "one,\"tw\\\"o\"\n"
+ + "one,\"t\\,wo\"\n"
+ + "one,two,\"th,ree\"\n"
+ + "\"a\\\\\"\n"
+ + "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 (",")
+ };
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String[][] tmp = parser.getAllValues();
+ assertEquals(res.length, tmp.length);
+ assertTrue(tmp.length > 0);
+ for (int i = 0; i < res.length; i++) {
+ assertTrue(Arrays.equals(res[i], tmp[i]));
+ }
+ }
- CSVParser parser = new CSVParser(new StringReader(code), strategy);
- String[][] tmp = parser.getAllValues();
- assertTrue(tmp.length > 0);
+ public void testBackslashEscaping() throws IOException {
- if (!CSVPrinterTest.equals(res, tmp)) {
- assertTrue(false);
+ // To avoid confusion over the need for escaping chars in java code,
+ // We will test with a forward slash as the escape char, and a single
+ // quote as the encapsulator.
+
+ String code =
+ "one,two,three\n" // 0
+ + "'',''\n" // 1) empty encapsulators
+ + "/',/'\n" // 2) single encapsulators
+ + "'/'','/''\n" // 3) single encapsulators encapsulated via escape
+ + "'''',''''\n" // 4) single encapsulators encapsulated via doubling
+ + "/,,/,\n" // 5) separator escaped
+ + "//,//\n" // 6) escape escaped
+ + "'//','//'\n" // 7) escape escaped in encapsulation
+ + " 8 , \"quoted \"\" /\" // string\" \n" // don't eat spaces
+ + "9, /\n \n" // escaped newline
+ + "";
+ String[][] res = {
+ {"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);
+
+ CSVParser parser = new CSVParser(new StringReader(code), strategy);
+ String[][] tmp = parser.getAllValues();
+ assertTrue(tmp.length > 0);
+ for (int i = 0; i < res.length; i++) {
+ assertTrue(Arrays.equals(res[i], tmp[i]));
+ }
}
- }
+ public void testBackslashEscaping2() throws IOException {
+ // To avoid confusion over the need for escaping chars in java code,
+ // We will test with a forward slash as the escape char, and a single
+ // quote as the encapsulator.
+
+ String code = ""
+ + " , , \n" // 1)
+ + " \t , , \n" // 2)
+ + " // , /, , /,\n" // 3)
+ + "";
+ String[][] res = {
+ {" ", " ", " "}, // 1
+ {" \t ", " ", " "}, // 2
+ {" / ", " , ", " ,"}, //3
+ };
- public void testDefaultStrategy() throws IOException {
- String code = ""
- + "a,b\n" // 1)
- + "\"\n\",\" \"\n" // 2)
- + "\"\",#\n" // 2)
- ;
- String[][] res = {
- { "a", "b" },
- { "\n", " " },
- { "", "#" },
- };
+ CSVStrategy strategy = new CSVStrategy(',', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '/', false, false, true, true);
- CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
- assertEquals(CSVStrategy.COMMENTS_DISABLED, strategy.getCommentStart());
+ CSVParser parser = new CSVParser(new StringReader(code), strategy);
+ String[][] tmp = parser.getAllValues();
+ assertTrue(tmp.length > 0);
- CSVParser parser = new CSVParser(new StringReader(code), strategy);
- String[][] tmp = parser.getAllValues();
- assertTrue(tmp.length > 0);
+ if (!CSVPrinterTest.equals(res, tmp)) {
+ assertTrue(false);
+ }
- if (!CSVPrinterTest.equals(res, tmp)) {
- assertTrue(false);
}
- String[][] res_comments = {
- { "a", "b" },
- { "\n", " " },
- { ""},
- };
- strategy = new CSVStrategy(',','"','#');
- parser = new CSVParser(new StringReader(code), strategy);
- tmp = parser.getAllValues();
+ public void testDefaultStrategy() throws IOException {
+
+ String code = ""
+ + "a,b\n" // 1)
+ + "\"\n\",\" \"\n" // 2)
+ + "\"\",#\n" // 2)
+ ;
+ String[][] res = {
+ {"a", "b"},
+ {"\n", " "},
+ {"", "#"},
+ };
- if (!CSVPrinterTest.equals(res_comments, tmp)) {
- assertTrue(false);
+ CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
+ assertEquals(CSVStrategy.COMMENTS_DISABLED, strategy.getCommentStart());
+
+ CSVParser parser = new CSVParser(new StringReader(code), strategy);
+ String[][] tmp = parser.getAllValues();
+ assertTrue(tmp.length > 0);
+
+ if (!CSVPrinterTest.equals(res, tmp)) {
+ assertTrue(false);
+ }
+
+ String[][] res_comments = {
+ {"a", "b"},
+ {"\n", " "},
+ {""},
+ };
+
+ strategy = new CSVStrategy(',', '"', '#');
+ parser = new CSVParser(new StringReader(code), strategy);
+ tmp = parser.getAllValues();
+
+ if (!CSVPrinterTest.equals(res_comments, tmp)) {
+ assertTrue(false);
+ }
}
- }
public void testUnicodeEscape() throws IOException {
- String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
- CSVParser parser = new CSVParser(new StringReader(code));
- parser.getStrategy().setUnicodeEscapeInterpretation(true);
- String[] data = parser.getLine();
- assertEquals(2, data.length);
- assertEquals("abc", data[0]);
- assertEquals("public", data[1]);
+ String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
+ CSVParser parser = new CSVParser(new StringReader(code));
+ parser.getStrategy().setUnicodeEscapeInterpretation(true);
+ String[] data = parser.getLine();
+ assertEquals(2, data.length);
+ assertEquals("abc", data[0]);
+ assertEquals("public", data[1]);
}
-
+
public void testCarriageReturnLineFeedEndings() throws IOException {
- String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
- CSVParser parser = new CSVParser(new StringReader(code));
- String[][] data = parser.getAllValues();
- assertEquals(4, data.length);
+ String code = "foo\r\nbaar,\r\nhello,world\r\n,kanu";
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String[][] data = parser.getAllValues();
+ assertEquals(4, data.length);
}
public void testCarriageReturnEndings() throws IOException {
- String code = "foo\rbaar,\rhello,world\r,kanu";
- CSVParser parser = new CSVParser(new StringReader(code));
- String[][] data = parser.getAllValues();
- assertEquals(4, data.length);
+ String code = "foo\rbaar,\rhello,world\r,kanu";
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String[][] data = parser.getAllValues();
+ assertEquals(4, data.length);
}
public void testLineFeedEndings() throws IOException {
- String code = "foo\nbaar,\nhello,world\n,kanu";
- CSVParser parser = new CSVParser(new StringReader(code));
- String[][] data = parser.getAllValues();
- assertEquals(4, data.length);
+ String code = "foo\nbaar,\nhello,world\n,kanu";
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String[][] data = parser.getAllValues();
+ assertEquals(4, data.length);
}
public void testIgnoreEmptyLines() throws IOException {
- String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
- //String code = "world\r\n\n";
- //String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
- CSVParser parser = new CSVParser(new StringReader(code));
- String[][] data = parser.getAllValues();
- assertEquals(3, data.length);
+ String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
+ //String code = "world\r\n\n";
+ //String code = "foo;baar\r\n\r\nhello;\r\n\r\nworld;\r\n";
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String[][] data = parser.getAllValues();
+ assertEquals(3, data.length);
}
-
+
public void testLineTokenConsistency() throws IOException {
- String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
- CSVParser parser = new CSVParser(new StringReader(code));
- String[][] data = parser.getAllValues();
- parser = new CSVParser(new StringReader(code));
- CSVParser parser1 = new CSVParser(new StringReader(code));
- for (int i = 0; i < data.length; i++) {
- assertTrue(Arrays.equals(parser1.getLine(), data[i]));
- for (int j = 0; j < data[i].length; j++) {
- assertEquals(parser.nextValue(), data[i][j]);
+ String code = "\nfoo,baar\n\r\n,\n\n,world\r\n\n";
+ CSVParser parser = new CSVParser(new StringReader(code));
+ String[][] data = parser.getAllValues();
+ parser = new CSVParser(new StringReader(code));
+ CSVParser parser1 = new CSVParser(new StringReader(code));
+ for (int i = 0; i < data.length; i++) {
+ assertTrue(Arrays.equals(parser1.getLine(), data[i]));
+ for (int j = 0; j < data[i].length; j++) {
+ assertEquals(parser.nextValue(), data[i][j]);
+ }
}
- }
}
// From SANDBOX-153
- public void testDelimiterIsWhitespace() throws IOException {
- String code = "one\ttwo\t\tfour \t five\t six";
- TestCSVParser parser = new TestCSVParser(new StringReader(code), CSVStrategy.TDF_STRATEGY);
- assertEquals(CSVParser.TT_TOKEN + ";one;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";two;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";four;", parser.testNextToken());
- assertEquals(CSVParser.TT_TOKEN + ";five;", parser.testNextToken());
- assertEquals(CSVParser.TT_EOF + ";six;", parser.testNextToken());
- }
+ public void testDelimiterIsWhitespace() throws IOException {
+ String code = "one\ttwo\t\tfour \t five\t six";
+ TestCSVParser parser = new TestCSVParser(new StringReader(code), CSVStrategy.TDF_STRATEGY);
+ assertEquals(CSVParser.TT_TOKEN + ";one;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";two;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";four;", parser.testNextToken());
+ assertEquals(CSVParser.TT_TOKEN + ";five;", parser.testNextToken());
+ assertEquals(CSVParser.TT_EOF + ";six;", parser.testNextToken());
+ }
}
Modified: commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java?rev=1065950&r1=1065949&r2=1065950&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java (original)
+++ commons/sandbox/csv/trunk/src/test/org/apache/commons/csv/CSVPrinterTest.java Tue Feb 1 08:46:00 2011
@@ -30,200 +30,219 @@ import junit.framework.TestSuite;
* CSVPrinterTest
*/
public class CSVPrinterTest extends TestCase {
-
- String lineSeparator = "\n";
- public void testPrinter1() throws IOException {
- StringWriter sw = new StringWriter();
- CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
- String[] line1 = {"a", "b"};
- printer.println(line1);
- assertEquals("a,b" + lineSeparator, sw.toString());
- }
-
- public void testPrinter2() throws IOException {
- StringWriter sw = new StringWriter();
- CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
- String[] line1 = {"a,b", "b"};
- printer.println(line1);
- assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
- }
-
- public void testPrinter3() throws IOException {
- StringWriter sw = new StringWriter();
- CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
- String[] line1 = {"a, b", "b "};
- printer.println(line1);
- assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
- }
-
- public void testPrinter4() throws IOException {
- StringWriter sw = new StringWriter();
- CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
- String[] line1 = {"a", "b\"c"};
- printer.println(line1);
- assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
- }
-
- public void testPrinter5() throws IOException {
- StringWriter sw = new StringWriter();
- CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
- String[] line1 = {"a", "b\nc"};
- printer.println(line1);
- assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
- }
-
- public void testPrinter6() throws IOException {
- StringWriter sw = new StringWriter();
- CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
- String[] line1 = {"a", "b\r\nc"};
- printer.println(line1);
- assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
- }
-
- public void testPrinter7() throws IOException {
- StringWriter sw = new StringWriter();
- CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
- String[] line1 = {"a", "b\\c"};
- printer.println(line1);
- assertEquals("a,b\\c" + lineSeparator, sw.toString());
- }
-
- public void testExcelPrinter1() throws IOException {
- StringWriter sw = new StringWriter();
- CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY);
- String[] line1 = {"a", "b"};
- printer.println(line1);
- assertEquals("a,b" + lineSeparator, sw.toString());
- }
-
- public void testExcelPrinter2() throws IOException {
- StringWriter sw = new StringWriter();
- CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY);
- String[] line1 = {"a,b", "b"};
- printer.println(line1);
- assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
- }
-
-
-
- public void testRandom() throws Exception {
- int iter=10000;
- strategy = CSVStrategy.DEFAULT_STRATEGY;
- doRandom(iter);
- strategy = CSVStrategy.EXCEL_STRATEGY;
- doRandom(iter);
-
- // Strategy for MySQL
- strategy = new CSVStrategy('\t', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED,'\\',false, false, false, false);
- doRandom(iter);
- }
-
- Random r = new Random();
- CSVStrategy strategy;
-
- public void doRandom(int iter) throws Exception {
- for (int i=0; i<iter; i++) {
- doOneRandom();
- }
- }
-
- public void doOneRandom() throws Exception {
- int nLines = r.nextInt(4)+1;
- int nCol = r.nextInt(3)+1;
- // nLines=1;nCol=2;
- String[][] lines = new String[nLines][];
- for (int i=0; i<nLines; i++) {
- String[] line = new String[nCol];
- lines[i] = line;
- for (int j=0; j<nCol; j++) {
- line[j] = randStr();
- }
- }
-
- StringWriter sw = new StringWriter();
- CSVPrinter printer = new CSVPrinter(sw, strategy);
-
- for (int i=0; i<nLines; i++) {
- // for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
- printer.println(lines[i]);
- }
-
- printer.flush();
- String result = sw.toString();
- // System.out.println("### :" + printable(result));
-
- StringReader reader = new StringReader(result);
-
- CSVParser parser = new CSVParser(reader, strategy);
- String[][] parseResult = parser.getAllValues();
-
- if (!equals(lines, parseResult)) {
- System.out.println("Printer output :" + printable(result));
- assertTrue(false);
- }
- }
-
- public static boolean equals(String[][] a, String[][] b) {
- if (a.length != b.length) {
- return false;
- }
- for (int i=0; i<a.length; i++) {
- String[] linea = a[i];
- String[] lineb = b[i];
- if (linea.length != lineb.length) {
- return false;
- }
- for (int j=0; j<linea.length; j++) {
- String aval = linea[j];
- String bval = lineb[j];
- if (!aval.equals(bval)) {
- System.out.println("expected :" + printable(aval));
- System.out.println("got :" + printable(bval));
- return false;
- }
- }
- }
- return true;
- }
-
- public static String printable(String s) {
- StringBuffer sb = new StringBuffer();
- for (int i=0; i<s.length(); i++) {
- char ch = s.charAt(i);
- if (ch<=' ' || ch>=128) {
- sb.append("(" + (int)ch + ")");
- } else {
- sb.append(ch);
- }
- }
- return sb.toString();
- }
-
- public String randStr() {
- int sz = r.nextInt(20);
- // sz = r.nextInt(3);
- char[] buf = new char[sz];
- for (int i=0; i<sz; i++) {
- // stick in special chars with greater frequency
- char ch;
- int what = r.nextInt(20);
- switch (what) {
- case 0: ch = '\r'; break;
- case 1: ch = '\n'; break;
- case 2: ch = '\t'; break;
- case 3: ch = '\f'; break;
- case 4: ch = ' '; break;
- case 5: ch = ','; break;
- case 6: ch = '"'; break;
- case 7: ch = '\''; break;
- case 8: ch = '\\'; break;
- default: ch = (char)r.nextInt(300); break;
- // default: ch = 'a'; break;
- }
- buf[i] = ch;
+ String lineSeparator = "\n";
+
+ public void testPrinter1() throws IOException {
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
+ String[] line1 = {"a", "b"};
+ printer.println(line1);
+ assertEquals("a,b" + lineSeparator, sw.toString());
+ }
+
+ public void testPrinter2() throws IOException {
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
+ String[] line1 = {"a,b", "b"};
+ printer.println(line1);
+ assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
+ }
+
+ public void testPrinter3() throws IOException {
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
+ String[] line1 = {"a, b", "b "};
+ printer.println(line1);
+ assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
+ }
+
+ public void testPrinter4() throws IOException {
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
+ String[] line1 = {"a", "b\"c"};
+ printer.println(line1);
+ assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
+ }
+
+ public void testPrinter5() throws IOException {
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
+ String[] line1 = {"a", "b\nc"};
+ printer.println(line1);
+ assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
+ }
+
+ public void testPrinter6() throws IOException {
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
+ String[] line1 = {"a", "b\r\nc"};
+ printer.println(line1);
+ assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
+ }
+
+ public void testPrinter7() throws IOException {
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.DEFAULT_STRATEGY);
+ String[] line1 = {"a", "b\\c"};
+ printer.println(line1);
+ assertEquals("a,b\\c" + lineSeparator, sw.toString());
+ }
+
+ public void testExcelPrinter1() throws IOException {
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY);
+ String[] line1 = {"a", "b"};
+ printer.println(line1);
+ assertEquals("a,b" + lineSeparator, sw.toString());
+ }
+
+ public void testExcelPrinter2() throws IOException {
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, CSVStrategy.EXCEL_STRATEGY);
+ String[] line1 = {"a,b", "b"};
+ printer.println(line1);
+ assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
+ }
+
+
+ public void testRandom() throws Exception {
+ int iter = 10000;
+ strategy = CSVStrategy.DEFAULT_STRATEGY;
+ doRandom(iter);
+ strategy = CSVStrategy.EXCEL_STRATEGY;
+ doRandom(iter);
+
+ // Strategy for MySQL
+ strategy = new CSVStrategy('\t', CSVStrategy.ENCAPSULATOR_DISABLED, CSVStrategy.COMMENTS_DISABLED, '\\', false, false, false, false);
+ doRandom(iter);
+ }
+
+ Random r = new Random();
+ CSVStrategy strategy;
+
+ public void doRandom(int iter) throws Exception {
+ for (int i = 0; i < iter; i++) {
+ doOneRandom();
+ }
+ }
+
+ public void doOneRandom() throws Exception {
+ int nLines = r.nextInt(4) + 1;
+ int nCol = r.nextInt(3) + 1;
+ // nLines=1;nCol=2;
+ String[][] lines = new String[nLines][];
+ for (int i = 0; i < nLines; i++) {
+ String[] line = new String[nCol];
+ lines[i] = line;
+ for (int j = 0; j < nCol; j++) {
+ line[j] = randStr();
+ }
+ }
+
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, strategy);
+
+ for (int i = 0; i < nLines; i++) {
+ // for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
+ printer.println(lines[i]);
+ }
+
+ printer.flush();
+ String result = sw.toString();
+ // System.out.println("### :" + printable(result));
+
+ StringReader reader = new StringReader(result);
+
+ CSVParser parser = new CSVParser(reader, strategy);
+ String[][] parseResult = parser.getAllValues();
+
+ if (!equals(lines, parseResult)) {
+ System.out.println("Printer output :" + printable(result));
+ assertTrue(false);
+ }
+ }
+
+ public static boolean equals(String[][] a, String[][] b) {
+ if (a.length != b.length) {
+ return false;
+ }
+ for (int i = 0; i < a.length; i++) {
+ String[] linea = a[i];
+ String[] lineb = b[i];
+ if (linea.length != lineb.length) {
+ return false;
+ }
+ for (int j = 0; j < linea.length; j++) {
+ String aval = linea[j];
+ String bval = lineb[j];
+ if (!aval.equals(bval)) {
+ System.out.println("expected :" + printable(aval));
+ System.out.println("got :" + printable(bval));
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ public static String printable(String s) {
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < s.length(); i++) {
+ char ch = s.charAt(i);
+ if (ch <= ' ' || ch >= 128) {
+ sb.append("(" + (int) ch + ")");
+ } else {
+ sb.append(ch);
+ }
+ }
+ return sb.toString();
+ }
+
+ public String randStr() {
+ int sz = r.nextInt(20);
+ // sz = r.nextInt(3);
+ char[] buf = new char[sz];
+ for (int i = 0; i < sz; i++) {
+ // stick in special chars with greater frequency
+ char ch;
+ int what = r.nextInt(20);
+ switch (what) {
+ case 0:
+ ch = '\r';
+ break;
+ case 1:
+ ch = '\n';
+ break;
+ case 2:
+ ch = '\t';
+ break;
+ case 3:
+ ch = '\f';
+ break;
+ case 4:
+ ch = ' ';
+ break;
+ case 5:
+ ch = ',';
+ break;
+ case 6:
+ ch = '"';
+ break;
+ case 7:
+ ch = '\'';
+ break;
+ case 8:
+ ch = '\\';
+ break;
+ default:
+ ch = (char) r.nextInt(300);
+ break;
+ // default: ch = 'a'; break;
+ }
+ buf[i] = ch;
+ }
+ return new String(buf);
}
- return new String(buf);
- }
}