forked from jenkinsci/analysis-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyleCopParser.java
111 lines (98 loc) · 3.63 KB
/
StyleCopParser.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
101
102
103
104
105
106
107
108
109
110
111
package edu.hm.hafner.analysis.parser;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.IssueParser;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.util.XmlElementUtil;
import static java.lang.Integer.*;
/**
* Parses a StyleCop (http://code.msdn.microsoft.com/sourceanalysis/) xml report file.
*
* @author Sebastian Seidl
*/
public class StyleCopParser extends IssueParser {
private static final long serialVersionUID = 7846052338159003458L;
@Override
public Report parse(final ReaderFactory readerFactory) throws ParsingException {
Document document = readerFactory.readDocument();
// Pre v4.3 uses SourceAnalysisViolations as the parent node name
NodeList mainNode = document.getElementsByTagName("SourceAnalysisViolations");
if (mainNode.getLength() == 0) {
// v4.3 uses StyleCopViolations as the parent node name
mainNode = document.getElementsByTagName("StyleCopViolations");
}
Element rootElement = (Element) mainNode.item(0);
return parseViolations(XmlElementUtil.getChildElementsByName(rootElement, "Violation"));
}
private Report parseViolations(final List<Element> elements) {
Report report = new Report();
for (Element element : elements) {
IssueBuilder builder = new IssueBuilder().setFileName(getString(element, "Source"))
.setLineStart(getLineNumber(element))
.setCategory(getCategory(element))
.setType(getString(element, "Rule"))
.setMessage(element.getTextContent())
.setSeverity(Severity.WARNING_NORMAL);
report.add(builder.build());
}
return report;
}
/**
* Returns the Category of a StyleCop Violation.
*
* @param element
* The Element which represents the violation
*
* @return Category of violation
*/
private String getCategory(final Element element) {
String ruleNameSpace = getString(element, "RuleNamespace");
int i = ruleNameSpace.lastIndexOf('.');
if (i == -1) {
return getString(element, "RuleId");
}
else {
return ruleNameSpace.substring(i + 1);
}
}
/***
* Returns the value for the named attribute if it exists.
*
* @param element
* the element to check for an attribute
* @param name
* the name of the attribute
* @return the value of the attribute; "" if there is no such attribute.
*/
private String getString(final Element element, final String name) {
if (element.hasAttribute(name)) {
return element.getAttribute(name);
}
else {
return StringUtils.EMPTY;
}
}
/***
* Returns the LineNumber for the given violation.
*
* @param violation
* the xml Element "violation" to get the Linenumber from.
* @return the lineNumber of the violation. 0 if there is no LineNumber or the LineNumber cant't be parsed into an
* Integer.
*/
private int getLineNumber(final Element violation) {
if (violation.hasAttribute("LineNumber")) {
return parseInt(violation.getAttribute("LineNumber"));
}
else {
return 0;
}
}
}