[core] cpd: Error Loading stylesheet cpdhtml.xslt

Fixes #2637
This commit is contained in:
Andreas Dangel 2021-05-20 16:19:58 +02:00
parent 0dab8818af
commit 53f88e7ed6
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
5 changed files with 176 additions and 1 deletions

View File

@ -29,6 +29,7 @@ This is a {{ site.pmd.release_type }} release.
* [#3183](https://github.com/pmd/pmd/issues/3183): \[apex] ApexUnitTestMethodShouldHaveIsTestAnnotation false positive with helper method
* [#3243](https://github.com/pmd/pmd/pull/3243): \[apex] Correct findBoundary when traversing AST
* core
* [#2637](https://github.com/pmd/pmd/issues/2637): \[cpd] Error Loading stylesheet cpdhtml.xslt
* [#2639](https://github.com/pmd/pmd/issues/2639): \[core] PMD CLI output file is not created if directory or directories in path don't exist
* doc
* [#3230](https://github.com/pmd/pmd/issues/3230): \[doc] Remove "Edit me" button for language index pages

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Stylesheet to turn the XML output of CPD into a nice-looking HTML page -->
<!-- $Id$ -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://pmd.sourceforge.net/report/2.0.0" version="2.0">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
doctype-system="http://www.w3.org/TR/html4/loose.dtd" indent="yes"/>

View File

@ -0,0 +1,84 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.File;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
public class CpdXsltTest {
/* Sample ant build.xml file. Run with "ant cpdxsl".
<project>
<target name="cpdxslt">
<xslt in="src/test/resources/net/sourceforge/pmd/cpd/SampleCpdReport.xml" style="etc/xslt/cpdhtml.xslt" out="cpd.html" />
</target>
</project>
*/
@Test
public void cpdhtml() throws Exception {
XSLTErrorListener errorListener = new XSLTErrorListener();
// note: using the default JDK factory, otherwise we would use Saxon from PMD's classpath
// which supports more xslt features.
TransformerFactory factory = TransformerFactory
.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", null);
factory.setErrorListener(errorListener);
StreamSource xslt = new StreamSource(new File("etc/xslt/cpdhtml.xslt"));
Templates template = factory.newTemplates(xslt);
StreamSource cpdReport = new StreamSource(CpdXsltTest.class.getResourceAsStream("SampleCpdReport.xml"));
StreamResult result = new StreamResult(new StringWriter());
Transformer transformer = template.newTransformer();
transformer.setErrorListener(errorListener);
transformer.transform(cpdReport, result);
String expected = IOUtils.toString(CpdXsltTest.class.getResourceAsStream("ExpectedCpdHtmlReport.html"), StandardCharsets.UTF_8);
Assert.assertEquals(expected, result.getWriter().toString());
Assert.assertTrue("XSLT errors occured: " + errorListener, errorListener.hasNoErrors());
}
private static class XSLTErrorListener implements ErrorListener {
final List<TransformerException> errors = new ArrayList<>();
@Override
public void warning(TransformerException exception) throws TransformerException {
errors.add(exception);
}
@Override
public void fatalError(TransformerException exception) throws TransformerException {
errors.add(exception);
}
@Override
public void error(TransformerException exception) throws TransformerException {
errors.add(exception);
}
public boolean hasNoErrors() {
return errors.isEmpty();
}
@Override
public String toString() {
return errors.toString();
}
}
}

View File

@ -0,0 +1,81 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function toggleCodeSection(btn, id)
{
area = document.getElementById(id);
if (area.style.display == 'none')
{
btn.innerHTML = '-';
area.style.display = 'inline';
}
else
{
btn.innerHTML = '+';
area.style.display = 'none';
}
}
</script>
<style>
.SummaryTitle { }
.SummaryNumber { background-color:#DDDDDD; text-align: center; }
.ItemNumber { background-color: #DDDDDD; }
.CodeFragment { background-color: #BBBBBB; display:none; font:normal normal normal 9pt Courier; }
.ExpandButton { background-color: #FFFFFF; font-size: 8pt; width: 20px; height: 20px; margin:0px; }
</style>
</head>
<body>
<h2>Summary of duplicated code</h2>
This page summarizes the code fragments that have been found to be replicated in the code.
Only those fragments longer than 30 lines of code are shown.
<p></p>
<table border="1" class="summary" cellpadding="2">
<tr style="background-color:#CCCCCC;">
<th># duplications</th><th>Total lines</th><th>Total tokens</th><th>Approx # bytes</th>
</tr>
<tr>
<td class="SummaryNumber">1</td><td class="SummaryNumber">60</td><td class="SummaryNumber">75</td><td class="SummaryNumber">300</td>
</tr>
</table>
<p></p>
You expand and collapse the code fragments using the + buttons. You can also navigate to the source code by clicking
on the file names.
<p></p>
<table>
<tr style="background-color: #444444; color: #DDDDDD;">
<td>ID</td><td>Files</td><td>Lines</td>
</tr>
<tr>
<td class="ItemNumber">1</td><td>
<table>
<tr>
<td><a href="../src//var/Foo.java.html#1">/var/Foo.java</a></td><td> line 1</td>
</tr>
<tr>
<td><a href="../src//var/Foo.java.html#73">/var/Foo.java</a></td><td> line 73</td>
</tr>
</table>
</td><td># lines : 60</td>
</tr>
<tr>
<td></td><td colspan="2" valign="top">
<table>
<tr>
<td valign="top"><button class="ExpandButton" onclick="blur(); toggleCodeSection(this, 'frag_1')">+</button></td><td><textarea cols="100" rows="30" wrap="off" class="CodeFragment" style="display:none;" id="frag_1">code
fragment</textarea></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="2">
<hr>
</td>
</tr>
</table>
</body>
</html>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<pmd-cpd>
<duplication lines="60" tokens="75">
<file column="2" endcolumn="3" endline="6" line="1" path="/var/Foo.java"/>
<file column="4" endcolumn="5" endline="78" line="73" path="/var/Foo.java"/>
<codefragment><![CDATA[code
fragment]]></codefragment>
</duplication>
</pmd-cpd>