You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CheckstyleAuditListener.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. package org.sonar.plugins.checkstyle;
  21. import com.puppycrawl.tools.checkstyle.api.AuditEvent;
  22. import com.puppycrawl.tools.checkstyle.api.AuditListener;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.sonar.api.BatchExtension;
  25. import org.sonar.api.batch.SensorContext;
  26. import org.sonar.api.profiles.RulesProfile;
  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.ActiveRule;
  31. import org.sonar.api.rules.Violation;
  32. /**
  33. * @since 2.3
  34. */
  35. public final class CheckstyleAuditListener implements AuditListener, BatchExtension {
  36. private final SensorContext context;
  37. private final Project project;
  38. private final RulesProfile profile;
  39. private Resource currentResource = null;
  40. public CheckstyleAuditListener(SensorContext context, Project project, RulesProfile profile) {
  41. this.context = context;
  42. this.project = project;
  43. this.profile = profile;
  44. }
  45. public void auditStarted(AuditEvent event) {
  46. }
  47. public void auditFinished(AuditEvent event) {
  48. }
  49. public void fileStarted(AuditEvent event) {
  50. }
  51. public void fileFinished(AuditEvent event) {
  52. currentResource = null;
  53. }
  54. public void addError(AuditEvent event) {
  55. String ruleKey = getRuleKey(event);
  56. if (ruleKey != null) {
  57. ActiveRule activeRule = profile.getActiveRule(CheckstyleConstants.REPOSITORY_KEY, ruleKey);
  58. if (activeRule != null) {
  59. initResource(event);
  60. Violation violation = new Violation(activeRule.getRule(), currentResource)
  61. .setLineId(getLineId(event))
  62. .setMessage(getMessage(event));
  63. context.saveViolation(violation);
  64. }
  65. }
  66. }
  67. private void initResource(AuditEvent event) {
  68. if (currentResource == null) {
  69. String absoluteFilename = event.getFileName();
  70. currentResource = JavaFile.fromAbsolutePath(absoluteFilename, project.getFileSystem().getSourceDirs(), false);
  71. }
  72. }
  73. private String getRuleKey(AuditEvent event) {
  74. String key = null;
  75. try {
  76. key = event.getModuleId();
  77. } catch (Exception e) {
  78. // checkstyle throws a NullPointer if the message is not set
  79. }
  80. if (StringUtils.isBlank(key)) {
  81. try {
  82. key = event.getSourceName();
  83. } catch (Exception e) {
  84. // checkstyle can throw a NullPointer if the message is not set
  85. }
  86. }
  87. return key;
  88. }
  89. private String getMessage(AuditEvent event) {
  90. try {
  91. return event.getMessage();
  92. } catch (Exception e) {
  93. // checkstyle can throw a NullPointer if the message is not set
  94. return null;
  95. }
  96. }
  97. private int getLineId(AuditEvent event) {
  98. try {
  99. return event.getLine();
  100. } catch (Exception e) {
  101. // checkstyle can throw a NullPointer if the message is not set
  102. return 0;
  103. }
  104. }
  105. public void addException(AuditEvent event, Throwable throwable) {
  106. // TODO waiting for sonar technical events ?
  107. }
  108. }