1 package org.simantics.excel.poi.parser;
\r
3 import java.util.HashMap;
\r
4 import java.util.HashSet;
\r
5 import java.util.Map;
\r
6 import java.util.Map.Entry;
\r
7 import java.util.Set;
\r
9 import org.apache.poi.ss.usermodel.Cell;
\r
10 import org.apache.poi.ss.usermodel.Row;
\r
12 public class ColumnResolver {
\r
14 private enum Type {SINGLE,MULTI};
\r
16 private Map<String,int[]> values = new HashMap<String, int[]>();
\r
17 private Map<String,Type> types = new HashMap<String, Type>();
\r
18 private Set<String> optional = new HashSet<String>();
\r
19 private Set<String> resolved = new HashSet<String>();
\r
21 public void addSingleColumn(String id) {
\r
22 addSingleColumn(id, true);
\r
25 public void addSingleColumn(String id, boolean required) {
\r
26 types.put(id, Type.SINGLE);
\r
28 this.optional.add(id);
\r
31 public void addMultiColumn(String id) {
\r
32 addMultiColumn(id, true);
\r
35 public void addMultiColumn(String id, boolean required) {
\r
36 types.put(id, Type.MULTI);
\r
38 this.optional.add(id);
\r
42 public int getSingleColumn(String id) {
\r
43 int[] val = values.get(id);
\r
46 if (val.length == 0)
\r
51 public int[] getMultiColumn(String id) {
\r
52 return values.get(id);
\r
55 public boolean resolve(Row row) {
\r
56 return resolve(row, true);
\r
59 public boolean resolve(Row row, boolean ignoreCase) {
\r
66 for (int i = 0; i <= row.getLastCellNum(); i++) {
\r
67 Cell cell = row.getCell(i);
\r
70 int colIndex = cell.getColumnIndex();
\r
71 String value = cell.getStringCellValue();
\r
72 for (Entry<String, Type> col : types.entrySet()) {
\r
73 String id = col.getKey();
\r
74 if (id.equals(value) || ignoreCase && id.equalsIgnoreCase(value)) {
\r
75 if (col.getValue() == Type.SINGLE) {
\r
76 values.put(id, new int[]{colIndex});
\r
78 int curr[] = values.get(id);
\r
80 values.put(id, new int[]{colIndex});
\r
82 int n[] = new int[curr.length+1];
\r
83 System.arraycopy(curr, 0, n, 0, curr.length);
\r
84 n[curr.length] = colIndex;
\r
94 // check that required columns have been resolved
\r
95 for (Entry<String, Type> col : types.entrySet()) {
\r
96 if (!optional.contains(col.getKey())) {
\r
97 if (!resolved.contains(col.getKey()))
\r