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 */ 020 package org.sonar.plugins.pmd; 021 022 import org.sonar.api.BatchExtension; 023 import org.sonar.api.Property; 024 import org.sonar.api.config.Settings; 025 import org.sonar.api.profiles.RulesProfile; 026 import org.sonar.api.resources.Project; 027 import org.sonar.api.utils.SonarException; 028 029 import java.io.File; 030 import java.io.IOException; 031 import java.io.StringWriter; 032 import java.util.Arrays; 033 import java.util.List; 034 035 @org.sonar.api.Properties({ 036 @Property( 037 key = PmdConfiguration.PROPERTY_GENERATE_XML, 038 defaultValue = "false", 039 name = "Generate XML Report", 040 project = false, 041 global = false 042 ) 043 }) 044 public class PmdConfiguration implements BatchExtension { 045 046 public static final String PROPERTY_GENERATE_XML = "sonar.pmd.generateXml"; 047 048 private PmdProfileExporter pmdProfileExporter; 049 private RulesProfile rulesProfile; 050 private Project project; 051 private Settings settings; 052 053 public PmdConfiguration(PmdProfileExporter pmdRulesRepository, RulesProfile rulesProfile, Project project, Settings settings) { 054 this.pmdProfileExporter = pmdRulesRepository; 055 this.rulesProfile = rulesProfile; 056 this.project = project; 057 this.settings = settings; 058 } 059 060 public List<String> getRulesets() { 061 return Arrays.asList(saveXmlFile().getAbsolutePath()); 062 } 063 064 private File saveXmlFile() { 065 try { 066 StringWriter pmdConfiguration = new StringWriter(); 067 pmdProfileExporter.exportProfile(rulesProfile, pmdConfiguration); 068 return project.getFileSystem().writeToWorkingDirectory(pmdConfiguration.toString(), "pmd.xml"); 069 070 } catch (IOException e) { 071 throw new SonarException("Fail to save the PMD configuration", e); 072 } 073 } 074 075 public File getTargetXMLReport() { 076 if (settings.getBoolean(PROPERTY_GENERATE_XML)) { 077 return new File(project.getFileSystem().getSonarWorkingDirectory(), "pmd-result.xml"); 078 } 079 return null; 080 } 081 }