When withHeader is set to any non-null value, the first record is the first <em>data</em> record, not the header record.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1508612 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
390800f288
commit
1533facb19
|
@ -614,22 +614,24 @@ public class CSVFormat implements Serializable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sets the header of the format. The header can either be parsed automatically from the
|
||||
* input file with:
|
||||
*
|
||||
* Sets the header of the format. The header can either be parsed automatically from the input file with:
|
||||
*
|
||||
* <pre>
|
||||
* CSVFormat format = aformat.withHeader();
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* or specified manually with:
|
||||
*
|
||||
*
|
||||
* <pre>
|
||||
* CSVFormat format = aformat.withHeader("name", "email", "phone");
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* When this option is is set to any non-null value, the first record is the first <em>data</em> record, not the
|
||||
* header record.
|
||||
*
|
||||
* @param header
|
||||
* the header, <tt>null</tt> if disabled, empty if parsed automatically, user specified otherwise.
|
||||
*
|
||||
*
|
||||
* @return A new CSVFormat that is equal to this but with the specified header
|
||||
*/
|
||||
public CSVFormat withHeader(final String... header) {
|
||||
|
|
|
@ -323,12 +323,12 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
|
|||
Map<String, Integer> hdrMap = null;
|
||||
String[] formatHeader = this.format.getHeader();
|
||||
if (formatHeader != null) {
|
||||
final CSVRecord record = this.nextRecord();
|
||||
hdrMap = new LinkedHashMap<String, Integer>();
|
||||
|
||||
String[] header = null;
|
||||
if (formatHeader.length == 0) {
|
||||
// read the header from the first line of the file
|
||||
final CSVRecord record = this.nextRecord();
|
||||
if (record != null) {
|
||||
header = record.values();
|
||||
}
|
||||
|
|
|
@ -506,6 +506,26 @@ public class CSVParserTest {
|
|||
assertFalse(records.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipSetHeader() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("a", "b", "c").parse(in).iterator();
|
||||
final CSVRecord record = records.next();
|
||||
assertEquals("1", record.get("a"));
|
||||
assertEquals("2", record.get("b"));
|
||||
assertEquals("3", record.get("c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSkipAutoHeader() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
|
||||
final CSVRecord record = records.next();
|
||||
assertEquals("1", record.get("a"));
|
||||
assertEquals("2", record.get("b"));
|
||||
assertEquals("3", record.get("c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderComment() throws Exception {
|
||||
final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
|
||||
|
@ -529,7 +549,7 @@ public class CSVParserTest {
|
|||
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator();
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertTrue(records.hasNext());
|
||||
final CSVRecord record = records.next();
|
||||
assertTrue(record.isMapped("A"));
|
||||
|
@ -544,25 +564,32 @@ public class CSVParserTest {
|
|||
assertFalse(records.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProvidedHeaderAuto() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
|
||||
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertTrue(records.hasNext());
|
||||
final CSVRecord record = records.next();
|
||||
assertTrue(record.isMapped("a"));
|
||||
assertTrue(record.isMapped("b"));
|
||||
assertTrue(record.isMapped("c"));
|
||||
assertFalse(record.isMapped("NOT MAPPED"));
|
||||
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 testMappedButNotSetAsOutlook2007ContactExport() throws Exception {
|
||||
final Reader in = new StringReader("a,b,c\n1,2\nx,y,z");
|
||||
|
||||
final Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader("A", "B", "C").parse(in).iterator();
|
||||
|
||||
// header record
|
||||
assertTrue(records.hasNext());
|
||||
CSVRecord record = records.next();
|
||||
assertTrue(record.isMapped("A"));
|
||||
assertTrue(record.isMapped("B"));
|
||||
assertTrue(record.isMapped("C"));
|
||||
assertTrue(record.isSet("A"));
|
||||
assertTrue(record.isSet("B"));
|
||||
assertTrue(record.isSet("C"));
|
||||
assertEquals("a", record.get("A"));
|
||||
assertEquals("b", record.get("B"));
|
||||
assertEquals("c", record.get("C"));
|
||||
assertTrue(record.isConsistent());
|
||||
CSVRecord record;
|
||||
|
||||
// 1st record
|
||||
record = records.next();
|
||||
|
@ -604,7 +631,7 @@ public class CSVParserTest {
|
|||
final Iterator<CSVRecord> records = parser.iterator();
|
||||
|
||||
// Parse to make sure getHeaderMap did not have a side-effect.
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertTrue(records.hasNext());
|
||||
final CSVRecord record = records.next();
|
||||
assertEquals(record.get(0), record.get("A"));
|
||||
|
|
Loading…
Reference in New Issue