Skip to content

Commit

Permalink
[MPMD-375] Replace *ReportGenerators with a new *ReportRenderers
Browse files Browse the repository at this point in the history
This closes #129
  • Loading branch information
michael-o committed May 10, 2023
1 parent 931a292 commit cca3d15
Show file tree
Hide file tree
Showing 7 changed files with 574 additions and 694 deletions.
19 changes: 18 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ under the License.
<pmdVersion>6.55.0</pmdVersion>
<slf4jVersion>1.7.36</slf4jVersion>
<aetherVersion>1.0.0.v20140518</aetherVersion>
<doxiaVersion>1.12.0</doxiaVersion>
<compilerPluginVersion>3.11.0</compilerPluginVersion>
<sitePluginVersion>3.12.1</sitePluginVersion>
<projectInfoReportsPluginVersion>3.4.3</projectInfoReportsPluginVersion>
Expand Down Expand Up @@ -190,7 +191,12 @@ under the License.
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-sink-api</artifactId>
<version>1.12.0</version>
<version>${doxiaVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-core</artifactId>
<version>${doxiaVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
Expand Down Expand Up @@ -227,6 +233,17 @@ under the License.
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-i18n</artifactId>
<version>1.0-beta-10</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- test -->
<dependency>
Expand Down
41 changes: 23 additions & 18 deletions src/main/java/org/apache/maven/plugins/pmd/CpdReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import net.sourceforge.pmd.cpd.JavaTokenizer;
import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.pmd.exec.CpdExecutor;
import org.apache.maven.plugins.pmd.exec.CpdRequest;
import org.apache.maven.plugins.pmd.exec.CpdResult;
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.toolchain.Toolchain;
import org.codehaus.plexus.i18n.I18N;

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

/**
* Internationalization component
*/
@Component
private I18N i18n;

/**
* Contains the result of the last CPD execution.
* It might be <code>null</code> which means, that CPD
* has not been executed yet.
*/
private CpdResult cpdResult;

/**
* {@inheritDoc}
*/
/** {@inheritDoc} */
public String getName(Locale locale) {
return getBundle(locale).getString("report.cpd.name");
return getI18nString(locale, "name");
}

/** {@inheritDoc} */
public String getDescription(Locale locale) {
return getI18nString(locale, "description");
}

/**
* {@inheritDoc}
* @param locale The locale
* @param key The key to search for
* @return The text appropriate for the locale.
*/
public String getDescription(Locale locale) {
return getBundle(locale).getString("report.cpd.description");
protected String getI18nString(Locale locale, String key) {
return i18n.getString("cpd-report", locale, "report.cpd." + key);
}

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

generateMavenSiteReport(locale);
CpdReportRenderer r = new CpdReportRenderer(
getSink(), i18n, locale, filesToProcess, cpdResult.getDuplications(), isAggregator());
r.render();
} finally {
Thread.currentThread().setContextClassLoader(origLoader);
}
Expand Down Expand Up @@ -209,22 +223,13 @@ private void executeCpd() throws MavenReportException {
}
}

private void generateMavenSiteReport(Locale locale) {
CpdReportGenerator gen = new CpdReportGenerator(getSink(), filesToProcess, getBundle(locale), isAggregator());
gen.generate(cpdResult.getDuplications());
}

/**
* {@inheritDoc}
*/
public String getOutputName() {
return "cpd";
}

private static ResourceBundle getBundle(Locale locale) {
return ResourceBundle.getBundle("cpd-report", locale, CpdReport.class.getClassLoader());
}

/**
* Create and return the correct renderer for the output type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@
*/
package org.apache.maven.plugins.pmd;

import javax.swing.text.html.HTML.Attribute;

import java.io.File;
import java.util.List;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.maven.doxia.sink.Sink;
import org.apache.maven.doxia.sink.SinkEventAttributes;
import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
import org.apache.maven.plugins.pmd.model.CpdFile;
import org.apache.maven.plugins.pmd.model.Duplication;
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.AbstractMavenReportRenderer;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.util.StringUtils;

/**
Expand All @@ -35,66 +41,66 @@
* @author mperham
* @version $Id$
*/
public class CpdReportGenerator {
private Sink sink;
public class CpdReportRenderer extends AbstractMavenReportRenderer {
private final I18N i18n;

private final Locale locale;

private Map<File, PmdFileInfo> fileMap;
private final Map<File, PmdFileInfo> files;

private ResourceBundle bundle;
private final Collection<Duplication> duplications;

private boolean aggregate;
private final boolean aggregate;

public CpdReportGenerator(Sink sink, Map<File, PmdFileInfo> fileMap, ResourceBundle bundle, boolean aggregate) {
this.sink = sink;
this.fileMap = fileMap;
this.bundle = bundle;
public CpdReportRenderer(
Sink sink,
I18N i18n,
Locale locale,
Map<File, PmdFileInfo> files,
Collection<Duplication> duplications,
boolean aggregate) {
super(sink);
this.i18n = i18n;
this.locale = locale;
this.files = files;
this.duplications = duplications;
this.aggregate = aggregate;
}

/**
* Method that returns the title of the CPD Report
*
* @return a String that contains the title
*/
private String getTitle() {
return bundle.getString("report.cpd.title");
@Override
public String getTitle() {
return getI18nString("title");
}

/**
* Method that generates the start of the CPD report.
* @param key The key.
* @return The translated string.
*/
public void beginDocument() {
sink.head();
sink.title();
sink.text(getTitle());
sink.title_();
sink.head_();

sink.body();
private String getI18nString(String key) {
return i18n.getString("cpd-report", locale, "report.cpd." + key);
}

sink.section1();
sink.sectionTitle1();
sink.text(getTitle());
sink.sectionTitle1_();
@Override
protected void renderBody() {
startSection(getTitle());

sink.paragraph();
sink.text(bundle.getString("report.cpd.cpdlink") + " ");
sink.link("https://pmd.github.io/latest/pmd_userdocs_cpd.html");
sink.text("CPD");
sink.link_();
sink.text(getI18nString("cpdlink") + " ");
link("https://pmd.github.io/latest/pmd_userdocs_cpd.html", "CPD");
sink.text(" " + AbstractPmdReport.getPmdVersion() + ".");
sink.paragraph_();

sink.section1_();

// TODO overall summary

sink.section1();
sink.sectionTitle1();
sink.text(bundle.getString("report.cpd.dupes"));
sink.sectionTitle1_();
if (!duplications.isEmpty()) {
renderDuplications();
} else {
paragraph(getI18nString("noProblems"));
}

// TODO files summary

endSection();
}

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

sink.tableRow();
sink.tableCell();
sink.text(filename);
sink.tableCell_();
tableCell(filename);
if (aggregate) {
sink.tableCell();
sink.text(projectFile.getName());
sink.tableCell_();
tableCell(projectFile.getName());
}
sink.tableCell();

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

/**
* Method that generates the contents of the CPD report
*
* @param duplications the found duplications
*/
public void generate(List<Duplication> duplications) {
beginDocument();

if (duplications.isEmpty()) {
sink.paragraph();
sink.text(bundle.getString("report.cpd.noProblems"));
sink.paragraph_();
}
private void renderDuplications() {
startSection(getI18nString("dupes"));

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

sink.table();
sink.tableRows(null, false);
startTable();
sink.tableRow();
sink.tableHeaderCell();
sink.text(bundle.getString("report.cpd.column.file"));
sink.tableHeaderCell_();
tableHeaderCell(getI18nString("column.file"));
if (aggregate) {
sink.tableHeaderCell();
sink.text(bundle.getString("report.cpd.column.project"));
sink.tableHeaderCell_();
tableHeaderCell(getI18nString("column.project"));
}
sink.tableHeaderCell();
sink.text(bundle.getString("report.cpd.column.line"));
sink.tableHeaderCell_();
tableHeaderCell(getI18nString("column.line"));
sink.tableRow_();

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

int colspan = 2;
if (aggregate) {
++colspan;
colspan = 3;
}
// TODO Cleaner way to do this?
sink.rawText("<td colspan='" + colspan + "'>");
sink.verbatim(null);
sink.text(code);
sink.verbatim_();
sink.rawText("</td>");
SinkEventAttributes att = new SinkEventAttributeSet();
att.addAttribute(Attribute.COLSPAN, colspan);
sink.tableCell(att);
verbatimText(code);
sink.tableCell_();
sink.tableRow_();
sink.tableRows_();
sink.table_();
endTable();
}

sink.section1_();
sink.body_();
sink.flush();
sink.close();
endSection();
}
}
Loading

0 comments on commit cca3d15

Please sign in to comment.