linkify(url, true)
.
*
@@ -1509,7 +1493,7 @@ public static String linkify(String url) {
}
/**
- * Build a html link to the given http URL. If the URL is not an http URL
+ * Build an HTML link to the given HTTP URL. If the URL is not an HTTP URL
* then it is returned as it was received.
*
* @param url the HTTP URL
@@ -1535,9 +1519,9 @@ public static String linkify(String url, boolean newTab) {
}
/**
- * Build an anchor with given name and a pack of attributes. Automatically
- * escapes href attributes and automatically escapes the name into HTML
- * entities.
+ * Build an anchor with given name and a pack of attributes.
+ * Assumes the href
attribute value is URI encoded.
+ * Automatically escapes the name into HTML entities.
*
* @param name displayed name of the anchor
* @param attrs map of attributes for the html element
@@ -1556,7 +1540,7 @@ public static String buildLink(String name, Maphref
attribute value is URI encoded.
+ * Automatically escapes the name into HTML entities.
*
* @param name displayed name of the anchor
* @param url anchor's URL
@@ -1579,17 +1563,16 @@ public static String buildLink(String name, Maphref
attribute value is URI encoded.
+ * Automatically escapes the name into HTML entities.
*
* @param name displayed name of the anchor
* @param url anchor's URL
@@ -1610,30 +1593,52 @@ public static String buildLink(String name, String url, boolean newTab)
return buildLink(name, attrs);
}
+ /**
+ * Callback function to provide replacement value for pattern match.
+ * It replaces the first group of the pattern and retains the rest of the pattern.
+ *
+ * @param result {@link MatchResult} object representing pattern match
+ * @param text original text containing the match
+ * @param url URL to be used for the replacement
+ * @return replacement for the first group in the pattern
+ */
+ private static String buildLinkReplacer(MatchResult result, String text, String url) {
+ if (result.groupCount() < 1) {
+ return result.group(0);
+ }
+ final String group1 = result.group(1);
+ final String appendedUrl = url + uriEncode(group1);
+ try {
+ StringBuilder stringBuilder = new StringBuilder();
+ if (result.start(0) < result.start(1)) {
+ stringBuilder.append(text.substring(result.start(0), result.start(1)));
+ }
+ stringBuilder.append(buildLink(group1, appendedUrl, true));
+ if (result.end(1) < result.end(0)) {
+ stringBuilder.append(text.substring(result.end(1), result.end(0)));
+ }
+ return stringBuilder.toString();
+ } catch (URISyntaxException | MalformedURLException e) {
+ LOGGER.log(Level.WARNING, "The given URL ''{0}'' is not valid", appendedUrl);
+ return result.group(0);
+ }
+ }
+
/**
* Replace all occurrences of pattern in the incoming text with the link
- * named name pointing to an URL. It is possible to use the regexp pattern
- * groups in name and URL when they are specified in the pattern.
+ * named name pointing to a URL.
*
- * @param text text to replace all patterns
+ * @param text text to replace all patterns
* @param pattern the pattern to match
- * @param name link display name
- * @param url link URL
+ * @param url link URL
* @return the text with replaced links
*/
- public static String linkifyPattern(String text, Pattern pattern, String name, String url) {
- try {
- String buildLink = buildLink(name, url, true);
- return pattern.matcher(text).replaceAll(buildLink);
- } catch (URISyntaxException | MalformedURLException ex) {
- LOGGER.log(Level.WARNING, "The given URL ''{0}'' is not valid", url);
- return text;
- }
+ public static String linkifyPattern(String text, Pattern pattern, String url) {
+ return pattern.matcher(text).replaceAll(match -> buildLinkReplacer(match, text, url));
}
/**
- * Try to complete the given URL part into full URL with server name, port,
- * scheme, ...
+ * Try to complete the given URL part into full URL with server name, port, scheme, ...
*