001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.plugins.checkstyle;
021    
022    import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
023    import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
024    import com.puppycrawl.tools.checkstyle.PropertiesExpander;
025    import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
026    import org.apache.commons.io.IOUtils;
027    import org.apache.commons.lang.CharEncoding;
028    import org.slf4j.Logger;
029    import org.slf4j.LoggerFactory;
030    import org.sonar.api.BatchExtension;
031    import org.sonar.api.CoreProperties;
032    import org.sonar.api.Property;
033    import org.sonar.api.config.Settings;
034    import org.sonar.api.profiles.RulesProfile;
035    import org.sonar.api.resources.Java;
036    import org.sonar.api.resources.ProjectFileSystem;
037    import org.sonar.api.utils.SonarException;
038    
039    import java.io.*;
040    import java.nio.charset.Charset;
041    import java.util.List;
042    import java.util.Locale;
043    import java.util.Properties;
044    
045    @org.sonar.api.Properties({
046        @Property(key = CheckstyleConfiguration.PROPERTY_GENERATE_XML,
047            defaultValue = "false",
048            name = "Generate XML Report",
049            project = false, global = false)})
050    public class CheckstyleConfiguration implements BatchExtension {
051    
052      private static final Logger LOG = LoggerFactory.getLogger(CheckstyleConfiguration.class);
053      public static final String PROPERTY_GENERATE_XML = "sonar.checkstyle.generateXml";
054    
055      private CheckstyleProfileExporter confExporter;
056      private RulesProfile profile;
057      private Settings conf;
058      private ProjectFileSystem fileSystem;
059    
060      public CheckstyleConfiguration(Settings conf, CheckstyleProfileExporter confExporter, RulesProfile profile, ProjectFileSystem fileSystem) {
061        this.conf = conf;
062        this.confExporter = confExporter;
063        this.profile = profile;
064        this.fileSystem = fileSystem;
065      }
066    
067      public File getXMLDefinitionFile() {
068        Writer writer = null;
069        File xmlFile = new File(fileSystem.getSonarWorkingDirectory(), "checkstyle.xml");
070        try {
071          writer = new OutputStreamWriter(new FileOutputStream(xmlFile, false), CharEncoding.UTF_8);
072          confExporter.exportProfile(profile, writer);
073          writer.flush();
074          return xmlFile;
075    
076        } catch (IOException e) {
077          throw new SonarException("Fail to save the Checkstyle configuration to " + xmlFile.getPath(), e);
078    
079        } finally {
080          IOUtils.closeQuietly(writer);
081        }
082      }
083    
084      public List<File> getSourceFiles() {
085        return fileSystem.getSourceFiles(Java.INSTANCE);
086      }
087    
088      public File getTargetXMLReport() {
089        if (conf.getBoolean(PROPERTY_GENERATE_XML)) {
090          return new File(fileSystem.getSonarWorkingDirectory(), "checkstyle-result.xml");
091        }
092        return null;
093      }
094    
095      public com.puppycrawl.tools.checkstyle.api.Configuration getCheckstyleConfiguration() throws IOException, CheckstyleException {
096        File xmlConfig = getXMLDefinitionFile();
097    
098        LOG.info("Checkstyle configuration: " + xmlConfig.getAbsolutePath());
099        com.puppycrawl.tools.checkstyle.api.Configuration configuration = toCheckstyleConfiguration(xmlConfig);
100        defineCharset(configuration);
101        return configuration;
102      }
103    
104      static com.puppycrawl.tools.checkstyle.api.Configuration toCheckstyleConfiguration(File xmlConfig) throws CheckstyleException {
105        return ConfigurationLoader.loadConfiguration(xmlConfig.getAbsolutePath(), new PropertiesExpander(new Properties()));
106      }
107    
108      private void defineCharset(com.puppycrawl.tools.checkstyle.api.Configuration configuration) {
109        com.puppycrawl.tools.checkstyle.api.Configuration[] modules = configuration.getChildren();
110        for (com.puppycrawl.tools.checkstyle.api.Configuration module : modules) {
111          if ("Checker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(module.getName())) {
112            if (module instanceof DefaultConfiguration) {
113              Charset charset = getCharset();
114              LOG.info("Checkstyle charset: " + charset.name());
115              ((DefaultConfiguration) module).addAttribute("charset", charset.name());
116            }
117          }
118        }
119      }
120    
121      public Locale getLocale() {
122        return new Locale(conf.getString(CoreProperties.CORE_VIOLATION_LOCALE_PROPERTY));
123      }
124    
125      public Charset getCharset() {
126        Charset charset = fileSystem.getSourceCharset();
127        if (charset == null) {
128          charset = Charset.forName(System.getProperty("file.encoding", CharEncoding.UTF_8));
129        }
130        return charset;
131      }
132    }