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.api.rules; 021 022 import com.thoughtworks.xstream.XStream; 023 import org.apache.commons.lang.StringUtils; 024 import org.sonar.api.profiles.RulesProfile; 025 import org.sonar.api.rules.xml.Profile; 026 import org.sonar.api.rules.xml.Property; 027 import org.sonar.api.utils.SonarException; 028 029 import java.util.ArrayList; 030 import java.util.List; 031 032 @Deprecated 033 public class StandardProfileXmlParser { 034 035 private final List<Rule> rules; 036 037 public StandardProfileXmlParser() { 038 rules = new ArrayList<Rule>(); 039 } 040 041 public StandardProfileXmlParser(List<Rule> rules) { 042 this.rules = rules; 043 } 044 045 /** 046 * see the XML format into the unit test src/test/java/.../StandardProfileXmlParserTest 047 */ 048 public Profile parse(String xml) { 049 return (Profile) getXStream().fromXML(xml); 050 } 051 052 private XStream getXStream() { 053 XStream xstream = new XStream(); 054 xstream.processAnnotations(Profile.class); 055 xstream.processAnnotations(Rule.class); 056 xstream.processAnnotations(Property.class); 057 return xstream; 058 } 059 060 public RulesProfile importConfiguration(String configuration) { 061 RulesProfile rulesProfile = new RulesProfile(); 062 List<ActiveRule> activeRules = new ArrayList<ActiveRule>(); 063 Profile profile = buildProfileFromXml(configuration); 064 065 rulesProfile.setName(profile.getName()); 066 rulesProfile.setLanguage(profile.getLanguage()); 067 068 if (StringUtils.isBlank(rulesProfile.getName())) { 069 throw new SonarException("Profile name can't be null or empty"); 070 } 071 072 buildActiveRulesFromProfile(profile, activeRules); 073 rulesProfile.setActiveRules(activeRules); 074 return rulesProfile; 075 } 076 077 protected Profile buildProfileFromXml(String configuration) { 078 StandardProfileXmlParser xstream = new StandardProfileXmlParser(); 079 return xstream.parse(configuration); 080 } 081 082 protected void buildActiveRulesFromProfile(Profile profile, List<ActiveRule> activeRules) { 083 if (profile.getRules() != null && !profile.getRules().isEmpty()) { 084 for (org.sonar.api.rules.xml.Rule module : profile.getRules()) { 085 String ref = module.getKey(); 086 for (Rule rule : rules) { 087 if (rule.getConfigKey().equals(ref)) { 088 RulePriority rulePriority = getRulePriority(module); 089 ActiveRule activeRule = new ActiveRule(null, rule, rulePriority); 090 activeRule.setActiveRuleParams(getActiveRuleParams(module, rule, activeRule)); 091 activeRules.add(activeRule); 092 break; 093 } 094 } 095 } 096 } 097 } 098 099 private RulePriority getRulePriority(org.sonar.api.rules.xml.Rule module) { 100 return StringUtils.isBlank(module.getPriority()) ? null : RulePriority.valueOfString(module.getPriority()); 101 } 102 103 private List<ActiveRuleParam> getActiveRuleParams(org.sonar.api.rules.xml.Rule module, Rule rule, ActiveRule activeRule) { 104 List<ActiveRuleParam> activeRuleParams = new ArrayList<ActiveRuleParam>(); 105 if (module.getProperties() != null) { 106 for (Property property : module.getProperties()) { 107 if (rule.getParams() != null) { 108 for (RuleParam ruleParam : rule.getParams()) { 109 if (ruleParam.getKey().equals(property.getName())) { 110 activeRuleParams.add(new ActiveRuleParam(activeRule, ruleParam, property.getValue())); 111 } 112 } 113 } 114 } 115 } 116 return activeRuleParams; 117 } 118 119 }