diff options
Diffstat (limited to 'sonar-ws-client/src/main')
4 files changed, 317 insertions, 0 deletions
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/Profile.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/Profile.java new file mode 100644 index 00000000000..1311526fffd --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/Profile.java @@ -0,0 +1,162 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ + +package org.sonar.wsclient.services; + +import java.util.*; + +/** + * @since 2.7 + */ +public class Profile extends Model { + + private String language; + private String name; + private boolean defaultProfile; + private boolean provided; + private String parentName; + private List<Rule> rules = new ArrayList<Rule>(); + + public String getLanguage() { + return language; + } + + public Profile setLanguage(String s) { + this.language = s; + return this; + } + + public String getName() { + return name; + } + + public Profile setName(String name) { + this.name = name; + return this; + } + + public boolean isDefaultProfile() { + return defaultProfile; + } + + public Profile setDefaultProfile(boolean b) { + this.defaultProfile = b; + return this; + } + + public boolean isProvided() { + return provided; + } + + public Profile setProvided(boolean b) { + this.provided = b; + return this; + } + + public String getParentName() { + return parentName; + } + + public Profile setParentName(String s) { + this.parentName = s; + return this; + } + + public List<Rule> getRules() { + return rules; + } + + public Rule getRule(String repositoryKey, String ruleKey) { + for (Rule rule : rules) { + if (repositoryKey.equals(rule.getRepository()) && ruleKey.equals(rule.getKey())) { + return rule; + } + } + return null; + } + + public Profile addRule(Rule rule) { + rules.add(rule); + return this; + } + + public static final class Rule { + private String key; + private String repository; + private String severity; + private String inheritance; + private Map<String,String> parameters; + + public String getKey() { + return key; + } + + public Rule setKey(String key) { + this.key = key; + return this; + } + + public String getRepository() { + return repository; + } + + public Rule setRepository(String repository) { + this.repository = repository; + return this; + } + + public String getSeverity() { + return severity; + } + + public Rule setSeverity(String severity) { + this.severity = severity; + return this; + } + + public String getInheritance() { + return inheritance; + } + + public Rule setInheritance(String inheritance) { + this.inheritance = inheritance; + return this; + } + + public Map<String, String> getParameters() { + if (parameters==null) { + return Collections.emptyMap(); + } + return parameters; + } + + public String getParameter(String key) { + return getParameters().get(key); + } + + public Rule addParameter(String key, String value) { + if (parameters==null) { + parameters = new HashMap<String,String>(); + } + parameters.put(key, value); + return this; + } + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ProfileQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ProfileQuery.java new file mode 100644 index 00000000000..0012c3e11df --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ProfileQuery.java @@ -0,0 +1,91 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.services; + +/** + * @since 2.7 + */ +public class ProfileQuery extends Query<Profile> { + public static final String BASE_URL = "/api/profiles"; + + private String language; + private String name;//optional + private String[] ruleRepositories;//optional + private String[] ruleSeverities;//optional + + private ProfileQuery(String language) { + this.language = language; + } + + public String getLanguage() { + return language; + } + + public String getName() { + return name; + } + + public String[] getRuleRepositories() { + return ruleRepositories; + } + + public String[] getRuleSeverities() { + return ruleSeverities; + } + + public ProfileQuery setName(String name) { + this.name = name; + return this; + } + + public ProfileQuery setRuleRepositories(String[] ruleRepositories) { + this.ruleRepositories = ruleRepositories; + return this; + } + + public ProfileQuery setRuleSeverities(String[] ruleSeverities) { + this.ruleSeverities = ruleSeverities; + return this; + } + + @Override + public Class<Profile> getModelClass() { + return Profile.class; + } + + @Override + public String getUrl() { + StringBuilder url = new StringBuilder(BASE_URL); + url.append('?'); + appendUrlParameter(url, "language", language); + appendUrlParameter(url, "name", name); + appendUrlParameter(url, "rule_repositories", ruleRepositories); + appendUrlParameter(url, "rule_severities", ruleSeverities); + return url.toString(); + } + + public static ProfileQuery createWithLanguage(String language) { + return new ProfileQuery(language); + } + + public static ProfileQuery create(String language, String name) { + return new ProfileQuery(language).setName(name); + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ProfileUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ProfileUnmarshaller.java new file mode 100644 index 00000000000..9af6118515c --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ProfileUnmarshaller.java @@ -0,0 +1,63 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.unmarshallers; + +import org.sonar.wsclient.services.Profile; +import org.sonar.wsclient.services.WSUtils; + +public class ProfileUnmarshaller extends AbstractUnmarshaller<Profile> { + + @Override + protected Profile parse(Object json) { + WSUtils utils = WSUtils.getINSTANCE(); + Profile profile = new Profile(); + profile + .setLanguage(utils.getString(json, "language")) + .setName(utils.getString(json, "name")) + .setDefaultProfile(utils.getBoolean(json, "default")) + .setParentName(utils.getString(json, "parent")) + .setProvided(utils.getBoolean(json, "provided")); + + Object rulesJson = utils.getField(json, "rules"); + if (rulesJson != null) { + for (int i = 0; i < utils.getArraySize(rulesJson); i++) { + Object ruleJson = utils.getArrayElement(rulesJson, i); + if (ruleJson != null) { + Profile.Rule rule = new Profile.Rule(); + rule.setKey(utils.getString(ruleJson, "key")); + rule.setRepository(utils.getString(ruleJson, "repo")); + rule.setSeverity(utils.getString(ruleJson, "severity")); + rule.setInheritance(utils.getString(ruleJson, "inheritance")); + + Object paramsJson = utils.getField(ruleJson, "params"); + if (paramsJson != null) { + for (int indexParam = 0; indexParam < utils.getArraySize(paramsJson); indexParam++) { + Object paramJson = utils.getArrayElement(paramsJson, indexParam); + rule.addParameter(utils.getString(paramJson, "key"), utils.getString(paramJson, "value")); + } + } + profile.addRule(rule); + } + } + } + return profile; + } + +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java index a97d7a45cca..629d30184bd 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java @@ -45,6 +45,7 @@ public final class Unmarshallers { unmarshallers.put(Plugin.class, new PluginUnmarshaller()); unmarshallers.put(Rule.class, new RuleUnmarshaller()); unmarshallers.put(TimeMachine.class, new TimeMachineUnmarshaller()); + unmarshallers.put(Profile.class, new ProfileUnmarshaller()); } public static <MODEL extends Model> Unmarshaller<MODEL> forModel(Class<MODEL> modelClass) { |