]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/CsvParserWithLinesAsMap.java
2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.util
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
23 * CSV parser allowing to process lines as maps whose keys are the header
26 public abstract class CsvParserWithLinesAsMap
extends CsvParser
{
29 * Actually processes a line.
32 * the current line number, starts at 1 (the header, if header
33 * processing is enabled, the first lien otherwise)
35 * the parsed tokens as a map whose keys are the header fields
37 protected abstract void processLine(Integer lineNumber
,
38 Map
<String
, String
> line
);
40 protected final void processLine(Integer lineNumber
, List
<String
> header
,
41 List
<String
> tokens
) {
43 throw new UtilsException("Only CSV with header is supported");
44 Map
<String
, String
> line
= new HashMap
<String
, String
>();
45 for (int i
= 0; i
< header
.size(); i
++) {
46 String key
= header
.get(i
);
48 if (i
< tokens
.size())
49 value
= tokens
.get(i
);
52 processLine(lineNumber
, line
);