use of org.sonar.api.utils.SonarException in project sonarqube by SonarSource.
the class DefaultHttpDownloaderTest method shouldNotCreateFileIfFailToDownload.
@Test
public void shouldNotCreateFileIfFailToDownload() throws Exception {
File toDir = temporaryFolder.newFolder();
File toFile = new File(toDir, "downloadToFile.txt");
try {
int port = new InetSocketAddress("localhost", 0).getPort();
new DefaultHttpDownloader(new MapSettings()).download(new URI("http://localhost:" + port), toFile);
} catch (SonarException e) {
assertThat(toFile).doesNotExist();
}
}
use of org.sonar.api.utils.SonarException 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.sonar.api.utils.SonarException 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.sonar.api.utils.SonarException 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.sonar.api.utils.SonarException in project sonarqube by SonarSource.
the class AnnotationRuleParser method addRuleProperty.
private static void addRuleProperty(Rule rule, Field field) {
org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
if (propertyAnnotation != null) {
String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
RuleParam param = rule.createParameter(fieldKey);
param.setDescription(propertyAnnotation.description());
param.setDefaultValue(propertyAnnotation.defaultValue());
if (!StringUtils.isBlank(propertyAnnotation.type())) {
try {
param.setType(PropertyType.valueOf(propertyAnnotation.type().trim()).name());
} catch (IllegalArgumentException e) {
throw new SonarException("Invalid property type [" + propertyAnnotation.type() + "]", e);
}
} else {
param.setType(guessType(field.getType()).name());
}
}
}
Aggregations