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.reports.Parser.CPPCHECK;
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 static se.bjurr.violations.lib.util.ViolationParserUtils.getIntegerAttribute;

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

public class CPPCheckParser implements ViolationsParser {

  @Override
  public List parseReportOutput(final String string) throws Exception {
    final List violations = new ArrayList<>();
    final List errorChunks = getChunks(string, "");
    for (int errorIndex = 0; errorIndex < errorChunks.size(); errorIndex++) {
      final String errorChunk = errorChunks.get(errorIndex);
      final String severity = getAttribute(errorChunk, "severity");
      final String msg = getAttribute(errorChunk, "msg");
      final Optional verbose = findAttribute(errorChunk, "verbose");
      final String id = getAttribute(errorChunk, "id");
      final List locationChunks = getChunks(errorChunk, "");

      for (final String locationChunk : locationChunks) {
        final Integer line = getIntegerAttribute(locationChunk, "line");
        final Optional info = findAttribute(locationChunk, "info");
        final String fileString = getAttribute(errorChunk, "file");
        final String message = this.constructMessage(msg, verbose, info);
        violations.add( //
            Violation.violationBuilder() //
                .setParser(CPPCHECK) //
                .setStartLine(line) //
                .setFile(fileString) //
                .setSeverity(this.toSeverity(severity)) //
                .setMessage(message) //
                .setRule(id) //
                .setGroup(Integer.toString(errorIndex)) //
                .build() //
            );
      }
    }

    final List errorChunksNoEndtag = getChunks(string, "");
    for (int errorIndex = 0; errorIndex < errorChunksNoEndtag.size(); errorIndex++) {
      final String errorChunk = errorChunksNoEndtag.get(errorIndex);
      final String severity = getAttribute(errorChunk, "severity");
      final String msg = getAttribute(errorChunk, "msg");
      final Optional verbose = findAttribute(errorChunk, "verbose");
      final String id = getAttribute(errorChunk, "id");
      final Optional resultLine = findIntegerAttribute(errorChunk, "line");
      final Optional resultFile = findAttribute(errorChunk, "file");
      final Optional resultInfo = findAttribute(errorChunk, "info");

      if (resultLine.isPresent() && resultFile.isPresent()) {
        final String message = this.constructMessage(msg, verbose, resultInfo);
        violations.add( //
            Violation.violationBuilder() //
                .setParser(CPPCHECK) //
                .setStartLine(resultLine.get()) //
                .setFile(resultFile.get()) //
                .setSeverity(this.toSeverity(severity)) //
                .setMessage(message) //
                .setRule(id) //
                .setGroup(Integer.toString(errorIndex)) //
                .build() //
            );
      }
    }

    return violations;
  }

  private String constructMessage(
      final String msg, final Optional verbose, final Optional info) {
    String message = "";
    if (verbose.orElse("").startsWith(msg)) {
      message = verbose.get();
    } else {
      message = msg + ". " + verbose.orElse("");
    }
    if (info.isPresent() && !message.contains(info.get())) {
      message = message + ". " + info.get();
    }
    return message;
  }

  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