Author: ebourg
Date: Sat Mar 17 01:39:04 2012
New Revision: 1301852
URL: http://svn.apache.org/viewvc?rev=1301852&view=rev
Log:
Header support (CSV-65)
Added:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/package.html
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java?rev=1301852&r1=1301851&r2=1301852&view=diff
==============================================================================
---
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
(original)
+++
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVFormat.java
Sat Mar 17 01:39:04 2012
@@ -31,6 +31,7 @@ public class CSVFormat implements Serial
/** According to RFC 4180, line breaks are delimited by CRLF */
private static final String CRLF = "\r\n";
+
private final char delimiter;
private final char encapsulator;
private final char commentStart;
@@ -39,7 +40,8 @@ public class CSVFormat implements Serial
private final boolean trailingSpacesIgnored;
private final boolean unicodeEscapesInterpreted;
private final boolean emptyLinesIgnored;
- private final String lineSeparator; // for output
+ private final String lineSeparator; // for outputs
+ private final String[] header;
/**
@@ -51,7 +53,7 @@ public class CSVFormat implements Serial
static final char DISABLED = '\ufffe';
/** Standard comma separated format as defined by <a
href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>. */
- public static final CSVFormat DEFAULT = new CSVFormat(',', '"', DISABLED,
DISABLED, true, true, false, true, CRLF);
+ public static final CSVFormat DEFAULT = new CSVFormat(',', '"', DISABLED,
DISABLED, true, true, false, true, CRLF, null);
/**
* Excel file format (using a comma as the value delimiter).
@@ -64,10 +66,10 @@ public class CSVFormat implements Serial
*
* <pre>CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');</pre>
*/
- public static final CSVFormat EXCEL = new CSVFormat(',', '"', DISABLED,
DISABLED, false, false, false, false, CRLF);
+ public static final CSVFormat EXCEL = new CSVFormat(',', '"', DISABLED,
DISABLED, false, false, false, false, CRLF, null);
/** Tab-delimited format, with quote; leading and trailing spaces ignored.
*/
- public static final CSVFormat TDF = new CSVFormat('\t', '"', DISABLED,
DISABLED, true, true, false, true, CRLF);
+ public static final CSVFormat TDF = new CSVFormat('\t', '"', DISABLED,
DISABLED, true, true, false, true, CRLF, null);
/**
* Default MySQL format used by the <tt>SELECT INTO OUTFILE</tt> and
@@ -77,7 +79,7 @@ public class CSVFormat implements Serial
*
* @see <a
href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html">http://dev.mysql.com/doc/refman/5.1/en/load-data.html</a>
*/
- public static final CSVFormat MYSQL = new CSVFormat('\t', DISABLED,
DISABLED, '\\', false, false, false, false, "\n");
+ public static final CSVFormat MYSQL = new CSVFormat('\t', DISABLED,
DISABLED, '\\', false, false, false, false, "\n", null);
/**
@@ -92,6 +94,7 @@ public class CSVFormat implements Serial
* @param unicodeEscapesInterpreted <tt>true</tt> when unicode escapes
should be interpreted
* @param emptyLinesIgnored <tt>true</tt> when the parser should
skip emtpy lines
* @param lineSeparator the line separator to use for output
+ * @param header the header
*/
CSVFormat(
char delimiter,
@@ -102,7 +105,8 @@ public class CSVFormat implements Serial
boolean trailingSpacesIgnored,
boolean unicodeEscapesInterpreted,
boolean emptyLinesIgnored,
- String lineSeparator) {
+ String lineSeparator,
+ String[] header) {
this.delimiter = delimiter;
this.encapsulator = encapsulator;
this.commentStart = commentStart;
@@ -112,6 +116,7 @@ public class CSVFormat implements Serial
this.unicodeEscapesInterpreted = unicodeEscapesInterpreted;
this.emptyLinesIgnored = emptyLinesIgnored;
this.lineSeparator = lineSeparator;
+ this.header = header;
}
/**
@@ -171,7 +176,7 @@ public class CSVFormat implements Serial
throw new IllegalArgumentException("The delimiter cannot be a line
break");
}
- return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator);
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
}
/**
@@ -195,7 +200,7 @@ public class CSVFormat implements Serial
throw new IllegalArgumentException("The encapsulator cannot be a
line break");
}
- return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator);
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
}
boolean isEncapsulating() {
@@ -223,7 +228,7 @@ public class CSVFormat implements Serial
throw new IllegalArgumentException("The comment start character
cannot be a line break");
}
- return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator);
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
}
/**
@@ -256,7 +261,7 @@ public class CSVFormat implements Serial
throw new IllegalArgumentException("The escape character cannot be
a line break");
}
- return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator);
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
}
boolean isEscaping() {
@@ -280,7 +285,7 @@ public class CSVFormat implements Serial
* @return A copy of this format with the specified left trimming behavior.
*/
public CSVFormat withLeadingSpacesIgnored(boolean leadingSpacesIgnored) {
- return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator);
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
}
/**
@@ -300,7 +305,7 @@ public class CSVFormat implements Serial
* @return A copy of this format with the specified right trimming
behavior.
*/
public CSVFormat withTrailingSpacesIgnored(boolean trailingSpacesIgnored) {
- return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator);
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
}
/**
@@ -311,7 +316,7 @@ public class CSVFormat implements Serial
* @return A copy of this format with the specified trimming behavior.
*/
public CSVFormat withSurroundingSpacesIgnored(boolean
surroundingSpacesIgnored) {
- return new CSVFormat(delimiter, encapsulator, commentStart, escape,
surroundingSpacesIgnored, surroundingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator);
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
surroundingSpacesIgnored, surroundingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
}
/**
@@ -332,7 +337,7 @@ public class CSVFormat implements Serial
* @return A copy of this format with the specified unicode escaping
behavior.
*/
public CSVFormat withUnicodeEscapesInterpreted(boolean
unicodeEscapesInterpreted) {
- return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator);
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
}
/**
@@ -352,7 +357,7 @@ public class CSVFormat implements Serial
* @return A copy of this format with the specified empty line skipping
behavior.
*/
public CSVFormat withEmptyLinesIgnored(boolean emptyLinesIgnored) {
- return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator);
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
}
/**
@@ -372,7 +377,29 @@ public class CSVFormat implements Serial
* @return A copy of this format using the specified output line separator
*/
public CSVFormat withLineSeparator(String lineSeparator) {
- return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator);
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
+ }
+
+ String[] getHeader() {
+ return header;
+ }
+
+ /**
+ * Returns a copy of this format using the specified header. The header can
+ * either be parsed automatically from the input file with:
+ *
+ * <pre>CSVFormat format = CSVFormat.DEFAULT.withHeader();</pre>
+ *
+ * or specified manually with:
+ *
+ * <pre>CSVFormat format = CSVFormat.DEFAULT.withHeader("name", "email",
"phone");</pre>
+ *
+ * @param header the header, <tt>null</tt> if disabled, empty if parsed
automatically, user specified otherwise.
+ *
+ * @return A copy of this format using the specified header
+ */
+ public CSVFormat withHeader(String... header) {
+ return new CSVFormat(delimiter, encapsulator, commentStart, escape,
leadingSpacesIgnored, trailingSpacesIgnored, unicodeEscapesInterpreted,
emptyLinesIgnored, lineSeparator, header);
}
/**
@@ -380,7 +407,7 @@ public class CSVFormat implements Serial
*
* @param in the input stream
*/
- public Iterable<String[]> parse(Reader in) {
+ public Iterable<CSVRecord> parse(Reader in) throws IOException {
return new CSVParser(in, this);
}
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java?rev=1301852&r1=1301851&r2=1301852&view=diff
==============================================================================
---
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
(original)
+++
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVParser.java
Sat Mar 17 01:39:04 2012
@@ -21,8 +21,10 @@ import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import java.util.NoSuchElementException;
import org.apache.commons.csv.CSVLexer.Token;
@@ -40,14 +42,14 @@ import static org.apache.commons.csv.CSV
* <pre>
* CSVFormat format = new CSVFormat('\t', '"', '#');
* Reader in = new StringReader("a\tb\nc\td");
- * String[][] records = new CSVParser(in, format).getRecords();
+ * List<CSVRecord> records = new CSVParser(in, format).getRecords();
* </pre>
*
* <p>Parsing of a csv-string in Excel CSV format, using a for-each loop:</p>
* <pre>
* Reader in = new StringReader("a;b\nc;d");
* CSVParser parser = new CSVParser(in, CSVFormat.EXCEL);
- * for (String[] record : parser) {
+ * for (CSVRecord record : parser) {
* ...
* }
* </pre>
@@ -59,13 +61,11 @@ import static org.apache.commons.csv.CSV
* <p>see <a href="package-summary.html">package documentation</a>
* for more details</p>
*/
-public class CSVParser implements Iterable<String[]> {
-
- /** Immutable empty String array. */
- private static final String[] EMPTY_STRING_ARRAY = new String[0];
+public class CSVParser implements Iterable<CSVRecord> {
private final CSVLexer lexer;
-
+ private Map<String, Integer> headerMapping;
+
// the following objects are shared to reduce garbage
/** A record buffer for getRecord(). Grows as necessary and is reused. */
@@ -78,7 +78,7 @@ public class CSVParser implements Iterab
* @param input a Reader containing "csv-formatted" input
* @throws IllegalArgumentException thrown if the parameters of the format
are inconsistent
*/
- public CSVParser(Reader input) {
+ public CSVParser(Reader input) throws IOException {
this(input, CSVFormat.DEFAULT);
}
@@ -89,7 +89,7 @@ public class CSVParser implements Iterab
* @param format the CSVFormat used for CSV parsing
* @throws IllegalArgumentException thrown if the parameters of the format
are inconsistent
*/
- public CSVParser(Reader input, CSVFormat format) {
+ public CSVParser(Reader input, CSVFormat format) throws IOException {
format.validate();
if (format.isUnicodeEscapesInterpreted()) {
@@ -97,6 +97,8 @@ public class CSVParser implements Iterab
}
this.lexer = new CSVLexer(format, new ExtendedBufferedReader(input));
+
+ initializeHeader(format);
}
/**
@@ -106,7 +108,7 @@ public class CSVParser implements Iterab
* @param format the CSVFormat used for CSV parsing
* @throws IllegalArgumentException thrown if the parameters of the format
are inconsistent
*/
- public CSVParser(String input, CSVFormat format) {
+ public CSVParser(String input, CSVFormat format) throws IOException{
this(new StringReader(input), format);
}
@@ -120,15 +122,15 @@ public class CSVParser implements Iterab
* @return matrix of records x values ('null' when end of file)
* @throws IOException on parse error or input read-failure
*/
- public String[][] getRecords() throws IOException {
- List<String[]> records = new ArrayList<String[]>();
- String[] record;
+ public List<CSVRecord> getRecords() throws IOException {
+ List<CSVRecord> records = new ArrayList<CSVRecord>();
+ CSVRecord record;
while ((record = getRecord()) != null) {
records.add(record);
}
if (!records.isEmpty()) {
- return records.toArray(new String[records.size()][]);
+ return records;
} else {
return null;
}
@@ -140,8 +142,8 @@ public class CSVParser implements Iterab
* @return the record as an array of values, or <tt>null</tt> if the end
of the stream has been reached
* @throws IOException on parse error or input read-failure
*/
- String[] getRecord() throws IOException {
- String[] result = EMPTY_STRING_ARRAY;
+ CSVRecord getRecord() throws IOException {
+ CSVRecord result = new CSVRecord(null, headerMapping);
record.clear();
do {
reusableToken.reset();
@@ -161,25 +163,50 @@ public class CSVParser implements Iterab
}
break;
case INVALID:
- // error: throw IOException
throw new IOException("(line " + getLineNumber() + ")
invalid parse sequence");
- // unreachable: break;
}
} while (reusableToken.type == TOKEN);
if (!record.isEmpty()) {
- result = record.toArray(new String[record.size()]);
+ result = new CSVRecord(record.toArray(new String[record.size()]),
headerMapping);
}
return result;
}
/**
+ * Initializes the name to index mapping if the format defines a header.
+ */
+ private void initializeHeader(CSVFormat format) throws IOException {
+ if (format.getHeader() != null) {
+ headerMapping = new HashMap<String, Integer>();
+
+ String[] header = null;
+ if (format.getHeader().length == 0) {
+ // read the header from the first line of the file
+ CSVRecord record = getRecord();
+ if (record != null) {
+ header = record.values();
+ }
+ } else {
+ header = format.getHeader();
+ }
+
+ // build the name to index mappings
+ if (header != null) {
+ for (int i = 0; i < header.length; i++) {
+ headerMapping.put(header[i], i);
+ }
+ }
+ }
+ }
+
+ /**
* Returns an iterator on the records. IOExceptions occuring
* during the iteration are wrapped in a RuntimeException.
*/
- public Iterator<String[]> iterator() {
- return new Iterator<String[]>() {
- private String[] current;
+ public Iterator<CSVRecord> iterator() {
+ return new Iterator<CSVRecord>() {
+ private CSVRecord current;
public boolean hasNext() {
if (current == null) {
@@ -189,8 +216,8 @@ public class CSVParser implements Iterab
return current != null;
}
- public String[] next() {
- String[] next = current;
+ public CSVRecord next() {
+ CSVRecord next = current;
current = null;
if (next == null) {
@@ -204,7 +231,7 @@ public class CSVParser implements Iterab
return next;
}
- private String[] getNextRecord() {
+ private CSVRecord getNextRecord() {
try {
return getRecord();
} catch (IOException e) {
Added:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java?rev=1301852&view=auto
==============================================================================
---
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java
(added)
+++
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/CSVRecord.java
Sat Mar 17 01:39:04 2012
@@ -0,0 +1,87 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * A CSV record
+ *
+ * @author Emmanuel Bourg
+ */
+public class CSVRecord implements Serializable, Iterable<String> {
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /** The values of the record */
+ private final String[] values;
+
+ /** The column name to index mapping. */
+ private final Map<String, Integer> mapping;
+
+ CSVRecord(String[] values, Map<String, Integer> mapping) {
+ this.values = values != null ? values : EMPTY_STRING_ARRAY;
+ this.mapping = mapping;
+ }
+
+ /**
+ * Returns a value by index.
+ *
+ * @param i the index of the column retrieved
+ */
+ public String get(int i) {
+ return values[i];
+ }
+
+ /**
+ * Returns a value by name.
+ *
+ * @param name the name of the column retrieved
+ */
+ public String get(String name) {
+ if (mapping == null) {
+ throw new IllegalStateException("No header was specified, the
record values can't be accessed by name");
+ }
+
+ Integer index = mapping.get(name);
+
+ return index != null ? values[index] : null;
+ }
+
+ public Iterator<String> iterator() {
+ return Arrays.asList(values).iterator();
+ }
+
+ String[] values() {
+ return values;
+ }
+
+ /**
+ * Returns the number of values in this record.
+ */
+ public int size() {
+ return values.length;
+ }
+
+ public String toString() {
+ return Arrays.toString(values);
+ }
+}
Modified:
commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/package.html
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/package.html?rev=1301852&r1=1301851&r2=1301852&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/package.html
(original)
+++ commons/proper/csv/trunk/src/main/java/org/apache/commons/csv/package.html
Sat Mar 17 01:39:04 2012
@@ -77,9 +77,9 @@
<p>Example usage:</p>
<blockquote><pre>
Reader in = new StringReader("a,b,c");
-for (String[] line : CSVFormat.DEFAULT.parse(in)) {
- for (int i = 0; i < line.length; i++) {
- System.out.println("value " + i + "=" + line[i]);
+for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
+ for (int i = 0; i < record.length; i++) {
+ System.out.println("value " + i + "=" + record.get(i));
}
}
</pre></blockquote>
Modified:
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java?rev=1301852&r1=1301851&r2=1301852&view=diff
==============================================================================
---
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
(original)
+++
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVFormatTest.java
Sat Mar 17 01:39:04 2012
@@ -30,7 +30,7 @@ public class CSVFormatTest {
@Test
public void testImmutalibity() {
- CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true,
true, "\r\n");
+ CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true,
true, "\r\n", null);
format.withDelimiter('?');
format.withEncapsulator('?');
@@ -56,7 +56,7 @@ public class CSVFormatTest {
@Test
public void testMutators() {
- CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true,
true, "\r\n");
+ CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, true,
true, "\r\n", null);
assertEquals('?', format.withDelimiter('?').getDelimiter());
assertEquals('?', format.withEncapsulator('?').getEncapsulator());
Modified:
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java?rev=1301852&r1=1301851&r2=1301852&view=diff
==============================================================================
---
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
(original)
+++
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVParserTest.java
Sat Mar 17 01:39:04 2012
@@ -58,7 +58,7 @@ public class CSVParserTest {
public void testGetLine() throws IOException {
CSVParser parser = new CSVParser(new StringReader(code));
for (String[] re : res) {
- assertTrue(Arrays.equals(re, parser.getRecord()));
+ assertTrue(Arrays.equals(re, parser.getRecord().values()));
}
assertTrue(parser.getRecord() == null);
@@ -67,11 +67,11 @@ public class CSVParserTest {
@Test
public void testGetRecords() throws IOException {
CSVParser parser = new CSVParser(new StringReader(code));
- String[][] tmp = parser.getRecords();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(res.length, records.size());
+ assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
+ assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
@@ -88,11 +88,11 @@ public class CSVParserTest {
{"\"hello\"", " \"world\"", "abc\ndef", ""}
};
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
- String[][] tmp = parser.getRecords();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(res.length, records.size());
+ assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
+ assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
@@ -107,11 +107,11 @@ public class CSVParserTest {
{"world", ""}
};
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
- String[][] tmp = parser.getRecords();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(res.length, records.size());
+ assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
+ assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
@@ -135,11 +135,11 @@ public class CSVParserTest {
for (String code : codes) {
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
- String[][] tmp = parser.getRecords();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(res.length, records.size());
+ assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
+ assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
}
@@ -162,11 +162,11 @@ public class CSVParserTest {
};
for (String code : codes) {
CSVParser parser = new CSVParser(new StringReader(code));
- String[][] tmp = parser.getRecords();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(res.length, records.size());
+ assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
+ assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
}
@@ -186,11 +186,11 @@ public class CSVParserTest {
};
for (String code : codes) {
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
- String[][] tmp = parser.getRecords();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(res.length, records.size());
+ assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
+ assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
}
@@ -208,11 +208,11 @@ public class CSVParserTest {
};
for (String code : codes) {
CSVParser parser = new CSVParser(new StringReader(code));
- String[][] tmp = parser.getRecords();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(res.length, records.size());
+ assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
+ assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
}
@@ -242,11 +242,11 @@ public class CSVParserTest {
{"a\\\\,b"} // backslash in quotes only escapes a delimiter
(",")
};
CSVParser parser = new CSVParser(new StringReader(code));
- String[][] tmp = parser.getRecords();
- assertEquals(res.length, tmp.length);
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(res.length, records.size());
+ assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
+ assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
@@ -283,13 +283,13 @@ public class CSVParserTest {
};
- CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/',
false, false, true, true, "\r\n");
+ CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/',
false, false, true, true, "\r\n", null);
CSVParser parser = new CSVParser(code, format);
- String[][] tmp = parser.getRecords();
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertTrue(records.size() > 0);
for (int i = 0; i < res.length; i++) {
- assertTrue(Arrays.equals(res[i], tmp[i]));
+ assertTrue(Arrays.equals(res[i], records.get(i).values()));
}
}
@@ -312,20 +312,17 @@ public class CSVParserTest {
};
- CSVFormat format = new CSVFormat(',', CSVFormat.DISABLED,
CSVFormat.DISABLED, '/', false, false, true, true, "\r\n");
+ CSVFormat format = new CSVFormat(',', CSVFormat.DISABLED,
CSVFormat.DISABLED, '/', false, false, true, true, "\r\n", null);
CSVParser parser = new CSVParser(code, format);
- String[][] tmp = parser.getRecords();
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertTrue(records.size() > 0);
- if (!CSVPrinterTest.equals(res, tmp)) {
- assertTrue(false);
- }
+ assertTrue(CSVPrinterTest.equals(res, records));
}
@Test
public void testDefaultFormat() throws IOException {
-
String code = ""
+ "a,b\n" // 1)
+ "\"\n\",\" \"\n" // 2)
@@ -341,12 +338,10 @@ public class CSVParserTest {
assertEquals(CSVFormat.DISABLED, format.getCommentStart());
CSVParser parser = new CSVParser(code, format);
- String[][] tmp = parser.getRecords();
- assertTrue(tmp.length > 0);
+ List<CSVRecord> records = parser.getRecords();
+ assertTrue(records.size() > 0);
- if (!CSVPrinterTest.equals(res, tmp)) {
- assertTrue(false);
- }
+ assertTrue(CSVPrinterTest.equals(res, records));
String[][] res_comments = {
{"a", "b"},
@@ -356,22 +351,20 @@ public class CSVParserTest {
format = CSVFormat.DEFAULT.withCommentStart('#');
parser = new CSVParser(code, format);
- tmp = parser.getRecords();
-
- if (!CSVPrinterTest.equals(res_comments, tmp)) {
- assertTrue(false);
- }
+ records = parser.getRecords();
+
+ assertTrue(CSVPrinterTest.equals(res_comments, records));
}
@Test
public void testUnicodeEscape() throws Exception {
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
CSVParser parser = new CSVParser(code,
CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
- final Iterator<String[]> iterator = parser.iterator();
- String[] data = iterator.next();
- assertEquals(2, data.length);
- assertEquals("abc", data[0]);
- assertEquals("public", data[1]);
+ final Iterator<CSVRecord> iterator = parser.iterator();
+ CSVRecord record = iterator.next();
+ assertEquals(2, record.size());
+ assertEquals("abc", record.get(0));
+ assertEquals("public", record.get(1));
assertFalse("Should not have any more records", iterator.hasNext());
}
@@ -379,11 +372,11 @@ public class CSVParserTest {
public void testUnicodeEscapeMySQL() throws Exception {
String code = "abc\t\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
CSVParser parser = new CSVParser(code,
CSVFormat.MYSQL.withUnicodeEscapesInterpreted(true));
- final Iterator<String[]> iterator = parser.iterator();
- String[] data = iterator.next();
- assertEquals(2, data.length);
- assertEquals("abc", data[0]);
- assertEquals("public", data[1]);
+ final Iterator<CSVRecord> iterator = parser.iterator();
+ CSVRecord record = iterator.next();
+ assertEquals(2, record.size());
+ assertEquals("abc", record.get(0));
+ assertEquals("public", record.get(1));
assertFalse("Should not have any more records", iterator.hasNext());
}
@@ -391,24 +384,24 @@ public class CSVParserTest {
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.getRecords();
- assertEquals(4, data.length);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(4, records.size());
}
@Test
public void testCarriageReturnEndings() throws IOException {
String code = "foo\rbaar,\rhello,world\r,kanu";
CSVParser parser = new CSVParser(new StringReader(code));
- String[][] data = parser.getRecords();
- assertEquals(4, data.length);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(4, records.size());
}
@Test
public void testLineFeedEndings() throws IOException {
String code = "foo\nbaar,\nhello,world\n,kanu";
CSVParser parser = new CSVParser(new StringReader(code));
- String[][] data = parser.getRecords();
- assertEquals(4, data.length);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(4, records.size());
}
@Test
@@ -417,31 +410,31 @@ public class CSVParserTest {
//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.getRecords();
- assertEquals(3, data.length);
+ List<CSVRecord> records = parser.getRecords();
+ assertEquals(3, records.size());
}
@Test
- public void testForEach() {
- List<String[]> records = new ArrayList<String[]>();
+ public void testForEach() throws Exception {
+ List<CSVRecord> records = new ArrayList<CSVRecord>();
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
- for (String[] record : CSVFormat.DEFAULT.parse(in)) {
+ for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
records.add(record);
}
assertEquals(3, records.size());
- assertTrue(Arrays.equals(new String[]{"a", "b", "c"}, records.get(0)));
- assertTrue(Arrays.equals(new String[]{"1", "2", "3"}, records.get(1)));
- assertTrue(Arrays.equals(new String[]{"x", "y", "z"}, records.get(2)));
+ assertTrue(Arrays.equals(new String[]{"a", "b", "c"},
records.get(0).values()));
+ assertTrue(Arrays.equals(new String[]{"1", "2", "3"},
records.get(1).values()));
+ assertTrue(Arrays.equals(new String[]{"x", "y", "z"},
records.get(2).values()));
}
@Test
- public void testIterator() {
+ public void testIterator() throws Exception {
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
- Iterator<String[]> iterator = CSVFormat.DEFAULT.parse(in).iterator();
+ Iterator<CSVRecord> iterator = CSVFormat.DEFAULT.parse(in).iterator();
assertTrue(iterator.hasNext());
try {
@@ -449,12 +442,12 @@ public class CSVParserTest {
fail("expected UnsupportedOperationException");
} catch (UnsupportedOperationException expected) {
}
- assertTrue(Arrays.equals(new String[]{"a", "b", "c"},
iterator.next()));
- assertTrue(Arrays.equals(new String[]{"1", "2", "3"},
iterator.next()));
+ assertTrue(Arrays.equals(new String[]{"a", "b", "c"},
iterator.next().values()));
+ assertTrue(Arrays.equals(new String[]{"1", "2", "3"},
iterator.next().values()));
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
assertTrue(iterator.hasNext());
- assertTrue(Arrays.equals(new String[]{"x", "y", "z"},
iterator.next()));
+ assertTrue(Arrays.equals(new String[]{"x", "y", "z"},
iterator.next().values()));
assertFalse(iterator.hasNext());
try {
@@ -464,4 +457,38 @@ public class CSVParserTest {
// expected
}
}
+
+ @Test
+ public void testHeader() throws Exception {
+ Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+
+ Iterator<CSVRecord> records =
CSVFormat.DEFAULT.withHeader().parse(in).iterator();
+
+ for (int i = 0; i < 2; i++) {
+ assertTrue(records.hasNext());
+ CSVRecord record = records.next();
+ assertEquals(record.get(0), record.get("a"));
+ assertEquals(record.get(1), record.get("b"));
+ assertEquals(record.get(2), record.get("c"));
+ }
+
+ assertFalse(records.hasNext());
+ }
+
+ @Test
+ public void testProvidedHeader() throws Exception {
+ Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
+
+ Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B",
"C").parse(in).iterator();
+
+ for (int i = 0; i < 3; i++) {
+ assertTrue(records.hasNext());
+ CSVRecord record = records.next();
+ assertEquals(record.get(0), record.get("A"));
+ assertEquals(record.get(1), record.get("B"));
+ assertEquals(record.get(2), record.get("C"));
+ }
+
+ assertFalse(records.hasNext());
+ }
}
Modified:
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1301852&r1=1301851&r2=1301852&view=diff
==============================================================================
---
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
(original)
+++
commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Sat Mar 17 01:39:04 2012
@@ -19,6 +19,7 @@ package org.apache.commons.csv;
import java.io.IOException;
import java.io.StringWriter;
+import java.util.List;
import java.util.Random;
import org.junit.Test;
@@ -178,7 +179,7 @@ public class CSVPrinterTest {
// System.out.println("### :" + printable(result));
CSVParser parser = new CSVParser(result, format);
- String[][] parseResult = parser.getRecords();
+ List<CSVRecord> parseResult = parser.getRecords();
if (!equals(lines, parseResult)) {
System.out.println("Printer output :" + printable(result));
@@ -186,13 +187,13 @@ public class CSVPrinterTest {
}
}
- public static boolean equals(String[][] a, String[][] b) {
- if (a.length != b.length) {
+ public static boolean equals(String[][] a, List<CSVRecord> b) {
+ if (a.length != b.size()) {
return false;
}
for (int i = 0; i < a.length; i++) {
String[] linea = a[i];
- String[] lineb = b[i];
+ String[] lineb = b.get(i).values();
if (linea.length != lineb.length) {
return false;
}