use of org.codehaus.staxmate.in.SMInputCursor in project sonarqube by SonarSource.
the class DebtRulesXMLImporter method processProperty.
private static Property processProperty(ValidationMessages validationMessages, SMInputCursor cursor) throws XMLStreamException {
SMInputCursor c = cursor.childElementCursor();
String key = null;
int value = 0;
String textValue = null;
while (c.getNext() != null) {
String node = c.getLocalName();
if (StringUtils.equals(node, PROPERTY_KEY)) {
key = c.collectDescendantText().trim();
} else if (StringUtils.equals(node, PROPERTY_VALUE)) {
String s = c.collectDescendantText().trim();
try {
Double valueDouble = NumberUtils.createDouble(s);
value = valueDouble.intValue();
} catch (NumberFormatException ex) {
validationMessages.addErrorText(String.format("Cannot import value '%s' for field %s - Expected a numeric value instead", s, key));
}
} else if (StringUtils.equals(node, PROPERTY_TEXT_VALUE)) {
textValue = c.collectDescendantText().trim();
textValue = "mn".equals(textValue) ? MINUTE : textValue;
}
}
return new Property(key, value, textValue);
}
use of org.codehaus.staxmate.in.SMInputCursor in project sonarqube by SonarSource.
the class XMLRuleParser method processRule.
private static void processRule(Rule rule, SMInputCursor ruleC) throws XMLStreamException {
/* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
String keyAttribute = ruleC.getAttrValue("key");
if (StringUtils.isNotBlank(keyAttribute)) {
rule.setKey(StringUtils.trim(keyAttribute));
}
/* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
String priorityAttribute = ruleC.getAttrValue("priority");
if (StringUtils.isNotBlank(priorityAttribute)) {
rule.setSeverity(RulePriority.valueOf(StringUtils.trim(priorityAttribute)));
}
List<String> tags = Lists.newArrayList();
SMInputCursor cursor = ruleC.childElementCursor();
while (cursor.getNext() != null) {
String nodeName = cursor.getLocalName();
if (StringUtils.equalsIgnoreCase("name", nodeName)) {
rule.setName(StringUtils.trim(cursor.collectDescendantText(false)));
} else if (StringUtils.equalsIgnoreCase("description", nodeName)) {
rule.setDescription(StringUtils.trim(cursor.collectDescendantText(false)));
} else if (StringUtils.equalsIgnoreCase("key", nodeName)) {
rule.setKey(StringUtils.trim(cursor.collectDescendantText(false)));
} else if (StringUtils.equalsIgnoreCase("configKey", nodeName)) {
rule.setConfigKey(StringUtils.trim(cursor.collectDescendantText(false)));
} else if (StringUtils.equalsIgnoreCase("priority", nodeName)) {
rule.setSeverity(RulePriority.valueOf(StringUtils.trim(cursor.collectDescendantText(false))));
} else if (StringUtils.equalsIgnoreCase("cardinality", nodeName)) {
rule.setCardinality(Cardinality.valueOf(StringUtils.trim(cursor.collectDescendantText(false))));
} else if (StringUtils.equalsIgnoreCase("status", nodeName)) {
rule.setStatus(StringUtils.trim(cursor.collectDescendantText(false)));
} else if (StringUtils.equalsIgnoreCase("param", nodeName)) {
processParameter(rule, cursor);
} else if (StringUtils.equalsIgnoreCase("tag", nodeName)) {
tags.add(StringUtils.trim(cursor.collectDescendantText(false)));
}
}
if (Strings.isNullOrEmpty(rule.getKey())) {
throw new SonarException("Node <key> is missing in <rule>");
}
rule.setTags(tags.toArray(new String[tags.size()]));
}
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);
}
}
Aggregations