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