]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.enterprise/src/org/argeo/util/CsvParserWithLinesAsMap.java
Make CMS Login refresh more robust.
[lgpl/argeo-commons.git] / org.argeo.enterprise / 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 the current line number, starts at 1 (the header, if header
17 * processing is enabled, the first lien otherwise)
18 * @param line the parsed tokens as a map whose keys are the header fields
19 */
20 protected abstract void processLine(Integer lineNumber, Map<String, String> line);
21
22 protected final void processLine(Integer lineNumber, List<String> header, List<String> tokens) {
23 if (header == null)
24 throw new IllegalArgumentException("Only CSV with header is supported");
25 Map<String, String> line = new HashMap<String, String>();
26 for (int i = 0; i < header.size(); i++) {
27 String key = header.get(i);
28 String value = null;
29 if (i < tokens.size())
30 value = tokens.get(i);
31 line.put(key, value);
32 }
33 processLine(lineNumber, line);
34 }
35
36 }