1 package net.sf.statcvs.pages;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Date;
5 import java.util.Set;
6 import java.util.logging.Logger;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import net.sf.statcvs.Messages;
11 import net.sf.statcvs.model.Author;
12 import net.sf.statcvs.model.Directory;
13
14
15
16
17
18
19
20
21 public final class HTML {
22 private static final SimpleDateFormat OUTPUT_DATE_FORMAT = new SimpleDateFormat(Messages.getString("DATE_FORMAT"));
23 private static final SimpleDateFormat OUTPUT_DATE_TIME_FORMAT = new SimpleDateFormat(Messages.getString("DATE_TIME_FORMAT"));
24 private static final Pattern HTTP_REGEXP2 = Pattern
25 .compile("\\b(http://|https://|www.)(\\w|\\d|\\.)+(\\.|/|\\w|\\d|%|;|&|=)*\\b", Pattern.CASE_INSENSITIVE);
26 private final static Logger LOGGER = Logger.getLogger("sf.net.statcvs");
27
28
29
30
31
32
33
34 public static String getLink(final String link, final String linkName) {
35 return getLink(link, linkName, "", "");
36 }
37
38
39
40
41
42
43
44
45
46 public static String getLink(final String link, final String linkName, final String prefix, final String suffix) {
47 if (link == null) {
48 return prefix + escape(linkName) + suffix;
49 }
50 return "<a href=\"" + escape(link) + "\">" + prefix + escape(linkName) + suffix + "</a>";
51 }
52
53
54
55
56
57
58 public static String getAuthorLink(final Author author) {
59 return "<a href=\"" + escape(DeveloperPageMaker.getURL(author)) + "\" class=\"author\">" + escape(author.getRealName()) + "</a>";
60 }
61
62
63
64
65
66
67 public static String getAuthorIdLink(final Author author) {
68 return "<a href=\"" + escape(DeveloperPageMaker.getURL(author)) + "\" class=\"author\">" + escape(author.getName()) + "</a>";
69 }
70
71
72
73
74
75
76 public static String getDate(final Date date) {
77 return "<span class=\"date\">" + OUTPUT_DATE_FORMAT.format(date) + "</span>";
78 }
79
80
81
82
83
84
85 public static String getAffectedFilesCount(final Set files) {
86 final StringBuffer sb = new StringBuffer();
87 sb.append("<span class=\"files\">" + files.size());
88 if (files.size() <= 1) {
89 sb.append(" file");
90 } else {
91 sb.append(" files");
92 }
93 sb.append("</span>");
94 return sb.toString();
95 }
96
97
98
99
100
101
102 public static String getDateAndTime(final Date date) {
103 return "<span class=\"date\">" + OUTPUT_DATE_TIME_FORMAT.format(date) + "</span>";
104 }
105
106
107
108
109
110
111 public static String getRevisionNumber(final String revisionNumber) {
112 return "<span class=\"revisionNumberOuter\">Rev.: <span class=\"revisionNumberInner\">" + revisionNumber + "</span></span>";
113 }
114
115
116
117
118
119
120 public static String getDirectoryLink(final Directory directory) {
121 final String caption = directory.isRoot() ? "/" : directory.getPath();
122 return "<a href=\"" + escape(DirectoryPageMaker.getURL(directory)) + "\" class=\"directory\">" + escape(caption) + "</a>";
123 }
124
125
126
127
128
129
130 public static String getIcon(final String iconFilename) {
131 final StringBuffer result = new StringBuffer("<img src=\"");
132 result.append(escape(iconFilename)).append("\" width=\"");
133 result.append(ReportSuiteMaker.ICON_WIDTH).append("\" height=\"");
134 result.append(ReportSuiteMaker.ICON_HEIGHT).append("\" alt=\"\"/>");
135 return result.toString();
136 }
137
138
139
140
141
142
143
144 public static String getIcon(final String iconFilename, final String title) {
145 final StringBuffer result = new StringBuffer("<img src=\"");
146 result.append(escape(iconFilename)).append("\" width=\"");
147 result.append(ReportSuiteMaker.ICON_WIDTH).append("\" height=\"");
148 result.append(ReportSuiteMaker.ICON_HEIGHT).append("\" alt=\"").append(title).append("\"");
149 result.append(" title=\"").append(title).append("\"");
150 result.append("/>");
151 return result.toString();
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 public static String escapeAuthorName(final String authorName) {
168 return authorName.replaceAll("#", "_").replaceAll("\\\\", "_");
169 }
170
171 public static String escapeDirectoryName(String directoryName) {
172 if (!directoryName.startsWith("/")) {
173 directoryName = "/" + directoryName;
174 }
175 return directoryName.substring(0, directoryName.length() - 1).replaceAll("/", "_");
176 }
177
178
179
180
181
182
183
184 public static String escape(final String text) {
185 String result = text.replaceAll("&", "&");
186 result = result.replaceAll("<", "<");
187 result = result.replaceAll(">", ">");
188 result = result.replaceAll("\n", "<br />\n");
189 return result;
190 }
191
192
193
194
195
196
197 public static String escapeUrl(final String text) {
198 String result = escape(text);
199 result = result.replaceAll("\"", "");
200 result = result.replaceAll(" ", "%20");
201 return result;
202 }
203
204
205
206
207
208 private HTML() {
209 }
210
211
212
213
214
215
216 public static String webifyLinksFromPlainText(final String plainText) {
217 final String escapedText = HTML.escape(plainText);
218 final Matcher m = HTTP_REGEXP2.matcher(escapedText);
219
220
221
222
223
224 String res = m.replaceAll("<a href=\"$0\">$0</a>");
225 res = res.replaceAll("href=\"www.", "href=\"http://www.");
226
227
228
229 return res;
230 }
231 }