View Javadoc

    1   package net.sourceforge.pmd;
    2
    3   import java.io.File;
    4   import java.util.HashMap;
    5   import java.util.Map;
    6
    7   /***
    8    * This class can give the SourceType of a source file.
    9    *
    10   * @author pieter_van_raemdonck - Application Engineers NV/SA - www.ae.be
    11   */
    12  public class SourceTypeDiscoverer {
    13
    14      /***
    15       * Map of (upper-case) file name extensions on the appropriate SourceType
    16       * object.
    17       */
    18      private Map mapExtensionOnSourceType = new HashMap();
    19
    20      /***
    21       * Public constructor.
    22       */
    23      public SourceTypeDiscoverer() {
    24          initialize();
    25      }
    26
    27      /***
    28       * Initialization of mapExtensionOnSourceType.
    29       */
    30      private void initialize() {
    31          mapExtensionOnSourceType.put(SourceFileConstants.JSP_EXTENSION_UPPERCASE, SourceType.JSP);
    32          mapExtensionOnSourceType.put(SourceFileConstants.JSPX_EXTENSION_UPPERCASE, SourceType.JSP);
    33
    34          // TODO: Do we want a default ??
    35          mapExtensionOnSourceType.put(SourceFileConstants.JAVA_EXTENSION_UPPERCASE, SourceType.JAVA_14);
    36      }
    37
    38      /***
    39       * Get the SourceType of a given source file.
    40       *
    41       * @param sourceFile The File
    42       * @return a SourceType
    43       */
    44      public SourceType getSourceTypeOfFile(File sourceFile) {
    45          String fileName = sourceFile.getName();
    46          return getSourceTypeOfFile(fileName);
    47      }
    48
    49      /***
    50       * Get the SourceType of a source file with given name.
    51       *
    52       * @param fileName The File
    53       * @return a SourceType ; null if the fileName is not recognized as a supported source type.
    54       */
    55      public SourceType getSourceTypeOfFile(String fileName) {
    56          SourceType sourceType = null;
    57
    58          int extensionIndex = 1 + fileName.lastIndexOf('.');
    59          if (extensionIndex > 0) {
    60              String extensionUppercase = fileName.substring(extensionIndex).toUpperCase();
    61
    62              sourceType = (SourceType) mapExtensionOnSourceType
    63                      .get(extensionUppercase);
    64          }
    65
    66          return sourceType;
    67      }
    68
    69      /***
    70       * Set the SourceType of files with ".java" extension. This chooses the Java
    71       * version.
    72       *
    73       * @param sourceType the wanted SourceType
    74       */
    75      public void setSourceTypeOfJavaFiles(SourceType sourceType) {
    76          mapExtensionOnSourceType.put(SourceFileConstants.JAVA_EXTENSION_UPPERCASE, sourceType);
    77      }
    78
    79      public SourceType getSourceTypeOfJavaFiles() {
    80          return (SourceType) mapExtensionOnSourceType.get(SourceFileConstants.JAVA_EXTENSION_UPPERCASE);
    81      }
    82  }