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