@Test public void testAnchorToTextConversion() { String content = "<a href=\"http: String convertedContent = UrlHelper.convertAnchorsToString(content); Assert.assertEquals("blah blah (http: content = "<a href=\"http: convertedContent = UrlHelper.convertAnchorsToString(content); Assert.assertEquals("http: content = "<a href=\"http: convertedContent = UrlHelper.convertAnchorsToString(content); Assert.assertEquals("... (http: content = "<a href=\"http: convertedContent = UrlHelper.convertAnchorsToString(content); Assert.assertEquals("http: content = "<a href=\"http: convertedContent = UrlHelper.convertAnchorsToString(content); Assert.assertEquals("blah blah http: content = "<a href=\"http: convertedContent = UrlHelper.convertAnchorsToString(content); Assert.assertEquals("http: convertedContent); }
public static String convertAnchorsToString(String content) { StringBuilder processedContent = new StringBuilder(); int start = 0; Matcher matcher = ANCHOR_PATTERN.matcher(content); while (matcher.find()) { processedContent.append(content.substring(start, matcher.start(0))); insertSpaceBeforeAfterLink(content, matcher.start(0) - 1, processedContent, true); String innerHTML = matcher.group(2); String href = matcher.group(1); if (innerHTML != null && innerHTML.length() > 0) { innerHTML = innerHTML.replace("&amp;", "&"); Matcher urlMatcher = URL_PATTERN.matcher(innerHTML); int innerStart = 0; boolean anchorProcessed = false; loop: while (urlMatcher.find()) { processedContent.append(innerHTML.substring(innerStart, urlMatcher.start(0))); String remainingText = innerHTML.substring(urlMatcher.end(0)); String foundUrl = urlMatcher.group(0); if (urlMatcher.group(1).startsWith("www.")) { foundUrl = "http: } if ((remainingText.startsWith(URL_SHORTENING_INDICATOR) || remainingText.startsWith("&" + URL_SHORTENING_INDICATOR) || remainingText .startsWith("?" + URL_SHORTENING_INDICATOR)) && href.startsWith(foundUrl)) { processedContent.append(href); int symbolCount = remainingText.startsWith(URL_SHORTENING_INDICATOR) ? 3 : 4; processedContent .append(innerHTML.substring(urlMatcher.end(0) + symbolCount)); anchorProcessed = true; break loop; } if (foundUrl.equals(href)) { processedContent.append(href); processedContent.append(remainingText); anchorProcessed = true; break loop; } processedContent.append(urlMatcher.group(0)); innerStart = urlMatcher.end(0); } if (!anchorProcessed) { processedContent.append(innerHTML.substring(innerStart)); processedContent.append(" ("); processedContent.append(href); processedContent.append(")"); } } else { processedContent.append(href); } start = matcher.end(0); insertSpaceBeforeAfterLink(content, start, processedContent, false); } if (start == 0) { return content; } processedContent.append(content.substring(start)); return processedContent.toString(); }
UrlHelper { public static String convertAnchorsToString(String content) { StringBuilder processedContent = new StringBuilder(); int start = 0; Matcher matcher = ANCHOR_PATTERN.matcher(content); while (matcher.find()) { processedContent.append(content.substring(start, matcher.start(0))); insertSpaceBeforeAfterLink(content, matcher.start(0) - 1, processedContent, true); String innerHTML = matcher.group(2); String href = matcher.group(1); if (innerHTML != null && innerHTML.length() > 0) { innerHTML = innerHTML.replace("&amp;", "&"); Matcher urlMatcher = URL_PATTERN.matcher(innerHTML); int innerStart = 0; boolean anchorProcessed = false; loop: while (urlMatcher.find()) { processedContent.append(innerHTML.substring(innerStart, urlMatcher.start(0))); String remainingText = innerHTML.substring(urlMatcher.end(0)); String foundUrl = urlMatcher.group(0); if (urlMatcher.group(1).startsWith("www.")) { foundUrl = "http: } if ((remainingText.startsWith(URL_SHORTENING_INDICATOR) || remainingText.startsWith("&" + URL_SHORTENING_INDICATOR) || remainingText .startsWith("?" + URL_SHORTENING_INDICATOR)) && href.startsWith(foundUrl)) { processedContent.append(href); int symbolCount = remainingText.startsWith(URL_SHORTENING_INDICATOR) ? 3 : 4; processedContent .append(innerHTML.substring(urlMatcher.end(0) + symbolCount)); anchorProcessed = true; break loop; } if (foundUrl.equals(href)) { processedContent.append(href); processedContent.append(remainingText); anchorProcessed = true; break loop; } processedContent.append(urlMatcher.group(0)); innerStart = urlMatcher.end(0); } if (!anchorProcessed) { processedContent.append(innerHTML.substring(innerStart)); processedContent.append(" ("); processedContent.append(href); processedContent.append(")"); } } else { processedContent.append(href); } start = matcher.end(0); insertSpaceBeforeAfterLink(content, start, processedContent, false); } if (start == 0) { return content; } processedContent.append(content.substring(start)); return processedContent.toString(); } }
UrlHelper { public static String convertAnchorsToString(String content) { StringBuilder processedContent = new StringBuilder(); int start = 0; Matcher matcher = ANCHOR_PATTERN.matcher(content); while (matcher.find()) { processedContent.append(content.substring(start, matcher.start(0))); insertSpaceBeforeAfterLink(content, matcher.start(0) - 1, processedContent, true); String innerHTML = matcher.group(2); String href = matcher.group(1); if (innerHTML != null && innerHTML.length() > 0) { innerHTML = innerHTML.replace("&amp;", "&"); Matcher urlMatcher = URL_PATTERN.matcher(innerHTML); int innerStart = 0; boolean anchorProcessed = false; loop: while (urlMatcher.find()) { processedContent.append(innerHTML.substring(innerStart, urlMatcher.start(0))); String remainingText = innerHTML.substring(urlMatcher.end(0)); String foundUrl = urlMatcher.group(0); if (urlMatcher.group(1).startsWith("www.")) { foundUrl = "http: } if ((remainingText.startsWith(URL_SHORTENING_INDICATOR) || remainingText.startsWith("&" + URL_SHORTENING_INDICATOR) || remainingText .startsWith("?" + URL_SHORTENING_INDICATOR)) && href.startsWith(foundUrl)) { processedContent.append(href); int symbolCount = remainingText.startsWith(URL_SHORTENING_INDICATOR) ? 3 : 4; processedContent .append(innerHTML.substring(urlMatcher.end(0) + symbolCount)); anchorProcessed = true; break loop; } if (foundUrl.equals(href)) { processedContent.append(href); processedContent.append(remainingText); anchorProcessed = true; break loop; } processedContent.append(urlMatcher.group(0)); innerStart = urlMatcher.end(0); } if (!anchorProcessed) { processedContent.append(innerHTML.substring(innerStart)); processedContent.append(" ("); processedContent.append(href); processedContent.append(")"); } } else { processedContent.append(href); } start = matcher.end(0); insertSpaceBeforeAfterLink(content, start, processedContent, false); } if (start == 0) { return content; } processedContent.append(content.substring(start)); return processedContent.toString(); } private  UrlHelper(); }
UrlHelper { public static String convertAnchorsToString(String content) { StringBuilder processedContent = new StringBuilder(); int start = 0; Matcher matcher = ANCHOR_PATTERN.matcher(content); while (matcher.find()) { processedContent.append(content.substring(start, matcher.start(0))); insertSpaceBeforeAfterLink(content, matcher.start(0) - 1, processedContent, true); String innerHTML = matcher.group(2); String href = matcher.group(1); if (innerHTML != null && innerHTML.length() > 0) { innerHTML = innerHTML.replace("&amp;", "&"); Matcher urlMatcher = URL_PATTERN.matcher(innerHTML); int innerStart = 0; boolean anchorProcessed = false; loop: while (urlMatcher.find()) { processedContent.append(innerHTML.substring(innerStart, urlMatcher.start(0))); String remainingText = innerHTML.substring(urlMatcher.end(0)); String foundUrl = urlMatcher.group(0); if (urlMatcher.group(1).startsWith("www.")) { foundUrl = "http: } if ((remainingText.startsWith(URL_SHORTENING_INDICATOR) || remainingText.startsWith("&" + URL_SHORTENING_INDICATOR) || remainingText .startsWith("?" + URL_SHORTENING_INDICATOR)) && href.startsWith(foundUrl)) { processedContent.append(href); int symbolCount = remainingText.startsWith(URL_SHORTENING_INDICATOR) ? 3 : 4; processedContent .append(innerHTML.substring(urlMatcher.end(0) + symbolCount)); anchorProcessed = true; break loop; } if (foundUrl.equals(href)) { processedContent.append(href); processedContent.append(remainingText); anchorProcessed = true; break loop; } processedContent.append(urlMatcher.group(0)); innerStart = urlMatcher.end(0); } if (!anchorProcessed) { processedContent.append(innerHTML.substring(innerStart)); processedContent.append(" ("); processedContent.append(href); processedContent.append(")"); } } else { processedContent.append(href); } start = matcher.end(0); insertSpaceBeforeAfterLink(content, start, processedContent, false); } if (start == 0) { return content; } processedContent.append(content.substring(start)); return processedContent.toString(); } private  UrlHelper(); static void assertIsValidUrl(String url); static String convertAnchorsToString(String content); static String getProtocolHostPort(String url, boolean stripUserInfo); static String insertSessionIdInUrl(String url, String sessionId); static boolean isAbsoluteHttpUrl(String url); static boolean isAbsoluteUrl(String url); static String shortenUrl(String protocolHostPart, String pathPart, String queryPart,
            String fragmentIdentifier); static String urlEncodeUrlPath(String path); }
UrlHelper { public static String convertAnchorsToString(String content) { StringBuilder processedContent = new StringBuilder(); int start = 0; Matcher matcher = ANCHOR_PATTERN.matcher(content); while (matcher.find()) { processedContent.append(content.substring(start, matcher.start(0))); insertSpaceBeforeAfterLink(content, matcher.start(0) - 1, processedContent, true); String innerHTML = matcher.group(2); String href = matcher.group(1); if (innerHTML != null && innerHTML.length() > 0) { innerHTML = innerHTML.replace("&amp;", "&"); Matcher urlMatcher = URL_PATTERN.matcher(innerHTML); int innerStart = 0; boolean anchorProcessed = false; loop: while (urlMatcher.find()) { processedContent.append(innerHTML.substring(innerStart, urlMatcher.start(0))); String remainingText = innerHTML.substring(urlMatcher.end(0)); String foundUrl = urlMatcher.group(0); if (urlMatcher.group(1).startsWith("www.")) { foundUrl = "http: } if ((remainingText.startsWith(URL_SHORTENING_INDICATOR) || remainingText.startsWith("&" + URL_SHORTENING_INDICATOR) || remainingText .startsWith("?" + URL_SHORTENING_INDICATOR)) && href.startsWith(foundUrl)) { processedContent.append(href); int symbolCount = remainingText.startsWith(URL_SHORTENING_INDICATOR) ? 3 : 4; processedContent .append(innerHTML.substring(urlMatcher.end(0) + symbolCount)); anchorProcessed = true; break loop; } if (foundUrl.equals(href)) { processedContent.append(href); processedContent.append(remainingText); anchorProcessed = true; break loop; } processedContent.append(urlMatcher.group(0)); innerStart = urlMatcher.end(0); } if (!anchorProcessed) { processedContent.append(innerHTML.substring(innerStart)); processedContent.append(" ("); processedContent.append(href); processedContent.append(")"); } } else { processedContent.append(href); } start = matcher.end(0); insertSpaceBeforeAfterLink(content, start, processedContent, false); } if (start == 0) { return content; } processedContent.append(content.substring(start)); return processedContent.toString(); } private  UrlHelper(); static void assertIsValidUrl(String url); static String convertAnchorsToString(String content); static String getProtocolHostPort(String url, boolean stripUserInfo); static String insertSessionIdInUrl(String url, String sessionId); static boolean isAbsoluteHttpUrl(String url); static boolean isAbsoluteUrl(String url); static String shortenUrl(String protocolHostPart, String pathPart, String queryPart,
            String fragmentIdentifier); static String urlEncodeUrlPath(String path); static final Pattern URL_PATTERN; }