View Javadoc

    1   package net.sourceforge.pmd.cpd;
    2
    3   import java.io.File;
    4   import java.io.FilenameFilter;
    5
    6   import net.sourceforge.pmd.AbstractConfiguration;
    7
    8   /**
    9    *
    10   * @author Brian Remedios
    11   */
    12  public class CPDConfiguration extends AbstractConfiguration {
    13
    14      private Language language;
    15      private int minimumTileSize;
    16      private boolean skipDuplicates;
    17      private Renderer renderer;
    18
    19  	private static final int MISSING_REQUIRED_ARGUMENT = 3;
    20
    21      public CPDConfiguration(String[] args) {
    22
    23      	 String languageString = findOptionalStringValue(args, "--language", "java");
    24      	 language = new LanguageFactory().createLanguage(languageString);
    25
    26           String formatString = findOptionalStringValue(args, "--format", "text");
    27           renderer = getRendererFromString(formatString);
    28
    29           final String systemDefaultEncoding = System.getProperty("file.encoding");
    30           setEncoding( findOptionalStringValue(args, "--encoding", systemDefaultEncoding) );
    31
    32           minimumTileSize = Integer.parseInt(findRequiredStringValue(args, "--minimum-tokens"));
    33
    34           skipDuplicates = findBooleanSwitch(args, "--skip-duplicate-files");
    35      }
    36
    37  	public CPDConfiguration(int theMinTileSize, Language theLanguage, String theEncoding) {
    38  		minimumTileSize = theMinTileSize;
    39  		language = theLanguage;
    40  		setEncoding(theEncoding);
    41  	}
    42
    43      public void setEncoding(String theEncoding) {
    44          super.setSourceEncoding(theEncoding);
    45
    46          if ( ! theEncoding.equals( System.getProperty("file.encoding") ) )
    47          	 System.setProperty("file.encoding", theEncoding);
    48      }
    49
    50      public SourceCode sourceCodeFor(File file) {
    51      	return new SourceCode(
    52          	new SourceCode.FileCodeLoader(file, getSourceEncoding())
    53  			);
    54      }
    55
    56      public Language language() { return language; }
    57
    58      public int minimumTileSize() { return minimumTileSize; }
    59
    60      public boolean skipDuplicates() { return skipDuplicates; }
    61
    62      public void skipDuplicates(boolean flag) { skipDuplicates = flag; }
    63
    64      public FilenameFilter filenameFilter() { return language.getFileFilter(); }
    65
    66      public Tokenizer tokenizer() { return language.getTokenizer(); }
    67
    68      public Renderer renderer() { return renderer; }
    69
    70      public static Renderer getRendererFromString(String name /*, String encoding*/) {
    71          if (name.equalsIgnoreCase("text") || name.equals("")) {
    72              return new SimpleRenderer();
    73          } else if ("xml".equals(name)) {
    74              return new XMLRenderer();
    75          }  else if ("csv".equals(name)) {
    76              return new CSVRenderer();
    77          }  else if ("vs".equals(name)) {
    78              return new VSRenderer();
    79          }
    80          try {
    81              return (Renderer) Class.forName(name).newInstance();
    82          } catch (Exception e) {
    83              System.out.println("Can't find class '" + name + "', defaulting to SimpleRenderer.");
    84          }
    85          return new SimpleRenderer();
    86      }
    87
    88      public static boolean findBooleanSwitch(String[] args, String name) {
    89          for (int i = 0; i < args.length; i++) {
    90              if (args[i].equals(name)) {
    91                  return true;
    92              }
    93          }
    94          return false;
    95      }
    96
    97      public static String findOptionalStringValue(String[] args, String name, String defaultValue) {
    98          for (int i = 0; i < args.length; i++) {
    99              if (args[i].equals(name)) {
    100                 return args[i + 1];
    101             }
    102         }
    103         return defaultValue;
    104     }
    105
    106     private static String findRequiredStringValue(String[] args, String name) {
    107         for (int i = 0; i < args.length; i++) {
    108             if (args[i].equals(name)) {
    109                 return args[i + 1];
    110             }
    111         }
    112         System.out.println("No " + name + " value passed in");
    113         CPD.showUsage();
    114         System.exit(MISSING_REQUIRED_ARGUMENT);
    115         return "";
    116     }
    117 }