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.*;
7   
8   /**
9    * Created by christoferdutz on 21.09.14.
10   */
11  public abstract class BaseLanguageModule implements Language {
12  
13      protected String name;
14      protected String shortName;
15      protected String terseName;
16      protected Class<?> ruleChainVisitorClass;
17      protected List<String> extensions;
18      protected Map<String, LanguageVersion> versions;
19      protected LanguageVersion defaultVersion;
20  
21      public BaseLanguageModule(String name, String shortName, String terseName, Class<?> ruleChainVisitorClass, String... extensions) {
22          this.name = name;
23          this.shortName = shortName;
24          this.terseName = terseName;
25          this.ruleChainVisitorClass = ruleChainVisitorClass;
26          this.extensions = Arrays.asList(extensions);
27      }
28  
29      protected void addVersion(String version, LanguageVersionHandler languageVersionHandler, boolean isDefault) {
30          if(versions == null) {
31              versions = new HashMap<>();
32          }
33          LanguageVersion languageVersion = new LanguageVersion(this, version, languageVersionHandler);
34          versions.put(version, languageVersion);
35          if(isDefault) {
36              defaultVersion = languageVersion;
37          }
38      }
39  
40      @Override
41      public String getName() {
42          return name;
43      }
44  
45      @Override
46      public String getShortName() {
47          return (shortName != null) ? shortName : name;
48      }
49  
50      @Override
51      public String getTerseName() {
52          return terseName;
53      }
54  
55      @Override
56      public Class<?> getRuleChainVisitorClass() {
57          return ruleChainVisitorClass;
58      }
59  
60      @Override
61      public List<String> getExtensions() {
62          return Collections.unmodifiableList(extensions);
63      }
64  
65      @Override
66      public boolean hasExtension(String extension) {
67          return extensions != null && extensions.contains(extension);
68      }
69  
70      @Override
71      public List<LanguageVersion> getVersions() {
72          return new ArrayList<>(versions.values());
73      }
74  
75      @Override
76      public boolean hasVersion(String version) {
77          return versions != null && versions.containsKey(version);
78      }
79  
80      public LanguageVersion getVersion(String versionName) {
81          if(versions != null) {
82              return versions.get(versionName);
83          }
84          return null;
85      }
86  
87      @Override
88      public LanguageVersion getDefaultVersion() {
89          return defaultVersion;
90      }
91  
92      @Override
93      public String toString() {
94          return "LanguageModule:" + name + "(" + this.getClass().getSimpleName() + ")";
95      }
96  }