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