59
59
import java .util .function .IntConsumer ;
60
60
import java .util .logging .Level ;
61
61
import java .util .logging .Logger ;
62
+ import java .util .regex .MatchResult ;
62
63
import java .util .regex .Matcher ;
63
64
import java .util .regex .Pattern ;
64
65
import java .util .stream .IntStream ;
@@ -367,7 +368,7 @@ public static String breadcrumbPath(String urlPrefix, String path) {
367
368
* @param path the full path from which the breadcrumb path is built
368
369
* @param sep separator to use to crack the given path
369
370
*
370
- * @return HTML markup fro the breadcrumb or the path itself.
371
+ * @return HTML markup for the breadcrumb or the path itself.
371
372
* @see #breadcrumbPath(String, String, char, String, boolean, boolean)
372
373
*/
373
374
public static String breadcrumbPath (String urlPrefix , String path , char sep ) {
@@ -939,16 +940,14 @@ public static String uriEncode(String q) {
939
940
* @param dest a defined target
940
941
* @throws IOException I/O
941
942
*/
942
- public static void uriEncode (String str , Appendable dest )
943
- throws IOException {
943
+ public static void uriEncode (String str , Appendable dest ) throws IOException {
944
944
String uenc = uriEncode (str );
945
945
dest .append (uenc );
946
946
}
947
947
948
948
/**
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.
952
951
*
953
952
* @param buf where to append the query string
954
953
* @param key the name of the parameter to add. Append as is!
@@ -957,8 +956,7 @@ public static void uriEncode(String str, Appendable dest)
957
956
* @throws NullPointerException if the given buffer is {@code null}.
958
957
* @see #uriEncode(String)
959
958
*/
960
- public static void appendQuery (StringBuilder buf , String key ,
961
- String value ) {
959
+ public static void appendQuery (StringBuilder buf , String key , String value ) {
962
960
963
961
if (value != null ) {
964
962
buf .append (AMP ).append (key ).append ('=' ).append (uriEncode (value ));
@@ -1610,25 +1608,32 @@ public static String buildLink(String name, String url, boolean newTab)
1610
1608
return buildLink (name , attrs );
1611
1609
}
1612
1610
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
+
1613
1625
/**
1614
1626
* 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
1616
1628
* groups in name and URL when they are specified in the pattern.
1617
1629
*
1618
- * @param text text to replace all patterns
1630
+ * @param text text to replace all patterns
1619
1631
* @param pattern the pattern to match
1620
- * @param name link display name
1621
- * @param url link URL
1632
+ * @param url link URL
1622
1633
* @return the text with replaced links
1623
1634
*/
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 ));
1632
1637
}
1633
1638
1634
1639
/**
0 commit comments