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