Element eltRule = new Element("rule"); addAttribute(eltRule, "ref", pmdRule.getRef()); addAttribute(eltRule, "class", pmdRule.getClazz()); addAttribute(eltRule, "message", pmdRule.getMessage()); addAttribute(eltRule, "name", pmdRule.getName());
Element eltRule = new Element("rule"); addAttribute(eltRule, "ref", pmdRule.getRef()); addAttribute(eltRule, "class", pmdRule.getClazz()); addAttribute(eltRule, "message", pmdRule.getMessage()); addAttribute(eltRule, "name", pmdRule.getName());
private static void exportPmdRulesetToXml(PmdRuleSet pmdRuleset, Writer writer, String profileName) { Element eltRuleset = new Element("ruleset"); addAttribute(eltRuleset, "name", pmdRuleset.getName()); addChild(eltRuleset, "description", pmdRuleset.getDescription()); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { Element eltRule = new Element("rule"); addAttribute(eltRule, "ref", pmdRule.getRef()); addAttribute(eltRule, "class", pmdRule.getClazz()); addAttribute(eltRule, "message", pmdRule.getMessage()); addAttribute(eltRule, "name", pmdRule.getName()); addAttribute(eltRule, "language", pmdRule.getLanguage()); addChild(eltRule, "priority", String.valueOf(pmdRule.getPriority())); if (pmdRule.hasProperties()) { Element ruleProperties = processRuleProperties(pmdRule); if (ruleProperties.getContentSize() > 0) { eltRule.addContent(ruleProperties); } } eltRuleset.addContent(eltRule); } XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat()); try { serializer.output(new Document(eltRuleset), writer); } catch (IOException e) { throw new IllegalStateException("An exception occurred while generating the PMD configuration file from profile: " + profileName, e); } }
protected RulesProfile createRuleProfile(PmdRuleset pmdRuleset, ValidationMessages messages) { RulesProfile profile = RulesProfile.create(); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { String ruleClassName = pmdRule.getClazz(); if (PmdConstants.XPATH_CLASS.equals(ruleClassName)) { messages.addWarningText("PMD XPath rule '" + pmdRule.getName() + "' can't be imported automatically. The rule must be created manually through the SonarQube web interface."); } else { String ruleRef = pmdRule.getRef(); if (ruleRef == null) { messages.addWarningText("A PMD rule without 'ref' attribute can't be imported. see '" + ruleClassName + "'"); } else { Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PmdConstants.REPOSITORY_KEY).withConfigKey(ruleRef)); if (rule != null) { ActiveRule activeRule = profile.activateRule(rule, PmdLevelUtils.fromLevel(pmdRule.getPriority())); setParameters(activeRule, pmdRule, rule, messages); } else { messages.addWarningText("Unable to import unknown PMD rule '" + ruleRef + "'"); } } } } return profile; }
protected RulesProfile createRuleProfile(PmdRuleset pmdRuleset, ValidationMessages messages) { RulesProfile profile = RulesProfile.create(); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { if (PmdConstants.XPATH_CLASS.equals(pmdRule.getClazz())) { messages.addWarningText("PMD XPath rule '" + pmdRule.getName() + "' can't be imported automatically. The rule must be created manually through the Sonar web interface."); continue; } if (pmdRule.getRef() == null) { messages.addWarningText("A PMD rule without 'ref' attribute can't be imported. see '" + pmdRule.getClazz() + "'"); continue; } Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PmdConstants.REPOSITORY_KEY).withConfigKey(pmdRule.getRef())); if (rule != null) { ActiveRule activeRule = profile.activateRule(rule, PmdLevelUtils.fromLevel(pmdRule.getPriority())); if (pmdRule.getProperties() != null) { for (PmdProperty prop : pmdRule.getProperties()) { if (rule.getParam(prop.getName()) == null) { messages.addWarningText("The property '" + prop.getName() + "' is not supported in the pmd rule: " + pmdRule.getRef()); continue; } activeRule.setParameter(prop.getName(), prop.getValue()); } } } else { messages.addWarningText("Unable to import unknown PMD rule '" + pmdRule.getRef() + "'"); } } return profile; }
@Override public RulesProfile importProfile(Reader pmdConfigurationFile, ValidationMessages messages) { PmdRuleSet pmdRuleset = PmdRuleSets.parse(pmdConfigurationFile, messages); RulesProfile profile = RulesProfile.create(); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { String ruleClassName = pmdRule.getClazz(); if (PmdConstants.XPATH_CLASS.equals(ruleClassName)) { messages.addWarningText("PMD XPath rule '" + pmdRule.getName() + "' can't be imported automatically. The rule must be created manually through the SonarQube web interface."); } else { String ruleRef = pmdRule.getRef(); if (ruleRef == null) { messages.addWarningText("A PMD rule without 'ref' attribute can't be imported. see '" + ruleClassName + "'"); } else { Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PmdConstants.REPOSITORY_KEY).withConfigKey(ruleRef)); if (rule != null) { ActiveRule activeRule = profile.activateRule(rule, PmdLevelUtils.fromLevel(pmdRule.getPriority())); setParameters(activeRule, pmdRule, rule, messages); } else { messages.addWarningText("Unable to import unknown PMD rule '" + ruleRef + "'"); } } } } return profile; } }