1919
2020package org .sonar .plugins .checkstyle ;
2121
22- import java .io .IOException ;
23- import java .io .InputStream ;
24- import java .util .Objects ;
25- import java .util .Properties ;
26-
2722import org .sonar .api .ExtensionPoint ;
2823import org .sonar .api .batch .ScannerSide ;
2924import org .sonar .api .server .rule .RulesDefinition ;
30- import org .sonar .api .server .rule .RulesDefinitionXmlLoader ;
3125import org .sonar .plugins .checkstyle .metadata .CheckstyleMetadata ;
32- import org .sonar .squidbridge .rules .ExternalDescriptionLoader ;
33- import org .sonar .squidbridge .rules .SqaleXmlLoader ;
34-
35- import com .google .common .annotations .VisibleForTesting ;
3626
3727@ ExtensionPoint
3828@ ScannerSide
@@ -43,65 +33,10 @@ public void define(Context context) {
4333 final NewRepository repository = context .createRepository (
4434 CheckstyleConstants .REPOSITORY_KEY , "java" ).setName (
4535 CheckstyleConstants .REPOSITORY_NAME );
46- try {
47- extractRulesData (repository , "/org/sonar/plugins/checkstyle/rules.xml" ,
48- "/org/sonar/l10n/checkstyle/rules/checkstyle" );
49- }
50- catch (IOException ex ) {
51- throw new IllegalStateException ("Exception during extractRulesData" , ex );
52- }
5336
5437 final CheckstyleMetadata checkstyleMetadata = new CheckstyleMetadata (repository );
5538 checkstyleMetadata .createRulesWithMetadata ();
5639
5740 repository .done ();
5841 }
59-
60- @ VisibleForTesting
61- static void extractRulesData (NewRepository repository , String xmlRulesFilePath ,
62- String htmlDescriptionFolder ) throws IOException {
63- final RulesDefinitionXmlLoader ruleLoader = new RulesDefinitionXmlLoader ();
64- try (InputStream resource = CheckstyleRulesDefinition .class
65- .getResourceAsStream (xmlRulesFilePath )) {
66- ruleLoader .load (repository , resource , "UTF-8" );
67- }
68- ExternalDescriptionLoader .loadHtmlDescriptions (repository , // NOSONAR
69- htmlDescriptionFolder );
70- try (InputStream resource = CheckstyleRulesDefinition .class
71- .getResourceAsStream ("/org/sonar/l10n/checkstyle.properties" )) {
72- loadNames (repository , resource );
73- }
74- SqaleXmlLoader .load (repository , "/com/sonar/sqale/checkstyle-model.xml" ); // NOSONAR
75- }
76-
77- private static void loadNames (NewRepository repository , InputStream stream ) {
78- // code taken from:
79- // https://github.com/SonarSource/sslr-squid-bridge/blob/2.5.2/
80- // src/main/java/org/sonar/squidbridge/rules/PropertyFileLoader.java#L46
81- final Properties properties = new Properties ();
82- try {
83- properties .load (stream );
84- }
85- catch (IOException ex ) {
86- throw new IllegalArgumentException ("Could not read names from properties" , ex );
87- }
88-
89- if (Objects .nonNull (repository .rules ())) {
90- repository .rules ().forEach (rule -> {
91- final String baseKey = "rule." + repository .key () + "." + rule .key ();
92- final String nameKey = baseKey + ".name" ;
93- final String ruleName = properties .getProperty (nameKey );
94- if (Objects .nonNull (ruleName )) {
95- rule .setName (ruleName );
96- }
97- rule .params ().forEach (param -> {
98- final String paramDescriptionKey = baseKey + ".param." + param .key ();
99- final String paramDescription = properties .getProperty (paramDescriptionKey );
100- if (Objects .nonNull (paramDescription )) {
101- param .setDescription (paramDescription );
102- }
103- });
104- });
105- }
106- }
10742}
0 commit comments