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 protected static final Pattern bugRegex = Pattern.compile("bug\\s*(?:number\\s*)?(?:#\\s*)?(\\d+)", Pattern.CASE_INSENSITIVE);
20
21
22
23
24 public static final BugTracker NO_BUG_TRACKER = new BugTracker("") {
25 public String bugURL(String bugNumber) {
26 return null;
27 }
28
29 public String getName() {
30 return "No bug tracker";
31 }
32
33 public String toHTMLWithLinks(String plainText) {
34 return HTML.webifyLinksFromPlainText(plainText);
35 }
36 };
37
38 private final String baseURL;
39
40
41
42
43
44
45 public BugTracker(final String baseURL) {
46 this.baseURL = baseURL + (baseURL.endsWith("/") ? "" : "/");
47 }
48
49
50
51
52
53 public abstract String getName();
54
55
56
57
58
59 public String baseURL() {
60 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 if (baseURL() == null || baseURL().length() == 0) {
78 return HTML.webifyLinksFromPlainText(plainTextInput);
79 }
80 final StringBuffer result = new StringBuffer();
81 final Matcher m = bugRegex.matcher(plainTextInput);
82 int offset = 0;
83 while (m.find()) {
84 final String linkLabel = m.group();
85 final String bugNumber = m.group(1);
86 final String bugURL = bugURL(bugNumber);
87 result.append(HTML.webifyLinksFromPlainText(plainTextInput.substring(offset, m.start())));
88 if (bugURL == null) {
89 result.append(HTML.webifyLinksFromPlainText(linkLabel));
90 } else {
91 result.append(HTML.getLink(bugURL, linkLabel, HTML.getIcon(ReportSuiteMaker.BUG_ICON, Messages.getString("BUG_ICON")), ""));
92 }
93 offset = m.end();
94 }
95 result.append(HTML.webifyLinksFromPlainText(plainTextInput.substring(offset, plainTextInput.length())));
96 return result.toString();
97 }
98 }