Skip to content

Commit 577d574

Browse files
committed
[MPMD-375] Replace *ReportGenerators with a new *ReportRenderers
This closes #130
1 parent 931a292 commit 577d574

File tree

7 files changed

+574
-694
lines changed

7 files changed

+574
-694
lines changed

pom.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ under the License.
8686
<pmdVersion>6.55.0</pmdVersion>
8787
<slf4jVersion>1.7.36</slf4jVersion>
8888
<aetherVersion>1.0.0.v20140518</aetherVersion>
89+
<doxiaVersion>1.12.0</doxiaVersion>
8990
<compilerPluginVersion>3.11.0</compilerPluginVersion>
9091
<sitePluginVersion>3.12.1</sitePluginVersion>
9192
<projectInfoReportsPluginVersion>3.4.3</projectInfoReportsPluginVersion>
@@ -190,7 +191,12 @@ under the License.
190191
<dependency>
191192
<groupId>org.apache.maven.doxia</groupId>
192193
<artifactId>doxia-sink-api</artifactId>
193-
<version>1.12.0</version>
194+
<version>${doxiaVersion}</version>
195+
</dependency>
196+
<dependency>
197+
<groupId>org.apache.maven.doxia</groupId>
198+
<artifactId>doxia-core</artifactId>
199+
<version>${doxiaVersion}</version>
194200
</dependency>
195201
<dependency>
196202
<groupId>org.apache.maven.doxia</groupId>
@@ -227,6 +233,17 @@ under the License.
227233
<groupId>org.codehaus.plexus</groupId>
228234
<artifactId>plexus-utils</artifactId>
229235
</dependency>
236+
<dependency>
237+
<groupId>org.codehaus.plexus</groupId>
238+
<artifactId>plexus-i18n</artifactId>
239+
<version>1.0-beta-10</version>
240+
<exclusions>
241+
<exclusion>
242+
<groupId>org.codehaus.plexus</groupId>
243+
<artifactId>plexus-component-api</artifactId>
244+
</exclusion>
245+
</exclusions>
246+
</dependency>
230247

231248
<!-- test -->
232249
<dependency>

src/main/java/org/apache/maven/plugins/pmd/CpdReport.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@
2222
import java.io.UnsupportedEncodingException;
2323
import java.util.Locale;
2424
import java.util.Properties;
25-
import java.util.ResourceBundle;
2625

2726
import net.sourceforge.pmd.cpd.JavaTokenizer;
2827
import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
28+
import org.apache.maven.plugins.annotations.Component;
2929
import org.apache.maven.plugins.annotations.Mojo;
3030
import org.apache.maven.plugins.annotations.Parameter;
3131
import org.apache.maven.plugins.pmd.exec.CpdExecutor;
3232
import org.apache.maven.plugins.pmd.exec.CpdRequest;
3333
import org.apache.maven.plugins.pmd.exec.CpdResult;
3434
import org.apache.maven.reporting.MavenReportException;
3535
import org.apache.maven.toolchain.Toolchain;
36+
import org.codehaus.plexus.i18n.I18N;
3637

3738
/**
3839
* Creates a report for PMD's Copy/Paste Detector (CPD) tool.
@@ -96,25 +97,36 @@ public class CpdReport extends AbstractPmdReport {
9697
@Parameter(property = "cpd.ignoreAnnotations", defaultValue = "false")
9798
private boolean ignoreAnnotations;
9899

100+
/**
101+
* Internationalization component
102+
*/
103+
@Component
104+
private I18N i18n;
105+
99106
/**
100107
* Contains the result of the last CPD execution.
101108
* It might be <code>null</code> which means, that CPD
102109
* has not been executed yet.
103110
*/
104111
private CpdResult cpdResult;
105112

106-
/**
107-
* {@inheritDoc}
108-
*/
113+
/** {@inheritDoc} */
109114
public String getName(Locale locale) {
110-
return getBundle(locale).getString("report.cpd.name");
115+
return getI18nString(locale, "name");
116+
}
117+
118+
/** {@inheritDoc} */
119+
public String getDescription(Locale locale) {
120+
return getI18nString(locale, "description");
111121
}
112122

113123
/**
114-
* {@inheritDoc}
124+
* @param locale The locale
125+
* @param key The key to search for
126+
* @return The text appropriate for the locale.
115127
*/
116-
public String getDescription(Locale locale) {
117-
return getBundle(locale).getString("report.cpd.description");
128+
protected String getI18nString(Locale locale, String key) {
129+
return i18n.getString("cpd-report", locale, "report.cpd." + key);
118130
}
119131

120132
/**
@@ -126,7 +138,9 @@ public void executeReport(Locale locale) throws MavenReportException {
126138
try {
127139
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
128140

129-
generateMavenSiteReport(locale);
141+
CpdReportRenderer r = new CpdReportRenderer(
142+
getSink(), i18n, locale, filesToProcess, cpdResult.getDuplications(), isAggregator());
143+
r.render();
130144
} finally {
131145
Thread.currentThread().setContextClassLoader(origLoader);
132146
}
@@ -209,22 +223,13 @@ private void executeCpd() throws MavenReportException {
209223
}
210224
}
211225

212-
private void generateMavenSiteReport(Locale locale) {
213-
CpdReportGenerator gen = new CpdReportGenerator(getSink(), filesToProcess, getBundle(locale), isAggregator());
214-
gen.generate(cpdResult.getDuplications());
215-
}
216-
217226
/**
218227
* {@inheritDoc}
219228
*/
220229
public String getOutputName() {
221230
return "cpd";
222231
}
223232

224-
private static ResourceBundle getBundle(Locale locale) {
225-
return ResourceBundle.getBundle("cpd-report", locale, CpdReport.class.getClassLoader());
226-
}
227-
228233
/**
229234
* Create and return the correct renderer for the output type.
230235
*

src/main/java/org/apache/maven/plugins/pmd/CpdReportGenerator.java renamed to src/main/java/org/apache/maven/plugins/pmd/CpdReportRenderer.java

Lines changed: 64 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@
1818
*/
1919
package org.apache.maven.plugins.pmd;
2020

21+
import javax.swing.text.html.HTML.Attribute;
22+
2123
import java.io.File;
22-
import java.util.List;
24+
import java.util.Collection;
25+
import java.util.Locale;
2326
import java.util.Map;
24-
import java.util.ResourceBundle;
2527

2628
import org.apache.maven.doxia.sink.Sink;
29+
import org.apache.maven.doxia.sink.SinkEventAttributes;
30+
import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
2731
import org.apache.maven.plugins.pmd.model.CpdFile;
2832
import org.apache.maven.plugins.pmd.model.Duplication;
2933
import org.apache.maven.project.MavenProject;
34+
import org.apache.maven.reporting.AbstractMavenReportRenderer;
35+
import org.codehaus.plexus.i18n.I18N;
3036
import org.codehaus.plexus.util.StringUtils;
3137

3238
/**
@@ -35,66 +41,66 @@
3541
* @author mperham
3642
* @version $Id$
3743
*/
38-
public class CpdReportGenerator {
39-
private Sink sink;
44+
public class CpdReportRenderer extends AbstractMavenReportRenderer {
45+
private final I18N i18n;
46+
47+
private final Locale locale;
4048

41-
private Map<File, PmdFileInfo> fileMap;
49+
private final Map<File, PmdFileInfo> files;
4250

43-
private ResourceBundle bundle;
51+
private final Collection<Duplication> duplications;
4452

45-
private boolean aggregate;
53+
private final boolean aggregate;
4654

47-
public CpdReportGenerator(Sink sink, Map<File, PmdFileInfo> fileMap, ResourceBundle bundle, boolean aggregate) {
48-
this.sink = sink;
49-
this.fileMap = fileMap;
50-
this.bundle = bundle;
55+
public CpdReportRenderer(
56+
Sink sink,
57+
I18N i18n,
58+
Locale locale,
59+
Map<File, PmdFileInfo> files,
60+
Collection<Duplication> duplications,
61+
boolean aggregate) {
62+
super(sink);
63+
this.i18n = i18n;
64+
this.locale = locale;
65+
this.files = files;
66+
this.duplications = duplications;
5167
this.aggregate = aggregate;
5268
}
5369

54-
/**
55-
* Method that returns the title of the CPD Report
56-
*
57-
* @return a String that contains the title
58-
*/
59-
private String getTitle() {
60-
return bundle.getString("report.cpd.title");
70+
@Override
71+
public String getTitle() {
72+
return getI18nString("title");
6173
}
6274

6375
/**
64-
* Method that generates the start of the CPD report.
76+
* @param key The key.
77+
* @return The translated string.
6578
*/
66-
public void beginDocument() {
67-
sink.head();
68-
sink.title();
69-
sink.text(getTitle());
70-
sink.title_();
71-
sink.head_();
72-
73-
sink.body();
79+
private String getI18nString(String key) {
80+
return i18n.getString("cpd-report", locale, "report.cpd." + key);
81+
}
7482

75-
sink.section1();
76-
sink.sectionTitle1();
77-
sink.text(getTitle());
78-
sink.sectionTitle1_();
83+
@Override
84+
protected void renderBody() {
85+
startSection(getTitle());
7986

8087
sink.paragraph();
81-
sink.text(bundle.getString("report.cpd.cpdlink") + " ");
82-
sink.link("https://pmd.github.io/latest/pmd_userdocs_cpd.html");
83-
sink.text("CPD");
84-
sink.link_();
88+
sink.text(getI18nString("cpdlink") + " ");
89+
link("https://pmd.github.io/latest/pmd_userdocs_cpd.html", "CPD");
8590
sink.text(" " + AbstractPmdReport.getPmdVersion() + ".");
8691
sink.paragraph_();
8792

88-
sink.section1_();
89-
9093
// TODO overall summary
9194

92-
sink.section1();
93-
sink.sectionTitle1();
94-
sink.text(bundle.getString("report.cpd.dupes"));
95-
sink.sectionTitle1_();
95+
if (!duplications.isEmpty()) {
96+
renderDuplications();
97+
} else {
98+
paragraph(getI18nString("noProblems"));
99+
}
96100

97101
// TODO files summary
102+
103+
endSection();
98104
}
99105

100106
/**
@@ -104,7 +110,7 @@ private void generateFileLine(CpdFile duplicationMark) {
104110
// Get information for report generation
105111
String filename = duplicationMark.getPath();
106112
File file = new File(filename);
107-
PmdFileInfo fileInfo = fileMap.get(file);
113+
PmdFileInfo fileInfo = files.get(file);
108114
File sourceDirectory = fileInfo.getSourceDirectory();
109115
filename = StringUtils.substring(
110116
filename, sourceDirectory.getAbsolutePath().length() + 1);
@@ -113,13 +119,9 @@ private void generateFileLine(CpdFile duplicationMark) {
113119
int line = duplicationMark.getLine();
114120

115121
sink.tableRow();
116-
sink.tableCell();
117-
sink.text(filename);
118-
sink.tableCell_();
122+
tableCell(filename);
119123
if (aggregate) {
120-
sink.tableCell();
121-
sink.text(projectFile.getName());
122-
sink.tableCell_();
124+
tableCell(projectFile.getName());
123125
}
124126
sink.tableCell();
125127

@@ -136,37 +138,19 @@ private void generateFileLine(CpdFile duplicationMark) {
136138
sink.tableRow_();
137139
}
138140

139-
/**
140-
* Method that generates the contents of the CPD report
141-
*
142-
* @param duplications the found duplications
143-
*/
144-
public void generate(List<Duplication> duplications) {
145-
beginDocument();
146-
147-
if (duplications.isEmpty()) {
148-
sink.paragraph();
149-
sink.text(bundle.getString("report.cpd.noProblems"));
150-
sink.paragraph_();
151-
}
141+
private void renderDuplications() {
142+
startSection(getI18nString("dupes"));
152143

153144
for (Duplication duplication : duplications) {
154145
String code = duplication.getCodefragment();
155146

156-
sink.table();
157-
sink.tableRows(null, false);
147+
startTable();
158148
sink.tableRow();
159-
sink.tableHeaderCell();
160-
sink.text(bundle.getString("report.cpd.column.file"));
161-
sink.tableHeaderCell_();
149+
tableHeaderCell(getI18nString("column.file"));
162150
if (aggregate) {
163-
sink.tableHeaderCell();
164-
sink.text(bundle.getString("report.cpd.column.project"));
165-
sink.tableHeaderCell_();
151+
tableHeaderCell(getI18nString("column.project"));
166152
}
167-
sink.tableHeaderCell();
168-
sink.text(bundle.getString("report.cpd.column.line"));
169-
sink.tableHeaderCell_();
153+
tableHeaderCell(getI18nString("column.line"));
170154
sink.tableRow_();
171155

172156
// Iterating on every token entry
@@ -179,22 +163,17 @@ public void generate(List<Duplication> duplications) {
179163

180164
int colspan = 2;
181165
if (aggregate) {
182-
++colspan;
166+
colspan = 3;
183167
}
184-
// TODO Cleaner way to do this?
185-
sink.rawText("<td colspan='" + colspan + "'>");
186-
sink.verbatim(null);
187-
sink.text(code);
188-
sink.verbatim_();
189-
sink.rawText("</td>");
168+
SinkEventAttributes att = new SinkEventAttributeSet();
169+
att.addAttribute(Attribute.COLSPAN, colspan);
170+
sink.tableCell(att);
171+
verbatimText(code);
172+
sink.tableCell_();
190173
sink.tableRow_();
191-
sink.tableRows_();
192-
sink.table_();
174+
endTable();
193175
}
194176

195-
sink.section1_();
196-
sink.body_();
197-
sink.flush();
198-
sink.close();
177+
endSection();
199178
}
200179
}

0 commit comments

Comments
 (0)