001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 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.resources;
021
022import com.google.common.collect.Maps;
023import org.apache.commons.lang.ArrayUtils;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.sonar.api.BatchComponent;
027import org.sonar.api.ServerComponent;
028
029import java.util.ArrayList;
030import java.util.Arrays;
031import java.util.List;
032import java.util.Map;
033
034/**
035 * A class to store the list of languages
036 *
037 * @since 1.10
038 */
039public class Languages implements BatchComponent, ServerComponent {
040
041  private static final Logger LOG = LoggerFactory.getLogger(Languages.class);
042
043  private final Map<String, Language> map = Maps.newLinkedHashMap();
044
045  /**
046   * Creates a list of languages
047   */
048  public Languages(Language... languages) {
049    LOG.debug("Available languages:");
050    for (Language language : languages) {
051      map.put(language.getKey(), language);
052      LOG.debug("  * " + language.getName() + " => \"" + language.getKey() + "\"");
053    }
054  }
055
056  /**
057   * No languages are installed
058   */
059  public Languages() {
060    LOG.debug("No language available");
061  }
062
063  /**
064   * @param keys the languages keys
065   * @return the list of suffix files associates to languages included in the current object
066   */
067  public String[] getSuffixes(String... keys) {
068    List<String> suffixes = new ArrayList<String>();
069
070    for (Map.Entry<String, Language> entry : map.entrySet()) {
071      if (ArrayUtils.isEmpty(keys) || ArrayUtils.contains(keys, entry.getKey())) {
072        suffixes.addAll(Arrays.asList(entry.getValue().getFileSuffixes()));
073      }
074    }
075    return suffixes.toArray(new String[suffixes.size()]);
076  }
077
078  /**
079   * Return a language from the current object based on its key
080   */
081  public Language get(String key) {
082    return map.get(key);
083  }
084
085  /**
086   * Adds a language to the current object
087   */
088  public void add(Language language) {
089    map.put(language.getKey(), language);
090  }
091
092}