use of org.codehaus.staxmate.in.SMHierarchicCursor in project sonarqube by SonarSource.
the class DebtRulesXMLImporter method importXML.
public List<RuleDebt> importXML(Reader xml, ValidationMessages validationMessages) {
List<RuleDebt> ruleDebts = newArrayList();
try {
SMInputFactory inputFactory = initStax();
SMHierarchicCursor cursor = inputFactory.rootElementCursor(xml);
// advance to <sqale>
cursor.advance();
SMInputCursor rootCursor = cursor.childElementCursor(CHARACTERISTIC);
while (rootCursor.getNext() != null) {
process(ruleDebts, validationMessages, rootCursor);
}
cursor.getStreamReader().closeCompletely();
} catch (XMLStreamException e) {
throw new IllegalStateException("XML is not valid", e);
}
return ruleDebts;
}
use of org.codehaus.staxmate.in.SMHierarchicCursor in project sonarqube by SonarSource.
the class DuplicationsParser method parse.
public List<Block> parse(DbSession session, ComponentDto component, @Nullable String branch, @Nullable String pullRequest, @Nullable String duplicationsData) {
Map<String, ComponentDto> componentsByKey = new LinkedHashMap<>();
List<Block> blocks = new ArrayList<>();
if (duplicationsData == null) {
return blocks;
}
DuplicationComparator duplicationComparator = new DuplicationComparator(component.uuid(), component.projectUuid());
try {
SMInputFactory inputFactory = initStax();
SMHierarchicCursor root = inputFactory.rootElementCursor(new StringReader(duplicationsData));
// <duplications>
root.advance();
SMInputCursor cursor = root.childElementCursor("g");
while (cursor.getNext() != null) {
List<Duplication> duplications = new ArrayList<>();
SMInputCursor bCursor = cursor.childElementCursor("b");
while (bCursor.getNext() != null) {
String from = bCursor.getAttrValue("s");
String size = bCursor.getAttrValue("l");
boolean disableLink = Boolean.parseBoolean(bCursor.getAttrValue("t"));
String componentDbKey = bCursor.getAttrValue("r");
if (from != null && size != null && componentDbKey != null) {
if (disableLink) {
// flag means that the target refers to an unchanged file in PRs that doesn't exist in DB.
// Display as text without a link or other details.
duplications.add(Duplication.newTextComponent(componentDbKey, Integer.valueOf(from), Integer.valueOf(size)));
} else {
duplications.add(createDuplication(componentsByKey, branch, pullRequest, from, size, componentDbKey, session));
}
}
}
duplications.sort(duplicationComparator);
blocks.add(new Block(duplications));
}
blocks.sort(BLOCK_COMPARATOR);
return blocks;
} catch (XMLStreamException e) {
throw new IllegalStateException("XML is not valid", e);
}
}
use of org.codehaus.staxmate.in.SMHierarchicCursor in project sonarqube by SonarSource.
the class QProfileBackuper method restore.
/**
* @param reader the XML backup
* @param toProfileName the target profile. If <code>null</code>, then use the
* lang and name declared in the backup
*/
public BulkChangeResult restore(Reader reader, @Nullable QProfileName toProfileName) {
try {
String profileLang = null;
String profileName = null;
List<RuleActivation> ruleActivations = Lists.newArrayList();
SMInputFactory inputFactory = initStax();
SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
// <profile>
rootC.advance();
if (!rootC.getLocalName().equals("profile")) {
throw new IllegalArgumentException("Backup XML is not valid. Root element must be <profile>.");
}
SMInputCursor cursor = rootC.childElementCursor();
while (cursor.getNext() != null) {
String nodeName = cursor.getLocalName();
if (StringUtils.equals("name", nodeName)) {
profileName = StringUtils.trim(cursor.collectDescendantText(false));
} else if (StringUtils.equals("language", nodeName)) {
profileLang = StringUtils.trim(cursor.collectDescendantText(false));
} else if (StringUtils.equals("rules", nodeName)) {
SMInputCursor rulesCursor = cursor.childElementCursor("rule");
ruleActivations = parseRuleActivations(rulesCursor);
}
}
QProfileName target = (QProfileName) ObjectUtils.defaultIfNull(toProfileName, new QProfileName(profileLang, profileName));
return reset.reset(target, ruleActivations);
} catch (XMLStreamException e) {
throw new IllegalStateException("Fail to restore Quality profile backup", e);
}
}
use of org.codehaus.staxmate.in.SMHierarchicCursor 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.SMHierarchicCursor in project sonarqube by SonarSource.
the class QProfileParser method readXml.
public ImportedQProfile readXml(Reader reader) {
List<ImportedRule> rules = new ArrayList<>();
String profileName = null;
String profileLang = null;
try {
SMInputFactory inputFactory = initStax();
SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
// <profile>
rootC.advance();
if (!ATTRIBUTE_PROFILE.equals(rootC.getLocalName())) {
throw new IllegalArgumentException("Backup XML is not valid. Root element must be <profile>.");
}
SMInputCursor cursor = rootC.childElementCursor();
while (cursor.getNext() != null) {
String nodeName = cursor.getLocalName();
if (StringUtils.equals(ATTRIBUTE_NAME, nodeName)) {
profileName = StringUtils.trim(cursor.collectDescendantText(false));
} else if (StringUtils.equals(ATTRIBUTE_LANGUAGE, nodeName)) {
profileLang = StringUtils.trim(cursor.collectDescendantText(false));
} else if (StringUtils.equals(ATTRIBUTE_RULES, nodeName)) {
SMInputCursor rulesCursor = cursor.childElementCursor("rule");
rules = parseRuleActivations(rulesCursor);
}
}
} catch (XMLStreamException e) {
throw new IllegalArgumentException("Fail to restore Quality profile backup, XML document is not well formed", e);
}
return new ImportedQProfile(profileName, profileLang, rules);
}
Aggregations