001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.api.profiles;
021
022 import com.google.common.base.Charsets;
023 import org.apache.commons.io.IOUtils;
024 import org.apache.commons.lang.StringUtils;
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.ServerComponent;
029 import org.sonar.api.rules.ActiveRule;
030 import org.sonar.api.rules.Rule;
031 import org.sonar.api.rules.RuleFinder;
032 import org.sonar.api.rules.RulePriority;
033 import org.sonar.api.utils.ValidationMessages;
034
035 import javax.xml.stream.XMLInputFactory;
036 import javax.xml.stream.XMLStreamException;
037 import java.io.InputStreamReader;
038 import java.io.Reader;
039 import java.util.HashMap;
040 import java.util.Map;
041
042 /**
043 * @since 2.3
044 */
045 public class XMLProfileParser implements ServerComponent {
046
047 private final RuleFinder ruleFinder;
048
049 /**
050 * For backward compatibility.
051 *
052 * @deprecated since 2.5. Plugins shouldn't directly instantiate this class,
053 * because it should be retrieved as an IoC dependency.
054 */
055 @Deprecated
056 public XMLProfileParser(RuleFinder ruleFinder) {
057 this.ruleFinder = ruleFinder;
058 }
059
060 public RulesProfile parseResource(ClassLoader classloader, String xmlClassPath, ValidationMessages messages) {
061 Reader reader = new InputStreamReader(classloader.getResourceAsStream(xmlClassPath), Charsets.UTF_8);
062 try {
063 return parse(reader, messages);
064
065 } finally {
066 IOUtils.closeQuietly(reader);
067 }
068 }
069
070 public RulesProfile parse(Reader reader, ValidationMessages messages) {
071 RulesProfile profile = RulesProfile.create();
072 SMInputFactory inputFactory = initStax();
073 try {
074 SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
075 rootC.advance(); // <profile>
076 SMInputCursor cursor = rootC.childElementCursor();
077 while (cursor.getNext() != null) {
078 String nodeName = cursor.getLocalName();
079 if (StringUtils.equals("rules", nodeName)) {
080 SMInputCursor rulesCursor = cursor.childElementCursor("rule");
081 processRules(rulesCursor, profile, messages);
082
083 } else if (StringUtils.equals("name", nodeName)) {
084 profile.setName(StringUtils.trim(cursor.collectDescendantText(false)));
085
086 } else if (StringUtils.equals("language", nodeName)) {
087 profile.setLanguage(StringUtils.trim(cursor.collectDescendantText(false)));
088 }
089 }
090 } catch (XMLStreamException e) {
091 messages.addErrorText("XML is not valid: " + e.getMessage());
092 }
093 checkProfile(profile, messages);
094 return profile;
095 }
096
097 private void checkProfile(RulesProfile profile, ValidationMessages messages) {
098 if (StringUtils.isBlank(profile.getName())) {
099 messages.addErrorText("The mandatory node <name> is missing.");
100 }
101 if (StringUtils.isBlank(profile.getLanguage())) {
102 messages.addErrorText("The mandatory node <language> is missing.");
103 }
104 }
105
106 private SMInputFactory initStax() {
107 XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
108 xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
109 xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
110 // just so it won't try to load DTD in if there's DOCTYPE
111 xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
112 xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
113 return new SMInputFactory(xmlFactory);
114 }
115
116 private void processRules(SMInputCursor rulesCursor, RulesProfile profile, ValidationMessages messages) throws XMLStreamException {
117 Map<String, String> parameters = new HashMap<String, String>();
118 while (rulesCursor.getNext() != null) {
119 SMInputCursor ruleCursor = rulesCursor.childElementCursor();
120
121 String repositoryKey = null, key = null;
122 RulePriority priority = null;
123 parameters.clear();
124
125 while (ruleCursor.getNext() != null) {
126 String nodeName = ruleCursor.getLocalName();
127
128 if (StringUtils.equals("repositoryKey", nodeName)) {
129 repositoryKey = StringUtils.trim(ruleCursor.collectDescendantText(false));
130
131 } else if (StringUtils.equals("key", nodeName)) {
132 key = StringUtils.trim(ruleCursor.collectDescendantText(false));
133
134 } else if (StringUtils.equals("priority", nodeName)) {
135 priority = RulePriority.valueOf(StringUtils.trim(ruleCursor.collectDescendantText(false)));
136
137 } else if (StringUtils.equals("parameters", nodeName)) {
138 SMInputCursor propsCursor = ruleCursor.childElementCursor("parameter");
139 processParameters(propsCursor, parameters);
140 }
141 }
142
143 Rule rule = ruleFinder.findByKey(repositoryKey, key);
144 if (rule == null) {
145 messages.addWarningText("Rule not found: " + ruleToString(repositoryKey, key));
146
147 } else {
148 ActiveRule activeRule = profile.activateRule(rule, priority);
149 for (Map.Entry<String, String> entry : parameters.entrySet()) {
150 if (rule.getParam(entry.getKey()) == null) {
151 messages.addWarningText("The parameter '" + entry.getKey() + "' does not exist in the rule: " + ruleToString(repositoryKey, key));
152 } else {
153 activeRule.setParameter(entry.getKey(), entry.getValue());
154 }
155 }
156 }
157 }
158 }
159
160 private String ruleToString(String repositoryKey, String key) {
161 return "[repository=" + repositoryKey + ", key=" + key + "]";
162 }
163
164 private void processParameters(SMInputCursor propsCursor, Map<String, String> parameters) throws XMLStreamException {
165 while (propsCursor.getNext() != null) {
166 SMInputCursor propCursor = propsCursor.childElementCursor();
167 String key = null;
168 String value = null;
169 while (propCursor.getNext() != null) {
170 String nodeName = propCursor.getLocalName();
171 if (StringUtils.equals("key", nodeName)) {
172 key = StringUtils.trim(propCursor.collectDescendantText(false));
173
174 } else if (StringUtils.equals("value", nodeName)) {
175 value = StringUtils.trim(propCursor.collectDescendantText(false));
176 }
177 }
178 if (key != null) {
179 parameters.put(key, value);
180 }
181 }
182 }
183
184 }