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; 021 022 import org.json.simple.JSONObject; 023 import org.json.simple.JSONValue; 024 import org.sonar.wsclient.services.WSUtils; 025 import org.sonar.wsclient.unmarshallers.JsonUtils; 026 027 import javax.annotation.CheckForNull; 028 029 import java.io.UnsupportedEncodingException; 030 import java.net.URLEncoder; 031 import java.text.SimpleDateFormat; 032 import java.util.ArrayList; 033 import java.util.Date; 034 import java.util.Set; 035 036 public final class JdkUtils extends WSUtils { 037 038 @Override 039 public String format(Date date, String format) { 040 SimpleDateFormat dateFormat = new SimpleDateFormat(format); 041 return dateFormat.format(date); 042 } 043 044 @Override 045 public String encodeUrl(String url) { 046 try { 047 return URLEncoder.encode(url, "UTF-8"); 048 049 } catch (UnsupportedEncodingException e) { 050 throw new IllegalStateException("Encoding not supported", e); 051 } 052 } 053 054 @Override 055 public Object getField(Object json, String field) { 056 return ((JSONObject) json).get(field); 057 } 058 059 @Override 060 @CheckForNull 061 public String getString(Object json, String field) { 062 return JsonUtils.getString((JSONObject) json, field); 063 } 064 065 @Override 066 @CheckForNull 067 public Boolean getBoolean(Object json, String field) { 068 return JsonUtils.getBoolean((JSONObject) json, field); 069 } 070 071 @Override 072 @CheckForNull 073 public Integer getInteger(Object json, String field) { 074 return JsonUtils.getInteger((JSONObject) json, field); 075 } 076 077 @Override 078 @CheckForNull 079 public Double getDouble(Object json, String field) { 080 return JsonUtils.getDouble((JSONObject) json, field); 081 } 082 083 @Override 084 @CheckForNull 085 public Long getLong(Object json, String field) { 086 return JsonUtils.getLong((JSONObject) json, field); 087 } 088 089 @Override 090 @CheckForNull 091 public Date getDateTime(Object json, String field) { 092 return JsonUtils.getDateTime((JSONObject) json, field); 093 } 094 095 @Override 096 public int getArraySize(Object array) { 097 return ((ArrayList) array).size(); 098 } 099 100 @Override 101 public Object getArrayElement(Object array, int i) { 102 return ((ArrayList) array).get(i); 103 } 104 105 @Override 106 public Object parse(String jsonStr) { 107 return JSONValue.parse(jsonStr); 108 } 109 110 @Override 111 public Set<String> getFields(Object json) { 112 return ((JSONObject) json).keySet(); 113 } 114 }