1 | |
package net.sf.statcvs.weblinks.bugs; |
2 | |
|
3 | |
import java.util.regex.Matcher; |
4 | |
import java.util.regex.Pattern; |
5 | |
|
6 | |
import net.sf.statcvs.Messages; |
7 | |
import net.sf.statcvs.pages.HTML; |
8 | |
import net.sf.statcvs.pages.ReportSuiteMaker; |
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
public abstract class BugTracker { |
19 | 32 | protected static final Pattern bugRegex = Pattern.compile("bug\\s*(?:number\\s*)?(?:#\\s*)?(\\d+)", Pattern.CASE_INSENSITIVE); |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | 12 | public static final BugTracker NO_BUG_TRACKER = new BugTracker("") { |
25 | |
public String bugURL(final String bugNumber) { |
26 | 0 | return null; |
27 | |
} |
28 | |
|
29 | |
public String getName() { |
30 | 0 | return "No bug tracker"; |
31 | |
} |
32 | |
|
33 | 20 | public String toHTMLWithLinks(final String plainText) { |
34 | 0 | return HTML.webifyLinksFromPlainText(plainText); |
35 | |
} |
36 | |
}; |
37 | |
|
38 | |
private final String baseURL; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 32 | public BugTracker(final String baseURL) { |
46 | 32 | this.baseURL = baseURL + (baseURL.endsWith("/") ? "" : "/"); |
47 | 32 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public abstract String getName(); |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public String baseURL() { |
60 | 0 | return this.baseURL; |
61 | |
} |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public abstract String bugURL(String bugNumber); |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public String toHTMLWithLinks(final String plainTextInput) { |
77 | 0 | if (baseURL() == null || baseURL().length() == 0) { |
78 | 0 | return HTML.webifyLinksFromPlainText(plainTextInput); |
79 | |
} |
80 | 0 | final StringBuffer result = new StringBuffer(); |
81 | 0 | final Matcher m = bugRegex.matcher(plainTextInput); |
82 | 0 | int offset = 0; |
83 | 0 | while (m.find()) { |
84 | 0 | final String linkLabel = m.group(); |
85 | 0 | final String bugNumber = m.group(1); |
86 | 0 | final String bugURL = bugURL(bugNumber); |
87 | 0 | result.append(HTML.webifyLinksFromPlainText(plainTextInput.substring(offset, m.start()))); |
88 | 0 | if (bugURL == null) { |
89 | 0 | result.append(HTML.webifyLinksFromPlainText(linkLabel)); |
90 | |
} else { |
91 | 0 | result.append(HTML.getLink(bugURL, linkLabel, HTML.getIcon(ReportSuiteMaker.BUG_ICON, Messages.getString("BUG_ICON")), "")); |
92 | |
} |
93 | 0 | offset = m.end(); |
94 | 0 | } |
95 | 0 | result.append(HTML.webifyLinksFromPlainText(plainTextInput.substring(offset, plainTextInput.length()))); |
96 | 0 | return result.toString(); |
97 | |
} |
98 | |
} |