View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang;
5   
6   import java.util.ArrayList;
7   import java.util.Collection;
8   import java.util.HashMap;
9   import java.util.List;
10  import java.util.Map;
11  import java.util.ServiceLoader;
12  
13  /**
14   * Created by christoferdutz on 20.09.14.
15   */
16  public final class LanguageRegistry {
17  
18      private static LanguageRegistry instance = new LanguageRegistry();
19  
20      private Map<String, Language> languages;
21  
22      private LanguageRegistry() {
23          languages = new HashMap<>();
24          ServiceLoader<Language> languageLoader = ServiceLoader.load(Language.class);
25          for (Language language : languageLoader) {
26              languages.put(language.getName(), language);
27          }
28      }
29  
30      public static LanguageRegistry getInstance() {
31          return instance;
32      }
33  
34      public static Collection<Language> getLanguages() {
35          return getInstance().languages.values();
36      }
37  
38      public static Language getLanguage(String languageName) {
39          return getInstance().languages.get(languageName);
40      }
41  
42      public static Language getDefaultLanguage() {
43          return getLanguage("Java");
44      }
45  
46      public static Language findLanguageByTerseName(String terseName) {
47          for (Language language : getInstance().languages.values()) {
48              if (language.getTerseName().equals(terseName)) {
49                  return language;
50              }
51          }
52          return null;
53      }
54  
55      public static LanguageVersion findLanguageVersionByTerseName(String terseNameAndVersion) {
56          String version;
57          String terseName;
58          if (terseNameAndVersion.contains(" ")) {
59              version = terseNameAndVersion.substring(terseNameAndVersion.lastIndexOf(' ') + 1);
60              terseName = terseNameAndVersion.substring(0, terseNameAndVersion.lastIndexOf(' '));
61          } else {
62              version = null;
63              terseName = terseNameAndVersion;
64          }
65          Language language = findLanguageByTerseName(terseName);
66          if (language != null) {
67              if (version == null) {
68                  return language.getDefaultVersion();
69              } else {
70                  return language.getVersion(version);
71              }
72          }
73          return null;
74      }
75  
76      public static List<Language> findByExtension(String extension) {
77          List<Language> languages = new ArrayList<>();
78          for (Language language : getInstance().languages.values()) {
79              if (language.hasExtension(extension)) {
80                  languages.add(language);
81              }
82          }
83          return languages;
84      }
85  
86      public static List<LanguageVersion> findAllVersions() {
87          List<LanguageVersion> versions = new ArrayList<>();
88          for (Language language : getLanguages()) {
89              for (LanguageVersion languageVersion : language.getVersions()) {
90                  versions.add(languageVersion);
91              }
92          }
93          return versions;
94      }
95  
96      /**
97       * A utility method to find the Languages which have Rule support.
98       * 
99       * @return A List of Languages with Rule support.
100      */
101     public static List<Language> findWithRuleSupport() {
102         List<Language> languages = new ArrayList<>();
103         for (Language language : getInstance().languages.values()) {
104             if (language.getRuleChainVisitorClass() != null) {
105                 languages.add(language);
106             }
107         }
108         return languages;
109     }
110 
111     public static String commaSeparatedTerseNamesForLanguage(List<Language> languages) {
112         StringBuilder builder = new StringBuilder();
113         for (Language language : languages) {
114             if (builder.length() > 0) {
115                 builder.append(", ");
116             }
117             builder.append(language.getTerseName());
118         }
119         return builder.toString();
120     }
121 
122     public static String commaSeparatedTerseNamesForLanguageVersion(List<LanguageVersion> languageVersions) {
123         if (languageVersions == null || languageVersions.isEmpty()) {
124             return "";
125         }
126 
127         StringBuilder builder = new StringBuilder();
128         builder.append(languageVersions.get(0).getTerseName());
129         for (int i = 1; i < languageVersions.size(); i++) {
130             builder.append(", ").append(languageVersions.get(i).getTerseName());
131         }
132         return builder.toString();
133     }
134 
135 }