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.api.charts; 021 022import javax.annotation.Nullable; 023import org.apache.commons.lang.CharEncoding; 024import org.apache.commons.lang.StringUtils; 025import org.apache.commons.lang.text.StrTokenizer; 026import org.sonar.api.utils.SonarException; 027 028import java.io.UnsupportedEncodingException; 029import java.net.URLDecoder; 030import java.util.HashMap; 031import java.util.Locale; 032import java.util.Map; 033 034/** 035 * The class to hold parameters to configure a chart 036 * @since 1.10 037 * @deprecated in 4.5.1, replaced by Javascript charts 038 */ 039@Deprecated 040public class ChartParameters { 041 private static final String[] EMPTY = new String[0]; 042 043 public static final String PARAM_WIDTH = "w"; 044 public static final String PARAM_BACKGROUND_COLOR = "bgc"; 045 public static final String PARAM_HEIGHT = "h"; 046 public static final int MAX_WIDTH = 900; 047 public static final String PARAM_LOCALE = "locale"; 048 049 public static final int MAX_HEIGHT = 900; 050 public static final int DEFAULT_WIDTH = 200; 051 052 public static final int DEFAULT_HEIGHT = 200; 053 054 055 private final Map<String, String> params; 056 057 /** 058 * Creates a ChartParameter based on a list of parameters 059 * @param params the list of parameters 060 */ 061 public ChartParameters(Map<String, String> params) { 062 this.params = params; 063 } 064 065 /** 066 * Creates a Chartparameter based on a query string with a format key1=value1&key2=value2... 067 * 068 * @param queryString string 069 */ 070 public ChartParameters(String queryString) { 071 this.params = new HashMap<>(); 072 String[] groups = StringUtils.split(queryString, "&"); 073 for (String group : groups) { 074 String[] keyval = StringUtils.split(group, "="); 075 params.put(keyval[0], keyval[1]); 076 } 077 } 078 079 /** 080 * Shortcut to getValue with no decoding and no default value 081 * @param key the param ket 082 * @return the value of the param 083 */ 084 public String getValue(String key) { 085 return getValue(key, "", false); 086 } 087 088 /** 089 * Returns the [decoded or not] value of a param from its key or the default value 090 * if id does not exist 091 * 092 * @param key the param ket 093 * @param defaultValue the default value if not exist 094 * @param decode whther the value should be decoded 095 * @return the value of the param 096 */ 097 098 public String getValue(String key, String defaultValue, boolean decode) { 099 String val = params.get(key); 100 if (decode) { 101 val = decode(val); 102 } 103 if (val == null) { 104 val = defaultValue; 105 } 106 return val; 107 } 108 109 /** 110 * Returns an array of a param values, given its key and the values delimiter 111 * 112 * @param key the param key 113 * @param delimiter the values delimiter 114 * @return the list of vaalues 115 */ 116 public String[] getValues(String key, String delimiter) { 117 String value = params.get(key); 118 if (value != null) { 119 return StringUtils.split(value, delimiter); 120 } 121 return EMPTY; 122 } 123 124 /** 125 * Returns an array of a param values, given its key and the values delimiter 126 * Values can be decoded or not 127 * 128 * @param key the param key 129 * @param delimiter the values delimiter 130 * @param decode whether to decode values 131 * @return the list of vaalues 132 */ 133 public String[] getValues(String key, String delimiter, boolean decode) { 134 String value = params.get(key); 135 if (value != null) { 136 if (decode) { 137 value = decode(value); 138 } 139 return new StrTokenizer(value, delimiter).setIgnoreEmptyTokens(false).getTokenArray(); 140 } 141 return EMPTY; 142 } 143 144 /** 145 * Get the chart width 146 * 147 * @return width 148 */ 149 public int getWidth() { 150 int width = Integer.parseInt(getValue(PARAM_WIDTH, Integer.toString(DEFAULT_WIDTH), false)); 151 return Math.min(width, MAX_WIDTH); 152 } 153 154 /** 155 * Get the chart height 156 * 157 * @return height 158 */ 159 public int getHeight() { 160 int height = Integer.parseInt(getValue(PARAM_HEIGHT, Integer.toString(DEFAULT_HEIGHT), false)); 161 return Math.min(height, MAX_HEIGHT); 162 } 163 164 /** 165 * Get the Locale 166 * 167 * @return Locale 168 */ 169 public Locale getLocale() { 170 String locale = getValue(PARAM_LOCALE); 171 if (StringUtils.isNotBlank(locale)) { 172 return new Locale(locale); 173 } 174 return Locale.ENGLISH; 175 } 176 177 private static String decode(@Nullable String val) { 178 if (val != null) { 179 try { 180 val = URLDecoder.decode(val, CharEncoding.UTF_8); 181 } catch (UnsupportedEncodingException e) { 182 throw new SonarException("Decoding chart parameter : " + val, e); 183 } 184 } 185 return val; 186 } 187}