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