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