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

se.bjurr.violations.lib.parsers.AndroidLintParser 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.ANDROIDLINT;
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.List;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.util.Optional;

public class AndroidLintParser implements ViolationsParser {

  @Override
  public List parseReportOutput(String string) throws Exception {
    List violations = new ArrayList<>();
    List issues = getChunks(string, "");
    for (String issueChunk : issues) {
      /** Only ever going to be one location. */
      String location = getChunks(issueChunk, "").get(0);

      String filename = getAttribute(location, "file");
      Optional line = findIntegerAttribute(location, "line");
      Optional charAttrib = findIntegerAttribute(location, "column");
      String severity = getAttribute(issueChunk, "severity");

      String id = getAttribute(issueChunk, "id");
      String message = getAttribute(issueChunk, "message");
      String summary = getAttribute(issueChunk, "summary").trim();
      String explanation = getAttribute(issueChunk, "explanation");

      String category = getAttribute(issueChunk, "category");

      violations.add( //
          violationBuilder() //
              .setParser(ANDROIDLINT) //
              .setStartLine(line.or(0)) //
              .setColumn(charAttrib.orNull()) //
              .setFile(filename) //
              .setSeverity(toSeverity(severity)) //
              .setRule(id) //
              .setCategory(category) //
              .setMessage(summary + "\n" + message + "\n" + explanation) //
              .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