]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.excel.poi/src/org/simantics/excel/poi/parser/CellDataResolverBase.java
CellDataResolverBase causes an exception when processing empty rows.
[simantics/interop.git] / org.simantics.excel.poi / src / org / simantics / excel / poi / parser / CellDataResolverBase.java
1 package org.simantics.excel.poi.parser;
2
3 import org.apache.poi.ss.usermodel.Cell;
4 import org.apache.poi.ss.usermodel.Row;
5
6 public abstract class CellDataResolverBase<T> implements DataResolver<T> {
7         
8                 
9         protected Double getCellNumericValue(Cell cell) {
10                 if (cell == null)
11                         return null;
12                 switch (cell.getCellTypeEnum()) {
13                 case BLANK:
14                         return null;
15                 case BOOLEAN:
16                         return null;
17                 case ERROR:
18                         return null;
19                 case FORMULA:
20                         return null;
21                 case NUMERIC:
22                         return cell.getNumericCellValue();
23                 case STRING:
24                         return null;
25                 case _NONE:
26                         return null;
27                 }
28                 throw new RuntimeException();
29         }
30         
31         protected String getCellStringValue(Cell cell) {
32                 if (cell == null)
33                         return null;
34                 switch (cell.getCellTypeEnum()) {
35                 case BLANK:
36                         return null;
37                 case BOOLEAN:
38                         return Boolean.toString(cell.getBooleanCellValue());
39                 case ERROR:
40                         return null;
41                 case FORMULA:
42                         return null;
43                 case NUMERIC:
44                         return Double.toString(cell.getNumericCellValue());
45                 case STRING:
46                         return cell.getStringCellValue();
47                 case _NONE:
48                         return null;
49                 }
50                 throw new RuntimeException();
51         }
52         
53         protected Boolean getCellBooleanValue(Cell cell) {
54                 if (cell == null)
55                         return null;
56                 switch (cell.getCellTypeEnum()) {
57                 case BLANK:
58                         return null;
59                 case BOOLEAN:
60                         return cell.getBooleanCellValue();
61                 case ERROR:
62                         return null;
63                 case FORMULA:
64                         return null;
65                 case NUMERIC:
66                         return null;
67                 case STRING:
68                         String value = cell.getStringCellValue();
69                         if ("true".equalsIgnoreCase(value))
70                                 return true;
71                         if ("1".equalsIgnoreCase(value))
72                                 return true;
73                         return false;
74                 case _NONE:
75                         return null;
76                 
77                 }
78                 throw new RuntimeException();
79         }
80         
81         protected Cell getCell(Row row, int colIndex) {
82                 short first = row.getFirstCellNum();
83                 short last = row.getLastCellNum();
84                 if (first < 0)
85                         return null;
86                 for (short i = first; i <= last; i++) {
87                         Cell c = row.getCell(i);
88                         if (c == null)
89                                 continue;
90                         if (c.getColumnIndex() == colIndex)
91                                 return c;
92                         
93                 }
94                 return null;
95         }
96
97 }