Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-57098] JSON Parser for Json report #177

Merged
merged 3 commits into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions src/main/java/edu/hm/hafner/analysis/parser/JsonBaseParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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
*/
public 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
*/
protected 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;
}
}
53 changes: 53 additions & 0 deletions src/main/java/edu/hm/hafner/analysis/parser/JsonLogParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package edu.hm.hafner.analysis.parser;

import java.util.Optional;
import java.util.stream.Stream;

import org.json.JSONException;
import org.json.JSONObject;

import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;

/**
* Parser for logs in JSON format.
*
* @author Jeremie Bresson
*/
public class JsonLogParser extends JsonBaseParser {
private static final long serialVersionUID = 1349282064371959197L;

@Override
public boolean accepts(final ReaderFactory readerFactory) {
return !readerFactory.getFileName().endsWith(".xml") && !readerFactory.getFileName().endsWith(".json");
}

@Override
public Report parse(final ReaderFactory readerFactory) throws ParsingException {
try (Stream<String> lines = readerFactory.readStream()) {
Report report = new Report();
lines.map(String::trim)
.filter(line -> !line.isEmpty())
.filter(line -> !line.startsWith("//"))
.filter(line -> line.charAt(0) != '#')
.map(line -> parseIssue(line, report))
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(report::add);
return report;
}
}

private Optional<Issue> parseIssue(final String line, final Report report) {
try {
JSONObject jsonIssue = new JSONObject(line);
return convertToIssue(jsonIssue);
}
catch (JSONException e) {
report.logException(e, "Could not parse line: «%s»", line);
return Optional.empty();
}
}
}
137 changes: 23 additions & 114 deletions src/main/java/edu/hm/hafner/analysis/parser/JsonParser.java
Original file line number Diff line number Diff line change
@@ -1,143 +1,52 @@
package edu.hm.hafner.analysis.parser;

import java.io.IOException;
import java.io.Reader;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

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.ParsingException;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Severity;

/**
* Parser for logs in JSON format.
* Parser report in JSON format as exported by the "Jenkins Warnings Next Generation Plugin".
*
* @author Jeremie Bresson
*/
public class JsonParser extends IssuePropertiesParser {
private static final long serialVersionUID = 1349282064371959197L;
public class JsonParser extends JsonBaseParser {
private static final long serialVersionUID = -6494117943149352139L;
private static final String ISSUES = "issues";

@Override
public boolean accepts(final ReaderFactory readerFactory) {
return !readerFactory.getFileName().endsWith(".xml");
return readerFactory.getFileName().endsWith(".json");
}

@Override
public Report parse(final ReaderFactory readerFactory) throws ParsingException {
try (Stream<String> lines = readerFactory.readStream()) {
try (Reader reader = readerFactory.create()) {
JSONObject jsonReport = (JSONObject) new JSONTokener(reader).nextValue();

Report report = new Report();
lines.map(String::trim)
.filter(line -> !line.isEmpty())
.map(line -> parseIssue(line, report))
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(report::add);
if (jsonReport.has(ISSUES)) {
JSONArray issues = jsonReport.getJSONArray(ISSUES);
StreamSupport.stream(issues.spliterator(), false)
.filter(o -> o instanceof JSONObject)
.map(o -> convertToIssue((JSONObject) o))
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(report::add);
}
return report;
}
}

private Optional<Issue> parseIssue(final String line, final Report report) {
try {
JSONObject jsonIssue = new JSONObject(line);

return convertToIssue(jsonIssue);
}
catch (JSONException e) {
report.logException(e, "Could not parse line: «%s»", line);
return Optional.empty();
}
}

private 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)));
}
catch (IOException | JSONException e) {
throw new ParsingException(e);
}
return lineRanges;
}
}
Loading