123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
-
- package org.sonar.plugins.checkstyle;
-
- import com.puppycrawl.tools.checkstyle.api.AuditEvent;
- import com.puppycrawl.tools.checkstyle.api.AuditListener;
- import org.apache.commons.lang.StringUtils;
- import org.sonar.api.BatchExtension;
- import org.sonar.api.batch.SensorContext;
- import org.sonar.api.profiles.RulesProfile;
- import org.sonar.api.resources.JavaFile;
- import org.sonar.api.resources.Project;
- import org.sonar.api.resources.Resource;
- import org.sonar.api.rules.ActiveRule;
- import org.sonar.api.rules.Rule;
- import org.sonar.api.rules.RuleFinder;
- import org.sonar.api.rules.Violation;
-
- /**
- * @since 2.3
- */
- public class CheckstyleAuditListener implements AuditListener, BatchExtension {
-
- private final SensorContext context;
- private final Project project;
- private final RuleFinder ruleFinder;
- private Resource currentResource = null;
-
- public CheckstyleAuditListener(SensorContext context, Project project, RuleFinder ruleFinder) {
- this.context = context;
- this.project = project;
- this.ruleFinder = ruleFinder;
- }
-
- public void auditStarted(AuditEvent event) {
-
- }
-
- public void auditFinished(AuditEvent event) {
-
- }
-
- public void fileStarted(AuditEvent event) {
-
- }
-
- public void fileFinished(AuditEvent event) {
- currentResource = null;
- }
-
- public void addError(AuditEvent event) {
- String ruleKey = getRuleKey(event);
- if (ruleKey != null) {
- Rule rule = ruleFinder.findByKey(CheckstyleConstants.REPOSITORY_KEY, ruleKey);
- if (rule != null) {
- initResource(event);
- Violation violation = Violation.create(rule, currentResource)
- .setLineId(getLineId(event))
- .setMessage(getMessage(event));
- context.saveViolation(violation);
- }
- }
- }
-
- private void initResource(AuditEvent event) {
- if (currentResource == null) {
- String absoluteFilename = event.getFileName();
- currentResource = JavaFile.fromAbsolutePath(absoluteFilename, project.getFileSystem().getSourceDirs(), false);
- }
- }
-
- private String getRuleKey(AuditEvent event) {
- String key = null;
- try {
- key = event.getModuleId();
- } catch (Exception e) {
- // checkstyle throws a NullPointer if the message is not set
- }
- if (StringUtils.isBlank(key)) {
- try {
- key = event.getSourceName();
- } catch (Exception e) {
- // checkstyle can throw a NullPointer if the message is not set
- }
- }
- return key;
- }
-
- private String getMessage(AuditEvent event) {
- try {
- return event.getMessage();
-
- } catch (Exception e) {
- // checkstyle can throw a NullPointer if the message is not set
- return null;
- }
- }
-
- private int getLineId(AuditEvent event) {
- try {
- return event.getLine();
-
- } catch (Exception e) {
- // checkstyle can throw a NullPointer if the message is not set
- return 0;
- }
- }
-
- public void addException(AuditEvent event, Throwable throwable) {
- // TODO waiting for sonar technical events ?
- }
-
- Resource getCurrentResource() {
- return currentResource;
- }
- }
|