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.io.File;
    7   import java.util.HashMap;
    8   import java.util.List;
    9   import java.util.Map;
    10
    11  /**
    12   * This class can discover the LanguageVersion of a source file.  Further, every
    13   * Language has a default LanguageVersion, which can be temporarily overridden
    14   * here.
    15   */
    16  public class LanguageVersionDiscoverer {
    17      private Map<Language, LanguageVersion> languageToLanguageVersion = new HashMap<Language, LanguageVersion>();
    18
    19      /**
    20       * Set the given LanguageVersion as the current default for it's Language.
    21       * @param languageVersion The new default for the Language.
    22       * @return The previous default version for the language.
    23       */
    24      public LanguageVersion setDefaultLanguageVersion(LanguageVersion languageVersion) {
    25  	LanguageVersion currentLanguageVersion = languageToLanguageVersion.put(languageVersion.getLanguage(),
    26  		languageVersion);
    27  	if (currentLanguageVersion == null) {
    28  	    currentLanguageVersion = languageVersion.getLanguage().getDefaultVersion();
    29  	}
    30  	return currentLanguageVersion;
    31      }
    32
    33      /**
    34       * Get the current default LanguageVersion for the given Language.
    35       * @param language The Language.
    36       * @return The current default version for the language.
    37       */
    38      public LanguageVersion getDefaultLanguageVersion(Language language) {
    39  	LanguageVersion languageVersion = languageToLanguageVersion.get(language);
    40  	if (languageVersion == null) {
    41  	    languageVersion = language.getDefaultVersion();
    42  	}
    43  	return languageVersion;
    44      }
    45
    46      /**
    47       * Get the default LanguageVersion for the first Language of a given source file.
    48       *
    49       * @param sourceFile The file.
    50       * @return The currently configured LanguageVersion for the source file,
    51       * or <code>null</code> if there are no supported Languages for the file.
    52       */
    53      public LanguageVersion getDefaultLanguageVersionForFile(File sourceFile) {
    54  	return getDefaultLanguageVersionForFile(sourceFile.getName());
    55      }
    56
    57      /**
    58       * Get the LanguageVersion for the first Language of a source file
    59       * with the given name.
    60       *
    61       * @param fileName The file name.
    62       * @return The currently configured LanguageVersion for the source file
    63       * or <code>null</code> if there are no supported Languages for the file.
    64       */
    65      public LanguageVersion getDefaultLanguageVersionForFile(String fileName) {
    66  	List<Language> languages = getLanguagesForFile(fileName);
    67  	LanguageVersion languageVersion = null;
    68  	if (!languages.isEmpty()) {
    69  	    languageVersion = getDefaultLanguageVersion(languages.get(0));
    70  	}
    71  	return languageVersion;
    72      }
    73
    74      /**
    75       * Get the Languages of a given source file.
    76       *
    77       * @param sourceFile The file.
    78       * @return The Languages for the source file, may be empty.
    79       */
    80      public List<Language> getLanguagesForFile(File sourceFile) {
    81  	return getLanguagesForFile(sourceFile.getName());
    82      }
    83
    84      /**
    85       * Get the Languages of a given source file.
    86       *
    87       * @param fileName The file name.
    88       * @return The Languages for the source file, may be empty.
    89       */
    90      public List<Language> getLanguagesForFile(String fileName) {
    91  	String extension = getExtension(fileName);
    92  	return Language.findByExtension(extension);
    93      }
    94
    95      // Get the extensions from a file
    96      private String getExtension(String fileName) {
    97  	String extension = null;
    98  	int extensionIndex = 1 + fileName.lastIndexOf('.');
    99  	if (extensionIndex > 0) {
    100 	    extension = fileName.substring(extensionIndex);
    101 	}
    102 	return extension;
    103     }
    104 }