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.ConfigurationLoader;
23 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
24 import com.puppycrawl.tools.checkstyle.PropertiesExpander;
25 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
26 import com.puppycrawl.tools.checkstyle.api.Configuration;
27 import org.apache.commons.io.IOUtils;
28 import org.apache.commons.lang.CharEncoding;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.sonar.api.BatchExtension;
32 import org.sonar.api.profiles.RulesProfile;
33 import org.sonar.api.resources.Java;
34 import org.sonar.api.resources.Project;
35 import org.sonar.api.utils.SonarException;
38 import java.nio.charset.Charset;
39 import java.util.List;
40 import java.util.Properties;
42 public class CheckstyleConfiguration implements BatchExtension {
44 private static Logger LOG = LoggerFactory.getLogger(CheckstyleConfiguration.class);
46 private CheckstyleProfileExporter confExporter;
47 private RulesProfile profile;
48 private Project project;
50 public CheckstyleConfiguration(CheckstyleProfileExporter confExporter, RulesProfile profile, Project project) {
51 this.confExporter = confExporter;
52 this.profile = profile;
53 this.project = project;
56 public File getXMLDefinitionFile() {
58 File xmlFile = new File(project.getFileSystem().getSonarWorkingDirectory(), "checkstyle.xml");
60 writer = new OutputStreamWriter(new FileOutputStream(xmlFile, false), CharEncoding.UTF_8);
61 confExporter.exportProfile(profile, writer);
65 } catch (IOException e) {
66 throw new SonarException("Fail to save the Checkstyle configuration to " + xmlFile.getPath(), e);
69 IOUtils.closeQuietly(writer);
73 public List<File> getSourceFiles() {
74 return project.getFileSystem().getSourceFiles(Java.INSTANCE);
77 public File getTargetXMLReport() {
78 if (project.getConfiguration().getBoolean(CheckstyleConstants.GENERATE_XML_KEY, CheckstyleConstants.GENERATE_XML_DEFAULT_VALUE)) {
79 return new File(project.getFileSystem().getSonarWorkingDirectory(), "checkstyle-result.xml");
84 public Configuration getCheckstyleConfiguration() throws IOException, CheckstyleException {
85 File xmlConfig = getXMLDefinitionFile();
87 LOG.info("Checkstyle configuration: " + xmlConfig.getAbsolutePath());
88 Configuration configuration = toCheckstyleConfiguration(xmlConfig);
89 defineCharset(configuration);
93 static Configuration toCheckstyleConfiguration(File xmlConfig) throws CheckstyleException {
94 return ConfigurationLoader.loadConfiguration(xmlConfig.getAbsolutePath(), new PropertiesExpander(new Properties()));
97 private void defineCharset(Configuration configuration) {
98 Configuration[] modules = configuration.getChildren();
99 for (Configuration module : modules) {
100 if ("Checker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(module.getName())) {
101 if (module instanceof DefaultConfiguration) {
102 Charset charset = getCharset();
103 LOG.info("Checkstyle charset: " + charset.name());
104 ((DefaultConfiguration) module).addAttribute("charset", charset.name());
110 public Charset getCharset() {
111 Charset charset = project.getFileSystem().getSourceCharset();
112 if (charset == null) {
113 charset = Charset.forName(System.getProperty("file.encoding", CharEncoding.UTF_8));