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 * A BugTracker generates links to numbered bugs. We use this to turn
12 * bug references in commit log messages (e.g. "Bug #123") into clickable
13 * links.
14 *
15 * @author Richard Cyganiak (richard@cyganiak.de)
16 * @version $Id: BugTracker.java,v 1.9 2009/03/09 21:45:42 benoitx Exp $
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 * A null object that can be used in place of a real bug tracker.
23 */
24 public static final BugTracker NO_BUG_TRACKER = new BugTracker("") {
25 public String bugURL(final String bugNumber) {
26 return null;
27 }
28
29 public String getName() {
30 return "No bug tracker";
31 }
32
33 public String toHTMLWithLinks(final String plainText) {
34 return HTML.webifyLinksFromPlainText(plainText);
35 }
36 };
37
38 private final String baseURL;
39
40 /**
41 * Creates a new BugTracker instance.
42 * @param baseURL The bug tracker's base URL; a slash is appended
43 * if it doesn't end in a slash
44 */
45 public BugTracker(final String baseURL) {
46 this.baseURL = baseURL + (baseURL.endsWith("/") ? "" : "/");
47 }
48
49 /**
50 * Returns the name of the bug tracker
51 * @return the name of the bug tracker
52 */
53 public abstract String getName();
54
55 /**
56 * Returns the bug tracker's base URL.
57 * @return The bug tracker's base URL
58 */
59 public String baseURL() {
60 return this.baseURL;
61 }
62
63 /**
64 * Returns the URL of the bug tracker page about a certain bug.
65 * @param bugNumber The bug number; one or more digits.
66 * @return The URL of the bug page
67 */
68 public abstract String bugURL(String bugNumber);
69
70 /**
71 * Filters a String, e.g. a commit message, replacing bug references with
72 * links to the tracker.
73 * @param plainTextInput String to examine for bug references
74 * @return A copy of <code>input</code>, with bug references replaced with HTML links
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 }