001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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.api.charts;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.commons.lang.text.StrTokenizer;
024    import org.sonar.api.utils.SonarException;
025    
026    import java.io.UnsupportedEncodingException;
027    import java.net.URLDecoder;
028    import java.util.HashMap;
029    import java.util.Locale;
030    import java.util.Map;
031    
032    /**
033     * @since 1.10
034     */
035    public class ChartParameters {
036      private static final String[] EMPTY = new String[0];
037    
038      public static final String PARAM_WIDTH = "w";
039      public static final String PARAM_BACKGROUND_COLOR = "bgc";
040      public static final String PARAM_HEIGHT = "h";
041      public static final int MAX_WIDTH = 700;
042      public static final String PARAM_LOCALE = "locale";
043    
044      public static final int MAX_HEIGHT = 500;
045      public static final int DEFAULT_WIDTH = 200;
046    
047      public static final int DEFAULT_HEIGHT = 200;
048    
049    
050      private Map<String, String> params;
051    
052      public ChartParameters(Map<String, String> params) {
053        this.params = params;
054      }
055    
056      public ChartParameters(String queryString) {
057        this.params = new HashMap<String, String>();
058        String[] groups = StringUtils.split(queryString, "&");
059        for (String group : groups) {
060          String[] keyval = StringUtils.split(group, "=");
061          params.put(keyval[0], keyval[1]);
062        }
063      }
064    
065      public String getValue(String key) {
066        return getValue(key, "", false);
067      }
068    
069      public String getValue(String key, String defaultValue, boolean decode) {
070        String val = params.get(key);
071        if (decode) {
072          val = decode(val);
073        }
074        if (val == null) {
075          val = defaultValue;
076        }
077        return val;
078      }
079    
080      public String[] getValues(String key, String delimiter) {
081        String value = params.get(key);
082        if (value != null) {
083          return StringUtils.split(value, delimiter);
084        }
085        return EMPTY;
086      }
087    
088      public String[] getValues(String key, String delimiter, boolean decode) {
089        String value = params.get(key);
090        if (value != null) {
091          if (decode) {
092            value = decode(value);
093          }
094          return new StrTokenizer(value, delimiter).setIgnoreEmptyTokens(false).getTokenArray();
095        }
096        return EMPTY;
097      }
098    
099      public int getWidth() {
100        int width = Integer.parseInt(getValue(PARAM_WIDTH, "" + DEFAULT_WIDTH, false));
101        return Math.min(width, MAX_WIDTH);
102      }
103    
104      public int getHeight() {
105        int height = Integer.parseInt(getValue(PARAM_HEIGHT, "" + DEFAULT_HEIGHT, false));
106        return Math.min(height, MAX_HEIGHT);
107      }
108    
109      public Locale getLocale() {
110        String locale = getValue(PARAM_LOCALE);
111        if (StringUtils.isNotBlank(locale)) {
112          return new Locale(locale);
113        }
114        return Locale.ENGLISH;
115      }
116    
117      private String decode(String val) {
118        if (val != null) {
119          try {
120            val = URLDecoder.decode(val, "UTF-8");
121          } catch (UnsupportedEncodingException e) {
122            throw new SonarException("Decoding chart parameter : " + val, e);
123          }
124        }
125        return val;
126      }
127    }