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