RuleSetWriter { public void write(RuleSet ruleSet) { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.newDocument(); ruleSetFileNames = new HashSet<>(); Element ruleSetElement = createRuleSetElement(ruleSet); document.appendChild(ruleSetElement); TransformerFactory transformerFactory = TransformerFactory.newInstance(); try { transformerFactory.setAttribute("indent-number", 3); } catch (IllegalArgumentException iae) { LOG.log(Level.FINE, "Couldn't set indentation", iae); } Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(new DOMSource(document), new StreamResult(outputStream)); } catch (DOMException | FactoryConfigurationError | ParserConfigurationException | TransformerException e) { throw new RuntimeException(e); } } RuleSetWriter(OutputStream outputStream); void close(); void write(RuleSet ruleSet); static final String RULESET_2_0_0_NS_URI; @Deprecated // To be removed in PMD 7.0.0
    static final String RULESET_NS_URI; }
@Test public void testWrite() throws Exception { RuleSet braces = RulesetsFactoryUtils.defaultFactory().createRuleSet("net/sourceforge/pmd/TestRuleset1.xml"); RuleSet ruleSet = new RuleSetBuilder(new Random().nextLong()) .withName("ruleset") .withDescription("ruleset description") .addRuleSetByReference(braces, true, "MockRule2") .build(); writer.write(ruleSet); String written = out.toString("UTF-8"); Assert.assertTrue(written.contains("<exclude name=\"MockRule2\"")); }
@Test public void testRuleReferenceOverriddenName() throws Exception { RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.defaultFactory(); RuleSet rs = ruleSetFactory.createRuleSet("dummy-basic"); RuleSetReference ruleSetReference = new RuleSetReference("rulesets/dummy/basic.xml"); RuleReference ruleRef = new RuleReference(); ruleRef.setRule(rs.getRuleByName("DummyBasicMockRule")); ruleRef.setRuleSetReference(ruleSetReference); ruleRef.setName("Foo"); RuleSet ruleSet = ruleSetFactory.createSingleRuleRuleSet(ruleRef); writer.write(ruleSet); String written = out.toString("UTF-8"); Assert.assertTrue(written.contains("ref=\"rulesets/dummy/basic.xml/DummyBasicMockRule\"")); }