Skip to content

Commit 3cd5c85

Browse files
committed
[CVS-92] Need a way to extract parsed headers, e.g. for use in formatting output.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1383934 13f79535-47bb-0310-9956-ffa450edef68
1 parent 25f4b3e commit 3cd5c85

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.csv.Token.Type.TOKEN;
21+
2022
import java.io.IOException;
2123
import java.io.Reader;
2224
import java.io.StringReader;
2325
import java.util.ArrayList;
2426
import java.util.HashMap;
2527
import java.util.Iterator;
28+
import java.util.LinkedHashMap;
2629
import java.util.List;
2730
import java.util.Map;
2831
import java.util.NoSuchElementException;
2932

30-
import static org.apache.commons.csv.Token.Type.*;
31-
3233
/**
3334
* Parses CSV files according to the specified configuration.
3435
*
@@ -121,6 +122,18 @@ public CSVParser(String input, CSVFormat format) throws IOException {
121122
this(new StringReader(input), format);
122123
}
123124

125+
/**
126+
* Returns a copy of the header map that iterates in column order.
127+
* <p>
128+
* The map keys are column names.
129+
* The map values are 0-based indices.
130+
*
131+
* @return a copy of the header map that iterates in column order.
132+
*/
133+
public Map<String, Integer> getHeaderMap() {
134+
return new LinkedHashMap<String, Integer>(headerMap);
135+
}
136+
124137
/**
125138
* Returns the current line number in the input stream.
126139
* <p/>
@@ -206,7 +219,7 @@ public List<CSVRecord> getRecords() throws IOException {
206219
private Map<String, Integer> initializeHeader(CSVFormat format) throws IOException {
207220
Map<String, Integer> hdrMap = null;
208221
if (format.getHeader() != null) {
209-
hdrMap = new HashMap<String, Integer>();
222+
hdrMap = new LinkedHashMap<String, Integer>();
210223

211224
String[] header = null;
212225
if (format.getHeader().length == 0) {

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import java.util.ArrayList;
2424
import java.util.Iterator;
2525
import java.util.List;
26+
import java.util.Map;
2627
import java.util.NoSuchElementException;
2728

29+
import junit.framework.Assert;
30+
2831
import org.junit.Ignore;
2932
import org.junit.Test;
3033

@@ -502,6 +505,28 @@ public void testProvidedHeader() throws Exception {
502505
assertFalse(records.hasNext());
503506
}
504507

508+
public void testGetHeaderMap() throws Exception {
509+
final CSVParser parser = new CSVParser("a,b,c\n1,2,3\nx,y,z", CSVFormat.DEFAULT.withHeader("A", "B", "C"));
510+
final Map<String, Integer> headerMap = parser.getHeaderMap();
511+
final Iterator<String> columnNames = headerMap.keySet().iterator();
512+
// Headers are iterated in column order.
513+
Assert.assertEquals("A", columnNames.next());
514+
Assert.assertEquals("B", columnNames.next());
515+
Assert.assertEquals("C", columnNames.next());
516+
Iterator<CSVRecord> records = parser.iterator();
517+
518+
// Parse to make sure getHeaderMap did not have a side-effect.
519+
for (int i = 0; i < 3; i++) {
520+
assertTrue(records.hasNext());
521+
CSVRecord record = records.next();
522+
assertEquals(record.get(0), record.get("A"));
523+
assertEquals(record.get(1), record.get("B"));
524+
assertEquals(record.get(2), record.get("C"));
525+
}
526+
527+
assertFalse(records.hasNext());
528+
}
529+
505530
@Test
506531
public void testGetLineNumberWithLF() throws Exception {
507532
CSVParser parser = new CSVParser("a\nb\nc", CSVFormat.DEFAULT.withLineSeparator("\n"));

0 commit comments

Comments
 (0)