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

se.bjurr.violations.lib.parsers.CPPCheckParser 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.CPPCHECK;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getIntegerAttribute;

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

public class CPPCheckParser implements ViolationsParser {

  @Override
  public List parseReportOutput(String string) throws Exception {
    List violations = new ArrayList<>();
    List errorChunks = getChunks(string, "");
    for (String errorChunk : errorChunks) {
      String severity = getAttribute(errorChunk, "severity");
      String msg = getAttribute(errorChunk, "msg");
      String verbose = getAttribute(errorChunk, "verbose");
      String id = getAttribute(errorChunk, "id");
      List locationChunks = getChunks(errorChunk, "");
      for (String locationChunk : locationChunks) {
        Integer line = getIntegerAttribute(locationChunk, "line");
        String fileString = getAttribute(errorChunk, "file");
        violations.add( //
            violationBuilder() //
                .setParser(CPPCHECK) //
                .setStartLine(line) //
                .setFile(fileString) //
                .setSeverity(toSeverity(severity)) //
                .setMessage(msg + ". " + verbose) //
                .setRule(id) //
                .build() //
            );
      }
    }
    return violations;
  }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy