123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2009 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
- 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.Violation;
-
- /**
- * @since 2.3
- */
- public final class CheckstyleAuditListener implements AuditListener, BatchExtension {
-
- private final SensorContext context;
- private final Project project;
- private final RulesProfile profile;
- private Resource currentResource = null;
-
- public CheckstyleAuditListener(SensorContext context, Project project, RulesProfile profile) {
- this.context = context;
- this.project = project;
- this.profile = profile;
- }
-
- 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) {
- ActiveRule activeRule = profile.getActiveRule(CheckstyleConstants.REPOSITORY_KEY, ruleKey);
- if (activeRule != null) {
- initResource(event);
- Violation violation = new Violation(activeRule.getRule(), 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 ?
- }
- }
|