001/* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 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 */ 020package org.sonar.plugins.pmd; 021 022import net.sourceforge.pmd.Report; 023import net.sourceforge.pmd.renderers.Renderer; 024import net.sourceforge.pmd.renderers.XMLRenderer; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027import org.sonar.api.BatchExtension; 028import org.sonar.api.Property; 029import org.sonar.api.config.Settings; 030import org.sonar.api.resources.ProjectFileSystem; 031import org.sonar.api.utils.SonarException; 032 033import java.io.File; 034import java.io.IOException; 035import java.io.StringWriter; 036 037@org.sonar.api.Properties({ 038 @Property( 039 key = PmdConfiguration.PROPERTY_GENERATE_XML, 040 defaultValue = "false", 041 name = "Generate XML Report", 042 project = false, 043 global = false 044 ) 045}) 046public class PmdConfiguration implements BatchExtension { 047 private static final Logger LOG = LoggerFactory.getLogger(PmdConfiguration.class); 048 049 public static final String PROPERTY_GENERATE_XML = "sonar.pmd.generateXml"; 050 public static final String PMD_RESULT_XML = "pmd-result.xml"; 051 052 private final ProjectFileSystem projectFileSystem; 053 private final Settings settings; 054 055 public PmdConfiguration(ProjectFileSystem projectFileSystem, Settings settings) { 056 this.projectFileSystem = projectFileSystem; 057 this.settings = settings; 058 } 059 060 public File getTargetXMLReport() { 061 if (settings.getBoolean(PROPERTY_GENERATE_XML)) { 062 return projectFileSystem.resolvePath(PMD_RESULT_XML); 063 } 064 return null; 065 } 066 067 public File dumpXmlRuleSet(String repositoryKey, String rulesXml) { 068 try { 069 File configurationFile = projectFileSystem.writeToWorkingDirectory(rulesXml, repositoryKey + ".xml"); 070 071 LOG.info("PMD configuration: " + configurationFile.getAbsolutePath()); 072 073 return configurationFile; 074 } catch (IOException e) { 075 throw new SonarException("Fail to save the PMD configuration", e); 076 } 077 } 078 079 public File dumpXmlReport(Report report) { 080 if (!settings.getBoolean(PROPERTY_GENERATE_XML)) { 081 return null; 082 } 083 084 try { 085 String reportAsString = reportToString(report); 086 087 File reportFile = projectFileSystem.writeToWorkingDirectory(reportAsString, PMD_RESULT_XML); 088 089 LOG.info("PMD output report: " + reportFile.getAbsolutePath()); 090 091 return reportFile; 092 } catch (IOException e) { 093 throw new SonarException("Fail to save the PMD report", e); 094 } 095 } 096 097 private static String reportToString(Report report) throws IOException { 098 StringWriter output = new StringWriter(); 099 100 Renderer xmlRenderer = new XMLRenderer(); 101 xmlRenderer.setWriter(output); 102 xmlRenderer.start(); 103 xmlRenderer.renderFileReport(report); 104 xmlRenderer.end(); 105 106 return output.toString(); 107 } 108}