@Override public void close() throws IOException { // e.g. XSSFEventBaseExcelExtractor passes a null-document if(_document != null) { @SuppressWarnings("resource") OPCPackage pkg = _document.getPackage(); if(pkg != null) { // revert the package to not re-write the file, which is very likely not wanted for a TextExtractor! pkg.revert(); } } super.close(); }
public static void main(String[] args) throws Exception { if (args.length < 1) { System.err.println("Use:"); System.err.println(" CommandLineTextExtractor <filename> [filename] [filename]"); System.exit(1); } for (String arg : args) { System.out.println(DIVIDER); File f = new File(arg); System.out.println(f); try (POITextExtractor extractor = ExtractorFactory.createExtractor(f)) { POITextExtractor metadataExtractor = extractor.getMetadataTextExtractor(); System.out.println(" " + DIVIDER); String metaData = metadataExtractor.getText(); System.out.println(metaData); System.out.println(" " + DIVIDER); String text = extractor.getText(); System.out.println(text); System.out.println(DIVIDER); System.out.println("Had " + metaData.length() + " characters of metadata and " + text.length() + " characters of text"); } } } }
@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; } }
public static void main(String[] args) throws Exception { if (args.length < 1) { System.err.println("Use:"); System.err.println(" CommandLineTextExtractor <filename> [filename] [filename]"); System.exit(1); } for (String arg : args) { System.out.println(DIVIDER); File f = new File(arg); System.out.println(f); try (POITextExtractor extractor = ExtractorFactory.createExtractor(f)) { POITextExtractor metadataExtractor = extractor.getMetadataTextExtractor(); System.out.println(" " + DIVIDER); String metaData = metadataExtractor.getText(); System.out.println(metaData); System.out.println(" " + DIVIDER); String text = extractor.getText(); System.out.println(text); System.out.println(DIVIDER); System.out.println("Had " + metaData.length() + " characters of metadata and " + text.length() + " characters of text"); } } } }
@Override public void close() throws IOException { // e.g. XSSFEventBaseExcelExtractor passes a null-document if(_document != null) { @SuppressWarnings("resource") OPCPackage pkg = _document.getPackage(); if(pkg != null) { // revert the package to not re-write the file, which is very likely not wanted for a TextExtractor! pkg.revert(); } } super.close(); }
@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; } }