]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.excel.poi/src/org/simantics/excel/poi/parser/streaming/XSSSheetHandler.java
Excel parsing interface
[simantics/interop.git] / org.simantics.excel.poi / src / org / simantics / excel / poi / parser / streaming / XSSSheetHandler.java
diff --git a/org.simantics.excel.poi/src/org/simantics/excel/poi/parser/streaming/XSSSheetHandler.java b/org.simantics.excel.poi/src/org/simantics/excel/poi/parser/streaming/XSSSheetHandler.java
new file mode 100644 (file)
index 0000000..5df804a
--- /dev/null
@@ -0,0 +1,120 @@
+package org.simantics.excel.poi.parser.streaming;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.apache.poi.ss.usermodel.Row;\r
+import org.apache.poi.xssf.model.SharedStringsTable;\r
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;\r
+import org.xml.sax.Attributes;\r
+import org.xml.sax.SAXException;\r
+import org.xml.sax.helpers.DefaultHandler;\r
+\r
+public class XSSSheetHandler extends DefaultHandler {\r
+       private SharedStringsTable sst;\r
+       private String lastContents;\r
+       private boolean nextIsString;\r
+       \r
+       int rowIndex = 0;\r
+       List<String> row = new ArrayList<String>();\r
+       List<String> rowTypes = new ArrayList<String>();\r
+       \r
+       String cellType;\r
+       String cellId;\r
+       \r
+       SheetImpl sheet;\r
+       \r
+       public XSSSheetHandler(SharedStringsTable sst) {\r
+               this.sst = sst;\r
+       }\r
+       \r
+       public void setSheet(SheetImpl sheet) {\r
+               this.sheet = sheet;\r
+       }\r
+       \r
+       public void writeRow(Row row) {\r
+               System.out.println(row);\r
+       }\r
+       \r
+       public void startElement(String uri, String localName, String name,\r
+                       Attributes attributes) throws SAXException {\r
+               // c => cell\r
+//             System.out.println(name);\r
+//             for (int i = 0; i < attributes.getLength(); i++) {\r
+//                     System.out.println("  " + attributes.getLocalName(i) + "; "+attributes.getValue(i) + "; " + attributes.getType(i) );\r
+//                     \r
+//             }\r
+               if(name.equals("c")) {\r
+                       // Figure out if the value is an index in the SST\r
+                       cellType = attributes.getValue("t");\r
+                       cellId = attributes.getValue("r");\r
+                       if(cellType != null) {\r
+                               if (cellType.equals("s"))\r
+                                       nextIsString = true;\r
+                               else\r
+                                       nextIsString = false;\r
+                       } else {\r
+                               nextIsString = false;\r
+                               cellType = "v";\r
+                       }\r
+               } \r
+               // Clear contents cache\r
+               lastContents = "";\r
+       }\r
+       \r
+       public void endElement(String uri, String localName, String name)\r
+                       throws SAXException {\r
+               // Process the last contents as required.\r
+               // Do now, as characters() may be called more than once\r
+               if(nextIsString) {\r
+                       int idx = Integer.parseInt(lastContents);\r
+                       lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();\r
+                       nextIsString = false;\r
+               } \r
+\r
+               // v => contents of a cell\r
+               // Output after we've seen the string contents\r
+               if(name.equals("c")) {\r
+                       int colIndex = getColIndex(cellId);\r
+                       for (int i = row.size(); i< colIndex; i++) {\r
+                               row.add(null);\r
+                               rowTypes.add("s");\r
+                       }\r
+                       row.add(lastContents);\r
+                       rowTypes.add(cellType);\r
+                       //System.out.println(lastContents);\r
+                       lastContents = "";\r
+               } else if (name.equals("row")) {\r
+                       writeRow( new RowImpl(sheet, row, rowTypes,rowIndex));\r
+                       rowIndex++;\r
+                       row.clear();\r
+                       rowTypes.clear();\r
+               }\r
+       }\r
+\r
+       public void characters(char[] ch, int start, int length)\r
+                       throws SAXException {\r
+               lastContents += new String(ch, start, length);\r
+//             System.out.println("char : " + lastContents);\r
+       }\r
+\r
+       private int getColIndex(String id) {\r
+               int len = 0;\r
+               int range = 'Z'-'A'+1;\r
+               for (int i = 0; i < id.length(); i++) {\r
+                       char c = id.charAt(i) ;\r
+                       if ('A' <= c && c <= 'Z') {\r
+                               len = i+1;\r
+                       } else {\r
+                               break;\r
+                       }\r
+               }\r
+               int index = 0;\r
+               for (int i = 0; i < len; i++) {\r
+                       char c = id.charAt(len-i-1);\r
+                       index += (c-'A') + i * range;\r
+               }\r
+               return index;\r
+               \r
+       }\r
+}\r