@Override
protected void processReport(final Project project, final SensorContext context, File report)
throws javax.xml.stream.XMLStreamException
{
StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() {
/**
* {@inheritDoc}
*/
public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
try{
rootCursor.advance();
}
catch(com.ctc.wstx.exc.WstxEOFException eofExc){
throw new EmptyReportException();
}
SMInputCursor errorCursor = rootCursor.childElementCursor("issue");
try {
while (errorCursor.getNext() != null){
String file = errorCursor.getAttrValue("file");
String line = errorCursor.getAttrValue("line");
String id = errorCursor.getAttrValue("number");
String msg = errorCursor.getAttrValue("desc");
if (isInputValid(file, line, id, msg)) {
//remap MISRA IDs. Only Unique rules for MISRA 2004 and 2008 has been created in the rule repository
if(msg.contains("MISRA 2004") || msg.contains("MISRA 2008")) {
id = mapMisraRulesToUniqueSonarRules(msg);
}
saveUniqueViolation(project, context, CxxPCLintRuleRepository.KEY,
file, line, id, msg);
} else {
CxxUtils.LOG.warn("PC-lint warning ignored: {}", msg);
CxxUtils.LOG.debug("File: " + file + ", Line: " + line + ", ID: "
+ id + ", msg: " + msg);
}
}
} catch (com.ctc.wstx.exc.WstxUnexpectedCharException e) {
CxxUtils.LOG.error("Ignore XML error from PC-lint '{}'", e.toString());
}
}
private boolean isInputValid(String file, String line, String id, String msg) {
if (StringUtils.isEmpty(file) || (Integer.valueOf(line)==0)) {
// issue for project or file level
return !StringUtils.isEmpty(id) && !StringUtils.isEmpty(msg);
}
return !StringUtils.isEmpty(file) && !StringUtils.isEmpty(id) && !StringUtils.isEmpty(msg);
}
/**
Concatenate M with the MISRA rule
number to get the new rule id to save the violation to.
**/
private String mapMisraRulesToUniqueSonarRules(String msg){
Pattern pattern = Pattern.compile("Rule\\x20(\\d{1,2}.\\d{1,2}|\\d{1,2}-\\d{1,2}-\\d{1,2}),");
Matcher matcher = pattern.matcher(msg);
matcher.find();
String misraRule = matcher.group(1);
String newKey = "M" + misraRule;
String debugText = "Remap MISRA rule " + misraRule + " to key " + newKey;
CxxUtils.LOG.debug(debugText);
return newKey;
}
});
parser.parse(report);
}