RulePackStAXParser { RulePack parse(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { SMInputFactory inputFactory = FortifyUtils.newStaxParser(); try { SMHierarchicCursor rootC = inputFactory.rootElementCursor(inputStream); rootC.advance(); SMInputCursor childCursor = rootC.childCursor(); RulePack rulePack = new RulePack(); while (childCursor.getNext() != null) { String nodeName = childCursor.getLocalName(); if ("Name".equals(nodeName)) { rulePack.setName(StringUtils.trim(childCursor.collectDescendantText(false))); } else if ("Language".equals(nodeName)) { rulePack.setLanguage(StringUtils.trim(childCursor.collectDescendantText(false))); } else if ("Rules".equals(nodeName)) { processRules(childCursor, rulePack); } } RulePackStAXParser.LOG.debug(rulePack.name() + " - " + rulePack.language() + " - " + rulePack.getRules().size()); return rulePack; } catch (XMLStreamException e) { throw new IllegalStateException("XML is not valid", e); } }    }
@Test public void test() { Collection<FortifyRule> rules = parse("rulepack/dummy-rulepack.xml"); assertThat(rules.size()).isEqualTo(6); for (FortifyRule rule : rules) { String key = rule.getRuleID(); if ("1".equals(key) && "3.3".equals(rule.getFormatVersion().toString())) { assertRule(rule, "Dummy cat: Dummy subcat", "java", "MAJOR", ""); } else if ("1".equals(key)) { assertRule(rule, "Dummy cat: Dummy subcat", "java", "MINOR", ""); } else if ("2".equals(key)) { assertRule(rule, null, "other", null, ""); } else if ("3".equals(key)) { assertRule( rule, "Dummy cat", "java", "CRITICAL", "<h2>ABSTRACT</h2><p>Dummy abstract</p><h2>EXPLANATION</h2><p>Dummy explanation</p><h2>RECOMMENDATIONS</h2><p>Dummy recommendations</p><h2>TIPS</h2><p><ul><li>Dummy tip 1</li><li>Dummy tip 2</li></ul></p><h2>REFERENCES</h2><p>\\[1\\] Dummy reference . - Dummy author .</p><p>\\[2\\] Dummy reference . - Dummy author .</p>"); } else if ("4".equals(key)) { assertRule(rule, "Dummy cat", null, "BLOCKER", ""); } else if ("5".equals(key)) { assertRule(rule, "Dummy cat", null, "INFO", ""); } else { fail("Rule " + key + " is not expected"); } } }
@Test public void testLanguage() { assertThat(parse("rulepack/dummy2-rulepack.xml").size()).isEqualTo(1); }
@Test public void testOtherLanguage() { assertThat(parse("rulepack/other-rulepack.xml").size()).isEqualTo(0); }