Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 179 |  | Methods: | 17
| NCLOC: | 148 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
CPD.java | 0% | 0% | 0% | 0% |

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.FileNotFoundException;
8 |  | import java.io.IOException;
9 |  | import java.util.HashMap;
10 |  | import java.util.HashSet;
11 |  | import java.util.Iterator;
12 |  | import java.util.List;
13 |  | import java.util.Map;
14 |  | import java.util.Set;
15 |  |
16 |  | public class CPD {
17 |  |
18 |  | private Map source = new HashMap();
19 |  | private CPDListener listener = new CPDNullListener();
20 |  | private Tokens tokens = new Tokens();
21 |  | private int minimumTileSize;
22 |  | private MatchAlgorithm matchAlgorithm;
23 |  | private Language language;
24 |  | private boolean skipDuplicates;
25 |  |
26 | 0 | public CPD(int minimumTileSize, Language language) {
27 | 0 | this.minimumTileSize = minimumTileSize;
28 | 0 | this.language = language;
29 |  | }
30 |  |
31 | 0 | public void skipDuplicates() {
32 | 0 | this.skipDuplicates = true;
33 |  | }
34 |  |
35 | 0 | public void setCpdListener(CPDListener cpdListener) {
36 | 0 | this.listener = cpdListener;
37 |  | }
38 |  |
39 | 0 | public void go() {
40 | 0 | TokenEntry.clearImages();
41 | 0 | matchAlgorithm = new MatchAlgorithm(source, tokens, minimumTileSize, listener);
42 | 0 | matchAlgorithm.findMatches();
43 |  | }
44 |  |
45 | 0 | public Iterator getMatches() {
46 | 0 | return matchAlgorithm.matches();
47 |  | }
48 |  |
49 | 0 | public void add(File file) throws IOException {
50 | 0 | add(1, file);
51 |  | }
52 |  |
53 | 0 | public void addAllInDirectory(String dir) throws IOException {
54 | 0 | addDirectory(dir, false);
55 |  | }
56 |  |
57 | 0 | public void addRecursively(String dir) throws IOException {
58 | 0 | addDirectory(dir, true);
59 |  | }
60 |  |
61 | 0 | public void add(List files) throws IOException {
62 | 0 | for (Iterator i = files.iterator(); i.hasNext();) {
63 | 0 | add(files.size(), (File) i.next());
64 |  | }
65 |  | }
66 |  |
67 | 0 | private void addDirectory(String dir, boolean recurse) throws IOException {
68 | 0 | if (!(new File(dir)).exists()) {
69 | 0 | throw new FileNotFoundException("Couldn't find directory " + dir);
70 |  | }
71 | 0 | FileFinder finder = new FileFinder();
72 |  | // TODO - could use SourceFileSelector here
73 | 0 | add(finder.findFilesFrom(dir, language.getFileFilter(), recurse));
74 |  | }
75 |  |
76 |  | private Set current = new HashSet();
77 |  |
78 | 0 | private void add(int fileCount, File file) throws IOException {
79 |  |
80 | 0 | if (skipDuplicates) {
81 |  | // TODO refactor this thing into a separate class
82 | 0 | String signature = file.getName() + '_' + file.length();
83 | 0 | if (current.contains(signature)) {
84 | 0 | System.out.println("Skipping " + file.getAbsolutePath() + " since it appears to be a duplicate file and --skip-duplicate-files is set");
85 | 0 | return;
86 |  | }
87 | 0 | current.add(signature);
88 |  | }
89 |  |
90 | 0 | if (!file.getCanonicalPath().equals(file.getAbsolutePath())) {
91 | 0 | System.out.println("Skipping " + file + " since it appears to be a symlink");
92 | 0 | return;
93 |  | }
94 |  |
95 | 0 | listener.addedFile(fileCount, file);
96 | 0 | SourceCode sourceCode = new SourceCode(new SourceCode.FileCodeLoader(file));
97 | 0 | language.getTokenizer().tokenize(sourceCode, tokens);
98 | 0 | source.put(sourceCode.getFileName(), sourceCode);
99 |  | }
100 |  |
101 | 0 | public static Renderer getRendererFromString(String name) {
102 | 0 | if (name.equalsIgnoreCase("text") || name.equals("")) {
103 | 0 | return new SimpleRenderer();
104 |  | }
105 | 0 | try {
106 | 0 | return (Renderer) Class.forName(name).newInstance();
107 |  | } catch (Exception e) {
108 | 0 | System.out.println("Can't find class '" + name + "', defaulting to SimpleRenderer.");
109 |  | }
110 | 0 | return new SimpleRenderer();
111 |  | }
112 |  |
113 | 0 | private static boolean findBooleanSwitch(String[] args, String name) {
114 | 0 | for (int i = 0; i < args.length; i++) {
115 | 0 | if (args[i].equals(name)) {
116 | 0 | return true;
117 |  | }
118 |  | }
119 | 0 | return false;
120 |  | }
121 |  |
122 | 0 | private static String findRequiredStringValue(String[] args, String name) {
123 | 0 | for (int i = 0; i < args.length; i++) {
124 | 0 | if (args[i].equals(name)) {
125 | 0 | return args[i + 1];
126 |  | }
127 |  | }
128 | 0 | System.out.println("No " + name + " value passed in");
129 | 0 | usage();
130 | 0 | throw new RuntimeException();
131 |  | }
132 |  |
133 | 0 | private static String findOptionalStringValue(String[] args, String name, String defaultValue) {
134 | 0 | for (int i = 0; i < args.length; i++) {
135 | 0 | if (args[i].equals(name)) {
136 | 0 | return args[i + 1];
137 |  | }
138 |  | }
139 | 0 | return defaultValue;
140 |  | }
141 |  |
142 | 0 | public static void main(String[] args) {
143 | 0 | if (args.length == 0) {
144 | 0 | usage();
145 |  | }
146 |  |
147 | 0 | try {
148 | 0 | boolean skipDuplicateFiles = findBooleanSwitch(args, "--skip-duplicate-files");
149 | 0 | String pathToFiles = findRequiredStringValue(args, "--files");
150 | 0 | String languageString = findOptionalStringValue(args, "--language", "java");
151 | 0 | String formatString = findOptionalStringValue(args, "--format", "text");
152 | 0 | int minimumTokens = Integer.parseInt(findRequiredStringValue(args, "--minimum-tokens"));
153 | 0 | LanguageFactory f = new LanguageFactory();
154 | 0 | Language language = f.createLanguage(languageString);
155 | 0 | Renderer renderer = CPD.getRendererFromString(formatString);
156 | 0 | CPD cpd = new CPD(minimumTokens, language);
157 | 0 | if (skipDuplicateFiles) {
158 | 0 | cpd.skipDuplicates();
159 |  | }
160 | 0 | cpd.addRecursively(pathToFiles);
161 | 0 | cpd.go();
162 | 0 | System.out.println(renderer.render(cpd.getMatches()));
163 |  | } catch (Exception e) {
164 | 0 | e.printStackTrace();
165 |  | }
166 |  | }
167 |  |
168 | 0 | private static void usage() {
169 | 0 | System.out.println("Usage:");
170 | 0 | System.out.println(" java net.sourceforge.pmd.cpd.CPD --minimum-tokens xxx --files xxx [--language xxx] [--format (xml|text|csv)] [--skip-duplicate-files] ");
171 | 0 | System.out.println("i.e: ");
172 | 0 | System.out.println(" java net.sourceforge.pmd.cpd.CPD --minimum-tokens 100 --files c:\\jdk14\\src\\java ");
173 | 0 | System.out.println("or: ");
174 | 0 | System.out.println(" java net.sourceforge.pmd.cpd.CPD --minimum-tokens 100 --files /path/to/c/code --language c ");
175 | 0 | System.out.println("or: ");
176 | 0 | System.out.println(" java net.sourceforge.pmd.cpd.CPD --minimum-tokens 100 --files /path/to/java/code --format xml");
177 |  | }
178 |  |
179 |  | }