All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.bjurr.violations.lib.parsers.ResharperParser Maven / Gradle / Ivy

There is a newer version: 1.157.3
Show newest version
package se.bjurr.violations.lib.parsers;

import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.reports.Parser.RESHARPER;
import static se.bjurr.violations.lib.util.ViolationParserUtils.findAttribute;
import static se.bjurr.violations.lib.util.ViolationParserUtils.findIntegerAttribute;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getChunks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

public class ResharperParser implements ViolationsParser {

  @Override
  public List parseReportOutput(final String string) throws Exception {
    final List violations = new ArrayList<>();
    final List issueTypeChunks = getChunks(string, "");
    final Map> issueTypesPerTypeId = new HashMap<>();
    for (final String issueTypesChunk : issueTypeChunks) {
      final Map issueType = new HashMap<>();
      final String id = getAttribute(issueTypesChunk, "Id");
      issueType.put("category", getAttribute(issueTypesChunk, "Category"));
      final Optional description = findAttribute(issueTypesChunk, "Description");
      issueType.put("description", description.orElse(id));
      issueType.put("severity", getAttribute(issueTypesChunk, "Severity"));
      issueType.put("url", findAttribute(issueTypesChunk, "WikiUrl").orElse(null));
      issueTypesPerTypeId.put(id, issueType);
    }

    final List issueChunks = getChunks(string, "");
    for (final String issueChunk : issueChunks) {
      final String typeId = getAttribute(issueChunk, "TypeId");
      final String filename = getAttribute(issueChunk, "File");
      final String url = issueTypesPerTypeId.get(typeId).get("url");
      final String message =
          findAttribute(issueChunk, "Message").orElse("")
              + ". "
              + issueTypesPerTypeId.get(typeId).get("category")
              + ". "
              + issueTypesPerTypeId.get(typeId).get("description")
              + (url != null ? ". For more info, visit " + url : "");
      final Integer line = findIntegerAttribute(issueChunk, "Line").orElse(0);
      final String severity = issueTypesPerTypeId.get(typeId).get("severity");
      violations.add( //
          violationBuilder() //
              .setParser(RESHARPER) //
              .setStartLine(line) //
              .setFile(filename) //
              .setSeverity(toSeverity(severity)) //
              .setMessage(message) //
              .setRule(typeId) //
              .build() //
          );
    }
    return violations;
  }

  public SEVERITY toSeverity(final String severity) {
    if (severity.equalsIgnoreCase("ERROR")) {
      return ERROR;
    }
    if (severity.equalsIgnoreCase("WARNING")) {
      return WARN;
    }
    return INFO;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy