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        Boolean defaultProfile = utils.getBoolean(json, "default");
032        profile
033            .setLanguage(utils.getString(json, "language"))
034            .setName(utils.getString(json, "name"))
035            .setDefaultProfile(defaultProfile != null ? defaultProfile : false)
036            .setParentName(utils.getString(json, "parent"));
037    
038        parseRules(utils, profile, json);
039        return profile;
040      }
041    
042      private void parseRules(WSUtils utils, Profile profile, Object json) {
043        Object rulesJson = utils.getField(json, "rules");
044        if (rulesJson != null) {
045          for (int i = 0; i < utils.getArraySize(rulesJson); i++) {
046            Object ruleJson = utils.getArrayElement(rulesJson, i);
047            if (ruleJson != null) {
048              Profile.Rule rule = new Profile.Rule();
049              rule.setKey(utils.getString(ruleJson, "key"));
050              rule.setRepository(utils.getString(ruleJson, "repo"));
051              rule.setSeverity(utils.getString(ruleJson, "severity"));
052              rule.setInheritance(utils.getString(ruleJson, "inheritance"));
053    
054              parseRuleParameters(utils, rule, ruleJson);
055              profile.addRule(rule);
056            }
057          }
058        }
059      }
060    
061      private void parseRuleParameters(WSUtils utils, Profile.Rule rule, Object ruleJson) {
062        Object paramsJson = utils.getField(ruleJson, "params");
063        if (paramsJson != null) {
064          for (int indexParam = 0; indexParam < utils.getArraySize(paramsJson); indexParam++) {
065            Object paramJson = utils.getArrayElement(paramsJson, indexParam);
066            rule.addParameter(utils.getString(paramJson, "key"), utils.getString(paramJson, "value"));
067          }
068        }
069      }
070    
071    }