001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 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.json.simple.JSONArray; 023 import org.json.simple.JSONObject; 024 import org.sonar.wsclient.services.Rule; 025 import org.sonar.wsclient.services.RuleParam; 026 027 import java.util.ArrayList; 028 import java.util.List; 029 030 /** 031 * @since 2.5 032 */ 033 public class RuleUnmarshaller extends AbstractUnmarshaller<Rule> { 034 035 @Override 036 protected Rule parse(JSONObject json) { 037 Rule rule = new Rule(); 038 parseRuleFields(json, rule); 039 parseParams(json, rule); 040 return rule; 041 } 042 043 private void parseRuleFields(JSONObject json, Rule rule) { 044 rule.setTitle(JsonUtils.getString(json, "title")) 045 .setKey(JsonUtils.getString(json, "key")) 046 .setRepository(JsonUtils.getString(json, "plugin")) 047 .setDescription(JsonUtils.getString(json, "description")) 048 .setSeverity(JsonUtils.getString(json, "priority")) 049 .setActive("ACTIVE".equals(JsonUtils.getString(json, "status"))); 050 } 051 052 private void parseParams(JSONObject json, Rule rule) { 053 JSONArray paramsJson = (JSONArray) json.get("params"); 054 if (paramsJson != null) { 055 rule.setParams(parseParams(paramsJson)); 056 } 057 } 058 059 private List<RuleParam> parseParams(JSONArray paramsJson) { 060 List<RuleParam> ruleParams = new ArrayList<RuleParam>(); 061 int len = paramsJson.size(); 062 for (int i = 0; i < len; i++) { 063 JSONObject paramJson = (JSONObject) paramsJson.get(i); 064 if (paramJson != null) { 065 RuleParam param = parseParam(paramJson); 066 ruleParams.add(param); 067 } 068 } 069 return ruleParams; 070 } 071 072 private RuleParam parseParam(JSONObject json) { 073 RuleParam param = new RuleParam(); 074 param.setName(JsonUtils.getString(json, "name")) 075 .setDescription(JsonUtils.getString(json, "description")) 076 .setValue(JsonUtils.getString(json, "value")); 077 return param; 078 } 079 080 }