Set EOL-style to native.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk@493488 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dennis Lundberg 2007-01-06 15:40:40 +00:00
parent e2f0367ba6
commit 1c642fc7ec
14 changed files with 1576 additions and 1576 deletions

142
pom.xml
View File

@ -1,71 +1,71 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.commons</groupId>
<artifactId>commons-sandbox</artifactId>
<version>1-SNAPSHOT</version>
</parent>
<artifactId>commons-csv</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Commons CSV</name>
<url>http://jakarta.apache.org/commons/sandbox/csv/</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<developers>
<developer>
<id>bayard</id>
<name>Henri Yandell</name>
<email>bayard AT apache DOT org</email>
<organization>Apache</organization>
<timezone>-5</timezone>
</developer>
</developers>
<contributors>
</contributors>
<scm>
<connection>scm:svn:http://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk</connection>
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk</developerConnection>
<url>http://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk</url>
</scm>
<distributionManagement>
<site>
<id>website</id>
<name>Apache Website</name>
<url>scp://people.apache.org/www/jakarta.apache.org/commons/sandbox/csv/</url>
</site>
</distributionManagement>
<build>
<sourceDirectory>src/java</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-changes-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<report>changes-report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.commons</groupId>
<artifactId>commons-sandbox</artifactId>
<version>1-SNAPSHOT</version>
</parent>
<artifactId>commons-csv</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Commons CSV</name>
<url>http://jakarta.apache.org/commons/sandbox/csv/</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<developers>
<developer>
<id>bayard</id>
<name>Henri Yandell</name>
<email>bayard AT apache DOT org</email>
<organization>Apache</organization>
<timezone>-5</timezone>
</developer>
</developers>
<contributors>
</contributors>
<scm>
<connection>scm:svn:http://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk</connection>
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk</developerConnection>
<url>http://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk</url>
</scm>
<distributionManagement>
<site>
<id>website</id>
<name>Apache Website</name>
<url>scp://people.apache.org/www/jakarta.apache.org/commons/sandbox/csv/</url>
</site>
</distributionManagement>
<build>
<sourceDirectory>src/java</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-changes-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<report>changes-report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>

View File

@ -1,191 +1,191 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv;
/**
* A simple StringBuffer replacement that aims to
* reduce copying as much as possible. The buffer
* grows as necessary.
* This class is not thread safe.
*
* @author Ortwin Glück
*/
public class CharBuffer {
private char[] c;
/**
* Actually used number of characters in the array.
* It is also the index at which
* a new character will be inserted into <code>c</code>.
*/
private int length;
/**
* Creates a new CharBuffer with an initial capacity of 32 characters.
*/
public CharBuffer() {
this(32);
}
/**
* Creates a new CharBuffer with an initial capacity
* of <code>length</code> characters.
*/
public CharBuffer(final int length) {
if (length == 0) throw new IllegalArgumentException("Can't create an empty CharBuffer");
this.c = new char[length];
}
/**
* Empties the buffer. The capacity still remains the same, so no memory is freed.
*/
public void clear() {
length = 0;
}
/**
* Returns the number of characters in the buffer.
* @return the number of characters
*/
public int length() {
return length;
}
/**
* Returns the current capacity of the buffer.
* @return the maximum number of characters that can be stored in this buffer without
* resizing it.
*/
public int capacity() {
return c.length;
}
/**
* Appends the contents of <code>cb</code> to the end of this CharBuffer.
* @param cb the CharBuffer to append or null
*/
public void append(final CharBuffer cb) {
if (cb == null) return;
provideCapacity(length + cb.length);
System.arraycopy(cb.c, 0, c, length, cb.length);
length += cb.length;
}
/**
* Appends <code>s</code> to the end of this CharBuffer.
* This method involves copying the new data once!
* @param s the String to append or null
*/
public void append(final String s) {
if (s == null) return;
append(s.toCharArray());
}
/**
* Appends <code>sb</code> to the end of this CharBuffer.
* This method involves copying the new data once!
* @param sb the StringBuffer to append or null
*/
public void append(final StringBuffer sb) {
if (sb == null) return;
provideCapacity(length + sb.length());
sb.getChars(0, sb.length(), c, length);
length += sb.length();
}
/**
* Appends <code>data</code> to the end of this CharBuffer.
* This method involves copying the new data once!
* @param data the char[] to append or null
*/
public void append(final char[] data) {
if (data == null) return;
provideCapacity(length + data.length);
System.arraycopy(data, 0, c, length, data.length);
length += data.length;
}
/**
* Appends a single character to the end of this CharBuffer.
* This method involves copying the new data once!
* @param data the char to append
*/
public void append(final char data) {
provideCapacity(length + 1);
c[length] = data;
length++;
}
/**
* Shrinks the capacity of the buffer to the current length if necessary.
* This method involves copying the data once!
*/
public void shrink() {
if (c.length == length) return;
char[] newc = new char[length];
System.arraycopy(c, 0, newc, 0, length);
c = newc;
}
/**
* Returns the contents of the buffer as a char[]. The returned array may
* be the internal array of the buffer, so the caller must take care when
* modifying it.
* This method allows to avoid copying if the caller knows the exact capacity
* before.
* @return
*/
public char[] getCharacters() {
if (c.length == length) return c;
char[] chars = new char[length];
System.arraycopy(c, 0, chars, 0, length);
return chars;
}
/**
* Converts the contents of the buffer into a StringBuffer.
* This method involves copying the new data once!
* @return
*/
public StringBuffer toStringBuffer() {
StringBuffer sb = new StringBuffer(length);
sb.append(c, 0, length);
return sb;
}
/**
* Converts the contents of the buffer into a StringBuffer.
* This method involves copying the new data once!
* @return
*/
public String toString() {
return new String(c, 0, length);
}
/**
* Copies the data into a new array of at least <code>capacity</code> size.
* @param capacity
*/
public void provideCapacity(final int capacity) {
if (c.length >= capacity) return;
int newcapacity = capacity;
char[] newc = new char[newcapacity];
System.arraycopy(c, 0, newc, 0, length);
c = newc;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv;
/**
* A simple StringBuffer replacement that aims to
* reduce copying as much as possible. The buffer
* grows as necessary.
* This class is not thread safe.
*
* @author Ortwin Glück
*/
public class CharBuffer {
private char[] c;
/**
* Actually used number of characters in the array.
* It is also the index at which
* a new character will be inserted into <code>c</code>.
*/
private int length;
/**
* Creates a new CharBuffer with an initial capacity of 32 characters.
*/
public CharBuffer() {
this(32);
}
/**
* Creates a new CharBuffer with an initial capacity
* of <code>length</code> characters.
*/
public CharBuffer(final int length) {
if (length == 0) throw new IllegalArgumentException("Can't create an empty CharBuffer");
this.c = new char[length];
}
/**
* Empties the buffer. The capacity still remains the same, so no memory is freed.
*/
public void clear() {
length = 0;
}
/**
* Returns the number of characters in the buffer.
* @return the number of characters
*/
public int length() {
return length;
}
/**
* Returns the current capacity of the buffer.
* @return the maximum number of characters that can be stored in this buffer without
* resizing it.
*/
public int capacity() {
return c.length;
}
/**
* Appends the contents of <code>cb</code> to the end of this CharBuffer.
* @param cb the CharBuffer to append or null
*/
public void append(final CharBuffer cb) {
if (cb == null) return;
provideCapacity(length + cb.length);
System.arraycopy(cb.c, 0, c, length, cb.length);
length += cb.length;
}
/**
* Appends <code>s</code> to the end of this CharBuffer.
* This method involves copying the new data once!
* @param s the String to append or null
*/
public void append(final String s) {
if (s == null) return;
append(s.toCharArray());
}
/**
* Appends <code>sb</code> to the end of this CharBuffer.
* This method involves copying the new data once!
* @param sb the StringBuffer to append or null
*/
public void append(final StringBuffer sb) {
if (sb == null) return;
provideCapacity(length + sb.length());
sb.getChars(0, sb.length(), c, length);
length += sb.length();
}
/**
* Appends <code>data</code> to the end of this CharBuffer.
* This method involves copying the new data once!
* @param data the char[] to append or null
*/
public void append(final char[] data) {
if (data == null) return;
provideCapacity(length + data.length);
System.arraycopy(data, 0, c, length, data.length);
length += data.length;
}
/**
* Appends a single character to the end of this CharBuffer.
* This method involves copying the new data once!
* @param data the char to append
*/
public void append(final char data) {
provideCapacity(length + 1);
c[length] = data;
length++;
}
/**
* Shrinks the capacity of the buffer to the current length if necessary.
* This method involves copying the data once!
*/
public void shrink() {
if (c.length == length) return;
char[] newc = new char[length];
System.arraycopy(c, 0, newc, 0, length);
c = newc;
}
/**
* Returns the contents of the buffer as a char[]. The returned array may
* be the internal array of the buffer, so the caller must take care when
* modifying it.
* This method allows to avoid copying if the caller knows the exact capacity
* before.
* @return
*/
public char[] getCharacters() {
if (c.length == length) return c;
char[] chars = new char[length];
System.arraycopy(c, 0, chars, 0, length);
return chars;
}
/**
* Converts the contents of the buffer into a StringBuffer.
* This method involves copying the new data once!
* @return
*/
public StringBuffer toStringBuffer() {
StringBuffer sb = new StringBuffer(length);
sb.append(c, 0, length);
return sb;
}
/**
* Converts the contents of the buffer into a StringBuffer.
* This method involves copying the new data once!
* @return
*/
public String toString() {
return new String(c, 0, length);
}
/**
* Copies the data into a new array of at least <code>capacity</code> size.
* @param capacity
*/
public void provideCapacity(final int capacity) {
if (c.length >= capacity) return;
int newcapacity = capacity;
char[] newc = new char[newcapacity];
System.arraycopy(c, 0, newc, 0, length);
c = newc;
}
}

View File

@ -1,287 +1,287 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* The CSVConfig is used to configure the CSV writer
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVConfig {
/** specifies if it is a fixed width csv file **/
private boolean fixedWidth;
/** list of fields **/
private List fields;
/** Do no do any filling **/
public static final int FILLNONE = 0;
/** Fill content the the left. Mainly usable together with fixedWidth **/
public static final int FILLLEFT = 1;
/** Fill content to the right. Mainly usable together with fixedWidth **/
public static final int FILLRIGHT = 2;
/** The fill pattern */
private int fill;
/** The fill char. Defaults to a space */
private char fillChar = ' ';
/** The seperator character. Defaults to , */
private char delimiter = ',';
/** Should we ignore the delimiter. Defaults to false */
private boolean ignoreDelimiter = false;
/** the value delimiter. Defaults to " */
private char valueDelimiter = '"';
/** Should we ignore the value delimiter. Defaults to true */
private boolean ignoreValueDelimiter = true;
/** Specifies if we want to use a field header */
private boolean fieldHeader = false;
/** Specifies if the end of the line needs to be trimmed */
private boolean endTrimmed = false;
/**
*
*/
public CSVConfig() {
super();
}
/**
* @return if the CSV file is fixedWidth
*/
public boolean isFixedWidth() {
return fixedWidth;
}
/**
* Specify if the CSV file is fixed width.
* Defaults to false
* @param fixedWidth the fixedwidth
*/
public void setFixedWidth(boolean fixedWidth) {
this.fixedWidth = fixedWidth;
}
public void addField(CSVField field) {
if (fields == null) {
fields = new ArrayList();
}
fields.add(field);
}
/**
* Set the fields that should be used by the writer.
* This will overwrite currently added fields completely!
* @param csvFields the csvfields array. If null it will do nothing
*/
public void setFields(CSVField[] csvFields) {
if (csvFields == null) {
return;
}
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) {
if (csvField == null) {
return;
}
fields = new ArrayList(csvField);
}
/**
* @return an array with the known fields (even if no fields are specified)
*/
public CSVField[] getFields() {
CSVField[] csvFields = new CSVField[0];
if (fields != null) {
return (CSVField[]) fields.toArray(csvFields);
}
return csvFields;
}
public CSVField getField(String name) {
if (fields == null || name == null) {
return null;
}
for(int i = 0; i < fields.size(); i++) {
CSVField field = (CSVField) fields.get(i);
if (name.equals(field.getName())) {
return field;
}
}
return null;
}
/**
* @return the fill pattern.
*/
public int getFill() {
return fill;
}
/**
* 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) {
this.fill = fill;
}
/**
*
* @return the fillchar. Defaults to a space.
*/
public char getFillChar() {
return fillChar;
}
/**
* Set the fill char
* @param fillChar the fill char
*/
public void setFillChar(char fillChar) {
this.fillChar = fillChar;
}
/**
* @return the delimeter used.
*/
public char getDelimiter() {
return delimiter;
}
/**
* Set the delimiter to use
* @param delimiter the delimiter character.
*/
public void setDelimiter(char delimiter) {
this.delimiter = delimiter;
}
/**
* @return if the writer should ignore the delimiter character.
*/
public boolean isDelimiterIgnored() {
return ignoreDelimiter;
}
/**
* Specify if the writer should ignore the delimiter.
* @param ignoreDelimiter defaults to false.
*/
public void setIgnoreDelimiter(boolean ignoreDelimiter) {
this.ignoreDelimiter = ignoreDelimiter;
}
/**
* @return the value delimeter used. Defaults to "
*/
public char getValueDelimiter() {
return valueDelimiter;
}
/**
* Set the value delimiter to use
* @param valueDelimiter the value delimiter character.
*/
public void setValueDelimiter(char valueDelimiter) {
this.valueDelimiter = valueDelimiter;
}
/**
* @return if the writer should ignore the value delimiter character.
* Defaults to true.
*/
public boolean isValueDelimiterIgnored() {
return ignoreValueDelimiter;
}
/**
* Specify if the writer should ignore the value delimiter.
* @param ignoreValueDelimiter defaults to false.
*/
public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) {
this.ignoreValueDelimiter = ignoreValueDelimiter;
}
/**
* @return if a field header is used. Defaults to false
*/
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) {
if (obj == null && !(obj instanceof CSVConfig)) {
return false;
}
return super.equals(obj);
// CSVConfig config = (CSVConfig) obj;
// getFill() == config.getFill()
// getFields().equals(config.getFields())
}
/**
* 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.
*/
public static CSVConfig guessConfig(InputStream inputStream) {
return null;
}
/**
* @return if the end of the line should be trimmed. Default is false.
*/
public boolean isEndTrimmed() {
return endTrimmed;
}
/**
* Specify if the end of the line needs to be trimmed. Defaults to false.
* @param endTrimmed
*/
public void setEndTrimmed(boolean endTrimmed) {
this.endTrimmed = endTrimmed;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* The CSVConfig is used to configure the CSV writer
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVConfig {
/** specifies if it is a fixed width csv file **/
private boolean fixedWidth;
/** list of fields **/
private List fields;
/** Do no do any filling **/
public static final int FILLNONE = 0;
/** Fill content the the left. Mainly usable together with fixedWidth **/
public static final int FILLLEFT = 1;
/** Fill content to the right. Mainly usable together with fixedWidth **/
public static final int FILLRIGHT = 2;
/** The fill pattern */
private int fill;
/** The fill char. Defaults to a space */
private char fillChar = ' ';
/** The seperator character. Defaults to , */
private char delimiter = ',';
/** Should we ignore the delimiter. Defaults to false */
private boolean ignoreDelimiter = false;
/** the value delimiter. Defaults to " */
private char valueDelimiter = '"';
/** Should we ignore the value delimiter. Defaults to true */
private boolean ignoreValueDelimiter = true;
/** Specifies if we want to use a field header */
private boolean fieldHeader = false;
/** Specifies if the end of the line needs to be trimmed */
private boolean endTrimmed = false;
/**
*
*/
public CSVConfig() {
super();
}
/**
* @return if the CSV file is fixedWidth
*/
public boolean isFixedWidth() {
return fixedWidth;
}
/**
* Specify if the CSV file is fixed width.
* Defaults to false
* @param fixedWidth the fixedwidth
*/
public void setFixedWidth(boolean fixedWidth) {
this.fixedWidth = fixedWidth;
}
public void addField(CSVField field) {
if (fields == null) {
fields = new ArrayList();
}
fields.add(field);
}
/**
* Set the fields that should be used by the writer.
* This will overwrite currently added fields completely!
* @param csvFields the csvfields array. If null it will do nothing
*/
public void setFields(CSVField[] csvFields) {
if (csvFields == null) {
return;
}
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) {
if (csvField == null) {
return;
}
fields = new ArrayList(csvField);
}
/**
* @return an array with the known fields (even if no fields are specified)
*/
public CSVField[] getFields() {
CSVField[] csvFields = new CSVField[0];
if (fields != null) {
return (CSVField[]) fields.toArray(csvFields);
}
return csvFields;
}
public CSVField getField(String name) {
if (fields == null || name == null) {
return null;
}
for(int i = 0; i < fields.size(); i++) {
CSVField field = (CSVField) fields.get(i);
if (name.equals(field.getName())) {
return field;
}
}
return null;
}
/**
* @return the fill pattern.
*/
public int getFill() {
return fill;
}
/**
* 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) {
this.fill = fill;
}
/**
*
* @return the fillchar. Defaults to a space.
*/
public char getFillChar() {
return fillChar;
}
/**
* Set the fill char
* @param fillChar the fill char
*/
public void setFillChar(char fillChar) {
this.fillChar = fillChar;
}
/**
* @return the delimeter used.
*/
public char getDelimiter() {
return delimiter;
}
/**
* Set the delimiter to use
* @param delimiter the delimiter character.
*/
public void setDelimiter(char delimiter) {
this.delimiter = delimiter;
}
/**
* @return if the writer should ignore the delimiter character.
*/
public boolean isDelimiterIgnored() {
return ignoreDelimiter;
}
/**
* Specify if the writer should ignore the delimiter.
* @param ignoreDelimiter defaults to false.
*/
public void setIgnoreDelimiter(boolean ignoreDelimiter) {
this.ignoreDelimiter = ignoreDelimiter;
}
/**
* @return the value delimeter used. Defaults to "
*/
public char getValueDelimiter() {
return valueDelimiter;
}
/**
* Set the value delimiter to use
* @param valueDelimiter the value delimiter character.
*/
public void setValueDelimiter(char valueDelimiter) {
this.valueDelimiter = valueDelimiter;
}
/**
* @return if the writer should ignore the value delimiter character.
* Defaults to true.
*/
public boolean isValueDelimiterIgnored() {
return ignoreValueDelimiter;
}
/**
* Specify if the writer should ignore the value delimiter.
* @param ignoreValueDelimiter defaults to false.
*/
public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) {
this.ignoreValueDelimiter = ignoreValueDelimiter;
}
/**
* @return if a field header is used. Defaults to false
*/
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) {
if (obj == null && !(obj instanceof CSVConfig)) {
return false;
}
return super.equals(obj);
// CSVConfig config = (CSVConfig) obj;
// getFill() == config.getFill()
// getFields().equals(config.getFields())
}
/**
* 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.
*/
public static CSVConfig guessConfig(InputStream inputStream) {
return null;
}
/**
* @return if the end of the line should be trimmed. Default is false.
*/
public boolean isEndTrimmed() {
return endTrimmed;
}
/**
* Specify if the end of the line needs to be trimmed. Defaults to false.
* @param endTrimmed
*/
public void setEndTrimmed(boolean endTrimmed) {
this.endTrimmed = endTrimmed;
}
}

View File

@ -1,189 +1,189 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Tries to guess a config based on an InputStream.
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVConfigGuesser {
/** The stream to read */
private InputStream in;
/**
* if the file has a field header (need this info, to be able to guess better)
* Defaults to false
*/
private boolean hasFieldHeader = false;
/** The found config */
protected CSVConfig config;
/**
*
*/
public CSVConfigGuesser() {
this.config = new CSVConfig();
}
/**
* @param in the inputstream to guess from
*/
public CSVConfigGuesser(InputStream in) {
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)
* records of a CSV file.
*
* @return the guessed config.
*/
public CSVConfig guess() {
try {
// tralalal
BufferedReader bIn = new BufferedReader(new InputStreamReader((getInputStream())));
String[] lines = new String[10];
String line = null;
int counter = 0;
while ( (line = bIn.readLine()) != null || counter > 10) {
lines[counter] = line;
counter++;
}
if (counter < 10) {
// remove nulls from the array, so we can skip the null checking.
String[] newLines = new String[counter];
System.arraycopy(lines, 0, newLines, 0, counter);
lines = newLines;
}
analyseLines(lines);
} catch(Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch(Exception e) {
// ignore exception.
}
}
}
CSVConfig conf = config;
// cleanup the config.
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) {
int lastLength = 0;
// assume fixedlength.
config.setFixedWidth(true);
for (int i = 0; i < lines.length; i++) {
if (i == 0) {
lastLength = lines[i].length();
} else {
if (lastLength != lines[i].length()) {
config.setFixedWidth(false);
}
}
}
}
protected void guessFieldSeperator(String[] lines) {
if (config.isFixedWidth()) {
guessFixedWidthSeperator(lines);
return;
}
for (int i = 0; i < lines.length; i++) {
}
}
protected void guessFixedWidthSeperator(String[] lines) {
// keep track of the fieldlength
int previousMatch = -1;
for (int i = 0; i < lines[0].length(); i++) {
char last = ' ';
boolean charMatches = true;
for (int j = 0; j < lines.length; j++) {
if (j == 0) {
last = lines[j].charAt(i);
}
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));
config.addField(field);
}
}
}
/**
*
* @return if the field uses a field header. Defaults to false.
*/
public boolean hasFieldHeader() {
return hasFieldHeader;
}
/**
* Specify if the CSV file has a field header
* @param hasFieldHeader true or false
*/
public void setHasFieldHeader(boolean hasFieldHeader) {
this.hasFieldHeader = hasFieldHeader;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Tries to guess a config based on an InputStream.
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVConfigGuesser {
/** The stream to read */
private InputStream in;
/**
* if the file has a field header (need this info, to be able to guess better)
* Defaults to false
*/
private boolean hasFieldHeader = false;
/** The found config */
protected CSVConfig config;
/**
*
*/
public CSVConfigGuesser() {
this.config = new CSVConfig();
}
/**
* @param in the inputstream to guess from
*/
public CSVConfigGuesser(InputStream in) {
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)
* records of a CSV file.
*
* @return the guessed config.
*/
public CSVConfig guess() {
try {
// tralalal
BufferedReader bIn = new BufferedReader(new InputStreamReader((getInputStream())));
String[] lines = new String[10];
String line = null;
int counter = 0;
while ( (line = bIn.readLine()) != null || counter > 10) {
lines[counter] = line;
counter++;
}
if (counter < 10) {
// remove nulls from the array, so we can skip the null checking.
String[] newLines = new String[counter];
System.arraycopy(lines, 0, newLines, 0, counter);
lines = newLines;
}
analyseLines(lines);
} catch(Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch(Exception e) {
// ignore exception.
}
}
}
CSVConfig conf = config;
// cleanup the config.
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) {
int lastLength = 0;
// assume fixedlength.
config.setFixedWidth(true);
for (int i = 0; i < lines.length; i++) {
if (i == 0) {
lastLength = lines[i].length();
} else {
if (lastLength != lines[i].length()) {
config.setFixedWidth(false);
}
}
}
}
protected void guessFieldSeperator(String[] lines) {
if (config.isFixedWidth()) {
guessFixedWidthSeperator(lines);
return;
}
for (int i = 0; i < lines.length; i++) {
}
}
protected void guessFixedWidthSeperator(String[] lines) {
// keep track of the fieldlength
int previousMatch = -1;
for (int i = 0; i < lines[0].length(); i++) {
char last = ' ';
boolean charMatches = true;
for (int j = 0; j < lines.length; j++) {
if (j == 0) {
last = lines[j].charAt(i);
}
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));
config.addField(field);
}
}
}
/**
*
* @return if the field uses a field header. Defaults to false.
*/
public boolean hasFieldHeader() {
return hasFieldHeader;
}
/**
* Specify if the CSV file has a field header
* @param hasFieldHeader true or false
*/
public void setHasFieldHeader(boolean hasFieldHeader) {
this.hasFieldHeader = hasFieldHeader;
}
}

View File

@ -1,113 +1,113 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
/**
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVField {
private String name;
private int size;
private int fill;
private boolean overrideFill;
/**
*
*/
public CSVField() {
}
/**
* @param name the name of the field
*/
public CSVField(String name) {
setName(name);
}
/**
* @param name the name of the field
* @param size the size of the field
*/
public CSVField(String name, int size) {
setName(name);
setSize(size);
}
/**
* @return the name of the field
*/
public String getName() {
return name;
}
/**
* Set the name of the field
* @param name the name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return the size of the field
*/
public int getSize() {
return size;
}
/**
* 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) {
this.size = size;
}
/**
* @return the fill pattern.
*/
public int getFill() {
return fill;
}
/**
* 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() {
return overrideFill;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
/**
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVField {
private String name;
private int size;
private int fill;
private boolean overrideFill;
/**
*
*/
public CSVField() {
}
/**
* @param name the name of the field
*/
public CSVField(String name) {
setName(name);
}
/**
* @param name the name of the field
* @param size the size of the field
*/
public CSVField(String name, int size) {
setName(name);
setSize(size);
}
/**
* @return the name of the field
*/
public String getName() {
return name;
}
/**
* Set the name of the field
* @param name the name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return the size of the field
*/
public int getSize() {
return size;
}
/**
* 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) {
this.size = size;
}
/**
* @return the fill pattern.
*/
public int getFill() {
return fill;
}
/**
* 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() {
return overrideFill;
}
}

View File

@ -1,133 +1,133 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.io.Writer;
import java.util.Arrays;
import java.util.Map;
/**
* CSVWriter
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVWriter {
/** The CSV config **/
private CSVConfig config;
/** The writer **/
private Writer writer;
/**
*
*/
public CSVWriter() {
}
public CSVWriter(CSVConfig config) {
setConfig(config);
}
public void writeRecord(Map map) {
CSVField[] fields = config.getFields();
try {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < fields.length; i++) {
String value = (String) map.get(fields[i].getName());
value = writeValue(fields[i], value);
sb.append(value);
if (!config.isDelimiterIgnored() && fields.length != (i+1)) {
sb.append(config.getDelimiter());
}
}
if (config.isEndTrimmed()) {
for (int i = sb.length()-1; i >= 0; i--) {
System.out.println("i : " + i);
if (Character.isWhitespace(sb.charAt(i))) {
sb.deleteCharAt(i);
} else {
break;
}
}
}
sb.append("\n");
String line = sb.toString();
writer.write(line);
} catch(Exception e) {
e.printStackTrace();
}
}
protected String writeValue(CSVField field, String value) throws Exception {
if (config.isFixedWidth()) {
if (value.length() < field.getSize()) {
int fillPattern = config.getFill();
if (field.overrideFill()) {
fillPattern = field.getFill();
}
StringBuffer sb = new StringBuffer();
int fillSize = (field.getSize() - value.length());
char[] fill = new char[fillSize];
Arrays.fill(fill, config.getFillChar());
if (fillPattern == CSVConfig.FILLLEFT) {
sb.append(fill);
sb.append(value);
value = sb.toString();
} else {
// defaults to fillpattern FILLRIGHT when fixedwidth is used
sb.append(value);
sb.append(fill);
value = sb.toString();
}
} else if (value.length() > field.getSize()) {
// value to big..
value = value.substring(0, field.getSize());
}
if (!config.isValueDelimiterIgnored()) {
// add the value delimiter..
value = config.getValueDelimiter()+value+config.getValueDelimiter();
}
}
return value;
}
/**
* @return the CVSConfig or null if not present
*/
public CSVConfig getConfig() {
return config;
}
/**
* 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) {
this.writer = writer;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.io.Writer;
import java.util.Arrays;
import java.util.Map;
/**
* CSVWriter
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVWriter {
/** The CSV config **/
private CSVConfig config;
/** The writer **/
private Writer writer;
/**
*
*/
public CSVWriter() {
}
public CSVWriter(CSVConfig config) {
setConfig(config);
}
public void writeRecord(Map map) {
CSVField[] fields = config.getFields();
try {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < fields.length; i++) {
String value = (String) map.get(fields[i].getName());
value = writeValue(fields[i], value);
sb.append(value);
if (!config.isDelimiterIgnored() && fields.length != (i+1)) {
sb.append(config.getDelimiter());
}
}
if (config.isEndTrimmed()) {
for (int i = sb.length()-1; i >= 0; i--) {
System.out.println("i : " + i);
if (Character.isWhitespace(sb.charAt(i))) {
sb.deleteCharAt(i);
} else {
break;
}
}
}
sb.append("\n");
String line = sb.toString();
writer.write(line);
} catch(Exception e) {
e.printStackTrace();
}
}
protected String writeValue(CSVField field, String value) throws Exception {
if (config.isFixedWidth()) {
if (value.length() < field.getSize()) {
int fillPattern = config.getFill();
if (field.overrideFill()) {
fillPattern = field.getFill();
}
StringBuffer sb = new StringBuffer();
int fillSize = (field.getSize() - value.length());
char[] fill = new char[fillSize];
Arrays.fill(fill, config.getFillChar());
if (fillPattern == CSVConfig.FILLLEFT) {
sb.append(fill);
sb.append(value);
value = sb.toString();
} else {
// defaults to fillpattern FILLRIGHT when fixedwidth is used
sb.append(value);
sb.append(fill);
value = sb.toString();
}
} else if (value.length() > field.getSize()) {
// value to big..
value = value.substring(0, field.getSize());
}
if (!config.isValueDelimiterIgnored()) {
// add the value delimiter..
value = config.getValueDelimiter()+value+config.getValueDelimiter();
}
}
return value;
}
/**
* @return the CVSConfig or null if not present
*/
public CSVConfig getConfig() {
return config;
}
/**
* 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) {
this.writer = writer;
}
}

View File

@ -1,33 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for default package");
//$JUnit-BEGIN$
suite.addTest(org.apache.commons.csv.AllTests.suite());
suite.addTest(org.apache.commons.csv.writer.AllTests.suite());
//$JUnit-END$
return suite;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for default package");
//$JUnit-BEGIN$
suite.addTest(org.apache.commons.csv.AllTests.suite());
suite.addTest(org.apache.commons.csv.writer.AllTests.suite());
//$JUnit-END$
return suite;
}
}

View File

@ -1,38 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for org.apache.commons.csv");
//$JUnit-BEGIN$
suite.addTest(ExtendedBufferedReaderTest.suite());
suite.addTest(CSVPrinterTest.suite());
suite.addTest(CSVParserTest.suite());
suite.addTest(CSVStrategyTest.suite());
suite.addTestSuite(CSVUtilsTest.class);
//$JUnit-END$
return suite;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for org.apache.commons.csv");
//$JUnit-BEGIN$
suite.addTest(ExtendedBufferedReaderTest.suite());
suite.addTest(CSVPrinterTest.suite());
suite.addTest(CSVParserTest.suite());
suite.addTest(CSVStrategyTest.suite());
suite.addTestSuite(CSVUtilsTest.class);
//$JUnit-END$
return suite;
}
}

View File

@ -1,196 +1,196 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv;
import junit.framework.TestCase;
/**
*
* @author Ortwin Glück
*/
public class CharBufferTest extends TestCase {
public void testCreate() {
CharBuffer cb = new CharBuffer();
assertEquals(0, cb.length());
try {
cb = new CharBuffer(0);
fail("Should not be possible");
} catch(IllegalArgumentException e) {
// expected
}
cb = new CharBuffer(128);
assertEquals(0, cb.length());
}
public void testAppendChar() {
CharBuffer cb = new CharBuffer(1);
String expected = "";
for (char c = 'a'; c < 'z'; c++) {
cb.append(c);
expected += c;
assertEquals(expected, cb.toString());
assertEquals(expected.length(), cb.length());
}
}
public void testAppendCharArray() {
CharBuffer cb = new CharBuffer(1);
char[] abcd = "abcd".toCharArray();
String expected = "";
for (int i=0; i<10; i++) {
cb.append(abcd);
expected += "abcd";
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
}
}
public void testAppendString() {
CharBuffer cb = new CharBuffer(1);
String abcd = "abcd";
String expected = "";
for (int i=0; i<10; i++) {
cb.append(abcd);
expected += abcd;
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
}
}
public void testAppendStringBuffer() {
CharBuffer cb = new CharBuffer(1);
StringBuffer abcd = new StringBuffer("abcd");
String expected = "";
for (int i=0; i<10; i++) {
cb.append(abcd);
expected += "abcd";
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
}
}
public void testAppendCharBuffer() {
CharBuffer cb = new CharBuffer(1);
CharBuffer abcd = new CharBuffer(17);
abcd.append("abcd");
String expected = "";
for (int i=0; i<10; i++) {
cb.append(abcd);
expected += "abcd";
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
}
}
public void testShrink() {
String data = "123456789012345678901234567890";
CharBuffer cb = new CharBuffer(data.length() + 100);
assertEquals(data.length() + 100, cb.capacity());
cb.append(data);
assertEquals(data.length() + 100, cb.capacity());
assertEquals(data.length(), cb.length());
cb.shrink();
assertEquals(data.length(), cb.capacity());
assertEquals(data.length(), cb.length());
assertEquals(data, cb.toString());
}
//-- the following test cases have been adapted from the HttpComponents project
//-- written by Oleg Kalnichevski
public void testSimpleAppend() throws Exception {
CharBuffer buffer = new CharBuffer(16);
assertEquals(16, buffer.capacity());
assertEquals(0, buffer.length());
char[] b1 = buffer.getCharacters();
assertNotNull(b1);
assertEquals(0, b1.length);
assertEquals(0, buffer.length());
char[] tmp = new char[] { '1', '2', '3', '4'};
buffer.append(tmp);
assertEquals(16, buffer.capacity());
assertEquals(4, buffer.length());
char[] b2 = buffer.getCharacters();
assertNotNull(b2);
assertEquals(4, b2.length);
for (int i = 0; i < tmp.length; i++) {
assertEquals(tmp[i], b2[i]);
}
assertEquals("1234", buffer.toString());
buffer.clear();
assertEquals(16, buffer.capacity());
assertEquals(0, buffer.length());
}
public void testAppendString2() throws Exception {
CharBuffer buffer = new CharBuffer(8);
buffer.append("stuff");
buffer.append(" and more stuff");
assertEquals("stuff and more stuff", buffer.toString());
}
public void testAppendNull() throws Exception {
CharBuffer buffer = new CharBuffer(8);
buffer.append((StringBuffer)null);
assertEquals("", buffer.toString());
buffer.append((String)null);
assertEquals("", buffer.toString());
buffer.append((CharBuffer)null);
assertEquals("", buffer.toString());
buffer.append((char[])null);
assertEquals("", buffer.toString());
}
public void testAppendCharArrayBuffer() throws Exception {
CharBuffer buffer1 = new CharBuffer(8);
buffer1.append(" and more stuff");
CharBuffer buffer2 = new CharBuffer(8);
buffer2.append("stuff");
buffer2.append(buffer1);
assertEquals("stuff and more stuff", buffer2.toString());
}
public void testAppendSingleChar() throws Exception {
CharBuffer buffer = new CharBuffer(4);
buffer.append('1');
buffer.append('2');
buffer.append('3');
buffer.append('4');
buffer.append('5');
buffer.append('6');
assertEquals("123456", buffer.toString());
}
public void testProvideCapacity() throws Exception {
CharBuffer buffer = new CharBuffer(4);
buffer.provideCapacity(2);
assertEquals(4, buffer.capacity());
buffer.provideCapacity(8);
assertTrue(buffer.capacity() >= 8);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv;
import junit.framework.TestCase;
/**
*
* @author Ortwin Glück
*/
public class CharBufferTest extends TestCase {
public void testCreate() {
CharBuffer cb = new CharBuffer();
assertEquals(0, cb.length());
try {
cb = new CharBuffer(0);
fail("Should not be possible");
} catch(IllegalArgumentException e) {
// expected
}
cb = new CharBuffer(128);
assertEquals(0, cb.length());
}
public void testAppendChar() {
CharBuffer cb = new CharBuffer(1);
String expected = "";
for (char c = 'a'; c < 'z'; c++) {
cb.append(c);
expected += c;
assertEquals(expected, cb.toString());
assertEquals(expected.length(), cb.length());
}
}
public void testAppendCharArray() {
CharBuffer cb = new CharBuffer(1);
char[] abcd = "abcd".toCharArray();
String expected = "";
for (int i=0; i<10; i++) {
cb.append(abcd);
expected += "abcd";
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
}
}
public void testAppendString() {
CharBuffer cb = new CharBuffer(1);
String abcd = "abcd";
String expected = "";
for (int i=0; i<10; i++) {
cb.append(abcd);
expected += abcd;
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
}
}
public void testAppendStringBuffer() {
CharBuffer cb = new CharBuffer(1);
StringBuffer abcd = new StringBuffer("abcd");
String expected = "";
for (int i=0; i<10; i++) {
cb.append(abcd);
expected += "abcd";
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
}
}
public void testAppendCharBuffer() {
CharBuffer cb = new CharBuffer(1);
CharBuffer abcd = new CharBuffer(17);
abcd.append("abcd");
String expected = "";
for (int i=0; i<10; i++) {
cb.append(abcd);
expected += "abcd";
assertEquals(expected, cb.toString());
assertEquals(4*(i+1), cb.length());
}
}
public void testShrink() {
String data = "123456789012345678901234567890";
CharBuffer cb = new CharBuffer(data.length() + 100);
assertEquals(data.length() + 100, cb.capacity());
cb.append(data);
assertEquals(data.length() + 100, cb.capacity());
assertEquals(data.length(), cb.length());
cb.shrink();
assertEquals(data.length(), cb.capacity());
assertEquals(data.length(), cb.length());
assertEquals(data, cb.toString());
}
//-- the following test cases have been adapted from the HttpComponents project
//-- written by Oleg Kalnichevski
public void testSimpleAppend() throws Exception {
CharBuffer buffer = new CharBuffer(16);
assertEquals(16, buffer.capacity());
assertEquals(0, buffer.length());
char[] b1 = buffer.getCharacters();
assertNotNull(b1);
assertEquals(0, b1.length);
assertEquals(0, buffer.length());
char[] tmp = new char[] { '1', '2', '3', '4'};
buffer.append(tmp);
assertEquals(16, buffer.capacity());
assertEquals(4, buffer.length());
char[] b2 = buffer.getCharacters();
assertNotNull(b2);
assertEquals(4, b2.length);
for (int i = 0; i < tmp.length; i++) {
assertEquals(tmp[i], b2[i]);
}
assertEquals("1234", buffer.toString());
buffer.clear();
assertEquals(16, buffer.capacity());
assertEquals(0, buffer.length());
}
public void testAppendString2() throws Exception {
CharBuffer buffer = new CharBuffer(8);
buffer.append("stuff");
buffer.append(" and more stuff");
assertEquals("stuff and more stuff", buffer.toString());
}
public void testAppendNull() throws Exception {
CharBuffer buffer = new CharBuffer(8);
buffer.append((StringBuffer)null);
assertEquals("", buffer.toString());
buffer.append((String)null);
assertEquals("", buffer.toString());
buffer.append((CharBuffer)null);
assertEquals("", buffer.toString());
buffer.append((char[])null);
assertEquals("", buffer.toString());
}
public void testAppendCharArrayBuffer() throws Exception {
CharBuffer buffer1 = new CharBuffer(8);
buffer1.append(" and more stuff");
CharBuffer buffer2 = new CharBuffer(8);
buffer2.append("stuff");
buffer2.append(buffer1);
assertEquals("stuff and more stuff", buffer2.toString());
}
public void testAppendSingleChar() throws Exception {
CharBuffer buffer = new CharBuffer(4);
buffer.append('1');
buffer.append('2');
buffer.append('3');
buffer.append('4');
buffer.append('5');
buffer.append('6');
assertEquals("123456", buffer.toString());
}
public void testProvideCapacity() throws Exception {
CharBuffer buffer = new CharBuffer(4);
buffer.provideCapacity(2);
assertEquals(4, buffer.capacity());
buffer.provideCapacity(8);
assertTrue(buffer.capacity() >= 8);
}
}

View File

@ -1,19 +1,19 @@
package org.apache.commons.csv.writer;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for org.apache.commons.csv.writer");
//$JUnit-BEGIN$
suite.addTestSuite(CSVConfigGuesserTest.class);
suite.addTestSuite(CSVConfigTest.class);
suite.addTestSuite(CSVFieldTest.class);
suite.addTestSuite(CSVWriterTest.class);
//$JUnit-END$
return suite;
}
}
package org.apache.commons.csv.writer;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for org.apache.commons.csv.writer");
//$JUnit-BEGIN$
suite.addTestSuite(CSVConfigGuesserTest.class);
suite.addTestSuite(CSVConfigTest.class);
suite.addTestSuite(CSVFieldTest.class);
suite.addTestSuite(CSVWriterTest.class);
//$JUnit-END$
return suite;
}
}

View File

@ -1,91 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.io.ByteArrayInputStream;
import junit.framework.TestCase;
/**
* Tests for the config guesser.
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVConfigGuesserTest extends TestCase {
public void testSetters() throws Exception {
CSVConfigGuesser guesser = new CSVConfigGuesser();
ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
guesser.setInputStream(in);
assertEquals(in, guesser.getInputStream());
guesser = new CSVConfigGuesser(in);
assertEquals(in, guesser.getInputStream());
assertEquals(false, guesser.hasFieldHeader());
guesser.setHasFieldHeader(true);
assertEquals(true, guesser.hasFieldHeader());
}
/**
* Test a format like
* 1234 ; abcd ; 1234 ;
*
*/
public void testConfigGuess1() {
CSVConfig expected = new CSVConfig();
expected.setDelimiter(';');
expected.setValueDelimiter(' ');
expected.setFill(CSVConfig.FILLRIGHT);
expected.setIgnoreValueDelimiter(false);
expected.setFixedWidth(true);
CSVField field = new CSVField();
field.setSize(4);
expected.addField(field);
expected.addField(field);
StringBuffer sb = new StringBuffer();
sb.append("1234;abcd;1234\n");
sb.append("abcd;1234;abcd");
ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
CSVConfigGuesser guesser = new CSVConfigGuesser(in);
CSVConfig guessed = guesser.guess();
assertEquals(expected.isFixedWidth(), guessed.isFixedWidth());
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();
expected.setDelimiter(';');
expected.setValueDelimiter(' ');
expected.setFill(CSVConfig.FILLRIGHT);
expected.setIgnoreValueDelimiter(false);
// expected.setFixedWidth(false);
StringBuffer sb = new StringBuffer();
sb.append("1,2,3,4\n");
sb.append("abcd,1234,abcd,1234");
ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
CSVConfigGuesser guesser = new CSVConfigGuesser(in);
CSVConfig guessed = guesser.guess();
assertEquals(expected.isFixedWidth(), guessed.isFixedWidth());
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.io.ByteArrayInputStream;
import junit.framework.TestCase;
/**
* Tests for the config guesser.
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVConfigGuesserTest extends TestCase {
public void testSetters() throws Exception {
CSVConfigGuesser guesser = new CSVConfigGuesser();
ByteArrayInputStream in = new ByteArrayInputStream(new byte[0]);
guesser.setInputStream(in);
assertEquals(in, guesser.getInputStream());
guesser = new CSVConfigGuesser(in);
assertEquals(in, guesser.getInputStream());
assertEquals(false, guesser.hasFieldHeader());
guesser.setHasFieldHeader(true);
assertEquals(true, guesser.hasFieldHeader());
}
/**
* Test a format like
* 1234 ; abcd ; 1234 ;
*
*/
public void testConfigGuess1() {
CSVConfig expected = new CSVConfig();
expected.setDelimiter(';');
expected.setValueDelimiter(' ');
expected.setFill(CSVConfig.FILLRIGHT);
expected.setIgnoreValueDelimiter(false);
expected.setFixedWidth(true);
CSVField field = new CSVField();
field.setSize(4);
expected.addField(field);
expected.addField(field);
StringBuffer sb = new StringBuffer();
sb.append("1234;abcd;1234\n");
sb.append("abcd;1234;abcd");
ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
CSVConfigGuesser guesser = new CSVConfigGuesser(in);
CSVConfig guessed = guesser.guess();
assertEquals(expected.isFixedWidth(), guessed.isFixedWidth());
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();
expected.setDelimiter(';');
expected.setValueDelimiter(' ');
expected.setFill(CSVConfig.FILLRIGHT);
expected.setIgnoreValueDelimiter(false);
// expected.setFixedWidth(false);
StringBuffer sb = new StringBuffer();
sb.append("1,2,3,4\n");
sb.append("abcd,1234,abcd,1234");
ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
CSVConfigGuesser guesser = new CSVConfigGuesser(in);
CSVConfig guessed = guesser.guess();
assertEquals(expected.isFixedWidth(), guessed.isFixedWidth());
}
}

View File

@ -1,103 +1,103 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.util.Collection;
import junit.framework.TestCase;
/**
* Testcase for the CSVConfig
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVConfigTest extends TestCase {
public void testFixedWith() {
CSVConfig config = new CSVConfig();
assertEquals(false, config.isFixedWidth());
config.setFixedWidth(true);
assertEquals(true, config.isFixedWidth());
}
public void testFields() {
CSVConfig config = new CSVConfig();
assertEquals(0, config.getFields().length);
config.setFields((CSVField[])null);
assertEquals(0, config.getFields().length);
config.setFields((Collection)null);
assertEquals(0, config.getFields().length);
CSVField field = new CSVField();
field.setName("field1");
config.addField(field);
assertEquals(field, config.getFields()[0]);
assertEquals(null, config.getField(null));
assertEquals(null, config.getField("field11"));
assertEquals(field, config.getField("field1"));
}
public void testFill() {
CSVConfig config = new CSVConfig();
assertEquals(CSVConfig.FILLNONE, config.getFill());
config.setFill(CSVConfig.FILLLEFT);
assertEquals(CSVConfig.FILLLEFT, config.getFill());
config.setFill(CSVConfig.FILLRIGHT);
assertEquals(CSVConfig.FILLRIGHT, config.getFill());
assertEquals(' ', config.getFillChar());
config.setFillChar('m');
assertEquals('m', config.getFillChar());
}
public void testDelimiter() {
CSVConfig config = new CSVConfig();
assertEquals(',', config.getDelimiter());
config.setDelimiter(';');
assertEquals(';', config.getDelimiter());
assertEquals(false, config.isDelimiterIgnored());
config.setIgnoreDelimiter(true);
assertEquals(true, config.isDelimiterIgnored());
}
public void testValueDelimiter() {
CSVConfig config = new CSVConfig();
assertEquals('"', config.getValueDelimiter());
config.setValueDelimiter('m');
assertEquals('m', config.getValueDelimiter());
assertEquals(true, config.isValueDelimiterIgnored());
config.setIgnoreValueDelimiter(false);
assertEquals(false, config.isValueDelimiterIgnored());
}
public void testFieldHeader() {
CSVConfig config = new CSVConfig();
assertEquals(false, config.isFieldHeader());
config.setFieldHeader(true);
assertEquals(true, config.isFieldHeader());
}
public void testTrimEnd() {
CSVConfig config = new CSVConfig();
assertEquals(false, config.isEndTrimmed());
config.setEndTrimmed(true);
assertEquals(true, config.isEndTrimmed());
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.util.Collection;
import junit.framework.TestCase;
/**
* Testcase for the CSVConfig
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVConfigTest extends TestCase {
public void testFixedWith() {
CSVConfig config = new CSVConfig();
assertEquals(false, config.isFixedWidth());
config.setFixedWidth(true);
assertEquals(true, config.isFixedWidth());
}
public void testFields() {
CSVConfig config = new CSVConfig();
assertEquals(0, config.getFields().length);
config.setFields((CSVField[])null);
assertEquals(0, config.getFields().length);
config.setFields((Collection)null);
assertEquals(0, config.getFields().length);
CSVField field = new CSVField();
field.setName("field1");
config.addField(field);
assertEquals(field, config.getFields()[0]);
assertEquals(null, config.getField(null));
assertEquals(null, config.getField("field11"));
assertEquals(field, config.getField("field1"));
}
public void testFill() {
CSVConfig config = new CSVConfig();
assertEquals(CSVConfig.FILLNONE, config.getFill());
config.setFill(CSVConfig.FILLLEFT);
assertEquals(CSVConfig.FILLLEFT, config.getFill());
config.setFill(CSVConfig.FILLRIGHT);
assertEquals(CSVConfig.FILLRIGHT, config.getFill());
assertEquals(' ', config.getFillChar());
config.setFillChar('m');
assertEquals('m', config.getFillChar());
}
public void testDelimiter() {
CSVConfig config = new CSVConfig();
assertEquals(',', config.getDelimiter());
config.setDelimiter(';');
assertEquals(';', config.getDelimiter());
assertEquals(false, config.isDelimiterIgnored());
config.setIgnoreDelimiter(true);
assertEquals(true, config.isDelimiterIgnored());
}
public void testValueDelimiter() {
CSVConfig config = new CSVConfig();
assertEquals('"', config.getValueDelimiter());
config.setValueDelimiter('m');
assertEquals('m', config.getValueDelimiter());
assertEquals(true, config.isValueDelimiterIgnored());
config.setIgnoreValueDelimiter(false);
assertEquals(false, config.isValueDelimiterIgnored());
}
public void testFieldHeader() {
CSVConfig config = new CSVConfig();
assertEquals(false, config.isFieldHeader());
config.setFieldHeader(true);
assertEquals(true, config.isFieldHeader());
}
public void testTrimEnd() {
CSVConfig config = new CSVConfig();
assertEquals(false, config.isEndTrimmed());
config.setEndTrimmed(true);
assertEquals(true, config.isEndTrimmed());
}
}

View File

@ -1,53 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import junit.framework.TestCase;
/**
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVFieldTest extends TestCase {
public void testCSVField() {
CSVField field = new CSVField();
assertEquals(null, field.getName());
field.setName("id");
assertEquals("id", field.getName());
assertEquals(0, field.getSize());
field.setSize(10);
assertEquals(10, field.getSize());
field = new CSVField("name");
assertEquals("name", field.getName());
field = new CSVField("name", 10);
assertEquals("name", field.getName());
assertEquals(10, field.getSize());
}
public void testFill() {
CSVField field = new CSVField();
assertEquals(CSVConfig.FILLNONE, field.getFill());
assertEquals(false, field.overrideFill());
field.setFill(CSVConfig.FILLLEFT);
assertEquals(true, field.overrideFill());
assertEquals(CSVConfig.FILLLEFT, field.getFill());
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import junit.framework.TestCase;
/**
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVFieldTest extends TestCase {
public void testCSVField() {
CSVField field = new CSVField();
assertEquals(null, field.getName());
field.setName("id");
assertEquals("id", field.getName());
assertEquals(0, field.getSize());
field.setSize(10);
assertEquals(10, field.getSize());
field = new CSVField("name");
assertEquals("name", field.getName());
field = new CSVField("name", 10);
assertEquals("name", field.getName());
assertEquals(10, field.getSize());
}
public void testFill() {
CSVField field = new CSVField();
assertEquals(CSVConfig.FILLNONE, field.getFill());
assertEquals(false, field.overrideFill());
field.setFill(CSVConfig.FILLLEFT);
assertEquals(true, field.overrideFill());
assertEquals(CSVConfig.FILLLEFT, field.getFill());
}
}

View File

@ -1,59 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
/**
* The testcase for the csv writer.
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVWriterTest extends TestCase {
public void testCSVConfig() {
CSVWriter writer = new CSVWriter();
assertEquals(null, writer.getConfig());
CSVConfig config = new CSVConfig();
writer.setConfig(config);
assertEquals(config, writer.getConfig());
writer = new CSVWriter(config);
assertEquals(config, writer.getConfig());
}
public void testWriter() {
CSVWriter writer = new CSVWriter();
CSVConfig config = new CSVConfig();
config.addField(new CSVField("field1", 5));
config.addField(new CSVField("field2", 4));
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
Map map = new HashMap();
map.put("field1", "12345");
map.put("field2", "1234");
writer.writeRecord(map);
assertEquals("12345,1234\n",sw.toString());
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.csv.writer;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
/**
* The testcase for the csv writer.
*
* @author Martin van den Bemt
* @version $Id: $
*/
public class CSVWriterTest extends TestCase {
public void testCSVConfig() {
CSVWriter writer = new CSVWriter();
assertEquals(null, writer.getConfig());
CSVConfig config = new CSVConfig();
writer.setConfig(config);
assertEquals(config, writer.getConfig());
writer = new CSVWriter(config);
assertEquals(config, writer.getConfig());
}
public void testWriter() {
CSVWriter writer = new CSVWriter();
CSVConfig config = new CSVConfig();
config.addField(new CSVField("field1", 5));
config.addField(new CSVField("field2", 4));
writer.setConfig(config);
StringWriter sw = new StringWriter();
writer.setWriter(sw);
Map map = new HashMap();
map.put("field1", "12345");
map.put("field2", "1234");
writer.writeRecord(map);
assertEquals("12345,1234\n",sw.toString());
}
}