01: package net.sourceforge.cruisecontrol.dashboard;
02:
03: import java.util.regex.Matcher;
04: import java.util.regex.Pattern;
05:
06: import org.apache.commons.lang.StringUtils;
07:
08: public class StoryTracker {
09:
10: private final String projectName;
11:
12: private final String baseUrl;
13:
14: private final String keywords;
15:
16: private Pattern pattern;
17:
18: public StoryTracker(String projectName, String baseUrl,
19: String keywords) {
20: this .projectName = projectName;
21: this .baseUrl = baseUrl;
22: this .keywords = keywords;
23: pattern = pattern();
24: }
25:
26: public String getBaseUrl() {
27: return baseUrl;
28: }
29:
30: public String getKeywords() {
31: return keywords;
32: }
33:
34: public String getProjectName() {
35: return projectName;
36: }
37:
38: private Pattern pattern() {
39: String chompedKeywords = StringUtils.chomp(keywords, ",");
40: String preGroup = "(.*?)";
41: String keywordsGroup = '(' + StringUtils.replaceChars(
42: chompedKeywords, ',', '|') + ')';
43: String spacesGroup = "([\t ]*)";
44: String numberGroup = "(\\d+)";
45: String postGroup = preGroup;
46: return Pattern.compile(preGroup + keywordsGroup + spacesGroup
47: + numberGroup + postGroup);
48: }
49:
50: public String getTextWithUrls(String inputString) {
51: Matcher matcher = pattern.matcher(inputString);
52: if (!matcher.matches()) {
53: return inputString;
54: }
55:
56: matcher.reset();
57:
58: String tail = "";
59: String resultString = "";
60: while (matcher.find()) {
61: String replacement = matcher.group(2) + matcher.group(3)
62: + matcher.group(4);
63: resultString += StringUtils.replace(matcher.group(0),
64: replacement, "<a href=\"" + baseUrl
65: + matcher.group(4) + "\">" + replacement
66: + "</a>");
67: tail = inputString.substring(matcher.end());
68: }
69: resultString += tail;
70: return resultString;
71: }
72: }
|