DefaultTextExtractor implements TextExtractor { @Override public ParsedContent extractContent(InputStream inputStream, ContentType contentType) throws Exception { if (contentType != null && contentType.asString().startsWith("text/")) { Charset charset = contentType.charset().orElse(StandardCharsets.UTF_8); return new ParsedContent(Optional.ofNullable(IOUtils.toString(inputStream, charset)), new HashMap<>()); } else { return new ParsedContent(Optional.empty(), new HashMap<>()); } }  @Override ParsedContent extractContent(InputStream inputStream, ContentType contentType);  }
@Test void textTest() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/Text.txt"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("text/plain")) .getTextualContent()) .contains("This is some awesome text text.\n\n"); }
@Test void extractContentShouldTakeIntoAccountCharset() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/simple-text-iso-8859-1.txt"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent(inputStream, ContentType.of("text/plain; charset=ISO-8859-1")) .getTextualContent()) .contains("\"é\" This text is not UTF-8 \"à\""); }
@Test void textMicrosoftWorldTest() throws Exception { InputStream inputStream = ClassLoader.getSystemResourceAsStream("documents/writter.docx"); assertThat(inputStream).isNotNull(); assertThat(textExtractor.extractContent( inputStream, ContentType.of("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) .getTextualContent()) .isEmpty(); }