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.pmd; 021 022 import java.io.Reader; 023 import java.util.List; 024 025 import org.jdom.Document; 026 import org.jdom.Element; 027 import org.jdom.Namespace; 028 import org.jdom.input.SAXBuilder; 029 import org.slf4j.Logger; 030 import org.slf4j.LoggerFactory; 031 import org.sonar.api.profiles.ProfileImporter; 032 import org.sonar.api.profiles.RulesProfile; 033 import org.sonar.api.resources.Java; 034 import org.sonar.api.rules.ActiveRule; 035 import org.sonar.api.rules.Rule; 036 import org.sonar.api.rules.RuleFinder; 037 import org.sonar.api.rules.RuleQuery; 038 import org.sonar.api.utils.ValidationMessages; 039 import org.sonar.plugins.pmd.xml.PmdProperty; 040 import org.sonar.plugins.pmd.xml.PmdRule; 041 import org.sonar.plugins.pmd.xml.PmdRuleset; 042 043 public class PmdProfileImporter extends ProfileImporter { 044 045 private final RuleFinder ruleFinder; 046 private static final Logger LOG = LoggerFactory.getLogger(PmdProfileImporter.class); 047 048 public PmdProfileImporter(RuleFinder ruleFinder) { 049 super(PmdConstants.REPOSITORY_KEY, PmdConstants.PLUGIN_NAME); 050 setSupportedLanguages(Java.KEY); 051 this.ruleFinder = ruleFinder; 052 } 053 054 @Override 055 public RulesProfile importProfile(Reader pmdConfigurationFile, ValidationMessages messages) { 056 PmdRuleset pmdRuleset = parsePmdRuleset(pmdConfigurationFile, messages); 057 return createRuleProfile(pmdRuleset, messages); 058 } 059 060 protected RulesProfile createRuleProfile(PmdRuleset pmdRuleset, ValidationMessages messages) { 061 RulesProfile profile = RulesProfile.create(); 062 for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { 063 if (PmdConstants.XPATH_CLASS.equals(pmdRule.getClazz())) { 064 messages.addWarningText("PMD XPath rule '" + pmdRule.getName() 065 + "' can't be imported automatically. The rule must be created manually through the Sonar web interface."); 066 continue; 067 } 068 if (pmdRule.getRef() == null) { 069 messages.addWarningText("A PMD rule without 'ref' attribute can't be imported. see '" + pmdRule.getClazz() + "'"); 070 continue; 071 } 072 Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PmdConstants.REPOSITORY_KEY).withConfigKey(pmdRule.getRef())); 073 if (rule != null) { 074 ActiveRule activeRule = profile.activateRule(rule, PmdLevelUtils.fromLevel(pmdRule.getPriority())); 075 if (pmdRule.getProperties() != null) { 076 for (PmdProperty prop : pmdRule.getProperties()) { 077 if (rule.getParam(prop.getName()) == null) { 078 messages.addWarningText("The property '" + prop.getName() + "' is not supported in the pmd rule: " + pmdRule.getRef()); 079 continue; 080 } 081 activeRule.setParameter(prop.getName(), prop.getValue()); 082 } 083 } 084 } else { 085 messages.addWarningText("Unable to import unknown PMD rule '" + pmdRule.getRef() + "'"); 086 } 087 } 088 return profile; 089 } 090 091 protected PmdRuleset parsePmdRuleset(Reader pmdConfigurationFile, ValidationMessages messages) { 092 try { 093 SAXBuilder parser = new SAXBuilder(); 094 Document dom = parser.build(pmdConfigurationFile); 095 Element eltResultset = dom.getRootElement(); 096 Namespace namespace = eltResultset.getNamespace(); 097 PmdRuleset pmdResultset = new PmdRuleset(); 098 for (Element eltRule : getChildren(eltResultset, "rule", namespace)) { 099 PmdRule pmdRule = new PmdRule(eltRule.getAttributeValue("ref")); 100 pmdRule.setClazz(eltRule.getAttributeValue("class")); 101 pmdRule.setName(eltRule.getAttributeValue("name")); 102 pmdRule.setMessage(eltRule.getAttributeValue("message")); 103 parsePmdPriority(eltRule, pmdRule, namespace); 104 parsePmdProperties(eltRule, pmdRule, namespace); 105 pmdResultset.addRule(pmdRule); 106 } 107 return pmdResultset; 108 } catch (Exception e) { 109 String errorMessage = "The PMD configuration file is not valid"; 110 messages.addErrorText(errorMessage + " : " + e.getMessage()); 111 LOG.error(errorMessage, e); 112 return new PmdRuleset(); 113 } 114 } 115 116 private List<Element> getChildren(Element parent, String childName, Namespace namespace) { 117 if (namespace == null) { 118 return (List<Element>) parent.getChildren(childName); 119 } else { 120 return (List<Element>) parent.getChildren(childName, namespace); 121 } 122 } 123 124 private void parsePmdProperties(Element eltRule, PmdRule pmdRule, Namespace namespace) { 125 for (Element eltProperties : getChildren(eltRule, "properties", namespace)) { 126 for (Element eltProperty : getChildren(eltProperties, "property", namespace)) { 127 pmdRule.addProperty(new PmdProperty(eltProperty.getAttributeValue("name"), eltProperty.getAttributeValue("value"))); 128 } 129 } 130 } 131 132 private void parsePmdPriority(Element eltRule, PmdRule pmdRule, Namespace namespace) { 133 for (Element eltPriority : getChildren(eltRule, "priority", namespace)) { 134 pmdRule.setPriority(eltPriority.getValue()); 135 } 136 } 137 }