[core] CPD: Also escape filename, explicitly set XML 1.0

This commit is contained in:
Andreas Dangel
2020-07-05 11:19:54 +02:00
parent 3004e76257
commit 3bb091e26d
2 changed files with 13 additions and 6 deletions
pmd-core/src
main/java/net/sourceforge/pmd/cpd
test/java/net/sourceforge/pmd/cpd

@ -77,6 +77,7 @@ public final class XMLRenderer implements Renderer, CPDRenderer {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
@ -120,7 +121,8 @@ public final class XMLRenderer implements Renderer, CPDRenderer {
mark = iterator.next();
final Element file = doc.createElement("file");
file.setAttribute("line", String.valueOf(mark.getBeginLine()));
file.setAttribute("path", mark.getFilename());
String filenameXml10 = StringEscapeUtils.unescapeXml(StringEscapeUtils.escapeXml10(mark.getFilename()));
file.setAttribute("path", filenameXml10);
file.setAttribute("endline", String.valueOf(mark.getEndLine()));
final int beginCol = mark.getBeginColumn();
final int endCol = mark.getEndColumn();

@ -31,6 +31,9 @@ import net.sourceforge.pmd.cpd.renderer.CPDRenderer;
public class XMLRendererTest {
private static final String ENCODING = (String) System.getProperties().get("file.encoding");
private static final String FORM_FEED = "\u000C"; // this character is invalid in XML 1.0 documents
private static final String FORM_FEED_ENTITY = ""; // this is also not allowed in XML 1.0 documents
@Test
public void testWithNoDuplication() throws IOException {
@ -186,8 +189,8 @@ public class XMLRendererTest {
CPDRenderer renderer = new XMLRenderer();
List<Match> list = new ArrayList<>();
final String espaceChar = "&lt;";
Mark mark1 = createMark("public", "/var/F" + '<' + "oo.java", 48, 6, "code fragment");
Mark mark2 = createMark("void", "/var/F<oo.java", 73, 6, "code fragment");
Mark mark1 = createMark("public", "/var/A<oo.java" + FORM_FEED, 48, 6, "code fragment");
Mark mark2 = createMark("void", "/var/B<oo.java", 73, 6, "code fragment");
Match match1 = new Match(75, mark1, mark2);
list.add(match1);
@ -195,12 +198,13 @@ public class XMLRendererTest {
renderer.render(list.iterator(), sw);
String report = sw.toString();
assertTrue(report.contains(espaceChar));
assertFalse(report.contains(FORM_FEED));
assertFalse(report.contains(FORM_FEED_ENTITY));
}
@Test
public void testRendererXMLEscaping() throws IOException {
String formfeed = "\u000C";
String codefragment = "code fragment" + formfeed + "\nline2\nline3";
String codefragment = "code fragment" + FORM_FEED + "\nline2\nline3";
CPDRenderer renderer = new XMLRenderer();
List<Match> list = new ArrayList<>();
Mark mark1 = createMark("public", "file1", 1, 2, codefragment);
@ -211,7 +215,8 @@ public class XMLRendererTest {
StringWriter sw = new StringWriter();
renderer.render(list.iterator(), sw);
String report = sw.toString();
assertFalse(report.contains(formfeed));
assertFalse(report.contains(FORM_FEED));
assertFalse(report.contains(FORM_FEED_ENTITY));
}
private Mark createMark(String image, String tokenSrcID, int beginLine, int lineCount, String code) {