getting ready to check in new CPD

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@554 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-07-30 19:34:26 +00:00
parent fee87034f2
commit 13e7d11dd2

View File

@ -1,225 +0,0 @@
/*
* User: tom
* Date: Jul 26, 2002
* Time: 8:43:19 PM
*/
package net.sourceforge.pmd;
import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.*;
// this class will go away as soon
// as this spike is over
// i didn't want to create a new directory though
// since i'm not sure if this will work out
public class CPD {
public static class JavaFileOrDirectoryFilter implements FilenameFilter {
public boolean accept(File dir, String filename) {
return filename.endsWith("java") || (new File(dir.getAbsolutePath() + System.getProperty("file.separator") + filename).isDirectory());
}
}
public static class TokenPtr {
private int location;
private File file;
private StringBuffer fileContents;
public TokenPtr(File file, StringBuffer fileContents, int location) {
this.file = file;
this.location = location;
this.fileContents = fileContents;
}
public boolean nextTokenAvailable(int imageLength) {
return (location+imageLength+1) < fileContents.length();
}
public String getNextCharacter(int imageLength) {
return String.valueOf(fileContents.charAt(location + imageLength));
}
}
public static class TokenTable {
// String->List (TokenPtr, TokenPtr, TokenPtr)
private Map tokens = new HashMap();
private int minimumTokenSize;
private Set tokensFound = new HashSet();
private boolean stopAnyway = false;
private int lastTokenCount;
public TokenTable(int minimumTokenSize) {
this.minimumTokenSize = minimumTokenSize;
}
public void add(File file, StringBuffer code) {
for (int i=0; i<code.length(); i++) {
String image = String.valueOf(code.charAt(i));
TokenPtr thisToken = new TokenPtr(file, code, i);
if (tokens.containsKey(image)) {
List tokenPtrs = (List)tokens.get(image);
tokenPtrs.add(thisToken);
} else {
List tokenPtrs = new ArrayList();
tokenPtrs.add(thisToken);
tokens.put(image, tokenPtrs);
}
}
}
public void frobnicate() {
while (!tokens.isEmpty() && !stopAnyway) {
deleteSoloTokens();
combineTokens();
}
}
private void combineTokens() {
System.out.println("tokens.size() = " + tokens.size());
Map newTokenPtrs = new HashMap();
for (Iterator i=tokens.keySet().iterator(); i.hasNext();) {
String image = (String)i.next();
List tokenPtrs = (List)tokens.get(image);
for (Iterator j = tokenPtrs.iterator(); j.hasNext();) {
TokenPtr tokenPtr = (TokenPtr)j.next();
if (tokenPtr.nextTokenAvailable(image.length())) {
String newImage = image + tokenPtr.getNextCharacter(image.length());
if (newTokenPtrs.containsKey(newImage)) {
List list = (List)newTokenPtrs.get(newImage);
list.add(tokenPtr);
} else {
List list = new ArrayList();
list.add(tokenPtr);
newTokenPtrs.put(newImage, list);
}
if (newImage.length() > minimumTokenSize) {
List list = (List)newTokenPtrs.get(newImage);
if (list.size() > 1) {
tokensFound.add(newImage);
}
}
}
}
}
tokens = newTokenPtrs;
// there must be a better way to halt
if (lastTokenCount == tokens.size()) {
stopAnyway = true;
}
lastTokenCount = tokens.size();
}
private void deleteSoloTokens() {
for (Iterator i=tokens.keySet().iterator(); i.hasNext();) {
String image = (String)i.next();
List tokenPtrs = (List)tokens.get(image);
if (tokenPtrs.size() == 1) {
i.remove();
}
}
}
public int getTokenCount() {
return tokens.size();
}
public Set getTokensFound() {
return this.tokensFound;
}
}
private Map files = new HashMap();
private TokenTable tokenTable;
public CPD(int minimumFragmentSize) {
tokenTable = new TokenTable(minimumFragmentSize);
}
public void addFile(File file) {
files.put(file, null);
}
public void addFiles(List newFiles) {
System.out.println("Adding " + newFiles.size() + " files");
for (Iterator i = newFiles.iterator(); i.hasNext();) {
files.put(i.next(), null);
}
}
public void loadFiles() throws IOException {
for (Iterator i = files.keySet().iterator(); i.hasNext();) {
File file = (File)i.next();
files.put(file, load(file));
}
}
public void buildTokenTable() {
for (Iterator i = files.keySet().iterator(); i.hasNext();) {
File file = (File)i.next();
StringBuffer contents = (StringBuffer)files.get(file);
tokenTable.add(file, contents);
}
}
public void frobnicate() {
tokenTable.frobnicate();
for (Iterator i = tokenTable.getTokensFound().iterator(); i.hasNext();) {
System.out.println(i.next());
}
}
private StringBuffer load(File file) throws IOException {
FileReader fr = new FileReader(file);
StringBuffer sb = new StringBuffer();
int c = 0;
while ((c = fr.read()) != -1) {
sb.append((char)c);
}
fr.close();
return sb;
}
public static void main(String[] args) {
CPD cpd = new CPD(15);
cpd.addFiles(findFilesRecursively("C:\\data\\pmd\\pmd\\test-data\\"));
try {
cpd.loadFiles();
} catch (IOException ioe) {
ioe.printStackTrace();
return;
}
cpd.buildTokenTable();
cpd.frobnicate();
}
private static List findFilesRecursively(String dir) {
File root = new File(dir);
List list = new ArrayList();
scanDirectory(root, list, true);
return list;
}
private static void scanDirectory(File dir, List list, boolean recurse) {
FilenameFilter filter = new JavaFileOrDirectoryFilter();
String[] possibles = dir.list(filter);
for (int i=0; i<possibles.length; i++) {
File tmp = new File(dir + System.getProperty("file.separator") + possibles[i]);
if (recurse && tmp.isDirectory()) {
scanDirectory(tmp, list, true);
} else {
list.add(new File(dir + System.getProperty("file.separator") + possibles[i]));
}
}
}
}