public static void load(NewRepository repository, String xmlResourcePath) { new SqaleXmlLoader(repository).loadXmlResource(xmlResourcePath); }
static void extractRulesData(NewRepository repository, String xmlRulesFilePath, String htmlDescriptionFolder) { RulesDefinitionXmlLoader ruleLoader = new RulesDefinitionXmlLoader(); ruleLoader.load(repository, PmdRulesDefinition.class.getResourceAsStream(xmlRulesFilePath), "UTF-8"); ExternalDescriptionLoader.loadHtmlDescriptions(repository, htmlDescriptionFolder); PropertyFileLoader.loadNames(repository, PmdRulesDefinition.class.getResourceAsStream("/org/sonar/l10n/pmd.properties")); SqaleXmlLoader.load(repository, "/com/sonar/sqale/pmd-model.xml"); } }
public static void loadHtmlDescriptions(NewRepository repository, String languageKey) { ExternalDescriptionLoader loader = new ExternalDescriptionLoader(repository, languageKey); for (NewRule newRule : repository.rules()) { loader.addHtmlDescription(newRule); } }
@Override public void define(Context context) { NewRepository repository = context.createRepository(REPOSITORY_KEY, ObjectiveC.KEY).setName(REPOSITORY_NAME); try(BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(RULES_FILE), Charset.forName("UTF-8")))){ List<String> lines = reader.lines().collect(Collectors.toList()); addRules(repository,lines); } catch (IOException e) { LOGGER.error("Failed to load OCLint rules", e); } SqaleXmlLoader.load(repository, "/com/sonar/sqale/oclint-model.xml"); repository.done(); }
@VisibleForTesting static void extractRulesData(NewRepository repository, String xmlRulesFilePath, String htmlDescriptionFolder) throws IOException { final RulesDefinitionXmlLoader ruleLoader = new RulesDefinitionXmlLoader(); try (InputStream resource = CheckstyleRulesDefinition.class .getResourceAsStream(xmlRulesFilePath)) { ruleLoader.load(repository, resource, "UTF-8"); } ExternalDescriptionLoader.loadHtmlDescriptions(repository, htmlDescriptionFolder); try (InputStream resource = CheckstyleRulesDefinition.class .getResourceAsStream("/org/sonar/l10n/checkstyle.properties")) { loadNames(repository, resource); } SqaleXmlLoader.load(repository, "/com/sonar/sqale/checkstyle-model.xml"); }
Map<String, String> propMap = childrenMap(ruleChildCursor.childElementCursor()); String key = propMap.get("key"); if ("remediationFunction".equals(key)) { offset = timeValue(propMap); remediationFactor = timeValue(propMap); if (rule != null) { rule.setDebtSubCharacteristic(subCharName); rule.setDebtRemediationFunction(remediationFunction( rule.debtRemediationFunctions(), remediationFunction, offset, remediationFactor));
public static void loadNames(NewRepository repository, String resourceAbsolutePath) { InputStream stream = PropertyFileLoader.class.getResourceAsStream(resourceAbsolutePath); if (stream == null) { throw new IllegalArgumentException("Cound not find resource: " + resourceAbsolutePath); } loadNames(repository, stream); }
public void loadXmlResource(String resourcePath) { InputStreamReader reader = reader(resourcePath); 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 { processRoot(reader, inputFactory); } catch (XMLStreamException e) { throw new IllegalStateException("SQALE XML is not valid: " + resourcePath, e); } }
public void addHtmlDescription(NewRule rule) { URL resource = ExternalDescriptionLoader.class.getResource(resourceBasePath + "/" + rule.key() + ".html"); if (resource != null) { addHtmlDescription(rule, resource); } }
public AnnotationBasedRulesDefinition(NewRepository repository, String languageKey) { this.repository = repository; this.languageKey = languageKey; String externalDescriptionBasePath = String.format("/org/sonar/l10n/%s/rules/%s", languageKey, repository.key()); this.externalDescriptionLoader = new ExternalDescriptionLoader(repository, externalDescriptionBasePath); }
private void processSubChar(SMInputCursor subCharCursor) throws XMLStreamException { String subCharName = null; SMInputCursor subCharChildCursor = subCharCursor.childElementCursor(); while (subCharChildCursor.getNext() != null) { String childName = subCharChildCursor.getLocalName(); if ("key".equals(childName)) { subCharName = subCharChildCursor.getElemStringValue(); } if ("chc".equals(childName)) { processRule(subCharName, subCharChildCursor); } } }
private void processRoot(InputStreamReader reader, SMInputFactory inputFactory) throws XMLStreamException { SMHierarchicCursor rootCursor = inputFactory.rootElementCursor(reader); rootCursor.advance(); SMInputCursor charCursor = rootCursor.childElementCursor("chc"); while (charCursor.getNext() != null) { SMInputCursor subCharCursor = charCursor.childElementCursor("chc"); while (subCharCursor.getNext() != null) { processSubChar(subCharCursor); } } }
@Override public void define(Context context) { NewRepository repository = context .createRepository(REPOSITORY_KEY, Java.KEY) .setName(REPOSITORY_NAME); RulesDefinitionXmlLoader ruleLoaderJsp = new RulesDefinitionXmlLoader(); ruleLoaderJsp.load(repository, FindSecurityBugsRulesDefinition.class.getResourceAsStream("/org/sonar/plugins/findbugs/rules-findbugs.xml"), "UTF-8"); SqaleXmlLoader.load(repository, "/com/sonar/sqale/findbugs-model.xml"); repository.done(); // RulesDefinitionXmlLoader ruleLoader = new RulesDefinitionXmlLoader(); // ruleLoader.load(repository, FindbugsRulesDefinition.class.getResourceAsStream("/org/sonar/plugins/findbugs/rules.xml"), "UTF-8"); // ExternalDescriptionLoader.loadHtmlDescriptions(repository, "/org/sonar/l10n/findbugs/rules/findbugs"); // SqaleXmlLoader.load(repository, "/com/sonar/sqale/findbugs-model.xml"); // repository.done(); }
public void addRuleClasses(boolean failIfSqaleNotFound, boolean failIfNoExplicitKey, Iterable<Class> ruleClasses) { new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(ruleClasses, Class.class)); List<NewRule> newRules = Lists.newArrayList(); for (Class<?> ruleClass : ruleClasses) { NewRule rule = newRule(ruleClass, failIfNoExplicitKey); externalDescriptionLoader.addHtmlDescription(rule); rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null); if (!isSqaleAnnotated(ruleClass) && failIfSqaleNotFound) { throw new IllegalArgumentException("No SqaleSubCharacteristic annotation was found on " + ruleClass); } try { setupSqaleModel(rule, ruleClass); } catch (RuntimeException e) { throw new IllegalArgumentException("Could not setup SQALE model on " + ruleClass, e); } newRules.add(rule); } setupExternalNames(newRules); }
static void extractRulesData(NewRepository repository, String xmlRulesFilePath, String htmlDescriptionFolder) { try (InputStream inputStream = PmdRulesDefinition.class.getResourceAsStream(xmlRulesFilePath)) { new RulesDefinitionXmlLoader() .load( repository, inputStream, StandardCharsets.UTF_8 ); } catch (IOException e) { LOGGER.error("Failed to load PMD RuleSet.", e); } ExternalDescriptionLoader.loadHtmlDescriptions(repository, htmlDescriptionFolder); loadNames(repository); SqaleXmlLoader.load(repository, "/com/sonar/sqale/pmd-model.xml"); }
@Override public void define(Context context) { NewRepository repository = context.createRepository(REPOSITORY_KEY, Swift.KEY).setName(REPOSITORY_NAME); try(Reader reader = new InputStreamReader(getClass().getResourceAsStream(RULES_FILE), Charset.forName("UTF-8"))){ JSONArray slRules = (JSONArray)JSONValue.parse(reader); if(slRules != null){ for (Object obj : slRules) { JSONObject slRule = (JSONObject) obj; repository.createRule((String) slRule.get("key")) .setName((String) slRule.get("name")) .setSeverity((String) slRule.get("severity")) .setHtmlDescription((String) slRule.get("description")); } } } catch (IOException e) { LOGGER.error("Failed to load SwiftLint rules", e); } SqaleXmlLoader.load(repository, "/com/sonar/sqale/swiftlint-model.xml"); repository.done(); } }
@Override public void define(final Context context) { NewRepository repository = context.createRepository(REPOSITORY_KEY, ObjectiveC.KEY).setName(REPOSITORY_NAME); try(Reader reader = new InputStreamReader(getClass().getResourceAsStream(RULES_FILE), Charset.forName("UTF-8"))){ JSONArray slRules = (JSONArray)JSONValue.parse(reader); if(slRules != null){ for (Object obj : slRules) { JSONObject slRule = (JSONObject) obj; repository.createRule((String) slRule.get("key")) .setName((String) slRule.get("name")) .setSeverity((String) slRule.get("severity")) .setHtmlDescription((String) slRule.get("description")); } } } catch (IOException e) { LOGGER.error("Failed to load FauxPas rules", e); } SqaleXmlLoader.load(repository, "/com/sonar/sqale/fauxpas-model.xml"); repository.done(); } }
@Override public void define(final Context context) { NewRepository repository = context.createRepository(REPOSITORY_KEY, Swift.KEY).setName(REPOSITORY_NAME); try(Reader reader = new InputStreamReader(getClass().getResourceAsStream(RULES_FILE), Charset.forName("UTF-8"))){ JSONArray slRules = (JSONArray)JSONValue.parse(reader); if(slRules != null){ for (Object obj : slRules) { JSONObject slRule = (JSONObject) obj; repository.createRule((String) slRule.get("key")) .setName((String) slRule.get("name")) .setSeverity((String) slRule.get("severity")) .setHtmlDescription(slRule.get("description") + " (<a href=" + slRule.get("styleguide") + ">" + slRule.get("styleguide") + "</a>)"); } } } catch (IOException e) { LOGGER.error("Failed to load tailor rules", e); } SqaleXmlLoader.load(repository, "/com/sonar/sqale/tailor-model.xml"); repository.done(); } }
@Override public void define(Context context) { NewRepository repository = context .createRepository(DelphiPmdConstants.REPOSITORY_KEY, DelphiLanguage.KEY) .setName(DelphiPmdConstants.REPOSITORY_NAME); List<org.sonar.api.rules.Rule> rules = DelphiRulesUtils.getInitialReferential(); // TODO Review // https://github.com/SonarCommunity/sonar-pmd/blob/master/src/main/java/org/sonar/plugins/pmd/PmdRulesDefinition.java for (org.sonar.api.rules.Rule rule : rules) { NewRule newRule = repository.createRule(rule.getKey()) .setName(rule.getName()) .setHtmlDescription(rule.getDescription()) .setInternalKey(rule.getConfigKey()) .setSeverity(rule.getSeverity().name()); for (RuleParam param : rule.getParams()) { newRule.createParam(param.getKey()) .setDefaultValue(param.getDefaultValue()) .setType(RuleParamType.parse(param.getType())) .setDescription(param.getDescription()); } } SqaleXmlLoader.load(repository, "/org/sonar/plugins/delphi/sqale/delphi-model.xml"); repository.done(); }