001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.wsclient.unmarshallers;
021
022 import org.sonar.wsclient.services.Rule;
023 import org.sonar.wsclient.services.RuleParam;
024 import org.sonar.wsclient.services.WSUtils;
025
026 import java.util.ArrayList;
027 import java.util.List;
028
029 /**
030 * @since 2.5
031 */
032 public class RuleUnmarshaller extends AbstractUnmarshaller<Rule> {
033
034 private static final String DESCRIPTION = "description";
035
036 @Override
037 protected Rule parse(Object json) {
038 Rule rule = new Rule();
039 parseRuleFields(json, rule);
040 parseParams(json, rule);
041 return rule;
042 }
043
044 private void parseRuleFields(Object json, Rule rule) {
045 WSUtils utils = WSUtils.getINSTANCE();
046
047 rule.setTitle(utils.getString(json, "title"))
048 .setKey(utils.getString(json, "key"))
049 .setConfigKey(utils.getString(json, "config_key"))
050 .setRepository(utils.getString(json, "plugin"))
051 .setDescription(utils.getString(json, DESCRIPTION))
052 .setSeverity(utils.getString(json, "priority"))
053 .setActive("ACTIVE".equals(utils.getString(json, "status")));
054 }
055
056 private void parseParams(Object json, Rule rule) {
057 WSUtils utils = WSUtils.getINSTANCE();
058
059 Object paramsJson = utils.getField(json, "params");
060 if (paramsJson != null) {
061 rule.setParams(parseParams(paramsJson));
062 }
063 }
064
065 private List<RuleParam> parseParams(Object paramsJson) {
066 WSUtils utils = WSUtils.getINSTANCE();
067
068 List<RuleParam> ruleParams = new ArrayList<RuleParam>();
069 int len = utils.getArraySize(paramsJson);
070 for (int i = 0; i < len; i++) {
071 Object paramJson = utils.getArrayElement(paramsJson, i);
072 if (paramJson != null) {
073 RuleParam param = parseParam(paramJson);
074 ruleParams.add(param);
075 }
076 }
077 return ruleParams;
078 }
079
080 private RuleParam parseParam(Object json) {
081 WSUtils utils = WSUtils.getINSTANCE();
082
083 RuleParam param = new RuleParam();
084 param.setName(utils.getString(json, "name"))
085 .setDescription(utils.getString(json, DESCRIPTION))
086 .setValue(utils.getString(json, "value"));
087 return param;
088 }
089
090 }