|
|
@@ -19,42 +19,62 @@ |
|
|
|
*/ |
|
|
|
package org.sonar.plugins.findbugs; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.Writer; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import org.sonar.api.profiles.ProfileExporter; |
|
|
|
import org.sonar.api.profiles.RulesProfile; |
|
|
|
import org.sonar.api.resources.Java; |
|
|
|
import org.sonar.api.rules.ActiveRule; |
|
|
|
import org.sonar.api.utils.SonarException; |
|
|
|
import org.sonar.plugins.findbugs.xml.Bug; |
|
|
|
import org.sonar.plugins.findbugs.xml.ClassFilter; |
|
|
|
import org.sonar.plugins.findbugs.xml.FindBugsFilter; |
|
|
|
import org.sonar.plugins.findbugs.xml.Match; |
|
|
|
|
|
|
|
import com.thoughtworks.xstream.XStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.Writer; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
public class FindbugsProfileExporter extends ProfileExporter { |
|
|
|
|
|
|
|
public FindbugsProfileExporter() { |
|
|
|
super(FindbugsConstants.REPOSITORY_KEY, FindbugsConstants.PLUGIN_NAME); |
|
|
|
|
|
|
|
setSupportedLanguages(Java.KEY); |
|
|
|
setMimeType("application/xml"); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void exportProfile(RulesProfile profile, Writer writer) { |
|
|
|
List<ActiveRule> activeRulesByRepository = profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY); |
|
|
|
FindBugsFilter filter = buildFindbugsFilter(activeRulesByRepository); |
|
|
|
|
|
|
|
writeXml(filter, writer); |
|
|
|
} |
|
|
|
|
|
|
|
public void exportExclusions(String[] exclusions, Writer writer) { |
|
|
|
FindBugsFilter filter = new FindBugsFilter(); |
|
|
|
if (exclusions != null) { |
|
|
|
for (String exclusion : exclusions) { |
|
|
|
ClassFilter classFilter = new ClassFilter(FindbugsAntConverter.antToJavaRegexpConvertor(exclusion)); |
|
|
|
filter.addMatch(new Match(classFilter)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
writeXml(filter, writer); |
|
|
|
} |
|
|
|
|
|
|
|
private static void writeXml(FindBugsFilter filter, Writer writer) { |
|
|
|
try { |
|
|
|
FindBugsFilter filter = buildFindbugsFilter(profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY)); |
|
|
|
XStream xstream = FindBugsFilter.createXStream(); |
|
|
|
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- Generated by Sonar -->\n".concat(xstream.toXML(filter))); |
|
|
|
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); |
|
|
|
writer.write("<!-- Generated by Sonar -->\n"); |
|
|
|
writer.write(filter.toXml()); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new SonarException("Fail to export the Findbugs profile : " + profile, e); |
|
|
|
throw new SonarException("Fail to export Findbugs configuration", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected static FindBugsFilter buildFindbugsFilter(List<ActiveRule> activeRules) { |
|
|
|
FindBugsFilter root = new FindBugsFilter(); |
|
|
|
|
|
|
|
for (ActiveRule activeRule : activeRules) { |
|
|
|
if (FindbugsConstants.REPOSITORY_KEY.equals(activeRule.getRepositoryKey())) { |
|
|
|
Match child = new Match(); |
|
|
@@ -62,6 +82,7 @@ public class FindbugsProfileExporter extends ProfileExporter { |
|
|
|
root.addMatch(child); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return root; |
|
|
|
} |
|
|
|
} |