HtmlUtil { public static String linkifyUrlsInHtml(String content) { Matcher existingLinksMatcher = existingLinksPattern.matcher(content); StringBuilder result = new StringBuilder(); int lastIndex = 0; while (existingLinksMatcher.find()) { int startIndex = existingLinksMatcher.start(); int endIndex = existingLinksMatcher.end(); String beforeText = content.substring(lastIndex, startIndex); result.append(linkifyUrlsInText(beforeText)); String existingLink = content.substring(startIndex, endIndex); result.append(existingLink); lastIndex = endIndex; } String afterText = content.substring(lastIndex, content.length()); result.append(linkifyUrlsInText(afterText)); return result.toString(); } private  HtmlUtil(); static String cleanAll(String text); static String cleanMost(String text); static String cleanSome(String text, String baseUri); static String clean(String text, Whitelist whitelist, String baseUri); static String decodeHTMLEntitiesForEmail(String body); static String linkifyUrlsInText(String content); static String linkifyUrlsInHtml(String content); static String makeRelativeLinksAbsolute(String html, String baseUrl); static String createLink(String url, String desc); static String addHtmlLineBreaks(String content); static String filterAndFormatContent(String content); static Document addNoFollowToLinkTagsInDocument(Document document);  }
@Test public void testLinkifyUrls__simpleUrl() { String input = "www.google.com"; String expected = "<a rel='nofollow' href='http: String message = "Simple URL linkification failed."; assertEquals(message, expected, HtmlUtil.linkifyUrlsInHtml(input)); }
@Test public void testLinkifyUrls__umlauts() { String input = "https: String expected = "<a rel='nofollow' href='https: String message = "Umlauts are not handled correctly in linkification."; assertEquals(message, expected, HtmlUtil.linkifyUrlsInHtml(input)); }
@Test public void testLinkifyUrls__parentheses() { String input = "Hi, please check my website! (www.google.com)"; String expected = "Hi, please check my website! (<a rel='nofollow' href='http: String message = "Parentheses are not handled correctly in linkification."; assertEquals(message, expected, HtmlUtil.linkifyUrlsInHtml(input)); }
@Test public void testLinkifyUrls__anchorTag() { String input = "<a href=\"http: String expected = "<a href=\"http: String message = "An URL inside an anchor tag should not be linkified."; assertEquals(message, expected, HtmlUtil.linkifyUrlsInHtml(input)); }
@Test public void testLinkifyUrls__comma() { String input = "I have this great website here www.google.com, where you can search for stuff!"; String expected = "I have this great website here <a rel='nofollow' href='http: String message = "A comma after the URL is not part of the URL."; assertEquals(message, expected, HtmlUtil.linkifyUrlsInHtml(input)); }
@Test public void testLinkifyUrls__imgTag() { String input = "Here's a very nice image: <img src=\"https: String expected = "Here's a very nice image: <img src=\"https: String message = "An URL inside an img tag should not be linkified."; assertEquals(message, expected, HtmlUtil.linkifyUrlsInHtml(input)); }
@Test public void testLinkifyUrls__multipleUrls() { String input = "Woow, multiple mixed URLs!!! www.google.com, <a href=\"http: String expected = "Woow, multiple mixed URLs!!! <a rel='nofollow' href='http: String message = "Multiple URLs are not linkifed correctly."; assertEquals(message, expected, HtmlUtil.linkifyUrlsInHtml(input)); }
@Test public void testLinkifyUrls__nestedTag() { String input = "<a href=\"www.google.com\"><b>www.google.com</b></a>"; String expected = "<a href=\"www.google.com\"><b>www.google.com</b></a>"; String message = "Nested tags break the linkification exception."; assertEquals(message, expected, HtmlUtil.linkifyUrlsInHtml(input)); }
@Test public void testLinkifyUrls__query() { String input = "Here's an interesting URL with query strings: http: String expected = "Here's an interesting URL with query strings: <a rel='nofollow' href='http: String message = "Linkification does not work correctly with query strings in the URL."; assertEquals(message, expected, HtmlUtil.linkifyUrlsInHtml(input)); }