X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.excel.poi%2Fsrc%2Forg%2Fsimantics%2Fexcel%2Fpoi%2Fparser%2FColumnResolver.java;fp=org.simantics.excel.poi%2Fsrc%2Forg%2Fsimantics%2Fexcel%2Fpoi%2Fparser%2FColumnResolver.java;h=1aaec8039c8dc455afd1cbd51411657c60e3cc73;hb=7abd05645636ea510269d77005717f43c2c445ba;hp=0000000000000000000000000000000000000000;hpb=f11cbe76b3f4be142c9f84ef9a7b6bc9dcc8ff23;p=simantics%2Finterop.git diff --git a/org.simantics.excel.poi/src/org/simantics/excel/poi/parser/ColumnResolver.java b/org.simantics.excel.poi/src/org/simantics/excel/poi/parser/ColumnResolver.java new file mode 100644 index 0000000..1aaec80 --- /dev/null +++ b/org.simantics.excel.poi/src/org/simantics/excel/poi/parser/ColumnResolver.java @@ -0,0 +1,106 @@ +package org.simantics.excel.poi.parser; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; + +public class ColumnResolver { + + private enum Type {SINGLE,MULTI}; + + private Map values = new HashMap(); + private Map types = new HashMap(); + private Set optional = new HashSet(); + private Set resolved = new HashSet(); + + public void addSingleColumn(String id) { + addSingleColumn(id, true); + } + + public void addSingleColumn(String id, boolean required) { + types.put(id, Type.SINGLE); + if (!required) + this.optional.add(id); + } + + public void addMultiColumn(String id) { + addMultiColumn(id, true); + } + + public void addMultiColumn(String id, boolean required) { + types.put(id, Type.MULTI); + if (!required) + this.optional.add(id); + } + + + public int getSingleColumn(String id) { + int[] val = values.get(id); + if (val == null) + return -1; + if (val.length == 0) + return -1; + return val[0]; + } + + public int[] getMultiColumn(String id) { + return values.get(id); + } + + public boolean resolve(Row row) { + return resolve(row, true); + } + + public boolean resolve(Row row, boolean ignoreCase) { + values.clear(); + resolved.clear(); + if (row == null) { + return false; + } + + for (int i = 0; i <= row.getLastCellNum(); i++) { + Cell cell = row.getCell(i); + if (cell == null) + continue; + int colIndex = cell.getColumnIndex(); + String value = cell.getStringCellValue(); + for (Entry col : types.entrySet()) { + String id = col.getKey(); + if (id.equals(value) || ignoreCase && id.equalsIgnoreCase(value)) { + if (col.getValue() == Type.SINGLE) { + values.put(id, new int[]{colIndex}); + } else { + int curr[] = values.get(id); + if (curr == null) { + values.put(id, new int[]{colIndex}); + } else { + int n[] = new int[curr.length+1]; + System.arraycopy(curr, 0, n, 0, curr.length); + n[curr.length] = colIndex; + values.put(id, n); + } + + } + resolved.add(id); + break; + } + } + } + // check that required columns have been resolved + for (Entry col : types.entrySet()) { + if (!optional.contains(col.getKey())) { + if (!resolved.contains(col.getKey())) + return false; + } + } + return true; + + } + + +}