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.Collections;
    8   import java.util.List;
    9
    10  import net.sourceforge.pmd.lang.cpp.CppHandler;
    11  import net.sourceforge.pmd.lang.ecmascript.Ecmascript3Handler;
    12  import net.sourceforge.pmd.lang.java.Java13Handler;
    13  import net.sourceforge.pmd.lang.java.Java14Handler;
    14  import net.sourceforge.pmd.lang.java.Java15Handler;
    15  import net.sourceforge.pmd.lang.java.Java16Handler;
    16  import net.sourceforge.pmd.lang.java.Java17Handler;
    17  import net.sourceforge.pmd.lang.jsp.JspHandler;
    18  import net.sourceforge.pmd.lang.xml.XmlHandler;
    19
    20  /**
    21   * This is an enumeration of the Language versions of which PMD is aware.  The
    22   * primary use of a LanguageVersion is for Rules, but they are also used by
    23   * utilities such as CPD.
    24   * <p>
    25   * The following are key components of a LanguageVersion in PMD:
    26   * <ul>
    27   *     <li>Language - The Language with which this version is associated</li>
    28   *     <li>Short name - The common short form of the Language</li>
    29   *     <li>Terse name - The shortest and simplest possible form of the Language
    30   *         name, generally used for Rule configuration</li>
    31   *     <li>Extensions - File extensions associated with the Language</li>
    32   *     <li>Rule Chain Visitor - The RuleChainVisitor implementation used for this
    33   *         Language</li>
    34   *     <li>Versions - The LanguageVersions associated with the Language</li>
    35   * </ul>
    36   *
    37   * @see LanguageVersion
    38   * @see LanguageVersionDiscoverer
    39   */
    40  public enum LanguageVersion {
    41
    42      //ANY(Language.ANY, "", null, true),
    43      //UNKNOWN(Language.UNKNOWN, "", null, true),
    44      CPP(Language.CPP, "", new CppHandler(), true),
    45      FORTRAN(Language.FORTRAN, "", null, true),
    46      ECMASCRIPT(Language.ECMASCRIPT, "3", new Ecmascript3Handler(), true),
    47      JAVA_13(Language.JAVA, "1.3", new Java13Handler(), false),
    48      JAVA_14(Language.JAVA, "1.4", new Java14Handler(), false),
    49      JAVA_15(Language.JAVA, "1.5", new Java15Handler(), false),
    50      JAVA_16(Language.JAVA, "1.6", new Java16Handler(), false),
    51      JAVA_17(Language.JAVA, "1.7", new Java17Handler(), true),
    52      JSP(Language.JSP, "", new JspHandler(), true),
    53      PHP(Language.PHP, "", null, true),
    54      RUBY(Language.RUBY, "", null, true),
    55      XSL(Language.XSL, "", new XmlHandler(), true),
    56      XML(Language.XML, "", new XmlHandler(), true);
    57
    58      private final Language language;
    59      private final String version;
    60      private final LanguageVersionHandler languageVersionHandler;
    61      private final boolean defaultVersion;
    62
    63      /**
    64       * LanguageVersion constructor.  The LanguageVersion will add itself as a
    65       * version of its Language.
    66       *
    67       * @param language The Language of this LanguageVersion.
    68       * @param version The version String for this LanguageVersion.
    69       * Must not be <code>null</code>, but may be an empty String.
    70       * @param languageVersionHandler The LanguageVersionHandler for this
    71       * LanguageVersion.   May be <code>null</code>.
    72       * @param defaultVersion If <code>true</code> then this is the default
    73       * version for the Language, otherwise this is not the default version.
    74       */
    75      private LanguageVersion(Language language, String version, LanguageVersionHandler languageVersionHandler,
    76          boolean defaultVersion) {
    77      if (language == null) {
    78          throw new IllegalArgumentException("Language must not be null.");
    79      }
    80      if (version == null) {
    81          throw new IllegalArgumentException("Version must not be null.");
    82      }
    83      this.language = language;
    84      this.version = version;
    85      this.languageVersionHandler = languageVersionHandler;
    86      this.defaultVersion = defaultVersion;
    87
    88      // Sanity check: There can only be a single default version per Language
    89      if (defaultVersion) {
    90          for (LanguageVersion languageVersion : language.getVersions()) {
    91          if (languageVersion.isDefaultVersion()) {
    92              throw new IllegalArgumentException(languageVersion.getLanguage() + " already has default "
    93                  + languageVersion + ", not " + version);
    94          }
    95          }
    96      }
    97      language.getVersions().add(this);
    98      // Make sure they are sorted (likely already are due to enum initialization order, but just in case)
    99      Collections.sort(language.getVersions());
    100     }
    101
    102     /**
    103      * Get the Language for this LanguageVersion.
    104      * @return The Language for this LanguageVersion.
    105      */
    106     public Language getLanguage() {
    107     return language;
    108     }
    109
    110     /**
    111      * Get the version String for this LanguageVersion.
    112      * @return The version String for this LanguageVersion.
    113      */
    114     public String getVersion() {
    115     return version;
    116     }
    117
    118     /**
    119      * Get the name of this LanguageVersion.  This is Language name
    120      * appended with the LanguageVersion version if not an empty String.
    121      * @return The name of this LanguageVersion.
    122      */
    123     public String getName() {
    124     return version.length() > 0 ? language.getName() + ' ' + version : language.getName();
    125     }
    126
    127     /**
    128      * Get the short name of this LanguageVersion.  This is Language short name
    129      * appended with the LanguageVersion version if not an empty String.
    130      * @return The short name of this LanguageVersion.
    131      */
    132     public String getShortName() {
    133     return version.length() > 0 ? language.getShortName() + ' ' + version : language.getShortName();
    134     }
    135
    136     /**
    137      * Get the terse name of this LanguageVersion.  This is Language terse name
    138      * appended with the LanguageVersion version if not an empty String.
    139      * @return The terse name of this LanguageVersion.
    140      */
    141     public String getTerseName() {
    142     return version.length() > 0 ? language.getTerseName() + ' ' + version : language.getTerseName();
    143     }
    144
    145     /**
    146      * Get the LanguageVersionHandler for this LanguageVersion.
    147      * @return The LanguageVersionHandler for this LanguageVersion.
    148      */
    149     public LanguageVersionHandler getLanguageVersionHandler() {
    150     return languageVersionHandler;
    151     }
    152
    153     /**
    154      * Returns if this LanguageVersion is the default version for the Language.
    155      * @return <code>true</code> if this is the default version for the Language,
    156      * <code>false</code> otherwise.
    157      */
    158     public boolean isDefaultVersion() {
    159     return defaultVersion;
    160     }
    161
    162     /**
    163      * A friendly String form of the LanguageVersion.
    164      */
    165     @Override
    166     public String toString() {
    167     return "LanguageVersion[" + language.getName() + " " + version + ']';
    168     }
    169
    170     /**
    171      * A utility method to find the LanguageVersion associated with the given
    172      * terse name.
    173      * @param terseName The LanguageVersion terse name.
    174      * @return The LanguageVersion with this terse name, <code>null</code> if there is
    175      * no LanguageVersion with this terse name.
    176      */
    177     public static LanguageVersion findByTerseName(String terseName) {
    178     for (LanguageVersion languageVersion : LanguageVersion.values()) {
    179         if (terseName.equals(languageVersion.getTerseName())) {
    180         return languageVersion;
    181         }
    182     }
    183     return null;
    184     }
    185
    186
    187     /**
    188      * A utility method to find the all version associated with the given
    189      * terse name.
    190      * @param languageTerseName The LanguageVersion terse name.
    191      * @return A list of versions associated with the terse name.
    192      */
    193     public static List<LanguageVersion> findVersionsForLanguageTerseName(String languageTerseName) {
    194         List<LanguageVersion> versionsAvailable = new ArrayList<LanguageVersion>(0);
    195         for (LanguageVersion languageVersion : LanguageVersion.values()) {
    196             if (languageVersion.getLanguage().getTerseName().equals(languageTerseName)) {
    197             versionsAvailable.add(languageVersion);
    198             }
    199         }
    200         return versionsAvailable;
    201     }
    202
    203     /**
    204      * A utility method to retrieve the appropriate enum, given the provided parameters
    205      *
    206      * @param languageTerseName The LanguageVersion terse name.
    207      * @param languageVersion The version of the language requested.
    208      * @return A list of versions associated with the terse name.
    209      */
    210     public static LanguageVersion findVersionsForLanguageTerseName(String languageTerseName, String languageVersion) {
    211         List<LanguageVersion> versionsAvailable = findVersionsForLanguageTerseName(languageTerseName);
    212         for ( LanguageVersion version : versionsAvailable ) {
    213         	if ( version.getVersion().equalsIgnoreCase(languageVersion) )
    214         		return version;
    215         }
    216         return null;
    217     }
    218
    219
    220     /**
    221      * Return a comma-separated list of LanguageVersion terse names.
    222      * @param languageVersions The language versions.
    223      * @return Comma-separated terse names.
    224      */
    225     public static String commaSeparatedTerseNames(List<LanguageVersion> languageVersions) {
    226
    227         if (languageVersions == null || languageVersions.isEmpty()) {
    228             return "";
    229         }
    230
    231         StringBuilder builder = new StringBuilder();
    232         builder.append(languageVersions.get(0).getTerseName());
    233         for (int i=1; i<languageVersions.size(); i++) {
    234             builder.append(", ").append(languageVersions.get(i).getTerseName());
    235         }
    236         return builder.toString();
    237     }
    238
    239     /**
    240      * Return the default version for PMD.
    241      *
    242      * @return the proper instance of LanguageVersion
    243      */
    244     public static LanguageVersion getDefaultVersion() {
    245         return LanguageVersion.JAVA_15;
    246     }
    247 }