forked from jenkinsci/analysis-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEclipseParser.java
100 lines (84 loc) · 3.23 KB
/
EclipseParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package edu.hm.hafner.analysis.parser;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import edu.hm.hafner.analysis.Categories;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.LookaheadParser;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.util.LookaheadStream;
/**
* A parser for Eclipse compiler warnings.
*
* @author Ullrich Hafner
* @author Jason Faust
*/
public class EclipseParser extends LookaheadParser {
private static final long serialVersionUID = 425883472788422955L;
private static final String ECLIPSE_FIRST_LINE_REGEXP =
".*\\d+\\.\\s*(?<severity>WARNING|ERROR|INFO) in (?<file>.*)\\s*\\(at line (?<line>\\d+)\\)";
static final String WARNING = "WARNING";
static final String ERROR = "ERROR";
static final String INFO = "INFO";
private static final String JAVADOC_PREFIX = "Javadoc:";
@Override
public boolean accepts(final ReaderFactory readerFactory) {
return !isXmlFile(readerFactory);
}
/**
* Creates a new instance of {@link EclipseParser}.
*/
public EclipseParser() {
super(ECLIPSE_FIRST_LINE_REGEXP);
}
@Override
protected boolean isLineInteresting(final String line) {
return line.contains(WARNING) || line.contains(ERROR) || line.contains(INFO);
}
@Override
protected Optional<Issue> createIssue(final Matcher matcher, final LookaheadStream lookahead,
final IssueBuilder builder) {
builder.guessSeverity(matcher.group("severity"))
.setFileName(matcher.group("file"))
.setLineStart(matcher.group("line"));
List<String> context = new ArrayList<>();
while (!lookahead.hasNext("^.*----------.*$") && lookahead.hasNext()) {
context.add(lookahead.next());
}
if (!context.isEmpty()) {
extractMessage(builder, context.remove(context.size() - 1));
}
builder.setAdditionalProperties(context.hashCode());
return builder.buildOptional();
}
static void extractMessage(final IssueBuilder builder, final String message) {
Pattern ant = Pattern.compile("^(?:.*\\[.+\\])?\\s*(.*)");
Matcher messageMatcher = ant.matcher(message);
if (messageMatcher.matches()) {
String msg = messageMatcher.group(1);
builder.setMessage(msg);
extractCategory(builder, msg);
}
}
/**
* Sets the issue's category to {@code Javadoc} if the message starts with {@value #JAVADOC_PREFIX}, {@code Other}
* otherwise. Unlike {@link #extractMessage(IssueBuilder, String)}, the {@code message} is assumed to be cleaned-up.
*
* @param builder
* IssueBuilder to populate.
* @param message
* issue to examine.
*/
static void extractCategory(final IssueBuilder builder, final String message) {
if (StringUtils.startsWith(message, JAVADOC_PREFIX)) {
builder.setCategory(Categories.JAVADOC);
}
else {
builder.setCategory(Categories.OTHER);
}
}
}