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

se.bjurr.violations.lib.parsers.CheckStyleParser 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.CHECKSTYLE;
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 CheckStyleParser implements ViolationsParser {

  @Override
  public List parseReportOutput(String string) throws Exception {
    final List violations = new ArrayList<>();
    final List files = getChunks(string, "");
    for (final String fileChunk : files) {
      final String filename = getAttribute(fileChunk, "name");
      final List errors = getChunks(fileChunk, "");
      final List longFormErrors = getChunks(fileChunk, "");
      errors.addAll(longFormErrors);
      for (final String errorChunk : errors) {
        final Integer line = getIntegerAttribute(errorChunk, "line");
        final Optional column = findIntegerAttribute(errorChunk, "column");
        final String severity = getAttribute(errorChunk, "severity");
        final String message = getAttribute(errorChunk, "message");
        final String rule = findAttribute(errorChunk, "source").orElse(null);
        violations.add( //
            violationBuilder() //
                .setParser(CHECKSTYLE) //
                .setStartLine(line) //
                .setColumn(column.orElse(null)) //
                .setFile(filename) //
                .setSeverity(toSeverity(severity)) //
                .setMessage(message) //
                .setRule(rule) //
                .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