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

file stats: | LOC: | 179 |  | Methods: | 16
| NCLOC: | 134 |  | Classes: | 3

Source file | Conditionals | Statements | Methods | TOTAL

CPDTask.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 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 |  | * CPDTask
22 |  | * <p/>
23 |  | * Runs the CPD utility via ant. The ant task looks like this:
24 |  | * <p/>
25 |  | * <project name="CPDProj" default="main" basedir=".">
26 |  | * <taskdef name="cpd" classname="net.sourceforge.pmd.cpd.CPDTask" />
27 |  | * <target name="main">
28 |  | * <cpd language="java" ignoreIdentifiers="true" ignoreLiterals="true" minimumTokenCount="100" outputFile="c:\cpdrun.txt">
29 |  | * <fileset dir="/path/to/my/src">
30 |  | * <include name="*.java"/>
31 |  | * </fileset>
32 |  | * </cpd>
33 |  | * </target>
34 |  | * </project>
35 |  | * <p/>
36 |  | * Required: minimumTokenCount, outputFile, and at least one file
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 String language = LanguageFactory.JAVA_KEY;
46 |  | private int minimumTokenCount;
47 |  | private boolean ignoreLiterals;
48 |  | private boolean ignoreIdentifiers;
49 |  | private File outputFile;
50 |  | private List filesets = new ArrayList();
51 |  |
52 | 0 | public void execute() throws BuildException {
53 | 0 | try {
54 | 0 | validateFields();
55 |  |
56 | 0 | log("Tokenizing files", Project.MSG_INFO);
57 | 0 | CPD cpd = new CPD(minimumTokenCount, createLanguage());
58 | 0 | tokenizeFiles(cpd);
59 |  |
60 | 0 | log("Starting to analyze code", Project.MSG_INFO);
61 | 0 | long timeTaken = analyzeCode(cpd);
62 | 0 | log("Done analyzing code; that took " + timeTaken + " milliseconds");
63 |  |
64 | 0 | log("Generating report", Project.MSG_INFO);
65 | 0 | report(cpd);
66 |  | } catch (IOException ioe) {
67 | 0 | log(ioe.toString(), Project.MSG_ERR);
68 | 0 | throw new BuildException("IOException during task execution", ioe);
69 |  | } catch (ReportException re) {
70 | 0 | re.printStackTrace();
71 | 0 | log(re.toString(), Project.MSG_ERR);
72 | 0 | throw new BuildException("ReportException during task execution", re);
73 |  | }
74 |  | }
75 |  |
76 | 0 | private Language createLanguage() {
77 | 0 | Properties p = new Properties();
78 | 0 | if (ignoreLiterals) {
79 | 0 | p.setProperty(JavaTokenizer.IGNORE_LITERALS, "true");
80 |  | }
81 | 0 | if (ignoreIdentifiers) {
82 | 0 | p.setProperty(JavaTokenizer.IGNORE_IDENTIFIERS, "true");
83 |  | }
84 | 0 | return new LanguageFactory().createLanguage(language, p);
85 |  | }
86 |  |
87 | 0 | private void report(CPD cpd) throws ReportException {
88 | 0 | if (!cpd.getMatches().hasNext()) {
89 | 0 | log("No duplicates over " + minimumTokenCount + " tokens found", Project.MSG_INFO);
90 |  | }
91 | 0 | Renderer renderer = createRenderer();
92 | 0 | if (outputFile.isAbsolute()) {
93 | 0 | new FileReporter(outputFile).report(renderer.render(cpd.getMatches()));
94 |  | } else {
95 | 0 | new FileReporter(new File(getProject().getBaseDir(), outputFile.toString()));
96 |  | }
97 |  | }
98 |  |
99 | 0 | private void tokenizeFiles(CPD cpd) throws IOException {
100 | 0 | for (Iterator iterator = filesets.iterator(); iterator.hasNext();) {
101 | 0 | FileSet fileSet = (FileSet) iterator.next();
102 | 0 | DirectoryScanner directoryScanner = fileSet.getDirectoryScanner(getProject());
103 | 0 | String[] includedFiles = directoryScanner.getIncludedFiles();
104 | 0 | for (int i = 0; i < includedFiles.length; i++) {
105 | 0 | File file = new File(directoryScanner.getBasedir() + System.getProperty("file.separator") + includedFiles[i]);
106 | 0 | log("Tokenizing " + file.getAbsolutePath(), Project.MSG_VERBOSE);
107 | 0 | cpd.add(file);
108 |  | }
109 |  | }
110 |  | }
111 |  |
112 | 0 | private long analyzeCode(CPD cpd) {
113 | 0 | long start = System.currentTimeMillis();
114 | 0 | cpd.go();
115 | 0 | long stop = System.currentTimeMillis();
116 | 0 | return stop - start;
117 |  | }
118 |  |
119 | 0 | private Renderer createRenderer() {
120 | 0 | if (format.equals(TEXT_FORMAT)) {
121 | 0 | return new SimpleRenderer();
122 | 0 | } else if (format.equals(CSV_FORMAT)) {
123 | 0 | return new CSVRenderer();
124 |  | }
125 | 0 | return new XMLRenderer();
126 |  | }
127 |  |
128 | 0 | private void validateFields() throws BuildException {
129 | 0 | if (minimumTokenCount == 0) {
130 | 0 | throw new BuildException("minimumTokenCount is required and must be greater than zero");
131 | 0 | } else if (outputFile == null) {
132 | 0 | throw new BuildException("outputFile is a required attribute");
133 | 0 | } else if (filesets.isEmpty()) {
134 | 0 | throw new BuildException("Must include at least one FileSet");
135 |  | }
136 |  | }
137 |  |
138 | 0 | public void addFileset(FileSet set) {
139 | 0 | filesets.add(set);
140 |  | }
141 |  |
142 | 0 | public void setMinimumTokenCount(int minimumTokenCount) {
143 | 0 | this.minimumTokenCount = minimumTokenCount;
144 |  | }
145 |  |
146 | 0 | public void setIgnoreLiterals(boolean value) {
147 | 0 | this.ignoreLiterals = value;
148 |  | }
149 |  |
150 | 0 | public void setIgnoreIdentifiers(boolean value) {
151 | 0 | this.ignoreIdentifiers = value;
152 |  | }
153 |  |
154 | 0 | public void setOutputFile(File outputFile) {
155 | 0 | this.outputFile = outputFile;
156 |  | }
157 |  |
158 | 0 | public void setFormat(FormatAttribute formatAttribute) {
159 | 0 | format = formatAttribute.getValue();
160 |  | }
161 |  |
162 | 0 | public void setLanguage(LanguageAttribute languageAttribute) {
163 | 0 | language = languageAttribute.getValue();
164 |  | }
165 |  |
166 |  | public static class FormatAttribute extends EnumeratedAttribute {
167 |  | private static final String[] FORMATS = new String[]{XML_FORMAT, TEXT_FORMAT, CSV_FORMAT};
168 | 0 | public String[] getValues() {
169 | 0 | return FORMATS;
170 |  | }
171 |  | }
172 |  |
173 |  | public static class LanguageAttribute extends EnumeratedAttribute {
174 |  | private String[] LANGUAGES = new String[]{LanguageFactory.JAVA_KEY, LanguageFactory.CPP_KEY, LanguageFactory.C_KEY, LanguageFactory.PHP_KEY, LanguageFactory.RUBY_KEY };
175 | 0 | public String[] getValues() {
176 | 0 | return LANGUAGES;
177 |  | }
178 |  | }
179 |  | }