Refine search
List<Rule> rules = Lists.newArrayList(); SMInputFactory inputFactory = initStax(); SMHierarchicCursor rootC = inputFactory.rootElementCursor(backup); rootC.advance(); // <profile> if (!"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));
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; }
private void parseRootNode(SMHierarchicCursor rootCursor, SensorContext context) throws XMLStreamException { String elementName = rootCursor.getLocalName(); if (!OLD_ROOT_ELEMENT.equals(elementName) && !ROOT_ELEMENT.equals(elementName)) { throw new IllegalStateException( "Unknown XML node, expected \"" + ROOT_ELEMENT + "\" but got \"" + elementName + "\" at line " + rootCursor.getCursorLocation().getLineNumber()); } if (OLD_ROOT_ELEMENT.equals(elementName)) { LOG.warn("Using '" + OLD_ROOT_ELEMENT + "' as root element of the report is deprecated. Please change to '" + ROOT_ELEMENT + "'."); } String version = rootCursor.getAttrValue("version"); if (!"1".equals(version)) { throw new IllegalStateException("Unknown report version: " + version + ". This parser only handles version 1."); } parseFiles(rootCursor.childElementCursor(), context); }
private void parseRootNode(SMHierarchicCursor rootCursor, SensorContext context) throws XMLStreamException { checkElementName(rootCursor, "coverage"); String version = rootCursor.getAttrValue("version"); if (!"1".equals(version)) { throw new IllegalStateException("Unknown report version: " + version + ". This parser only handles version 1."); } parseFiles(rootCursor.childElementCursor(), context); }
/** * @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; }
SMHierarchicCursor root = inputFactory.rootElementCursor(new StringReader(duplicationsData)); root.advance(); // <duplications> 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");
/** * Loads rules by reading the XML input stream. The reader is not closed by the method, so it * should be handled by the caller. * @since 4.3 */ public void load(RulesDefinition.NewRepository repo, 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); rootC.advance(); // <rules> SMInputCursor rulesC = rootC.childElementCursor("rule"); while (rulesC.getNext() != null) { // <rule> processRule(repo, rulesC); } } catch (XMLStreamException e) { throw new IllegalStateException("XML is not valid", e); } }
public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { SMInputCursor testSuite = rootCursor.constructDescendantCursor(new ElementFilter("testsuite")); SMEvent testSuiteEvent; for (testSuiteEvent = testSuite.getNext(); testSuiteEvent != null; testSuiteEvent = testSuite.getNext()) { if (testSuiteEvent.compareTo(SMEvent.START_ELEMENT) == 0) { String testSuiteClassName = testSuite.getAttrValue("name"); if (StringUtils.contains(testSuiteClassName, "$")) { // test suites for inner classes are ignored return; } parseTestCase(testSuiteClassName, testSuite.childCursor(new ElementFilter("testcase"))); } } }
public ZapReport parse(InputStream inputStream) { SMInputFactory inputFactory = ZapUtils.newStaxParser(); try { SMHierarchicCursor rootC = inputFactory.rootElementCursor(inputStream); SMInputCursor owaspZapReportCursor = rootC.advance(); // <OWASPZAPReport> String gererated = owaspZapReportCursor.getAttrValue("generated"); String versionZAP = owaspZapReportCursor.getAttrValue("version"); SMInputCursor childCursor = rootC .childCursor(); // Child of <OWASPZAPReport>, here only <site> List<Site> sites = new ArrayList<Site>(); while (childCursor.getNext() != null) { String nodeName = childCursor.getLocalName(); if ("site".equals(nodeName)) { sites.add(processSite(childCursor)); } } return new ZapReport(gererated, versionZAP, sites); } catch (XMLStreamException e) { throw new IllegalStateException("XML is not valid", e); } }
public void parseReport(File xmlFile, SensorContext context, final Map<InputFile, NewCoverage> coverageData) throws XMLStreamException { LOG.info("Parsing report '{}'", xmlFile); unresolvedFilenameCount = 0; StaxParser parser = new StaxParser(rootCursor -> { File defaultBaseDirectory = context.fileSystem().baseDir(); List<File> baseDirectories = Collections.singletonList(defaultBaseDirectory); try { rootCursor.advance(); } catch (com.ctc.wstx.exc.WstxEOFException eofExc) { LOG.debug("Unexpected end of file is encountered", eofExc); throw new EmptyReportException(); } SMInputCursor cursor = rootCursor.childElementCursor(); while (cursor.getNext() != null) { if ("sources".equals(cursor.getLocalName())) { baseDirectories = extractBaseDirectories(cursor, defaultBaseDirectory); } else if ("packages".equals(cursor.getLocalName())) { collectFileMeasures(cursor.descendantElementCursor("class"), context, coverageData, baseDirectories); } } }); parser.parse(xmlFile); if (unresolvedFilenameCount > 1) { LOG.error("Cannot resolve {} file paths, ignoring coverage measures for those files", unresolvedFilenameCount); } }
private static TestSuites processRoot(File file, SMInputFactory inputFactory) throws XMLStreamException { SMHierarchicCursor rootCursor = inputFactory.rootElementCursor(file); rootCursor.advance(); if (!"testsuites".equals(rootCursor.getLocalName())) { throw new XMLStreamException("Report should start with <testsuites>"); } SMInputCursor childCursor = rootCursor.childElementCursor("testsuite"); List<TestSuite> testSuites = new ArrayList<>(); while (childCursor.getNext() != null) { testSuites.add(processTestSuite(childCursor)); } return new TestSuites(testSuites); }
@Override public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { SMInputCursor testSuiteCursor = rootCursor.constructDescendantCursor(new ElementFilter("testsuite")); while (testSuiteCursor.getNext() != null) { String testSuiteClassName = testSuiteCursor.getAttrValue("name"); TestSuite testSuite = new TestSuite(testSuiteClassName); testSuites.add(testSuite); SMInputCursor testCaseCursor = testSuiteCursor.childElementCursor("testcase"); while (testCaseCursor.getNext() != null) { testSuite.addTestCase(parseTestCaseTag(testCaseCursor)); } } }
@Override public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { rootCursor.advance(); SMInputCursor fileCursor = rootCursor.descendantElementCursor("file"); while (fileCursor.getNext() != null) { String fileName = fileCursor.getAttrValue("name"); SMInputCursor violationCursor = fileCursor.descendantElementCursor("violation"); while (violationCursor.getNext() != null) { String beginLine = violationCursor.getAttrValue("beginline"); String ruleKey = violationCursor.getAttrValue("rule"); String message = StringUtils.trim(violationCursor.collectDescendantText()); addIssue(ruleKey, fileName, Integer.parseInt(beginLine), message); } } } });
try { fileInputStream = new FileInputStream(file); SMHierarchicCursor cursor = inputFactory.rootElementCursor(new InputStreamReader(fileInputStream, project.getFileSystem().getSourceCharset())); SMInputCursor mainCursor = cursor.advance().childElementCursor(); while (mainCursor.getNext() != null) { if (NAMESPACES.equals(mainCursor.getQName().getLocalPart())) { parseNamespacesBloc(mainCursor); } else if (TARGETS.equals(mainCursor.getQName().getLocalPart())) { cursor.getStreamReader().closeCompletely(); } catch (XMLStreamException e) { throw new SonarException("Error while reading FxCop result file: " + file.getAbsolutePath(), e);
@Override public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { rootCursor.advance(); SMInputCursor items = rootCursor.descendantElementCursor(); List<String> sourceDirectories = new LinkedList<>(); while (items.getNext() != null) { String localName = items.getLocalName(); if ("Project".equals(localName)) { extractSourceDirectories(items, sourceDirectories); } else if ("Package".equals(localName)) { extractIssues(items, sourceDirectories); } } }
private Collection<String> getAllRuleKeysFromFile(InputStream inputStream) { SMInputFactory inputFactory = ZapUtils.newStaxParser(); try { SMHierarchicCursor rootC = inputFactory.rootElementCursor(inputStream); SMInputCursor rulesCursor = rootC.advance(); return processRules(rulesCursor); } catch (XMLStreamException e) { throw new IllegalStateException("XML is not valid", e); } }
/** * {@inheritDoc} */ @Override public RulesProfile importProfile(Reader reader, ValidationMessages messages) { RulesProfile profile = RulesProfile.create(); profile.setLanguage(languageKey); SMInputFactory inputFactory = initStax(); try { SMHierarchicCursor cursor = inputFactory.rootElementCursor(reader); SMInputCursor rulesetCursor = cursor.advance().childElementCursor(); parseRules(profile, rulesetCursor); cursor.getStreamReader().closeCompletely(); } catch (XMLStreamException e) { messages.addErrorText("Failed to read the profile to import: " + e.getMessage()); } return profile; }
public void stream(final SMHierarchicCursor rootCursor) throws XMLStreamException { final SMInputCursor file = rootCursor.advance().childElementCursor( "file"); while (null != file.getNext()) { collectViolationsFor(file); } }
/** * Gets the coverage. * * @param coverageReportFile the coverage report file * @return the coverage */ private static CoverageNode getCoverage(File coverageReportFile) { SMInputFactory inputFactory = JUnitLogParserForPhpUnit.inputFactory(); try { SMHierarchicCursor rootCursor = inputFactory.rootElementCursor(coverageReportFile); rootCursor.advance(); if (!"coverage".equals(rootCursor.getLocalName())) { throw new XMLStreamException("Report should start with <coverage>"); } return parseCoverageNode(rootCursor); } catch (XMLStreamException e) { throw new IllegalStateException("Can't read phpUnit report: " + coverageReportFile.getName(), e); } }