001/* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 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 */ 020package org.sonar.wsclient.gwt; 021 022import com.google.gwt.i18n.client.DateTimeFormat; 023import com.google.gwt.json.client.JSONObject; 024import com.google.gwt.json.client.JSONParser; 025import com.google.gwt.json.client.JSONValue; 026import org.sonar.gwt.JsonUtils; 027import org.sonar.wsclient.services.WSUtils; 028 029import java.util.Date; 030import java.util.Set; 031 032public class GwtUtils extends WSUtils { 033 @Override 034 public String format(Date date, String format) { 035 return DateTimeFormat.getFormat(format).format(date); 036 } 037 038 @Override 039 public String encodeUrl(String url) { 040 return com.google.gwt.http.client.URL.encode(url); 041 } 042 043 @Override 044 public Object getField(Object json, String field) { 045 return ((JSONObject) json).get(field); 046 } 047 048 @Override 049 public String getString(Object json, String field) { 050 return JsonUtils.getString((JSONObject) json, field); 051 } 052 053 @Override 054 public Boolean getBoolean(Object json, String field) { 055 return JsonUtils.getBoolean((JSONObject) json, field); 056 } 057 058 @Override 059 public Integer getInteger(Object json, String field) { 060 return JsonUtils.getInteger((JSONObject) json, field); 061 } 062 063 @Override 064 public Double getDouble(Object json, String field) { 065 return JsonUtils.getDouble((JSONObject) json, field); 066 } 067 068 @Override 069 public Long getLong(Object json, String field) { 070 Double d = JsonUtils.getDouble((JSONObject) json, field); 071 if (d != null) { 072 return d.longValue(); 073 } 074 return null; 075 } 076 077 @Override 078 public Date getDateTime(Object json, String field) { 079 return JsonUtils.getDate((JSONObject) json, field); 080 } 081 082 @Override 083 public int getArraySize(Object array) { 084 return JsonUtils.getArraySize((JSONValue) array); 085 } 086 087 @Override 088 public Object getArrayElement(Object array, int i) { 089 return JsonUtils.getArray((JSONValue) array, i); 090 } 091 092 @Override 093 public Object parse(String jsonStr) { 094 return JSONParser.parse(jsonStr); 095 } 096 097 @Override 098 public Set<String> getFields(Object json) { 099 return ((JSONObject) json).keySet(); 100 } 101 102}