001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
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 @Override
035 protected Rule parse(Object json) {
036 Rule rule = new Rule();
037 parseRuleFields(json, rule);
038 parseParams(json, rule);
039 return rule;
040 }
041
042 private void parseRuleFields(Object json, Rule rule) {
043 WSUtils utils = WSUtils.getINSTANCE();
044
045 rule.setTitle(utils.getString(json, "title"))
046 .setKey(utils.getString(json, "key"))
047 .setRepository(utils.getString(json, "plugin"))
048 .setDescription(utils.getString(json, "description"))
049 .setSeverity(utils.getString(json, "priority"))
050 .setActive("ACTIVE".equals(utils.getString(json, "status")));
051 }
052
053 private void parseParams(Object json, Rule rule) {
054 WSUtils utils = WSUtils.getINSTANCE();
055
056 Object paramsJson = utils.getField(json, "params");
057 if (paramsJson != null) {
058 rule.setParams(parseParams(paramsJson));
059 }
060 }
061
062 private List<RuleParam> parseParams(Object paramsJson) {
063 WSUtils utils = WSUtils.getINSTANCE();
064
065 List<RuleParam> ruleParams = new ArrayList<RuleParam>();
066 int len = utils.getArraySize(paramsJson);
067 for (int i = 0; i < len; i++) {
068 Object paramJson = utils.getArrayElement(paramsJson, i);
069 if (paramJson != null) {
070 RuleParam param = parseParam(paramJson);
071 ruleParams.add(param);
072 }
073 }
074 return ruleParams;
075 }
076
077 private RuleParam parseParam(Object json) {
078 WSUtils utils = WSUtils.getINSTANCE();
079
080 RuleParam param = new RuleParam();
081 param.setName(utils.getString(json, "name"))
082 .setDescription(utils.getString(json, "description"))
083 .setValue(utils.getString(json, "value"));
084 return param;
085 }
086
087 }