001/* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 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 */ 020package org.sonar.wsclient.unmarshallers; 021 022import org.sonar.wsclient.services.Rule; 023import org.sonar.wsclient.services.RuleParam; 024import org.sonar.wsclient.services.WSUtils; 025 026import java.util.ArrayList; 027import java.util.List; 028 029/** 030 * @since 2.5 031 */ 032public 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 .setConfigKey(utils.getString(json, "config_key")) 048 .setRepository(utils.getString(json, "plugin")) 049 .setDescription(utils.getString(json, "description")) 050 .setSeverity(utils.getString(json, "priority")) 051 .setActive("ACTIVE".equals(utils.getString(json, "status"))); 052 } 053 054 private void parseParams(Object json, Rule rule) { 055 WSUtils utils = WSUtils.getINSTANCE(); 056 057 Object paramsJson = utils.getField(json, "params"); 058 if (paramsJson != null) { 059 rule.setParams(parseParams(paramsJson)); 060 } 061 } 062 063 private List<RuleParam> parseParams(Object paramsJson) { 064 WSUtils utils = WSUtils.getINSTANCE(); 065 066 List<RuleParam> ruleParams = new ArrayList<RuleParam>(); 067 int len = utils.getArraySize(paramsJson); 068 for (int i = 0; i < len; i++) { 069 Object paramJson = utils.getArrayElement(paramsJson, i); 070 if (paramJson != null) { 071 RuleParam param = parseParam(paramJson); 072 ruleParams.add(param); 073 } 074 } 075 return ruleParams; 076 } 077 078 private RuleParam parseParam(Object json) { 079 WSUtils utils = WSUtils.getINSTANCE(); 080 081 RuleParam param = new RuleParam(); 082 param.setName(utils.getString(json, "name")) 083 .setDescription(utils.getString(json, "description")) 084 .setValue(utils.getString(json, "value")); 085 return param; 086 } 087 088}