View Javadoc

1   package com.atlassian.theplugin.util;
2   
3   import java.util.regex.Pattern;
4   import java.util.regex.Matcher;
5   
6   /**
7    * User: kalamon
8    * Date: Apr 24, 2009
9    * Time: 3:23:30 PM
10   */
11  public class Htmlizer {
12  
13      public static final String HYPERLINK_PATTERN = "(https?://[\\w|:|-|/|\\.|\\?|\\&|=]+)";
14  
15      public static final String A_PATTERN = "(<a\\s+.*</a>)";
16      private boolean haveA;
17      private boolean haveHyperlink;
18      private String linkTextReplacement;
19      private int truncateLinkTextAt = -1;
20  
21      public Htmlizer() {
22      }
23  
24      public Htmlizer(int truncateLinkTextAt) {
25          this.truncateLinkTextAt = truncateLinkTextAt;
26      }
27  
28      public Htmlizer(String linkTextReplacement) {
29          this.linkTextReplacement = linkTextReplacement;
30      }
31  
32      public String htmlizeHyperlinks(String text) {
33          Pattern patternHyperlink = Pattern.compile(HYPERLINK_PATTERN);
34          Matcher matcherHyperlink = patternHyperlink.matcher(text);
35          Pattern patternA = Pattern.compile(A_PATTERN);
36          Matcher matcherA = patternA.matcher(text);
37          StringBuilder result = new StringBuilder();
38          int index = 0;
39          haveA = false;
40          haveHyperlink = false;
41          while (matchBoth(matcherHyperlink, matcherA, index)) {
42              boolean copyA;
43              if (haveA && haveHyperlink) {
44                  copyA = matcherA.start() < matcherHyperlink.start();
45              } else {
46                  copyA = haveA;
47              }
48              if (copyA) {
49                  index = copyAHref(text, matcherA, result, index);
50              } else {
51                  index = replaceHyperlinkWithAHref(text, matcherHyperlink, result, index);
52              }
53              haveA = false;
54              haveHyperlink = false;
55          }
56          String rest = text.substring(index, text.length());
57          result.append(rest);
58          return result.toString();
59      }
60  
61      public String replaceBrackets(String text) {
62          String result = text.replaceAll("<", "&lt;");
63          result = result.replaceAll(">", "&gt;");
64          return result;
65      }
66      
67      public String replaceWhitespace(String text) {
68          String result = text.replaceAll("    ", "&nbsp;&nbsp;&nbsp;&nbsp;");
69          result = result.replaceAll("   ", "&nbsp;&nbsp;&nbsp;");
70          result = result.replaceAll("  ", "&nbsp;&nbsp;");
71          result = result.replaceAll("\n ", "<br>&nbsp;");
72          result = result.replaceAll("\n\t", "<br>&nbsp;&nbsp;&nbsp;&nbsp;");
73          result = result.replaceAll("\r", "").replaceAll("\n", "<br>");
74          return result;
75      }
76  
77      private boolean matchBoth(Matcher matcherHyperlink, Matcher matcherA, int index) {
78          haveA = matcherA.find(index);
79          haveHyperlink = matcherHyperlink.find(index);
80          return haveA || haveHyperlink;
81      }
82  
83      private int copyAHref(String text, Matcher matcher, StringBuilder result, int index) {
84          result.append(text.substring(index, matcher.start()));
85          String a = matcher.group(1);
86          result.append(a);
87          return matcher.end();
88      }
89  
90      private int replaceHyperlinkWithAHref(String text, Matcher matcher, StringBuilder result, int index) {
91          result.append(text.substring(index, matcher.start()));
92          String link = matcher.group(1);
93          String linkText = linkTextReplacement != null ? linkTextReplacement : truncateLinkText(link);
94          String replacement =
95                  "<a href=\"" + link + "\">"
96                  + linkText
97                  + "</a>";
98          result.append(replacement);
99          return matcher.end();
100     }
101 
102     private String truncateLinkText(String text) {
103         if (truncateLinkTextAt > 0 && text.length() > truncateLinkTextAt) {
104             return text.substring(0, truncateLinkTextAt) + "...";
105         }
106         return text;
107     }
108 }