FileSpewer extends Spewer implements Serializable { @Override protected void writeDocument(TikaDocument tikaDocument, TikaDocument parent, TikaDocument root, int level) throws IOException { final Path outputPath = getOutputPath(tikaDocument); Path contentsOutputPath; if (null != outputExtension) { contentsOutputPath = outputPath.getFileSystem().getPath(outputPath.toString() + "." + outputExtension); } else { contentsOutputPath = outputPath; } logger.info(String.format("Outputting to file: \"%s\".", contentsOutputPath)); final Path outputParent = contentsOutputPath.getParent(); if (null != outputParent) { final File outputFileParent = outputParent.toFile(); final boolean madeDirs = outputFileParent.mkdirs(); if (!madeDirs && !outputFileParent.isDirectory()) { throw new TaggedIOException(new IOException(String.format("Unable to make directories for file: \"%s\".", contentsOutputPath)), this); } } TaggedOutputStream tagged = null; try (final OutputStream output = Files.newOutputStream(contentsOutputPath)) { tagged = new TaggedOutputStream(output); copy(tikaDocument.getReader(), tagged); } catch (IOException e) { if (null != tagged && tagged.isCauseOf(e)) { throw new TaggedIOException(new IOException(String.format("Error writing output to file: \"%s\".", contentsOutputPath), e), this); } else { throw e; } } if (outputMetadata) { writeMetadata(tikaDocument); } } FileSpewer(final FieldNames fields); @Override FileSpewer configure(final Options<String> options); void setOutputDirectory(final Path outputDirectory); @Override void close();  }
@Test public void test_spew_root_document_in_memory() throws Exception { final TikaDocument tikaDocument = factory.create("test.txt"); tikaDocument.setReader(new StringReader("this is a content")); fileSpewer.writeDocument(tikaDocument, null, null, 0); String[] list = folder.getRoot().list(); assertThat(list).isNotEmpty(); assertThat(list).contains("test.txt.json", "test.txt.txt"); assertThat( readAllLines(folder.getRoot().toPath().resolve("test.txt.json"), Charset.defaultCharset())).contains("{ }"); assertThat( readAllLines(folder.getRoot().toPath().resolve("test.txt.txt"), Charset.defaultCharset())).contains("this is a content"); }
@Test public void test_spew_root_document_on_filesystem() throws Exception { final Extractor extractor = new Extractor(factory); Path path = get(getClass().getResource("/documents/text/plain.txt").getPath()); TikaDocument tikaDocument = extractor.extract(path); fileSpewer.writeDocument(tikaDocument, null, null, 0); String[] list = folder.getRoot().list(); assertThat(list).isNotEmpty(); assertThat(list).contains("home"); assertThat(readAllLines(get(folder.getRoot().toPath().resolve(path.toString().substring(1)).toString() + ".json"), Charset.defaultCharset())). hasSize(8); assertThat(readAllLines(get(folder.getRoot().toPath().resolve(path.toString().substring(1)).toString() + ".txt"), Charset.defaultCharset())). contains("This is a test."); }