public static void advanceCursor(SMInputCursor cursor){ try{ cursor.advance(); }catch(XMLStreamException e){ throw new SonarPluginException("Error while trying to move the cursor", e); } }
public static String findNextElementName(SMInputCursor cursor){ try{ return cursor.advance().getLocalName(); }catch(XMLStreamException e){ throw new SonarPluginException("Error while trying to get the next element name", e); } } }
private void collectProjectMeasures(SMInputCursor rootCursor) throws ParseException, XMLStreamException { SMInputCursor projectCursor = rootCursor.descendantElementCursor("project"); SMInputCursor projectChildrenCursor = projectCursor.advance().childElementCursor(); projectChildrenCursor.setFilter(new SimpleFilter(SMEvent.START_ELEMENT)); //Skip the metrics tag. projectChildrenCursor.advance(); collectPackageMeasures(projectChildrenCursor); }
private static void collectFileData(SMInputCursor classCursor, NewCoverage coverage) throws XMLStreamException { SMInputCursor line = classCursor.childElementCursor("lines").advance().childElementCursor("line"); while (line.getNext() != null) { int lineId = Integer.parseInt(line.getAttrValue("number")); coverage.lineHits(lineId, Integer.parseInt(line.getAttrValue("hits"))); String isBranch = line.getAttrValue("branch"); String text = line.getAttrValue("condition-coverage"); if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) { String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/"); coverage.conditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0])); } } } }
private static void collectFileData(SMInputCursor classCursor, NewCoverage coverage) throws XMLStreamException { SMInputCursor line = classCursor.childElementCursor("lines").advance().childElementCursor("line"); while (line.getNext() != null) { int lineId = Integer.parseInt(line.getAttrValue("number")); coverage.lineHits(lineId, Integer.parseInt(line.getAttrValue("hits"))); String isBranch = line.getAttrValue("branch"); String text = line.getAttrValue("condition-coverage"); if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) { String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/"); coverage.conditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0])); } } } }
/** * @return */ public List<PhpCodeSnifferViolation> getViolations(File reportFile) { LOG.debug("Report file for PHP_CodeSniffer is " + reportFile); if (reportFile == null || !reportFile.exists()) { throw new SonarException("The XML report '" + reportFile + "' can't be found"); } String reportPath = reportFile.getAbsolutePath(); LOG.debug("Getting violations form report file"); List<PhpCodeSnifferViolation> violations = new ArrayList<PhpCodeSnifferViolation>(); try {// <checkstyle> SMInputFactory inputFactory = new SMInputFactory(XMLInputFactory.newInstance());// <checkstyle> SMInputCursor rootNodeCursor = inputFactory.rootElementCursor(reportFile).advance(); // <file> SMInputCursor fileNodeCursor = rootNodeCursor.childElementCursor(FILE_NODE_NAME).advance(); while (fileNodeCursor.asEvent() != null) { String fileName = fileNodeCursor.getAttrValue(FILE_NAME_ATTRIBUTE_NAME); SMInputCursor violationNodeCursor = fileNodeCursor.childElementCursor().advance(); // <error> while (violationNodeCursor.asEvent() != null) { violations.add(getViolation(fileName, violationNodeCursor)); violationNodeCursor.advance(); } fileNodeCursor.advance(); } rootNodeCursor.getStreamReader().closeCompletely(); } catch (XMLStreamException e) { throw new XmlParserException("Unable to parse the XML Report '" + reportPath + "'", e); } return violations; }
private static void collectFileData(SMInputCursor clazz, NewCoverage newCoverage) throws XMLStreamException { SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line"); while (line.getNext() != null) { int lineId = Integer.parseInt(line.getAttrValue("number")); try { newCoverage.lineHits(lineId, (int) ParsingUtils.parseNumber(line.getAttrValue("hits"), Locale.ENGLISH)); } catch (ParseException e) { throw new IllegalStateException(e); } String isBranch = line.getAttrValue("branch"); String text = line.getAttrValue("condition-coverage"); if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) { String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/"); newCoverage.conditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0])); } } newCoverage.save(); }
private void collectFileData(final SMInputCursor clazz) throws XMLStreamException { final CoverageMeasuresBuilder builder = builderFor(clazz); final SMInputCursor line = clazz.childElementCursor("lines").advance() .childElementCursor("line"); while (null != line.getNext()) { recordCoverageFor(line, builder); } }
private void collectFileMeasures(SMInputCursor classCursor, SensorContext context, Map<InputFile, NewCoverage> coverageData, List<File> baseDirectories) throws XMLStreamException { while (classCursor.getNext() != null) { String filename = FilenameUtils.normalize(classCursor.getAttrValue("filename")); InputFile inputFile = resolve(context, baseDirectories, filename); if (inputFile != null) { NewCoverage coverage = coverageData.computeIfAbsent(inputFile, f -> context.newCoverage().onFile(f)); collectFileData(classCursor, coverage); } else { classCursor.advance(); } } }
private void collectPackageMeasures(SMInputCursor packCursor) throws ParseException, XMLStreamException { while (packCursor.getNext() != null) { SMInputCursor packChildrenCursor = packCursor.descendantElementCursor(); packChildrenCursor.setFilter(new SimpleFilter(SMEvent.START_ELEMENT)); //Skip the metrics tag. packChildrenCursor.advance(); collectFileMeasures(packChildrenCursor); } }
SMInputFactory inf = new SMInputFactory(XMLInputFactory.newInstance()); SMInputCursor cursor = inf.rootElementCursor(findbugsXmlReport).advance(); SMInputCursor bugInstanceCursor = cursor.childElementCursor("BugInstance").advance(); while (bugInstanceCursor.asEvent() != null) { XmlBugInstance xmlBugInstance = new XmlBugInstance(); SMInputCursor bugInstanceChildCursor = bugInstanceCursor.childElementCursor().advance(); while (bugInstanceChildCursor.asEvent() != null) { String nodeName = bugInstanceChildCursor.getLocalName(); lines.add(xmlSourceLineAnnotation); bugInstanceChildCursor.advance(); bugInstanceCursor.advance();
private void collectFileMeasures(SMInputCursor classCursor, SensorContext context, Map<InputFile, NewCoverage> coverageData, List<File> baseDirectories) throws XMLStreamException { while (classCursor.getNext() != null) { String filename = FilenameUtils.normalize(classCursor.getAttrValue("filename")); InputFile inputFile = resolve(context, baseDirectories, filename); if (inputFile != null) { NewCoverage coverage = coverageData.computeIfAbsent(inputFile, f -> context.newCoverage().onFile(f)); collectFileData(classCursor, coverage); } else { classCursor.advance(); } } }
SMInputCursor fileNodeCursor = rootNodeCursor.childElementCursor(FILE_NODE_NAME).advance(); while (fileNodeCursor.asEvent() != null) { String fileName = fileNodeCursor.getAttrValue(FILE_NAME_ATTRIBUTE_NAME); SMInputCursor violationNodeCursor = fileNodeCursor.childElementCursor().advance(); while (violationNodeCursor.asEvent() != null) { PhpmdViolation violation = new PhpmdViolation(); violation.setLongMessage(violationNodeCursor.getElemStringValue()); violations.add(violation); violationNodeCursor.advance(); fileNodeCursor.advance();
private static void collectFileMeasures(SensorContext context, SMInputCursor clazz) throws XMLStreamException { FileSystem fileSystem = context.fileSystem(); FilePredicates predicates = fileSystem.predicates(); Map<String, InputFile> inputFileByFilename = new HashMap<>(); while (clazz.getNext() != null) { String fileName = clazz.getAttrValue("filename"); InputFile inputFile; // mxml files are not supported by the plugin if (inputFileByFilename.containsKey(fileName)) { inputFile = inputFileByFilename.get(fileName); } else { inputFile = findInputFile(fileSystem, predicates, fileName); inputFileByFilename.put(fileName, inputFile); if (inputFile == null && !fileName.endsWith(".mxml")) { LOG.warn("Cannot save coverage result for file: {}, because resource not found.", fileName); } } if (inputFile != null) { collectFileData( clazz, context.newCoverage() .onFile(inputFile)); } else { SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line"); while (line.getNext() != null) { // advance } } } }
private static void collectFileData(SMInputCursor clazz, ParsingResult parsingResult) throws XMLStreamException { SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line"); while (line.getNext() != null) { int lineId = Integer.parseInt(line.getAttrValue("number")); boolean validLine = parsingResult.isValidLine(lineId); if (!validLine && parsingResult.fileExists()) { LOG.info("Hit on invalid line for file " + parsingResult.filename + " (line: " + lineId + "/" + parsingResult.inputFile.lines() + ")"); } try { int hits = (int) parseNumber(line.getAttrValue("hits"), ENGLISH); if (validLine) { parsingResult.coverage = parsingResult.coverage.lineHits(lineId, hits); } } catch (ParseException e) { throw MessageException.of("Unable to parse Cobertura report.", e); } String isBranch = line.getAttrValue("branch"); String text = line.getAttrValue("condition-coverage"); if (validLine && StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) { String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/"); parsingResult.coverage = parsingResult.coverage.conditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0])); } } }
private void parseModuleMessagesBloc(SMInputCursor cursor) throws XMLStreamException { // Cursor on <Module> SMInputCursor moduleChildrenCursor = cursor.childElementCursor(); while (moduleChildrenCursor.getNext() != null) { if (MESSAGES.equals(moduleChildrenCursor.getQName().getLocalPart())) { // We are on <Messages>, look for <Message> SMInputCursor messagesCursor = moduleChildrenCursor.childElementCursor(MESSAGE); while (messagesCursor.getNext() != null) { createViolationFromMessageAtProjectLevel(messagesCursor); } } else if (NAMESPACES.equals(moduleChildrenCursor.getQName().getLocalPart())) { // We are on <Namespaces>, get <Namespace> SMInputCursor namespaceCursor = moduleChildrenCursor.childElementCursor(); while (namespaceCursor.getNext() != null) { String namespaceName = namespaceCursor.getAttrValue(NAME); SMInputCursor typeCursor = namespaceCursor.childElementCursor().advance().childElementCursor(); while (typeCursor.getNext() != null) { parseTypeBloc(namespaceName, typeCursor); } } } } }