1 /*** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package net.sourceforge.pmd.cpd; 5 6 import java.io.File; 7 import java.io.IOException; 8 import java.util.HashMap; 9 import java.util.Iterator; 10 import java.util.List; 11 import java.util.Map; 12 13 public class CPD { 14 15 private Map source = new HashMap(); 16 private CPDListener listener = new CPDNullListener(); 17 private Tokens tokens = new Tokens(); 18 private int minimumTileSize; 19 private MatchAlgorithm matchAlgorithm; 20 private Language language; 21 22 public CPD(int minimumTileSize, Language language) { 23 this.minimumTileSize = minimumTileSize; 24 this.language = language; 25 } 26 27 public void setCpdListener(CPDListener cpdListener) { 28 this.listener = cpdListener; 29 } 30 31 public void go() { 32 TokenEntry.clearImages(); 33 matchAlgorithm = new MatchAlgorithm(source, tokens, minimumTileSize, listener); 34 matchAlgorithm.findMatches(); 35 } 36 37 public Iterator getMatches() { 38 return matchAlgorithm.matches(); 39 } 40 41 public void add(File file) throws IOException { 42 add(1, file); 43 } 44 45 public void addAllInDirectory(String dir) throws IOException { 46 addDirectory(dir, false); 47 } 48 49 public void addRecursively(String dir) throws IOException { 50 addDirectory(dir, true); 51 } 52 53 public void add(List files) throws IOException { 54 for (Iterator i = files.iterator(); i.hasNext();) { 55 add(files.size(), (File) i.next()); 56 } 57 } 58 59 private void addDirectory(String dir, boolean recurse) throws IOException { 60 FileFinder finder = new FileFinder(); 61 add(finder.findFilesFrom(dir, language.getFileFilter(), recurse)); 62 } 63 64 private void add(int fileCount, File file) throws IOException { 65 listener.addedFile(fileCount, file); 66 SourceCode sourceCode = new SourceCode(file.getAbsolutePath()); 67 language.getTokenizer().tokenize(sourceCode, tokens); 68 source.put(sourceCode.getFileName(), sourceCode); 69 } 70 71 public static void main(String[] args) { 72 if (args.length >3 || args.length < 2) { 73 usage(); 74 System.exit(1); 75 } 76 77 try { 78 String lang = LanguageFactory.JAVA_KEY; 79 if (args.length == 3) { 80 lang = args[2]; 81 } 82 LanguageFactory f = new LanguageFactory(); 83 Language language = f.createLanguage(lang); 84 CPD cpd = new CPD(Integer.parseInt(args[0]), language); 85 cpd.addRecursively(args[1]); 86 long start = System.currentTimeMillis(); 87 cpd.go(); 88 long total = System.currentTimeMillis() - start; 89 System.out.println(new SimpleRenderer().render(cpd.getMatches())); 90 System.out.println("That took " + total + " milliseconds"); 91 } catch (Exception e) { 92 e.printStackTrace(); 93 } 94 } 95 96 private static void usage() { 97 System.out.println("Usage:"); 98 System.out.println(" java net.sourceforge.pmd.cpd.CPD <tile size> <directory> [<language>]"); 99 System.out.println("i.e: "); 100 System.out.println(" java net.sourceforge.pmd.cpd.CPD 100 c://jdk14//src//java "); 101 System.out.println("or: "); 102 System.out.println(" java net.sourceforge.pmd.cpd.CPD 100 c://apache//src// cpp"); 103 } 104 105 }