Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for using javax.xml #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.mulesoft.services</groupId>
<artifactId>mule-validation-sonarqube-plugin</artifactId>
<version>1.0.6</version>
<version>1.1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<url>http://maven.apache.org</url>
Expand All @@ -27,6 +27,12 @@
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
<!-- <dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency> -->
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api</artifactId>
Expand All @@ -36,6 +42,20 @@
<!-- mandatory scope -->
<scope>provided</scope>
</dependency>
<!-- Try to bring in the fix for https://github.com/SonarSource/sonar-analyzer-commons/issues/127 -->
<!-- <dependency>
<groupId>org.sonarsource.analyzer-commons</groupId>
<artifactId>sonar-xml-parsing</artifactId>
<version>1.13.0.662</version>
<type>pom</type>
</dependency> -->
<!-- For sonar-plugin-api >= 8.0-->
<!-- <groupId>org.sonarsource.sonarqube</groupId>
<dependency>
<artifactId>sonar-plugin-api-impl</artifactId>
<version>${sonar.apiVersion}</version>
<scope>test</scope>
</dependency> -->
<dependency>
<groupId>org.sonarsource.analyzer-commons</groupId>
<artifactId>sonar-xml-parsing</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void define(Context context) {
MuleLanguage.LANGUAGE_KEY);
profile4.setDefault(true);
activeRule(profile4, MuleRulesDefinition.MULE4_REPOSITORY_KEY, "file:extensions/plugins/rules-4.xml");
activeRule(profile4, MuleRulesDefinition.MULE4_REPOSITORY_KEY, "file:extensions/plugins/rules-4-custom.xml");
profile4.done();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package com.mulesoft.services.tools.sonarqube.rule;

import static java.util.Arrays.asList;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import javax.xml.bind.JAXBException;

import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

import com.mulesoft.services.tools.sonarqube.language.MuleLanguage;
import com.mulesoft.services.tools.sonarqube.properties.MuleProperties;
import com.mulesoft.services.tools.validation.Constants;
Expand All @@ -24,6 +17,14 @@
import com.mulesoft.services.tools.validation.rules.Ruleset;
import com.mulesoft.services.tools.validation.rules.Rulestore;

import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

public class MuleRulesDefinition implements RulesDefinition {

private final Logger logger = Loggers.get(MuleRulesDefinition.class);
Expand All @@ -37,6 +38,7 @@ public interface PARAMS {
String SCOPE = "scope";
String XPATH = "xpath-expression";
String XPATH_LOCATION_HINT = "xpath-location-hint";
String PLUGIN_VERSION = "plugin-version";
}

@Override
Expand All @@ -47,32 +49,30 @@ public void define(Context context) {
logger.info("Working Directory = {}", System.getProperty("user.dir"));

createRepository(context, MULE3_REPOSITORY_KEY, MuleLanguage.LANGUAGE_KEY, "Mule3 Analyzer",
"file:extensions/plugins/rules-3.xml");
asList("file:extensions/plugins/rules-3.xml"));
createRepository(context, MULE4_REPOSITORY_KEY, MuleLanguage.LANGUAGE_KEY, "Mule4 Analyzer",
"file:extensions/plugins/rules-4.xml");
asList("file:extensions/plugins/rules-4.xml","file:extensions/plugins/rules-4-custom.xml"));

}

private void createRepository(Context context, String repositoryKey, String language, String repositoryName,
String ruleFilename) {
List<String> ruleFilenames) {
NewRepository repository = context.createRepository(repositoryKey, language).setName(repositoryName);
try {
Rulestore rulestore = RuleFactory.loadRulesFromXml(ruleFilename);
List<Ruleset> rulesetList = rulestore.getRuleset();

for (Iterator<Ruleset> iterator = rulesetList.iterator(); iterator.hasNext();) {
Ruleset ruleset = iterator.next();
logger.debug("Rule Category : " + ruleset.getCategory());
List<com.mulesoft.services.tools.validation.rules.Rule> ruleList = ruleset.getRule();
for (Iterator<com.mulesoft.services.tools.validation.rules.Rule> ruleIterator = ruleList
.iterator(); ruleIterator.hasNext();) {
com.mulesoft.services.tools.validation.rules.Rule rule = ruleIterator.next();
logger.debug("Rule Id : " + rule.getId());
addRule(repository, ruleset, rule, language);
}
for (String ruleFilename: ruleFilenames) {
Rulestore rulestore = RuleFactory.loadRulesFromXml(ruleFilename);
List<Ruleset> rulesets = rulestore.getRuleset();

for (Ruleset ruleset : rulesets) {
logger.debug("Rule Category : " + ruleset.getCategory());
List<com.mulesoft.services.tools.validation.rules.Rule> rules = ruleset.getRule();
for (com.mulesoft.services.tools.validation.rules.Rule rule : rules) {
logger.debug("Rule Id : " + rule.getId());
addRule(repository, ruleset, rule, language);
}

}
}

} catch (JAXBException | IOException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
Expand Down Expand Up @@ -102,7 +102,8 @@ private void addRuleTemplate(NewRepository repository, String language) {
.setType(RuleParamType.STRING);
x1Rule.createParam(PARAMS.SCOPE).setDescription(prop.getProperty("rule.template.parameter.scope"))
.setType(RuleParamType.STRING);

x1Rule.createParam(PARAMS.PLUGIN_VERSION).setDescription(prop.getProperty("rule.template.parameter.pluginversion"))
.setType(RuleParamType.STRING);
logger.info("addRuleTemplate x1Rule="+x1Rule);

}
Expand All @@ -124,7 +125,11 @@ private void addRule(NewRepository repository, Ruleset ruleset,
if (rule.getApplies() != null) {
x1Rule.createParam(PARAMS.SCOPE).setDefaultValue(rule.getApplies()).setType(RuleParamType.STRING);
}

String pluginVersion = rule.getPluginVersion();
if (pluginVersion == null || pluginVersion.isEmpty()) {
pluginVersion = ruleset.getPluginVersion();
}
x1Rule.createParam(PARAMS.PLUGIN_VERSION).setDefaultValue(pluginVersion).setType(RuleParamType.STRING);
}

private String getSeverity(com.mulesoft.services.tools.validation.rules.Rule rule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
import java.util.Map;
import java.util.Set;

import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;

import com.mulesoft.services.tools.sonarqube.rule.MuleRulesDefinition;
import com.mulesoft.services.xpath.XPathProcessor;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
Expand All @@ -19,9 +25,8 @@
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

import com.mulesoft.services.tools.sonarqube.rule.MuleRulesDefinition;
import com.mulesoft.services.xpath.XPathProcessor;
import org.sonarsource.analyzer.commons.xml.XmlFile;
import org.w3c.dom.Node;

public class ApplicationStrategyScope implements ScopeStrategy {
private final Logger logger = Loggers.get(ApplicationStrategyScope.class);
Expand All @@ -31,27 +36,39 @@ public class ApplicationStrategyScope implements ScopeStrategy {
@Override
public void validate(XPathProcessor xpathValidator, Map<RuleKey, List<NewIssue>> issues, SensorContext context,
InputFile t, ActiveRule rule) {
boolean valid;
try {
Document document = saxBuilder.build(t.inputStream());
Element rootElement = document.getRootElement();
String ruleId = rule.ruleKey().toString();
boolean valid = xpathValidator.processXPath(rule.param(MuleRulesDefinition.PARAMS.XPATH).trim(),
rootElement, Boolean.class).booleanValue();
logger.info("Validation Result: " + valid + " : File: " + t.filename() + " :Rule:" + rule.ruleKey()+" internalKey="+rule.internalKey());
if (!valid && !valids.contains(ruleId) && !issues.containsKey(rule.ruleKey())) {
NewIssue newIssue = context.newIssue().forRule(rule.ruleKey());
NewIssueLocation primaryLocation = newIssue.newLocation().on(t);
newIssue.at(primaryLocation);
addIssue(issues, rule, newIssue);
if (rule.param(MuleRulesDefinition.PARAMS.PLUGIN_VERSION).trim().equalsIgnoreCase("1.1")) {
logger.info("Rule v1.1, Application scope, File: " + t.filename() + " Rule:" + rule.ruleKey());
XmlFile xmlFile = XmlFile.create(t);
Node root = xmlFile.getDocument().getFirstChild();

String subjectsXPath = rule.param(MuleRulesDefinition.PARAMS.XPATH).trim();
valid = (Boolean)xpathValidator.processXPathAsNodeSet(subjectsXPath, root, XPathConstants.BOOLEAN);
} else {
if (valid && !valids.contains(ruleId)) {
valids.add(ruleId);
issues.remove(rule.ruleKey());
}
}
logger.info("Rule v1.0: File: " + t.filename() + " Rule:" + rule.ruleKey());
Document document = saxBuilder.build(t.inputStream());
Element rootElement = document.getRootElement();

} catch (JDOMException | IOException e) {
valid = xpathValidator.processXPath(rule.param(MuleRulesDefinition.PARAMS.XPATH).trim(),
rootElement, Boolean.class).booleanValue();
}
} catch (JDOMException | IOException | XPathExpressionException e) {
logger.error(e.getMessage(), e);
return;
}
String ruleId = rule.ruleKey().toString();
logger.info("Validation Result: " + valid + " : File: " + t.filename() + " :Rule:" + rule.ruleKey()+" internalKey="+rule.internalKey());
if (!valid && !valids.contains(ruleId) && !issues.containsKey(rule.ruleKey())) {
NewIssue newIssue = context.newIssue().forRule(rule.ruleKey());
NewIssueLocation primaryLocation = newIssue.newLocation().on(t);
newIssue.at(primaryLocation);
addIssue(issues, rule, newIssue);
} else {
if (valid && !valids.contains(ruleId)) {
valids.add(ruleId);
issues.remove(rule.ruleKey());
}
}
}

Expand Down
Loading