1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.cpd; |
5 |
| |
6 |
| import org.apache.tools.ant.BuildException; |
7 |
| import org.apache.tools.ant.DirectoryScanner; |
8 |
| import org.apache.tools.ant.Project; |
9 |
| import org.apache.tools.ant.Task; |
10 |
| import org.apache.tools.ant.types.EnumeratedAttribute; |
11 |
| import org.apache.tools.ant.types.FileSet; |
12 |
| |
13 |
| import java.io.File; |
14 |
| import java.io.IOException; |
15 |
| import java.util.ArrayList; |
16 |
| import java.util.Iterator; |
17 |
| import java.util.List; |
18 |
| import java.util.Properties; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class CPDTask extends Task { |
39 |
| |
40 |
| private static final String TEXT_FORMAT = "text"; |
41 |
| private static final String XML_FORMAT = "xml"; |
42 |
| private static final String CSV_FORMAT = "csv"; |
43 |
| |
44 |
| private String format = TEXT_FORMAT; |
45 |
| private int minimumTokenCount; |
46 |
| private boolean ignoreLiterals; |
47 |
| private boolean ignoreIdentifiers; |
48 |
| private File outputFile; |
49 |
| private List filesets = new ArrayList(); |
50 |
| |
51 |
0
| public void execute() throws BuildException {
|
52 |
0
| try {
|
53 |
0
| validateFields();
|
54 |
| |
55 |
0
| log("Tokenizing files", Project.MSG_INFO);
|
56 |
0
| Properties p = new Properties();
|
57 |
0
| if (ignoreLiterals) {
|
58 |
0
| p.setProperty(JavaTokenizer.IGNORE_LITERALS, "true");
|
59 |
| } |
60 |
0
| if (ignoreIdentifiers) {
|
61 |
0
| p.setProperty(JavaTokenizer.IGNORE_IDENTIFIERS, "true");
|
62 |
| } |
63 |
0
| CPD cpd = new CPD(minimumTokenCount, new JavaLanguage(p));
|
64 |
0
| tokenizeFiles(cpd);
|
65 |
| |
66 |
0
| log("Starting to analyze code", Project.MSG_INFO);
|
67 |
0
| long timeTaken = analyzeCode(cpd);
|
68 |
0
| log("Done analyzing code; that took " + timeTaken + " milliseconds");
|
69 |
| |
70 |
0
| log("Generating report", Project.MSG_INFO);
|
71 |
0
| report(cpd);
|
72 |
| } catch (IOException ioe) { |
73 |
0
| log(ioe.toString(), Project.MSG_ERR);
|
74 |
0
| throw new BuildException("IOException during task execution", ioe);
|
75 |
| } catch (ReportException re) { |
76 |
0
| re.printStackTrace();
|
77 |
0
| log(re.toString(), Project.MSG_ERR);
|
78 |
0
| throw new BuildException("ReportException during task execution", re);
|
79 |
| } |
80 |
| } |
81 |
| |
82 |
0
| private void report(CPD cpd) throws ReportException {
|
83 |
0
| if (!cpd.getMatches().hasNext()) {
|
84 |
0
| log("No duplicates over " + minimumTokenCount + " tokens found", Project.MSG_INFO);
|
85 |
| } |
86 |
0
| Renderer renderer = createRenderer();
|
87 |
0
| if (outputFile.isAbsolute()) {
|
88 |
0
| new FileReporter(outputFile).report(renderer.render(cpd.getMatches()));
|
89 |
| } else { |
90 |
0
| new FileReporter(new File(getProject().getBaseDir(), outputFile.toString()));
|
91 |
| } |
92 |
| } |
93 |
| |
94 |
| |
95 |
0
| private void tokenizeFiles(CPD cpd) throws IOException {
|
96 |
0
| for (Iterator iterator = filesets.iterator(); iterator.hasNext();) {
|
97 |
0
| FileSet fileSet = (FileSet) iterator.next();
|
98 |
0
| DirectoryScanner directoryScanner = fileSet.getDirectoryScanner(getProject());
|
99 |
0
| String[] includedFiles = directoryScanner.getIncludedFiles();
|
100 |
0
| for (int i = 0; i < includedFiles.length; i++) {
|
101 |
0
| File file = new File(directoryScanner.getBasedir() + System.getProperty("file.separator") + includedFiles[i]);
|
102 |
0
| log("Tokenizing " + file.getAbsolutePath(), Project.MSG_VERBOSE);
|
103 |
0
| cpd.add(file);
|
104 |
| } |
105 |
| } |
106 |
| } |
107 |
| |
108 |
0
| private long analyzeCode(CPD cpd) {
|
109 |
0
| long start = System.currentTimeMillis();
|
110 |
0
| cpd.go();
|
111 |
0
| long stop = System.currentTimeMillis();
|
112 |
0
| return stop - start;
|
113 |
| } |
114 |
| |
115 |
0
| private Renderer createRenderer() {
|
116 |
0
| if (format.equals(TEXT_FORMAT)) {
|
117 |
0
| return new SimpleRenderer();
|
118 |
0
| } else if (format.equals(CSV_FORMAT)) {
|
119 |
0
| return new CSVRenderer();
|
120 |
| } |
121 |
0
| return new XMLRenderer();
|
122 |
| } |
123 |
| |
124 |
0
| private void validateFields() throws BuildException {
|
125 |
0
| if (minimumTokenCount == 0) {
|
126 |
0
| throw new BuildException("minimumTokenCount is required and must be greater than zero");
|
127 |
0
| } else if (outputFile == null) {
|
128 |
0
| throw new BuildException("outputFile is a required attribute");
|
129 |
0
| } else if (filesets.isEmpty()) {
|
130 |
0
| throw new BuildException("Must include at least one FileSet");
|
131 |
| } |
132 |
| } |
133 |
| |
134 |
0
| public void addFileset(FileSet set) {
|
135 |
0
| filesets.add(set);
|
136 |
| } |
137 |
| |
138 |
0
| public void setMinimumTokenCount(int minimumTokenCount) {
|
139 |
0
| this.minimumTokenCount = minimumTokenCount;
|
140 |
| } |
141 |
| |
142 |
0
| public void setIgnoreLiterals(boolean value) {
|
143 |
0
| this.ignoreLiterals = value;
|
144 |
| } |
145 |
| |
146 |
0
| public void setIgnoreIdentifiers(boolean value) {
|
147 |
0
| this.ignoreIdentifiers = value;
|
148 |
| } |
149 |
| |
150 |
0
| public void setOutputFile(File outputFile) {
|
151 |
0
| this.outputFile = outputFile;
|
152 |
| } |
153 |
| |
154 |
0
| public void setFormat(FormatAttribute formatAttribute) {
|
155 |
0
| format = formatAttribute.getValue();
|
156 |
| } |
157 |
| |
158 |
| public static class FormatAttribute extends EnumeratedAttribute { |
159 |
| private String[] formats = new String[]{XML_FORMAT, TEXT_FORMAT, CSV_FORMAT}; |
160 |
| |
161 |
0
| public String[] getValues() {
|
162 |
0
| return formats;
|
163 |
| } |
164 |
| } |
165 |
| } |