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.checkstyle;
021    
022    import java.io.Reader;
023    import java.util.Map;
024    
025    import javax.xml.stream.XMLInputFactory;
026    import javax.xml.stream.XMLStreamException;
027    
028    import com.google.common.collect.Maps;
029    import org.apache.commons.lang.StringUtils;
030    import org.codehaus.stax2.XMLInputFactory2;
031    import org.codehaus.staxmate.SMInputFactory;
032    import org.codehaus.staxmate.in.SMHierarchicCursor;
033    import org.codehaus.staxmate.in.SMInputCursor;
034    import org.sonar.api.profiles.ProfileImporter;
035    import org.sonar.api.profiles.RulesProfile;
036    import org.sonar.api.resources.Java;
037    import org.sonar.api.rules.ActiveRule;
038    import org.sonar.api.rules.Rule;
039    import org.sonar.api.rules.RuleFinder;
040    import org.sonar.api.rules.RuleQuery;
041    import org.sonar.api.utils.ValidationMessages;
042    
043    public class CheckstyleProfileImporter extends ProfileImporter {
044    
045      private static final String CHECKER_MODULE = "Checker";
046      private static final String TREEWALKER_MODULE = "TreeWalker";
047      private static final String MODULE_NODE = "module";
048      private final RuleFinder ruleFinder;
049    
050      public CheckstyleProfileImporter(RuleFinder ruleFinder) {
051        super(CheckstyleConstants.REPOSITORY_KEY, CheckstyleConstants.PLUGIN_NAME);
052        setSupportedLanguages(Java.KEY);
053        this.ruleFinder = ruleFinder;
054      }
055    
056      @Override
057      public RulesProfile importProfile(Reader reader, ValidationMessages messages) {
058        SMInputFactory inputFactory = initStax();
059        RulesProfile profile = RulesProfile.create();
060        try {
061          SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
062          rootC.advance(); // <module name="Checker">
063          SMInputCursor rootModulesCursor = rootC.childElementCursor(MODULE_NODE);
064          while (rootModulesCursor.getNext() != null) {
065            String configKey = rootModulesCursor.getAttrValue("name");
066            if (StringUtils.equals(TREEWALKER_MODULE, configKey)) {
067              SMInputCursor treewalkerCursor = rootModulesCursor.childElementCursor(MODULE_NODE);
068              while (treewalkerCursor.getNext() != null) {
069                processModule(profile, CHECKER_MODULE + "/" + TREEWALKER_MODULE + "/", treewalkerCursor, messages);
070              }
071            } else {
072              processModule(profile, CHECKER_MODULE + "/", rootModulesCursor, messages);
073            }
074          }
075        } catch (XMLStreamException e) {
076          messages.addErrorText("XML is not valid: " + e.getMessage());
077        }
078        return profile;
079      }
080    
081      private SMInputFactory initStax() {
082        XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
083        xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
084        xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
085        xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
086        xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
087        return new SMInputFactory(xmlFactory);
088      }
089    
090      private void processModule(RulesProfile profile, String path, SMInputCursor moduleCursor, ValidationMessages messages)
091          throws XMLStreamException {
092        String moduleName = moduleCursor.getAttrValue("name");
093        if (isFilter(moduleName)) {
094          messages.addWarningText("Checkstyle filters are not imported: " + moduleName);
095    
096        } else if (!isIgnored(moduleName)) {
097          processRule(profile, path, moduleName, moduleCursor, messages);
098        }
099      }
100    
101      static boolean isIgnored(String configKey) {
102        return StringUtils.equals(configKey, "FileContentsHolder");
103      }
104    
105      static boolean isFilter(String configKey) {
106        return StringUtils.equals(configKey, "SuppressionCommentFilter") ||
107            StringUtils.equals(configKey, "SeverityMatchFilter") ||
108            StringUtils.equals(configKey, "SuppressionFilter") ||
109            StringUtils.equals(configKey, "SuppressWithNearbyCommentFilter");
110      }
111    
112      private void processRule(RulesProfile profile, String path, String moduleName, SMInputCursor moduleCursor, ValidationMessages messages)
113          throws XMLStreamException {
114        Map<String, String> properties = processProps(moduleCursor);
115    
116        Rule rule;
117        String id = properties.get("id");
118        String warning;
119        if (StringUtils.isNotBlank(id)) {
120          rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withKey(id));
121          warning = "Checkstyle rule with key '" + id + "' not found";
122    
123        } else {
124          String configKey = path + moduleName;
125          rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withConfigKey(configKey));
126          warning = "Checkstyle rule with config key '" + configKey + "' not found";
127        }
128    
129        if (rule == null) {
130          messages.addWarningText(warning);
131    
132        } else {
133          ActiveRule activeRule = profile.activateRule(rule, null);
134          activateProperties(activeRule, properties);
135        }
136      }
137    
138      private Map<String, String> processProps(SMInputCursor moduleCursor) throws XMLStreamException {
139        Map<String, String> props = Maps.newHashMap();
140        SMInputCursor propertyCursor = moduleCursor.childElementCursor("property");
141        while (propertyCursor.getNext() != null) {
142          String key = propertyCursor.getAttrValue("name");
143          String value = propertyCursor.getAttrValue("value");
144          props.put(key, value);
145        }
146        return props;
147      }
148    
149      private void activateProperties(ActiveRule activeRule, Map<String, String> properties) {
150        for (Map.Entry<String, String> property : properties.entrySet()) {
151          if (StringUtils.equals("severity", property.getKey())) {
152            activeRule.setSeverity(CheckstyleSeverityUtils.fromSeverity(property.getValue()));
153    
154          } else if (!StringUtils.equals("id", property.getKey())) {
155            activeRule.setParameter(property.getKey(), property.getValue());
156          }
157        }
158      }
159    }