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.

CheckstyleConfiguration.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.ConfigurationLoader;
  22. import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
  23. import com.puppycrawl.tools.checkstyle.PropertiesExpander;
  24. import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
  25. import com.puppycrawl.tools.checkstyle.api.Configuration;
  26. import org.apache.commons.io.IOUtils;
  27. import org.apache.commons.lang.CharEncoding;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.sonar.api.BatchExtension;
  31. import org.sonar.api.batch.maven.MavenPlugin;
  32. import org.sonar.api.batch.maven.MavenUtils;
  33. import org.sonar.api.profiles.RulesProfile;
  34. import org.sonar.api.resources.Java;
  35. import org.sonar.api.resources.Project;
  36. import org.sonar.api.utils.SonarException;
  37. import java.io.*;
  38. import java.nio.charset.Charset;
  39. import java.util.List;
  40. import java.util.Properties;
  41. public class CheckstyleConfiguration implements BatchExtension {
  42. private static Logger LOG = LoggerFactory.getLogger(CheckstyleConfiguration.class);
  43. private CheckstyleProfileExporter confExporter;
  44. private RulesProfile profile;
  45. private Project project;
  46. public CheckstyleConfiguration(CheckstyleProfileExporter confExporter, RulesProfile profile, Project project) {
  47. this.confExporter = confExporter;
  48. this.profile = profile;
  49. this.project = project;
  50. }
  51. public File getXMLDefinitionFile() {
  52. if (project.getReuseExistingRulesConfig()) {
  53. return findExistingXML();
  54. }
  55. Writer writer = null;
  56. File xmlFile = new File(project.getFileSystem().getSonarWorkingDirectory(), "checkstyle.xml");
  57. try {
  58. writer = new OutputStreamWriter(new FileOutputStream(xmlFile, false), CharEncoding.UTF_8);
  59. confExporter.exportProfile(profile, writer);
  60. writer.flush();
  61. return xmlFile;
  62. } catch (IOException e) {
  63. throw new SonarException("Fail to save the Checkstyle configuration to " + xmlFile.getPath(), e);
  64. } finally {
  65. IOUtils.closeQuietly(writer);
  66. }
  67. }
  68. private File findExistingXML() {
  69. File file = null;
  70. MavenPlugin mavenPlugin = MavenPlugin.getPlugin(project.getPom(), MavenUtils.GROUP_ID_APACHE_MAVEN, "maven-checkstyle-plugin");
  71. if (mavenPlugin != null) {
  72. String location = mavenPlugin.getParameter("configLocation");
  73. if (location != null) {
  74. file = new File(location);
  75. if (!file.exists()) {
  76. file = new File(project.getFileSystem().getBasedir(), location);
  77. }
  78. }
  79. }
  80. if (file == null || !file.isFile() || !file.exists()) {
  81. throw new SonarException("The checkstyle configuration file does not exist: " + file);
  82. }
  83. return file;
  84. }
  85. public List<File> getSourceFiles() {
  86. return project.getFileSystem().getSourceFiles(Java.INSTANCE);
  87. }
  88. public File getTargetXMLReport() {
  89. if (project.getConfiguration().getBoolean(CheckstyleConstants.GENERATE_XML_KEY, CheckstyleConstants.GENERATE_XML_DEFAULT_VALUE)) {
  90. return new File(project.getFileSystem().getSonarWorkingDirectory(), "checkstyle-result.xml");
  91. }
  92. return null;
  93. }
  94. public Configuration getCheckstyleConfiguration() throws IOException, CheckstyleException {
  95. File xmlConfig = getXMLDefinitionFile();
  96. LOG.info("Checkstyle configuration: " + xmlConfig.getAbsolutePath());
  97. Configuration configuration = ConfigurationLoader.loadConfiguration(xmlConfig.getAbsolutePath(), new PropertiesExpander(new Properties()));
  98. defineCharset(configuration);
  99. return configuration;
  100. }
  101. private void defineCharset(Configuration configuration) {
  102. Configuration[] modules = configuration.getChildren();
  103. for (Configuration module : modules) {
  104. if ("Checker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(module.getName())) {
  105. if (module instanceof DefaultConfiguration) {
  106. Charset charset = getCharset();
  107. LOG.info("Checkstyle charset: " + charset.name());
  108. ((DefaultConfiguration) module).addAttribute("charset", charset.name());
  109. }
  110. }
  111. }
  112. }
  113. public Charset getCharset() {
  114. Charset charset = project.getFileSystem().getSourceCharset();
  115. if (charset == null) {
  116. charset = Charset.forName(System.getProperty("file.encoding", CharEncoding.UTF_8));
  117. }
  118. return charset;
  119. }
  120. }