001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 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.Profile;
023    import org.sonar.wsclient.services.WSUtils;
024    
025    public class ProfileUnmarshaller extends AbstractUnmarshaller<Profile> {
026    
027      @Override
028      protected Profile parse(Object json) {
029        WSUtils utils = WSUtils.getINSTANCE();
030        Profile profile = new Profile();
031        profile
032            .setLanguage(utils.getString(json, "language"))
033            .setName(utils.getString(json, "name"))
034            .setDefaultProfile(utils.getBoolean(json, "default"))
035            .setParentName(utils.getString(json, "parent"));
036    
037        parseRules(utils, profile, json);
038        return profile;
039      }
040    
041      private void parseRules(WSUtils utils, Profile profile, Object json) {
042        Object rulesJson = utils.getField(json, "rules");
043        if (rulesJson != null) {
044          for (int i = 0; i < utils.getArraySize(rulesJson); i++) {
045            Object ruleJson = utils.getArrayElement(rulesJson, i);
046            if (ruleJson != null) {
047              Profile.Rule rule = new Profile.Rule();
048              rule.setKey(utils.getString(ruleJson, "key"));
049              rule.setRepository(utils.getString(ruleJson, "repo"));
050              rule.setSeverity(utils.getString(ruleJson, "severity"));
051              rule.setInheritance(utils.getString(ruleJson, "inheritance"));
052    
053              parseRuleParameters(utils, rule, ruleJson);
054              profile.addRule(rule);
055            }
056          }
057        }
058      }
059    
060      private void parseRuleParameters(WSUtils utils, Profile.Rule rule, Object ruleJson) {
061        Object paramsJson = utils.getField(ruleJson, "params");
062        if (paramsJson != null) {
063          for (int indexParam = 0; indexParam < utils.getArraySize(paramsJson); indexParam++) {
064            Object paramJson = utils.getArrayElement(paramsJson, indexParam);
065            rule.addParameter(utils.getString(paramJson, "key"), utils.getString(paramJson, "value"));
066          }
067        }
068      }
069    
070    }