1 package de.example.plugins.custom.java;
3 import java.io.IOException;
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;
13 import com.google.common.base.Charsets;
14 import com.google.common.io.Resources;
15 import com.google.gson.Gson;
17 import de.example.custom.java.checks.CheckList;
19 public class CustomProfile extends ProfileDefinition {
20 private final Gson gson = new Gson();
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);
28 for (String key : jsonProfile.ruleKeys) {
29 Rule rule = Rule.create(CheckList.REPOSITORY_KEY, key);
30 profile.activateRule(rule, null);
36 private String readResource(URL resource) {
38 return Resources.toString(resource, Charsets.UTF_8);
39 } catch (IOException e) {
40 throw new IllegalStateException("Failed to read: " + resource, e);
44 private static class Profile {
46 List<String> ruleKeys;
48 public String getName() {
52 public List<String> getRuleKeys() {