001: package org.acm.seguin.pmd.cpd;
002:
003: import java.io.File;
004: import java.io.FileReader;
005: import java.io.IOException;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Map;
010:
011: public class CPD {
012: private Map source = new HashMap();
013: private CPDListener listener = new CPDNullListener();
014: private Tokens tokens = new Tokens();
015: private int minimumTileSize;
016: private MatchAlgorithm matchAlgorithm;
017: private Language language;
018:
019: public CPD(int minimumTileSize, Language language) {
020: this .minimumTileSize = minimumTileSize;
021: this .language = language;
022: }
023:
024: public void setCpdListener(CPDListener cpdListener) {
025: this .listener = cpdListener;
026: }
027:
028: public void go() {
029: matchAlgorithm = new MatchAlgorithm(source, tokens);
030: matchAlgorithm.setListener(listener);
031: matchAlgorithm.findMatches(minimumTileSize);
032: }
033:
034: public Iterator getMatches() {
035: return matchAlgorithm.matches();
036: }
037:
038: public void add(File file) throws IOException {
039: add(1, file);
040: }
041:
042: public void addAllInDirectory(String dir) throws IOException {
043: addDirectory(dir, false);
044: }
045:
046: public void addRecursively(String dir) throws IOException {
047: addDirectory(dir, true);
048: }
049:
050: public void add(List files) throws IOException {
051: for (Iterator i = files.iterator(); i.hasNext();) {
052: add(files.size(), (File) i.next());
053: }
054: }
055:
056: private void addDirectory(String dir, boolean recurse)
057: throws IOException {
058: FileFinder finder = new FileFinder();
059: add(finder
060: .findFilesFrom(dir, language.getFileFilter(), recurse));
061: }
062:
063: private void add(int fileCount, File file) throws IOException {
064: listener.addedFile(fileCount, file);
065: SourceCode sourceCode = new SourceCode(file.getAbsolutePath());
066: FileReader reader = new FileReader(file);
067: language.getTokenizer().tokenize(sourceCode, tokens, reader);
068: reader.close();
069: source.put(sourceCode.getFileName(), sourceCode);
070: }
071:
072: public static void main(String[] args) {
073: if (args.length > 3 || args.length < 2) {
074: usage();
075: System.exit(1);
076: }
077:
078: try {
079: String lang = LanguageFactory.JAVA_KEY;
080: if (args.length == 3) {
081: lang = args[2];
082: }
083: LanguageFactory f = new LanguageFactory();
084: Language language = f.createLanguage(lang);
085: CPD cpd = new CPD(Integer.parseInt(args[0]), language);
086: cpd.addRecursively(args[1]);
087: long start = System.currentTimeMillis();
088: cpd.go();
089: long total = System.currentTimeMillis() - start;
090: System.out.println(new SimpleRenderer().render(cpd
091: .getMatches()));
092: System.out.println("That took " + total + " milliseconds");
093: } catch (Exception e) {
094: e.printStackTrace();
095: }
096: }
097:
098: private static void usage() {
099: System.out.println("Usage:");
100: System.out
101: .println(" java org.acm.seguin.pmd.cpd.CPD <tile size> <directory> [<language>]");
102: System.out.println("i.e: ");
103: System.out
104: .println(" java org.acm.seguin.pmd.cpd.CPD 100 c:\\jdk14\\src\\java ");
105: System.out.println("or: ");
106: System.out
107: .println(" java org.acm.seguin.pmd.cpd.CPD 100 c:\\apache\\src\\ cpp");
108: }
109:
110: }
|