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 com.google.common.collect.Lists.newArrayList;
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 java.io.File;
import java.util.List;

import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.io.Files;

public class CheckStyleParser extends ViolationsParser {

 @Override
 public List parseFile(File file) throws Exception {
  String string = Files.toString(file, Charsets.UTF_8);
  List violations = newArrayList();
  List files = getChunks(string, "");
  for (String fileChunk : files) {
   String filename = getAttribute(fileChunk, "name");
   List errors = getChunks(fileChunk, "");
   for (String errorChunk : errors) {
    Integer line = getIntegerAttribute(errorChunk, "line");
    Optional column = findIntegerAttribute(errorChunk, "column");
    String severity = getAttribute(errorChunk, "severity");
    String message = getAttribute(errorChunk, "message");
    String rule = getAttribute(errorChunk, "source");
    violations.add(//
      violationBuilder()//
        .setStartLine(line)//
        .setColumn(column.orNull())//
        .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