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.resources; 021 022 import com.google.common.collect.Maps; 023 import org.apache.commons.lang.ArrayUtils; 024 import org.slf4j.Logger; 025 import org.slf4j.LoggerFactory; 026 import org.sonar.api.BatchComponent; 027 import org.sonar.api.ServerComponent; 028 029 import java.util.ArrayList; 030 import java.util.Arrays; 031 import java.util.Collection; 032 import java.util.List; 033 import java.util.Map; 034 035 /** 036 * A class to store the list of languages 037 * 038 * @since 1.10 039 */ 040 public class Languages implements BatchComponent, ServerComponent { 041 042 private static final Logger LOG = LoggerFactory.getLogger(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 }