@SuppressWarnings("unchecked") public static <T extends POITextExtractor> T createExtractor(File f) throws IOException, OpenXML4JException, XmlException { POIFSFileSystem fs = null; try { fs = new POIFSFileSystem(f); if (fs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { return (T)createEncryptedOOXMLExtractor(fs); } POITextExtractor extractor = createExtractor(fs); extractor.setFilesystem(fs); return (T)extractor; } catch (OfficeXmlFileException e) { // ensure file-handle release IOUtils.closeQuietly(fs); OPCPackage pkg = OPCPackage.open(f.toString(), PackageAccess.READ); T t = (T)createExtractor(pkg); t.setFilesystem(pkg); return t; } catch (NotOLE2FileException ne) { // ensure file-handle release IOUtils.closeQuietly(fs); throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file"); } catch (OpenXML4JException | Error | RuntimeException | IOException | XmlException e) { // NOSONAR // ensure file-handle release IOUtils.closeQuietly(fs); throw e; } }
@SuppressWarnings("unchecked") public static <T extends POITextExtractor> T createExtractor(File f) throws IOException, OpenXML4JException, XmlException { POIFSFileSystem fs = null; try { fs = new POIFSFileSystem(f); if (fs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { return (T)createEncryptedOOXMLExtractor(fs); } POITextExtractor extractor = createExtractor(fs); extractor.setFilesystem(fs); return (T)extractor; } catch (OfficeXmlFileException e) { // ensure file-handle release IOUtils.closeQuietly(fs); OPCPackage pkg = OPCPackage.open(f.toString(), PackageAccess.READ); T t = (T)createExtractor(pkg); t.setFilesystem(pkg); return t; } catch (NotOLE2FileException ne) { // ensure file-handle release IOUtils.closeQuietly(fs); throw new IllegalArgumentException("Your File was neither an OLE2 file, nor an OOXML file"); } catch (OpenXML4JException | Error | RuntimeException | IOException | XmlException e) { // NOSONAR // ensure file-handle release IOUtils.closeQuietly(fs); throw e; } }