2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.checkstyle;
22 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
23 import com.puppycrawl.tools.checkstyle.api.AuditListener;
24 import org.apache.commons.lang.StringUtils;
25 import org.sonar.api.BatchExtension;
26 import org.sonar.api.batch.SensorContext;
27 import org.sonar.api.resources.JavaFile;
28 import org.sonar.api.resources.Project;
29 import org.sonar.api.resources.Resource;
30 import org.sonar.api.rules.Rule;
31 import org.sonar.api.rules.RuleFinder;
32 import org.sonar.api.rules.Violation;
37 public class CheckstyleAuditListener implements AuditListener, BatchExtension {
39 private final SensorContext context;
40 private final Project project;
41 private final RuleFinder ruleFinder;
42 private Resource currentResource = null;
44 public CheckstyleAuditListener(SensorContext context, Project project, RuleFinder ruleFinder) {
45 this.context = context;
46 this.project = project;
47 this.ruleFinder = ruleFinder;
50 public void auditStarted(AuditEvent event) {
54 public void auditFinished(AuditEvent event) {
58 public void fileStarted(AuditEvent event) {
62 public void fileFinished(AuditEvent event) {
63 currentResource = null;
66 public void addError(AuditEvent event) {
67 String ruleKey = getRuleKey(event);
68 if (ruleKey != null) {
69 Rule rule = ruleFinder.findByKey(CheckstyleConstants.REPOSITORY_KEY, ruleKey);
72 Violation violation = Violation.create(rule, currentResource)
73 .setLineId(getLineId(event))
74 .setMessage(getMessage(event));
75 context.saveViolation(violation);
80 private void initResource(AuditEvent event) {
81 if (currentResource == null) {
82 String absoluteFilename = event.getFileName();
83 currentResource = JavaFile.fromAbsolutePath(absoluteFilename, project.getFileSystem().getSourceDirs(), false);
87 private String getRuleKey(AuditEvent event) {
90 key = event.getModuleId();
91 } catch (Exception e) {
92 // checkstyle throws a NullPointer if the message is not set
94 if (StringUtils.isBlank(key)) {
96 key = event.getSourceName();
97 } catch (Exception e) {
98 // checkstyle can throw a NullPointer if the message is not set
104 private String getMessage(AuditEvent event) {
106 return event.getMessage();
108 } catch (Exception e) {
109 // checkstyle can throw a NullPointer if the message is not set
114 private int getLineId(AuditEvent event) {
116 return event.getLine();
118 } catch (Exception e) {
119 // checkstyle can throw a NullPointer if the message is not set
124 public void addException(AuditEvent event, Throwable throwable) {
125 // TODO waiting for sonar technical events ?
128 Resource getCurrentResource() {
129 return currentResource;