Search in sources :

Example 6 with InvalidFileException

use of nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException in project timbuctoo by HuygensING.

the class XlsxLoader method loadData.

@Override
public void loadData(List<Tuple<String, File>> files, Importer importer) throws InvalidFileException, IOException {
    try {
        RowCellHandler rowCellHandler = makeRowCellHandler(importer);
        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
        saxFactory.setNamespaceAware(true);
        for (Tuple<String, File> file : files) {
            OPCPackage pkg = OPCPackage.open(file.getRight().getPath());
            XSSFReader xssfReader = new XSSFReader(pkg);
            final SharedStringsTable sharedStringsTable = xssfReader.getSharedStringsTable();
            XSSFReader.SheetIterator worksheets = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
            while (worksheets.hasNext()) {
                final InputStream sheet = worksheets.next();
                XMLReader sheetParser = saxFactory.newSAXParser().getXMLReader();
                sheetParser.setContentHandler(new SheetXmlParser(sharedStringsTable, rowCellHandler));
                rowCellHandler.start(worksheets.getSheetName());
                sheetParser.parse(new InputSource(sheet));
                rowCellHandler.finish();
            }
        }
    } catch (SAXException | OpenXML4JException | ParserConfigurationException e) {
        throw new InvalidFileException("Not a valid Excel file", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) SAXException(org.xml.sax.SAXException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) InvalidFileException(nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Aggregations

InvalidFileException (nl.knaw.huygens.timbuctoo.bulkupload.InvalidFileException)6 File (java.io.File)5 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ImporterStubs (nl.knaw.huygens.timbuctoo.bulkupload.parsingstatemachine.ImporterStubs)2 Tuple.tuple (nl.knaw.huygens.timbuctoo.util.Tuple.tuple)2 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 ZipEntry (java.util.zip.ZipEntry)1 ZipException (java.util.zip.ZipException)1 ZipInputStream (java.util.zip.ZipInputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1