forked from jenkinsci/analysis-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonBaseParser.java
116 lines (110 loc) · 4.15 KB
/
JsonBaseParser.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
112
113
114
115
116
package edu.hm.hafner.analysis.parser;
import java.util.Optional;
import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONObject;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.LineRange;
import edu.hm.hafner.analysis.LineRangeList;
import edu.hm.hafner.analysis.Severity;
/**
* Base Parser JSON format.
*
* @author Jeremie Bresson
*/
abstract class JsonBaseParser extends IssuePropertiesParser {
private static final long serialVersionUID = -2318844382394973833L;
/**
* Deserialize an Issue from a JSON object.
*
* @param jsonIssue
* the issue as JSON object
*
* @return issue instance
*/
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
Optional<Issue> convertToIssue(final JSONObject jsonIssue) {
IssueBuilder builder = new IssueBuilder();
if (jsonIssue.has(ADDITIONAL_PROPERTIES)) {
builder.setAdditionalProperties(jsonIssue.getString(ADDITIONAL_PROPERTIES));
}
if (jsonIssue.has(CATEGORY)) {
builder.setCategory(jsonIssue.getString(CATEGORY));
}
if (jsonIssue.has(COLUMN_END)) {
builder.setColumnEnd(jsonIssue.getInt(COLUMN_END));
}
if (jsonIssue.has(COLUMN_START)) {
builder.setColumnStart(jsonIssue.getInt(COLUMN_START));
}
if (jsonIssue.has(DESCRIPTION)) {
builder.setDescription(jsonIssue.getString(DESCRIPTION));
}
if (jsonIssue.has(DIRECTORY)) {
builder.setDirectory(jsonIssue.getString(DIRECTORY));
}
if (jsonIssue.has(FINGERPRINT)) {
builder.setFingerprint(jsonIssue.getString(FINGERPRINT));
}
if (jsonIssue.has(FILE_NAME)) {
builder.setFileName(jsonIssue.getString(FILE_NAME));
}
if (jsonIssue.has(ID)) {
builder.setId(UUID.fromString(jsonIssue.getString(ID)));
}
if (jsonIssue.has(LINE_END)) {
builder.setLineEnd(jsonIssue.getInt(LINE_END));
}
if (jsonIssue.has(LINE_RANGES)) {
JSONArray jsonRanges = jsonIssue.getJSONArray(LINE_RANGES);
LineRangeList lineRanges = convertToLineRangeList(jsonRanges);
builder.setLineRanges(lineRanges);
}
if (jsonIssue.has(LINE_START)) {
builder.setLineStart(jsonIssue.getInt(LINE_START));
}
if (jsonIssue.has(MESSAGE)) {
builder.setMessage(jsonIssue.getString(MESSAGE));
}
if (jsonIssue.has(MODULE_NAME)) {
builder.setModuleName(jsonIssue.getString(MODULE_NAME));
}
if (jsonIssue.has(ORIGIN)) {
builder.setOrigin(jsonIssue.getString(ORIGIN));
}
if (jsonIssue.has(PACKAGE_NAME)) {
builder.setPackageName(jsonIssue.getString(PACKAGE_NAME));
}
if (jsonIssue.has(REFERENCE)) {
builder.setReference(jsonIssue.getString(REFERENCE));
}
if (jsonIssue.has(SEVERITY)) {
builder.setSeverity(Severity.valueOf(jsonIssue.getString(SEVERITY)));
}
if (jsonIssue.has(TYPE)) {
builder.setType(jsonIssue.getString(TYPE));
}
return builder.buildOptional();
}
private LineRangeList convertToLineRangeList(final JSONArray jsonRanges) {
LineRangeList lineRanges = new LineRangeList();
for (int i = 0; i < jsonRanges.length(); i++) {
JSONObject jsonRange = jsonRanges.getJSONObject(i);
if (jsonRange.has(LINE_RANGE_START)) {
if (jsonRange.has(LINE_RANGE_END)) {
lineRanges.add(new LineRange(jsonRange.getInt(LINE_RANGE_START),
jsonRange.getInt(LINE_RANGE_END)));
}
else {
lineRanges.add(new LineRange(jsonRange.getInt(LINE_RANGE_START)));
}
}
else if (jsonRange.has(LINE_RANGE_END)) {
lineRanges.add(new LineRange(jsonRange.getInt(LINE_RANGE_END),
jsonRange.getInt(LINE_RANGE_END)));
}
}
return lineRanges;
}
}