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.checkstyle; 021 022 import com.google.common.collect.Maps; 023 import org.apache.commons.lang.StringUtils; 024 import org.codehaus.stax2.XMLInputFactory2; 025 import org.codehaus.staxmate.SMInputFactory; 026 import org.codehaus.staxmate.in.SMHierarchicCursor; 027 import org.codehaus.staxmate.in.SMInputCursor; 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 037 import javax.xml.stream.XMLInputFactory; 038 import javax.xml.stream.XMLStreamException; 039 040 import java.io.Reader; 041 import java.util.Map; 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) throws XMLStreamException { 091 String moduleName = moduleCursor.getAttrValue("name"); 092 if (isFilter(moduleName)) { 093 messages.addWarningText("Checkstyle filters are not imported: " + moduleName); 094 095 } else if (!isIgnored(moduleName)) { 096 processRule(profile, path, moduleName, moduleCursor, messages); 097 } 098 } 099 100 static boolean isIgnored(String configKey) { 101 return StringUtils.equals(configKey, "FileContentsHolder"); 102 } 103 104 static boolean isFilter(String configKey) { 105 return StringUtils.equals(configKey, "SuppressionCommentFilter") || 106 StringUtils.equals(configKey, "SeverityMatchFilter") || 107 StringUtils.equals(configKey, "SuppressionFilter") || 108 StringUtils.equals(configKey, "SuppressWithNearbyCommentFilter"); 109 } 110 111 private void processRule(RulesProfile profile, String path, String moduleName, SMInputCursor moduleCursor, ValidationMessages messages) throws XMLStreamException { 112 Map<String, String> properties = processProps(moduleCursor); 113 114 Rule rule; 115 String id = properties.get("id"); 116 String warning; 117 if (StringUtils.isNotBlank(id)) { 118 rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withKey(id)); 119 warning = "Checkstyle rule with key '" + id + "' not found"; 120 121 } else { 122 String configKey = path + moduleName; 123 rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(CheckstyleConstants.REPOSITORY_KEY).withConfigKey(configKey)); 124 warning = "Checkstyle rule with config key '" + configKey + "' not found"; 125 } 126 127 if (rule == null) { 128 messages.addWarningText(warning); 129 130 } else { 131 ActiveRule activeRule = profile.activateRule(rule, null); 132 activateProperties(activeRule, properties); 133 } 134 } 135 136 private Map<String, String> processProps(SMInputCursor moduleCursor) throws XMLStreamException { 137 Map<String, String> props = Maps.newHashMap(); 138 SMInputCursor propertyCursor = moduleCursor.childElementCursor("property"); 139 while (propertyCursor.getNext() != null) { 140 String key = propertyCursor.getAttrValue("name"); 141 String value = propertyCursor.getAttrValue("value"); 142 props.put(key, value); 143 } 144 return props; 145 } 146 147 private void activateProperties(ActiveRule activeRule, Map<String, String> properties) { 148 for (Map.Entry<String, String> property : properties.entrySet()) { 149 if (StringUtils.equals("severity", property.getKey())) { 150 activeRule.setSeverity(CheckstyleSeverityUtils.fromSeverity(property.getValue())); 151 152 } else if (!StringUtils.equals("id", property.getKey())) { 153 activeRule.setParameter(property.getKey(), property.getValue()); 154 } 155 } 156 } 157 }