]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CsvParserWithLinesAsMap.java
aa3198fc4ddc35d70181c15b9087a597ed38567a
[lgpl/argeo-commons.git] / CsvParserWithLinesAsMap.java
1 package org.argeo.util;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.argeo.ArgeoException;
8
9 /**
10 * CSV parser allowing to process lines as maps whose keys are the header
11 * fields.
12 */
13 public abstract class CsvParserWithLinesAsMap extends CsvParser {
14
15 /**
16 * Actually processes a line.
17 *
18 * @param lineNumber
19 * the current line number, starts at 1 (the header, if header
20 * processing is enabled, the first lien otherwise)
21 * @param line
22 * the parsed tokens as a map whose keys are the header fields
23 */
24 protected abstract void processLine(Integer lineNumber,
25 Map<String, String> line);
26
27 protected final void processLine(Integer lineNumber, List<String> header,
28 List<String> tokens) {
29 if (header == null)
30 throw new ArgeoException("Only CSV with header is supported");
31 Map<String, String> line = new HashMap<String, String>();
32 for (int i = 0; i < header.size(); i++) {
33 String key = header.get(i);
34 String value = null;
35 if (i < tokens.size())
36 value = tokens.get(i);
37 line.put(key, value);
38 }
39 processLine(lineNumber, line);
40 }
41
42 }