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