@Test public void toPlainTextShouldWorkWithMoreComplexHTML() throws Exception { String html = IOUtils.toString(ClassLoader.getSystemResource("example.html"), StandardCharsets.UTF_8); String expectedPlainText = "\n" + " Why a new Logo?\n" + "\n" + "\n" + " We are happy with our current logo, but for the\n" + " upcoming James Server 3.0 release, we would like to\n" + " give our community the opportunity to create a new image for James.\n" + "\n" + "\n" + "\n" + "\n" + " Don't be shy, take your inkscape and gimp, and send us on\n" + " the James Server User mailing list\n" + " your creations. We will publish them on this page.\n" + "\n" + "\n" + "\n" + "\n" + " We need an horizontal logo (100p height) to be show displayed on the upper\n" + " left corner of this page, an avatar (48x48p) to be used on a Twitter stream for example.\n" + " The used fonts should be redistributable (or commonly available on Windows and Linux).\n" + " The chosen logo should be delivered in SVG format.\n" + " We also like the Apache feather.\n" + "\n" + "\n" + "\n"; assertThat(textExtractor.toPlainText(html)).isEqualTo(expectedPlainText); }
@Override public String toPlainText(String html) { try { Document document = Jsoup.parse(html); Element body = Optional.ofNullable(document.body()).orElse(document); return flatten(body, INITIAL_LIST_NESTED_LEVEL) .map(this::convertNodeToText) .reduce("", (s1, s2) -> s1 + s2); } catch (Exception e) { LOGGER.warn("Failed extracting text from html", e); return html; } }
JsoupHtmlTextExtractor implements HtmlTextExtractor { @Override public String toPlainText(String html) { try { Document document = Jsoup.parse(html); Element body = Optional.ofNullable(document.body()).orElse(document); return flatten(body, INITIAL_LIST_NESTED_LEVEL) .map(this::convertNodeToText) .reduce("", (s1, s2) -> s1 + s2); } catch (Exception e) { LOGGER.warn("Failed extracting text from html", e); return html; } } }
JsoupHtmlTextExtractor implements HtmlTextExtractor { @Override public String toPlainText(String html) { try { Document document = Jsoup.parse(html); Element body = Optional.ofNullable(document.body()).orElse(document); return flatten(body, INITIAL_LIST_NESTED_LEVEL) .map(this::convertNodeToText) .reduce("", (s1, s2) -> s1 + s2); } catch (Exception e) { LOGGER.warn("Failed extracting text from html", e); return html; } }  }
JsoupHtmlTextExtractor implements HtmlTextExtractor { @Override public String toPlainText(String html) { try { Document document = Jsoup.parse(html); Element body = Optional.ofNullable(document.body()).orElse(document); return flatten(body, INITIAL_LIST_NESTED_LEVEL) .map(this::convertNodeToText) .reduce("", (s1, s2) -> s1 + s2); } catch (Exception e) { LOGGER.warn("Failed extracting text from html", e); return html; } }  @Override String toPlainText(String html); }
JsoupHtmlTextExtractor implements HtmlTextExtractor { @Override public String toPlainText(String html) { try { Document document = Jsoup.parse(html); Element body = Optional.ofNullable(document.body()).orElse(document); return flatten(body, INITIAL_LIST_NESTED_LEVEL) .map(this::convertNodeToText) .reduce("", (s1, s2) -> s1 + s2); } catch (Exception e) { LOGGER.warn("Failed extracting text from html", e); return html; } }  @Override String toPlainText(String html); static final String BR_TAG; static final String UL_TAG; static final String OL_TAG; static final String LI_TAG; static final String P_TAG; static final String IMG_TAG; static final String ALT_TAG; static final int INITIAL_LIST_NESTED_LEVEL; }