Extractor { public void extract(final Path path, final Spewer spewer) throws IOException { long before = currentTimeMillis(); TikaDocument document = extract(path); logger.info("{} extracted in {}ms", path, currentTimeMillis() - before); spewer.write(document); } Extractor(final DocumentFactory factory);  Extractor(); Extractor configure(final Options<String> options); void setOutputFormat(final OutputFormat outputFormat); OutputFormat getOutputFormat(); void setEmbedHandling(final EmbedHandling embedHandling); EmbedHandling getEmbedHandling(); void setEmbedOutputPath(final Path embedOutput); Path getEmbedOutputPath(); void setOcrLanguage(final String ocrLanguage); void setOcrTimeout(final Duration duration); void setDigestAlgorithm(final String digestAlgorithm); void setDigester(final DigestingParser.Digester digester); void disableOcr(); void extract(final Path path, final Spewer spewer); void extract(final Path path, final Spewer spewer, final Reporter reporter); TikaDocument extract(final Path path);  }
@Test public void testOcr() throws Throwable { String text; TikaDocument tikaDocument = extractor.extract(Paths.get(getClass().getResource("/documents/ocr/simple.tiff").getPath())); try (Reader reader = tikaDocument.getReader()) { text = Spewer.toString(reader); } Assert.assertEquals("image/tiff", tikaDocument.getMetadata().get(Metadata.CONTENT_TYPE)); Assert.assertEquals("HEAVY\nMETAL", text.trim()); }
@Test public void testRtfFile() throws Throwable { String text; TikaDocument tikaDocument = extractor.extract(Paths.get(getClass().getResource("/documents/text/doc.rtf").getPath())); try (Reader reader = tikaDocument.getReader()) { text = Spewer.toString(reader); } Assert.assertEquals("application/rtf", tikaDocument.getMetadata().get(Metadata.CONTENT_TYPE)); Assert.assertEquals("RTF Text Document", text.trim()); }
@Test public void testFileNotFound() throws Throwable { thrown.expect(NoSuchFileException.class); thrown.expectMessage("nothing"); extractor.extract(Paths.get("nothing")); }
@Test public void testEncryptedPdf() throws Throwable { thrown.expect(IOException.class); thrown.expectMessage(""); thrown.expectCause(new CauseMatcher(EncryptedDocumentException.class, "Unable to process: document is encrypted")); final int read; TikaDocument tikaDocument = extractor.extract(Paths.get(getClass().getResource("/documents/pdf/encrypted.pdf").getPath())); try (final Reader reader = tikaDocument.getReader()) { read = reader.read(); } catch (IOException e) { Assert.assertEquals("application/pdf", tikaDocument.getMetadata().get(Metadata.CONTENT_TYPE)); throw e; } Assert.fail(String.format("Read \"%d\" while expecting exception.", read)); }
@Test public void testGarbage() throws Throwable { TikaDocument tikaDocument = extractor.extract(Paths.get(getClass().getResource("/documents/garbage.bin").getPath())); thrown.expect(IOException.class); thrown.expectMessage(""); thrown.expectCause(new CauseMatcher(TikaException.class, "Parse error")); final int read; try (final Reader reader = tikaDocument.getReader()) { read = reader.read(); } catch (IOException e) { Assert.assertEquals("application/octet-stream", tikaDocument.getMetadata().get(Metadata.CONTENT_TYPE)); throw e; } Assert.fail(String.format("Read \"%d\" while expecting exception.", read)); }
@Test public void testEmbeds() throws Throwable { TikaDocument tikaDocument = extractor.extract(Paths.get(getClass().getResource("/documents/ocr/embedded.pdf").getPath())); String text; try (final Reader reader = tikaDocument.getReader()) { text = Spewer.toString(reader); } Assert.assertEquals("application/pdf", tikaDocument.getMetadata().get(Metadata.CONTENT_TYPE)); Assert.assertThat(text, RegexMatcher.matchesRegex("^\\s+HEAVY\\sMETAL\\s+HEAVY\\sMETAL\\s+$")); }
@Test public void testRecursiveEmbedded() throws Exception { TikaDocument tikaDocument = extractor.extract(Paths.get(getClass().getResource("/documents/recursive_embedded.docx").getPath())); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); new PrintStreamSpewer(new PrintStream(outputStream), new FieldNames()).write(tikaDocument); String allContents = new String(outputStream.toByteArray()); assertThat(allContents).contains("embed_0"); assertThat(allContents).contains("embed_1a"); assertThat(allContents).contains("embed_1b"); assertThat(allContents).contains("embed_2a"); assertThat(allContents).contains("embed_2b"); assertThat(allContents).contains("embed_3"); assertThat(allContents).contains("dissolve the political bands"); assertThat(allContents).contains("embed_4"); }