forked from phoedos/pmd
[core] CPD: Add correct XML 1.0 escaping for code snippets
This commit is contained in:
parent
e8d749dc5e
commit
3004e76257
@ -134,6 +134,10 @@
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ow2.asm</groupId>
|
||||
<artifactId>asm</artifactId>
|
||||
|
@ -18,6 +18,7 @@ import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.apache.commons.text.StringEscapeUtils;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
@ -140,7 +141,7 @@ public final class XMLRenderer implements Renderer, CPDRenderer {
|
||||
// the code snippet has normalized line endings
|
||||
String platformSpecific = codeSnippet.replace("\n", System.lineSeparator());
|
||||
Element codefragment = doc.createElement("codefragment");
|
||||
codefragment.appendChild(doc.createCDATASection(platformSpecific));
|
||||
codefragment.appendChild(doc.createCDATASection(StringEscapeUtils.escapeXml10(platformSpecific)));
|
||||
duplication.appendChild(codefragment);
|
||||
}
|
||||
return duplication;
|
||||
|
@ -5,6 +5,7 @@
|
||||
package net.sourceforge.pmd.cpd;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
@ -196,6 +197,23 @@ public class XMLRendererTest {
|
||||
assertTrue(report.contains(espaceChar));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRendererXMLEscaping() throws IOException {
|
||||
String formfeed = "\u000C";
|
||||
String codefragment = "code fragment" + formfeed + "\nline2\nline3";
|
||||
CPDRenderer renderer = new XMLRenderer();
|
||||
List<Match> list = new ArrayList<>();
|
||||
Mark mark1 = createMark("public", "file1", 1, 2, codefragment);
|
||||
Mark mark2 = createMark("public", "file2", 5, 2, codefragment);
|
||||
Match match1 = new Match(75, mark1, mark2);
|
||||
list.add(match1);
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
renderer.render(list.iterator(), sw);
|
||||
String report = sw.toString();
|
||||
assertFalse(report.contains(formfeed));
|
||||
}
|
||||
|
||||
private Mark createMark(String image, String tokenSrcID, int beginLine, int lineCount, String code) {
|
||||
Mark result = new Mark(new TokenEntry(image, tokenSrcID, beginLine));
|
||||
|
||||
@ -214,8 +232,4 @@ public class XMLRendererTest {
|
||||
result.setSourceCode(new SourceCode(new SourceCode.StringCodeLoader(code)));
|
||||
return result;
|
||||
}
|
||||
|
||||
public static junit.framework.Test suite() {
|
||||
return new junit.framework.JUnit4TestAdapter(XMLRendererTest.class);
|
||||
}
|
||||
}
|
||||
|
@ -95,7 +95,6 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
<version>1.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
|
5
pom.xml
5
pom.xml
@ -669,6 +669,11 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.8.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
<version>1.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
|
Loading…
x
Reference in New Issue
Block a user