File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\console\UrlAnnotator.java.html

1 package hudson.console;
2
3 import hudson.Extension;
4 import hudson.MarkupText;
5 import hudson.MarkupText.SubText;
6
7 import java.util.regex.Pattern;
8
9 /**
10 * Annotates URLs in the console output to hyperlink.
11 *
12 * @author Kohsuke Kawaguchi
13 */
14 @Extension
15 public class UrlAnnotator extends ConsoleAnnotatorFactory<Object> {
16 @Override
17 public ConsoleAnnotator newInstance(Object context) {
18 return new UrlConsoleAnnotator();
19 }
20
21 private static class UrlConsoleAnnotator extends ConsoleAnnotator {
22 public ConsoleAnnotator annotate(Object context, MarkupText text) {
23 for (SubText t : text.findTokens(URL)) {
24 int prev = t.start() - 1;
25 char ch = prev>=0 ? text.charAt(prev) : ' ';
26 int idx = OPEN.indexOf(ch);
27 if (idx>=0) {// if inside a bracket, exclude the end bracket.
28 t=t.subText(0,t.getText().indexOf(CLOSE.charAt(idx)));
29 }
30 t.href(t.getText());
31 }
32 return this;
33 }
34
35 private static final long serialVersionUID = 1L;
36
37 /**
38 * Starts with a word boundary and protocol identifier,
39 * don't include any whitespace, '&lt;', nor '>'.
40 * In addition, the last character shouldn't be ',' ':', '"', etc, as often those things show up right next
41 * to URL in plain text (e.g., test="http://www.example.com/")
42 */
43 private static final Pattern URL = Pattern.compile("\\b(http|https|ftp)://[^\\s<>]+[^\\s<>,\\.:\"'()\\[\\]=]");
44
45 private static final String OPEN = "'\"()[]<>";
46 private static final String CLOSE= "'\")(][><";
47 }
48 }
49