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.TimeMachine; 023 import org.sonar.wsclient.services.TimeMachineCell; 024 import org.sonar.wsclient.services.TimeMachineColumn; 025 import org.sonar.wsclient.services.WSUtils; 026 027 public class TimeMachineUnmarshaller extends AbstractUnmarshaller<TimeMachine> { 028 029 @Override 030 protected TimeMachine parse(Object json) { 031 WSUtils utils = WSUtils.getINSTANCE(); 032 Object cols = utils.getField(json, "cols"); 033 if (cols == null) { 034 throw new IllegalArgumentException("cols must be set"); 035 } 036 Object cells = utils.getField(json, "cells"); 037 if (cells == null) { 038 throw new IllegalArgumentException("cells must be set"); 039 } 040 return new TimeMachine(toColumns(cols), toCells(cells)); 041 } 042 043 private TimeMachineColumn[] toColumns(Object cols) { 044 WSUtils utils = WSUtils.getINSTANCE(); 045 int size = utils.getArraySize(cols); 046 TimeMachineColumn[] result = new TimeMachineColumn[size]; 047 for (int index = 0; index < size; index++) { 048 Object colJson = utils.getArrayElement(cols, index); 049 result[index] = new TimeMachineColumn(index, utils.getString(colJson, "metric"), null, null); 050 } 051 return result; 052 } 053 054 private TimeMachineCell[] toCells(Object cells) { 055 WSUtils utils = WSUtils.getINSTANCE(); 056 int size = utils.getArraySize(cells); 057 TimeMachineCell[] result = new TimeMachineCell[size]; 058 for (int i = 0; i < size; i++) { 059 Object cellJson = utils.getArrayElement(cells, i); 060 Object valuesJson = utils.getField(cellJson, "v"); 061 if (valuesJson != null) { 062 Object[] resultValues = new Object[utils.getArraySize(valuesJson)]; 063 for (int indexValue = 0; indexValue < utils.getArraySize(valuesJson); indexValue++) { 064 Object value = utils.getArrayElement(valuesJson, indexValue); 065 resultValues[indexValue] = value; 066 } 067 result[i] = new TimeMachineCell(utils.getDateTime(cellJson, "d"), resultValues); 068 } 069 } 070 return result; 071 } 072 073 }