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.gwt;
021    
022    import com.google.gwt.i18n.client.Dictionary;
023    
024    import java.util.Set;
025    
026    public final class Configuration {
027    
028      private Configuration() {
029        // only static methods
030      }
031    
032      public static String getSonarVersion() {
033        return getParameter("version");
034      }
035    
036      /**
037       * Get the id of the selected resource. Can be null if none resource is selected.
038       */
039      public static String getResourceId() {
040        return getParameter("resource_key");
041      }
042    
043      public static String getParameter(String key) {
044        return getParameter(key, null);
045      }
046    
047      public static String getParameter(String key, String defaultValue) {
048        String result = getDictionaryEntry("config", key);
049        if (result == null) {
050          result = defaultValue;
051        }
052        return result;
053      }
054    
055      public static native void setParameter(String key, String val)  /*-{
056        $wnd.config[key] = val;
057      }-*/;
058    
059      public static String getRequestParameter(String key) {
060        return getDictionaryEntry("rp", key);
061      }
062    
063      public static Set<String> getParameterKeys() {
064        return getDictionaryKeys("config");
065      }
066    
067      public static Set<String> getRequestParameterKeys() {
068        return getDictionaryKeys("rp");
069      }
070    
071      private static String getDictionaryEntry(String dictionaryName, String key) {
072        try {
073          Dictionary dic = Dictionary.getDictionary(dictionaryName);
074          if (dic != null) {
075            return dic.get(key);
076          }
077          return null;
078    
079        } catch (Exception e) {
080          return null;
081        }
082      }
083    
084      private static Set<String> getDictionaryKeys(String dictionaryName) {
085        Dictionary dic = Dictionary.getDictionary(dictionaryName);
086        if (dic != null) {
087          return dic.keySet();
088        }
089        return null;
090      }
091    
092    }