Clover coverage report - PMD - 3.7
Coverage timestamp: Wed May 31 2006 09:25:59 EDT

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