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