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