]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - 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
diff --git a/org.simantics.excel.poi/src/org/simantics/excel/poi/parser/CellDataResolverBase.java b/org.simantics.excel.poi/src/org/simantics/excel/poi/parser/CellDataResolverBase.java
new file mode 100644 (file)
index 0000000..5a78735
--- /dev/null
@@ -0,0 +1,88 @@
+package org.simantics.excel.poi.parser;\r
+\r
+import org.apache.poi.ss.usermodel.Cell;\r
+import org.apache.poi.ss.usermodel.Row;\r
+\r
+public abstract class CellDataResolverBase<T> implements DataResolver<T> {\r
+       \r
+               \r
+       protected Double getCellNumericValue(Cell cell) {\r
+               if (cell == null)\r
+                       return null;\r
+               switch (cell.getCellType()) {\r
+               case Cell.CELL_TYPE_BLANK:\r
+                       return null;\r
+               case Cell.CELL_TYPE_BOOLEAN:\r
+                       return null;\r
+               case Cell.CELL_TYPE_ERROR:\r
+                       return null;\r
+               case Cell.CELL_TYPE_FORMULA:\r
+                       return null;\r
+               case Cell.CELL_TYPE_NUMERIC:\r
+                       return cell.getNumericCellValue();\r
+               case Cell.CELL_TYPE_STRING:\r
+                       return null;\r
+                       \r
+               }\r
+               throw new RuntimeException();\r
+       }\r
+       \r
+       protected String getCellStringValue(Cell cell) {\r
+               if (cell == null)\r
+                       return null;\r
+               switch (cell.getCellType()) {\r
+               case Cell.CELL_TYPE_BLANK:\r
+                       return null;\r
+               case Cell.CELL_TYPE_BOOLEAN:\r
+                       return Boolean.toString(cell.getBooleanCellValue());\r
+               case Cell.CELL_TYPE_ERROR:\r
+                       return null;\r
+               case Cell.CELL_TYPE_FORMULA:\r
+                       return null;\r
+               case Cell.CELL_TYPE_NUMERIC:\r
+                       return Double.toString(cell.getNumericCellValue());\r
+               case Cell.CELL_TYPE_STRING:\r
+                       return cell.getStringCellValue();\r
+                       \r
+               }\r
+               throw new RuntimeException();\r
+       }\r
+       \r
+       protected Boolean getCellBooleanValue(Cell cell) {\r
+               if (cell == null)\r
+                       return null;\r
+               switch (cell.getCellType()) {\r
+               case Cell.CELL_TYPE_BLANK:\r
+                       return null;\r
+               case Cell.CELL_TYPE_BOOLEAN:\r
+                       return cell.getBooleanCellValue();\r
+               case Cell.CELL_TYPE_ERROR:\r
+                       return null;\r
+               case Cell.CELL_TYPE_FORMULA:\r
+                       return null;\r
+               case Cell.CELL_TYPE_NUMERIC:\r
+                       return null;\r
+               case Cell.CELL_TYPE_STRING:\r
+                       String value = cell.getStringCellValue();\r
+                       if ("true".equalsIgnoreCase(value))\r
+                               return true;\r
+                       if ("1".equalsIgnoreCase(value))\r
+                               return true;\r
+                       return false;           \r
+               }\r
+               throw new RuntimeException();\r
+       }\r
+       \r
+       protected Cell getCell(Row row, int colIndex) {\r
+               for (short i = row.getFirstCellNum(); i <= row.getLastCellNum(); i++) {\r
+                       Cell c = row.getCell(i);\r
+                       if (c == null)\r
+                               continue;\r
+                       if (c.getColumnIndex() == colIndex)\r
+                               return c;\r
+                       \r
+               }\r
+               return null;\r
+       }\r
+\r
+}\r