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 * TODO should be an interface
044 *
045 * @since 2.3
046 */
047 public class XMLProfileParser implements ServerComponent {
048
049 private final RuleFinder ruleFinder;
050
051 /**
052 * For backward compatibility.
053 *
054 * @deprecated since 2.5. Plugins shouldn't directly instantiate this class,
055 * because it should be retrieved as an IoC dependency.
056 */
057 @Deprecated
058 public XMLProfileParser(RuleFinder ruleFinder) {
059 this.ruleFinder = ruleFinder;
060 }
061
062 public RulesProfile parseResource(ClassLoader classloader, String xmlClassPath, ValidationMessages messages) {
063 Reader reader = new InputStreamReader(classloader.getResourceAsStream(xmlClassPath), Charsets.UTF_8);
064 try {
065 return parse(reader, messages);
066
067 } finally {
068 IOUtils.closeQuietly(reader);
069 }
070 }
071
072 public RulesProfile parse(Reader reader, ValidationMessages messages) {
073 RulesProfile profile = RulesProfile.create();
074 SMInputFactory inputFactory = initStax();
075 try {
076 SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
077 rootC.advance(); // <profile>
078 SMInputCursor cursor = rootC.childElementCursor();
079 while (cursor.getNext() != null) {
080 String nodeName = cursor.getLocalName();
081 if (StringUtils.equals("rules", nodeName)) {
082 SMInputCursor rulesCursor = cursor.childElementCursor("rule");
083 processRules(rulesCursor, profile, messages);
084
085 } else if (StringUtils.equals("name", nodeName)) {
086 profile.setName(StringUtils.trim(cursor.collectDescendantText(false)));
087
088 } else if (StringUtils.equals("language", nodeName)) {
089 profile.setLanguage(StringUtils.trim(cursor.collectDescendantText(false)));
090 }
091 }
092 } catch (XMLStreamException e) {
093 messages.addErrorText("XML is not valid: " + e.getMessage());
094 }
095 checkProfile(profile, messages);
096 return profile;
097 }
098
099 private void checkProfile(RulesProfile profile, ValidationMessages messages) {
100 if (StringUtils.isBlank(profile.getName())) {
101 messages.addErrorText("The mandatory node <name> is missing.");
102 }
103 if (StringUtils.isBlank(profile.getLanguage())) {
104 messages.addErrorText("The mandatory node <language> is missing.");
105 }
106 }
107
108 private SMInputFactory initStax() {
109 XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
110 xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
111 xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
112 // just so it won't try to load DTD in if there's DOCTYPE
113 xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
114 xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
115 return new SMInputFactory(xmlFactory);
116 }
117
118 private void processRules(SMInputCursor rulesCursor, RulesProfile profile, ValidationMessages messages) throws XMLStreamException {
119 Map<String, String> parameters = new HashMap<String, String>();
120 while (rulesCursor.getNext() != null) {
121 SMInputCursor ruleCursor = rulesCursor.childElementCursor();
122
123 String repositoryKey = null, key = null;
124 RulePriority priority = null;
125 parameters.clear();
126
127 while (ruleCursor.getNext() != null) {
128 String nodeName = ruleCursor.getLocalName();
129
130 if (StringUtils.equals("repositoryKey", nodeName)) {
131 repositoryKey = StringUtils.trim(ruleCursor.collectDescendantText(false));
132
133 } else if (StringUtils.equals("key", nodeName)) {
134 key = StringUtils.trim(ruleCursor.collectDescendantText(false));
135
136 } else if (StringUtils.equals("priority", nodeName)) {
137 priority = RulePriority.valueOf(StringUtils.trim(ruleCursor.collectDescendantText(false)));
138
139 } else if (StringUtils.equals("parameters", nodeName)) {
140 SMInputCursor propsCursor = ruleCursor.childElementCursor("parameter");
141 processParameters(propsCursor, parameters);
142 }
143 }
144
145 Rule rule = ruleFinder.findByKey(repositoryKey, key);
146 if (rule == null) {
147 messages.addWarningText("Rule not found: " + ruleToString(repositoryKey, key));
148
149 } else {
150 ActiveRule activeRule = profile.activateRule(rule, priority);
151 for (Map.Entry<String, String> entry : parameters.entrySet()) {
152 if (rule.getParam(entry.getKey()) == null) {
153 messages.addWarningText("The parameter '" + entry.getKey() + "' does not exist in the rule: " + ruleToString(repositoryKey, key));
154 } else {
155 activeRule.setParameter(entry.getKey(), entry.getValue());
156 }
157 }
158 }
159 }
160 }
161
162 private String ruleToString(String repositoryKey, String key) {
163 return "[repository=" + repositoryKey + ", key=" + key + "]";
164 }
165
166 private void processParameters(SMInputCursor propsCursor, Map<String, String> parameters) throws XMLStreamException {
167 while (propsCursor.getNext() != null) {
168 SMInputCursor propCursor = propsCursor.childElementCursor();
169 String key = null;
170 String value = null;
171 while (propCursor.getNext() != null) {
172 String nodeName = propCursor.getLocalName();
173 if (StringUtils.equals("key", nodeName)) {
174 key = StringUtils.trim(propCursor.collectDescendantText(false));
175
176 } else if (StringUtils.equals("value", nodeName)) {
177 value = StringUtils.trim(propCursor.collectDescendantText(false));
178 }
179 }
180 if (key != null) {
181 parameters.put(key, value);
182 }
183 }
184 }
185
186 }