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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010-2011, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.console;
25
26 import hudson.Extension;
27 import hudson.MarkupText;
28
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 /**
33 * Placed on the beginning of the exception stack trace produced by Hudson, which in turn produces hyperlinked stack trace.
34 *
35 * <p>
36 * Exceptions in the user code (like junit etc) should be handled differently. This is only for exceptions
37 * that occur inside Hudson.
38 *
39 * @author Kohsuke Kawaguchi
40 * @since 1.349
41 */
42 public class HudsonExceptionNote extends ConsoleNote<Object> {
43
44 @Override
45 public ConsoleAnnotator annotate(Object context, MarkupText text, int charPos) {
46 // An exception stack trace looks like this:
47 // org.acme.FooBarException: message
48 // <TAB>at org.acme.Foo.method(Foo.java:123)
49 // Caused by: java.lang.ClassNotFoundException:
50 String line = text.getText();
51 int end = line.indexOf(':',charPos);
52 if (end<0) {
53 if (CLASSNAME.matcher(line.substring(charPos)).matches())
54 end = line.length();
55 else
56 return null; // unexpected format. abort.
57 }
58 text.addHyperlinkLowKey(charPos,end,annotateClassName(line.substring(charPos,end)));
59
60 return new ConsoleAnnotator() {
61 public ConsoleAnnotator annotate(Object context, MarkupText text) {
62 String line = text.getText();
63
64 Matcher m = STACK_TRACE_ELEMENT.matcher(line);
65 if (m.find()) {// allow the match to happen in the middle of a line to cope with prefix. Ant and Maven put them, among many other tools.
66 text.addHyperlinkLowKey(m.start()+4,m.end(),annotateMethodName(m.group(1),m.group(2),m.group(3),Integer.parseInt(m.group(4))));
67 return this;
68 }
69
70 int idx = line.indexOf(CAUSED_BY);
71 if (idx>=0) {
72 int s = idx + CAUSED_BY.length();
73 int e = line.indexOf(':', s);
74 if (e<0) e = line.length();
75 text.addHyperlinkLowKey(s,e,annotateClassName(line.substring(s,e)));
76 return this;
77 }
78
79 if (AND_MORE.matcher(line).matches())
80 return this;
81
82 // looks like we are done with the stack trace
83 return null;
84 }
85 };
86 }
87
88 // TODO; separate out the annotations and mark up
89
90 private String annotateMethodName(String className, String methodName, String sourceFileName, int lineNumber) {
91 return "http://stacktrace.jenkins-ci.org/search/?query="+className+'.'+methodName+"&entity=method";
92 }
93
94 private String annotateClassName(String className) {
95 return "http://stacktrace.jenkins-ci.org/search?query="+className;
96 }
97
98 @Extension
99 public static final class DescriptorImpl extends ConsoleAnnotationDescriptor {
100 @Override
101 public String getDisplayName() {
102 return "Exception Stack Trace";
103 }
104 }
105
106 /**
107 * Regular expression that represents a valid class name.
108 */
109 private static final String CLASSNAME_PATTERN = "[\\p{L}0-9$_.]+";
110
111 private static final Pattern CLASSNAME = Pattern.compile(CLASSNAME_PATTERN+"\r?\n?");
112
113 /**
114 * Matches to the line like "\tat org.acme.Foo.method(File.java:123)"
115 * and captures class name, method name, source file name, and line number separately.
116 */
117 private static final Pattern STACK_TRACE_ELEMENT = Pattern.compile("\tat ("+CLASSNAME_PATTERN+")\\.([\\p{L}0-9$_<>]+)\\((\\S+):([0-9]+)\\)");
118
119 private static final String CAUSED_BY = "Caused by: ";
120
121 private static final Pattern AND_MORE = Pattern.compile("\t... [0-9]+ more\n");
122 }
123