/** * Materialize the current active rule set for the profile * * @param activeRules * @param profileName * @return */ protected PmdRuleset createRuleset(List<ActiveRule> activeRules, String profileName) { PmdRuleset ruleset = new PmdRuleset(profileName); for (ActiveRule activeRule : activeRules) { if (activeRule .getRule() .getPluginName() .equals(PhpCodeSnifferRuleRepository.PHPCS_REPOSITORY_KEY)) { String configKey = activeRule.getRule().getKey(); PmdRule rule = new PmdRule(configKey, mapper.to(activeRule.getPriority())); List<PmdProperty> properties = null; List<ActiveRuleParam> activeRuleParams = activeRule.getActiveRuleParams(); if (activeRuleParams != null && !activeRuleParams.isEmpty()) { properties = new ArrayList<PmdProperty>(); for (ActiveRuleParam activeRuleParam : activeRuleParams) { properties.add( new PmdProperty( activeRuleParam.getRuleParam().getKey(), activeRuleParam.getValue())); } } rule.setProperties(properties); ruleset.addRule(rule); processXPathRule(activeRule.getRuleKey(), rule); } } return ruleset; }
@Test public void testBuildPhpmdRuleset() { Reader reader = new StringReader( TestUtils.getResourceContent("/org/sonar/plugins/php/pmd/simple-ruleset.xml")); PmdRuleset ruleset = importer.parsePmdRuleset(reader, messages); assertThat(ruleset.getPmdRules().size(), is(3)); }
/** * @param pmdRuleset * @return */ protected String exportRulesetToXml(PmdRuleset pmdRuleset) { Element rulesetNode = new Element("ruleset"); addAttribute(rulesetNode, "name", pmdRuleset.getName()); addChild(rulesetNode, "description", pmdRuleset.getDescription()); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { Element ruleNode = new Element("rule"); addAttribute(ruleNode, "ref", pmdRule.getRef()); addAttribute(ruleNode, "class", pmdRule.getClazz()); addAttribute(ruleNode, "message", pmdRule.getMessage()); addAttribute(ruleNode, "name", pmdRule.getName()); String priority = pmdRule.getPriority(); addChild(ruleNode, "priority", priority); addChild(ruleNode, "severity", priority); // Then manager properties (kind of parameters) if (pmdRule.hasProperties()) { Element propertiesNode = new Element("properties"); ruleNode.addContent(propertiesNode); for (PmdProperty property : pmdRule.getProperties()) { Element propertyNode = new Element("property"); propertyNode.setAttribute("name", property.getName()); if (property.isCdataValue()) { Element valueNode = new Element("value"); valueNode.addContent(new CDATA(property.getCdataValue())); propertyNode.addContent(valueNode); } else { propertyNode.setAttribute("value", property.getValue()); } propertiesNode.addContent(propertyNode); } } rulesetNode.addContent(ruleNode); } XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat()); StringWriter xml = new StringWriter(); try { serializer.output(new Document(rulesetNode), xml); } catch (IOException e) { throw new SonarException( "A exception occured while generating the PMD configuration file.", e); } return xml.toString(); }