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