001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 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 */
020package org.sonar.wsclient.unmarshallers;
021
022import org.sonar.wsclient.services.DependencyTree;
023import org.sonar.wsclient.services.WSUtils;
024
025import java.util.ArrayList;
026import java.util.List;
027
028public class DependencyTreeUnmarshaller extends AbstractUnmarshaller<DependencyTree> {
029  @Override
030  protected DependencyTree parse(Object json) {
031    WSUtils utils = WSUtils.getINSTANCE();
032    DependencyTree tree = new DependencyTree()
033        .setDepId(utils.getString(json, "did"))
034        .setResourceId(utils.getString(json, "rid"))
035        .setResourceKey(utils.getString(json, "k"))
036        .setResourceName(utils.getString(json, "n"))
037        .setResourceScope(utils.getString(json, "s"))
038        .setResourceQualifier(utils.getString(json, "q"))
039        .setResourceVersion(utils.getString(json, "v"))
040        .setUsage(utils.getString(json, "u"))
041        .setWeight(utils.getInteger(json, "w"));
042
043    List<DependencyTree> to = new ArrayList<DependencyTree>();
044    tree.setTo(to);
045
046    Object toJson = utils.getField(json, "to");
047    if (toJson != null) {
048      for (int i = 0; i < utils.getArraySize(toJson); i++) {
049        to.add(parse(utils.getArrayElement(toJson, i)));
050      }
051    }
052    return tree;
053  }
054}