5959import java .util .function .IntConsumer ;
6060import java .util .logging .Level ;
6161import java .util .logging .Logger ;
62+ import java .util .regex .MatchResult ;
6263import java .util .regex .Matcher ;
6364import java .util .regex .Pattern ;
6465import java .util .stream .IntStream ;
@@ -367,7 +368,7 @@ public static String breadcrumbPath(String urlPrefix, String path) {
367368 * @param path the full path from which the breadcrumb path is built
368369 * @param sep separator to use to crack the given path
369370 *
370- * @return HTML markup fro the breadcrumb or the path itself.
371+ * @return HTML markup for the breadcrumb or the path itself.
371372 * @see #breadcrumbPath(String, String, char, String, boolean, boolean)
372373 */
373374 public static String breadcrumbPath (String urlPrefix , String path , char sep ) {
@@ -939,16 +940,14 @@ public static String uriEncode(String q) {
939940 * @param dest a defined target
940941 * @throws IOException I/O
941942 */
942- public static void uriEncode (String str , Appendable dest )
943- throws IOException {
943+ public static void uriEncode (String str , Appendable dest ) throws IOException {
944944 String uenc = uriEncode (str );
945945 dest .append (uenc );
946946 }
947947
948948 /**
949- * Append '&name=value" to the given buffer. If the given
950- * <var>value</var>
951- * is {@code null}, this method does nothing.
949+ * Append "&name=value" to the given buffer. If the given <var>value</var> is {@code null},
950+ * this method does nothing.
952951 *
953952 * @param buf where to append the query string
954953 * @param key the name of the parameter to add. Append as is!
@@ -957,8 +956,7 @@ public static void uriEncode(String str, Appendable dest)
957956 * @throws NullPointerException if the given buffer is {@code null}.
958957 * @see #uriEncode(String)
959958 */
960- public static void appendQuery (StringBuilder buf , String key ,
961- String value ) {
959+ public static void appendQuery (StringBuilder buf , String key , String value ) {
962960
963961 if (value != null ) {
964962 buf .append (AMP ).append (key ).append ('=' ).append (uriEncode (value ));
@@ -1610,25 +1608,32 @@ public static String buildLink(String name, String url, boolean newTab)
16101608 return buildLink (name , attrs );
16111609 }
16121610
1611+ private static String buildLinkReplacer (MatchResult result , String text , String url ) {
1612+ final String appendedUrl = url + result .group (1 );
1613+ try {
1614+ StringBuilder stringBuilder = new StringBuilder ();
1615+ stringBuilder .append (text .substring (result .start (0 ), result .start (1 )));
1616+ stringBuilder .append (buildLink (appendedUrl , appendedUrl , true ));
1617+ stringBuilder .append (text .substring (result .end (1 ), result .end (0 )));
1618+ return stringBuilder .toString ();
1619+ } catch (URISyntaxException |MalformedURLException e ) {
1620+ LOGGER .log (Level .WARNING , "The given URL ''{0}'' is not valid" , appendedUrl );
1621+ return result .group (0 );
1622+ }
1623+ }
1624+
16131625 /**
16141626 * Replace all occurrences of pattern in the incoming text with the link
1615- * named name pointing to an URL. It is possible to use the regexp pattern
1627+ * named name pointing to a URL. It is possible to use the regexp pattern
16161628 * groups in name and URL when they are specified in the pattern.
16171629 *
1618- * @param text text to replace all patterns
1630+ * @param text text to replace all patterns
16191631 * @param pattern the pattern to match
1620- * @param name link display name
1621- * @param url link URL
1632+ * @param url link URL
16221633 * @return the text with replaced links
16231634 */
1624- public static String linkifyPattern (String text , Pattern pattern , String name , String url ) {
1625- try {
1626- String buildLink = buildLink (name , url , true );
1627- return pattern .matcher (text ).replaceAll (buildLink );
1628- } catch (URISyntaxException | MalformedURLException ex ) {
1629- LOGGER .log (Level .WARNING , "The given URL ''{0}'' is not valid" , url );
1630- return text ;
1631- }
1635+ public static String linkifyPattern (String text , Pattern pattern , String url ) {
1636+ return pattern .matcher (text ).replaceAll (match -> buildLinkReplacer (match , text , url ));
16321637 }
16331638
16341639 /**
0 commit comments