270fe70dcddcf3385b4fae63999f7cdfa58d37f8
[JavaForFun] /
1 package de.example.plugins.custom.java;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.List;
6
7 import org.sonar.api.profiles.ProfileDefinition;
8 import org.sonar.api.profiles.RulesProfile;
9 import org.sonar.api.rules.Rule;
10 import org.sonar.api.utils.ValidationMessages;
11 import org.sonar.plugins.java.Java;
12
13 import com.google.common.base.Charsets;
14 import com.google.common.io.Resources;
15 import com.google.gson.Gson;
16
17 import de.example.custom.java.checks.CheckList;
18
19 public class CustomProfile extends ProfileDefinition {
20         private final Gson gson = new Gson();
21
22         @Override
23         public RulesProfile createProfile(ValidationMessages messages) {
24                 URL resource = CustomRulesDefinition.class.getResource("/de/example/l10n/java/rules/custom/Custom_profile.json");
25                 Profile jsonProfile = gson.fromJson(readResource(resource), Profile.class);
26                 RulesProfile profile = RulesProfile.create(jsonProfile.getName(), Java.KEY);
27                 
28                 for (String key : jsonProfile.ruleKeys) {
29                         Rule rule = Rule.create(CheckList.REPOSITORY_KEY, key);
30                         profile.activateRule(rule, null);
31                 }
32                 
33                 return profile;
34         }
35
36         private String readResource(URL resource) {
37                 try {
38                         return Resources.toString(resource, Charsets.UTF_8);
39                 } catch (IOException e) {
40                         throw new IllegalStateException("Failed to read: " + resource, e);
41                 }
42         }
43
44         private static class Profile {
45                 String name;
46                 List<String> ruleKeys;
47                 
48                 public String getName() {
49                         return name;
50                 }
51                 
52                 public List<String> getRuleKeys() {
53                         return ruleKeys;
54                 }
55         }
56
57 }