use of org.codehaus.staxmate.in.SMInputCursor in project sonarqube by SonarSource.
the class XMLRuleParser method processParameter.
private static void processParameter(Rule rule, SMInputCursor ruleC) throws XMLStreamException {
RuleParam param = rule.createParameter();
String keyAttribute = ruleC.getAttrValue("key");
if (StringUtils.isNotBlank(keyAttribute)) {
/* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
param.setKey(StringUtils.trim(keyAttribute));
}
String typeAttribute = ruleC.getAttrValue("type");
if (StringUtils.isNotBlank(typeAttribute)) {
/* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
param.setType(type(StringUtils.trim(typeAttribute)));
}
SMInputCursor paramC = ruleC.childElementCursor();
while (paramC.getNext() != null) {
String propNodeName = paramC.getLocalName();
String propText = StringUtils.trim(paramC.collectDescendantText(false));
if (StringUtils.equalsIgnoreCase("key", propNodeName)) {
param.setKey(propText);
} else if (StringUtils.equalsIgnoreCase("description", propNodeName)) {
param.setDescription(propText);
} else if (StringUtils.equalsIgnoreCase("type", propNodeName)) {
param.setType(type(propText));
} else if (StringUtils.equalsIgnoreCase("defaultValue", propNodeName)) {
param.setDefaultValue(propText);
}
}
if (Strings.isNullOrEmpty(param.getKey())) {
throw new SonarException("Node <key> is missing in <param>");
}
}
use of org.codehaus.staxmate.in.SMInputCursor in project sonarqube by SonarSource.
the class XMLRuleParser method parse.
public List<Rule> parse(Reader reader) {
XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
// just so it won't try to load DTD in if there's DOCTYPE
xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
try {
SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
// <rules>
rootC.advance();
List<Rule> rules = new ArrayList<>();
SMInputCursor rulesC = rootC.childElementCursor("rule");
while (rulesC.getNext() != null) {
// <rule>
Rule rule = Rule.create();
rules.add(rule);
processRule(rule, rulesC);
}
return rules;
} catch (XMLStreamException e) {
throw new SonarException("XML is not valid", e);
}
}
use of org.codehaus.staxmate.in.SMInputCursor in project sonarqube by SonarSource.
the class RulesDefinitionXmlLoader method processRule.
private static void processRule(RulesDefinition.NewRepository repo, SMInputCursor ruleC) throws XMLStreamException {
String key = null;
String name = null;
String description = null;
// enum is not used as variable type as we want to raise an exception with the rule key when format is not supported
String descriptionFormat = DescriptionFormat.HTML.name();
String internalKey = null;
String severity = Severity.defaultSeverity();
String type = null;
RuleStatus status = RuleStatus.defaultStatus();
boolean template = false;
String gapDescription = null;
String debtRemediationFunction = null;
String debtRemediationFunctionGapMultiplier = null;
String debtRemediationFunctionBaseEffort = null;
List<ParamStruct> params = new ArrayList<>();
List<String> tags = new ArrayList<>();
/* BACKWARD COMPATIBILITY WITH VERY OLD FORMAT */
String keyAttribute = ruleC.getAttrValue("key");
if (isNotBlank(keyAttribute)) {
key = trim(keyAttribute);
}
String priorityAttribute = ruleC.getAttrValue("priority");
if (isNotBlank(priorityAttribute)) {
severity = trim(priorityAttribute);
}
SMInputCursor cursor = ruleC.childElementCursor();
while (cursor.getNext() != null) {
String nodeName = cursor.getLocalName();
if (equalsIgnoreCase("name", nodeName)) {
name = nodeValue(cursor);
} else if (equalsIgnoreCase("type", nodeName)) {
type = nodeValue(cursor);
} else if (equalsIgnoreCase("description", nodeName)) {
description = nodeValue(cursor);
} else if (equalsIgnoreCase("descriptionFormat", nodeName)) {
descriptionFormat = nodeValue(cursor);
} else if (equalsIgnoreCase("key", nodeName)) {
key = nodeValue(cursor);
} else if (equalsIgnoreCase("configKey", nodeName)) {
// deprecated field, replaced by internalKey
internalKey = nodeValue(cursor);
} else if (equalsIgnoreCase("internalKey", nodeName)) {
internalKey = nodeValue(cursor);
} else if (equalsIgnoreCase("priority", nodeName) || equalsIgnoreCase("severity", nodeName)) {
// "priority" is deprecated field and has been replaced by "severity"
severity = nodeValue(cursor);
} else if (equalsIgnoreCase("cardinality", nodeName)) {
template = Cardinality.MULTIPLE == Cardinality.valueOf(nodeValue(cursor));
} else if (equalsIgnoreCase("gapDescription", nodeName) || equalsIgnoreCase("effortToFixDescription", nodeName)) {
gapDescription = nodeValue(cursor);
} else if (equalsIgnoreCase("remediationFunction", nodeName) || equalsIgnoreCase("debtRemediationFunction", nodeName)) {
debtRemediationFunction = nodeValue(cursor);
} else if (equalsIgnoreCase("remediationFunctionBaseEffort", nodeName) || equalsIgnoreCase("debtRemediationFunctionOffset", nodeName)) {
debtRemediationFunctionGapMultiplier = nodeValue(cursor);
} else if (equalsIgnoreCase("remediationFunctionGapMultiplier", nodeName) || equalsIgnoreCase("debtRemediationFunctionCoefficient", nodeName)) {
debtRemediationFunctionBaseEffort = nodeValue(cursor);
} else if (equalsIgnoreCase("status", nodeName)) {
String s = nodeValue(cursor);
if (s != null) {
status = RuleStatus.valueOf(s);
}
} else if (equalsIgnoreCase("param", nodeName)) {
params.add(processParameter(cursor));
} else if (equalsIgnoreCase("tag", nodeName)) {
tags.add(nodeValue(cursor));
}
}
try {
RulesDefinition.NewRule rule = repo.createRule(key).setSeverity(severity).setName(name).setInternalKey(internalKey).setTags(tags.toArray(new String[tags.size()])).setTemplate(template).setStatus(status).setGapDescription(gapDescription);
if (type != null) {
rule.setType(RuleType.valueOf(type));
}
fillDescription(rule, descriptionFormat, description);
fillRemediationFunction(rule, debtRemediationFunction, debtRemediationFunctionGapMultiplier, debtRemediationFunctionBaseEffort);
fillParams(rule, params);
} catch (Exception e) {
throw new IllegalStateException(format("Fail to load the rule with key [%s:%s]", repo.key(), key), e);
}
}
use of org.codehaus.staxmate.in.SMInputCursor in project sonarqube by SonarSource.
the class XMLProfileParser method processRules.
private void processRules(SMInputCursor rulesCursor, RulesProfile profile, ValidationMessages messages) throws XMLStreamException {
Map<String, String> parameters = new HashMap<>();
while (rulesCursor.getNext() != null) {
SMInputCursor ruleCursor = rulesCursor.childElementCursor();
String repositoryKey = null;
String key = null;
RulePriority priority = null;
parameters.clear();
while (ruleCursor.getNext() != null) {
String nodeName = ruleCursor.getLocalName();
if (StringUtils.equals("repositoryKey", nodeName)) {
repositoryKey = StringUtils.trim(ruleCursor.collectDescendantText(false));
} else if (StringUtils.equals("key", nodeName)) {
key = StringUtils.trim(ruleCursor.collectDescendantText(false));
} else if (StringUtils.equals("priority", nodeName)) {
priority = RulePriority.valueOf(StringUtils.trim(ruleCursor.collectDescendantText(false)));
} else if (StringUtils.equals("parameters", nodeName)) {
SMInputCursor propsCursor = ruleCursor.childElementCursor("parameter");
processParameters(propsCursor, parameters);
}
}
Rule rule = ruleFinder.findByKey(repositoryKey, key);
if (rule == null) {
messages.addWarningText("Rule not found: " + ruleToString(repositoryKey, key));
} else {
ActiveRule activeRule = profile.activateRule(rule, priority);
for (Map.Entry<String, String> entry : parameters.entrySet()) {
if (rule.getParam(entry.getKey()) == null) {
messages.addWarningText("The parameter '" + entry.getKey() + "' does not exist in the rule: " + ruleToString(repositoryKey, key));
} else {
activeRule.setParameter(entry.getKey(), entry.getValue());
}
}
}
}
}
use of org.codehaus.staxmate.in.SMInputCursor in project sonarqube by SonarSource.
the class GenericTestExecutionReportParser method parseTestCase.
private void parseTestCase(SMInputCursor cursor, MutableTestPlan testPlan) throws XMLStreamException {
checkElementName(cursor, "testCase");
MutableTestCase testCase = testPlan.addTestCase(mandatoryAttribute(cursor, NAME_ATTR));
TestCase.Status status = TestCase.Status.OK;
testCase.setDurationInMs(longValue(mandatoryAttribute(cursor, DURATION_ATTR), cursor, DURATION_ATTR, 0));
SMInputCursor child = cursor.descendantElementCursor();
if (child.getNext() != null) {
String elementName = child.getLocalName();
if (SKIPPED.equals(elementName)) {
status = TestCase.Status.SKIPPED;
} else if (FAILURE.equals(elementName)) {
status = TestCase.Status.FAILURE;
} else if (ERROR.equals(elementName)) {
status = TestCase.Status.ERROR;
}
if (TestCase.Status.OK != status) {
testCase.setMessage(mandatoryAttribute(child, MESSAGE_ATTR));
testCase.setStackTrace(child.collectDescendantText());
}
}
testCase.setStatus(status);
}
Aggregations