From 40c2a5d7686bbbb04b0d8a87c85e2e207acd46e7 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 2 Jun 2012 12:00:55 +0000 Subject: [PATCH] cpd: fixed bug #3523614, preserving the newlines in the XML report Excluding the transitive xalan dependency and adding the missing saxon-dom dependency, so that we have saxon complete and only one XML library. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7680 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 1 + pmd/pom.xml | 10 ++++++++++ .../main/java/net/sourceforge/pmd/cpd/XMLRenderer.java | 5 ++++- .../java/net/sourceforge/pmd/cpd/XMLRendererTest.java | 5 ++--- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index a61bb33299..75bde99d80 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -27,6 +27,7 @@ TODO - Release blockers - Must implement before this release can be finally fini and pmd-java:typeof). +Fixed bug 3523614: newline characters stripped from CPD data in PMD 5.0.0 Fixed bug 3516101: InsufficientStringBufferDeclaration fails to parse hex Fixed bug 1503099: InefficientStringBuffering bug false + Fixed bug 3109408: String.InefficientStringBuffering false + diff --git a/pmd/pom.xml b/pmd/pom.xml index cda9d5a074..1aedf56c41 100644 --- a/pmd/pom.xml +++ b/pmd/pom.xml @@ -481,6 +481,10 @@ xercesImpl xerces + + xalan + xalan + @@ -493,6 +497,12 @@ saxon 9.1.0.8 + + net.sourceforge.saxon + saxon + 9.1.0.8 + dom + org.mozilla rhino diff --git a/pmd/src/main/java/net/sourceforge/pmd/cpd/XMLRenderer.java b/pmd/src/main/java/net/sourceforge/pmd/cpd/XMLRenderer.java index f7fd1d599f..42f0a472fd 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/cpd/XMLRenderer.java +++ b/pmd/src/main/java/net/sourceforge/pmd/cpd/XMLRenderer.java @@ -41,10 +41,13 @@ public final class XMLRenderer implements Renderer { try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, System.getProperty("file.encoding")); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "codefragment"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); - return writer.getBuffer().toString().replaceAll("\n|\r", ""); + return writer.getBuffer().toString(); } catch (TransformerException e) { throw new IllegalStateException(e); } diff --git a/pmd/src/test/java/net/sourceforge/pmd/cpd/XMLRendererTest.java b/pmd/src/test/java/net/sourceforge/pmd/cpd/XMLRendererTest.java index ab043496bb..85e47ded8b 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/cpd/XMLRendererTest.java +++ b/pmd/src/test/java/net/sourceforge/pmd/cpd/XMLRendererTest.java @@ -51,10 +51,9 @@ public class XMLRendererTest { List list = new ArrayList(); Match match = new Match(75, new TokenEntry("public", "/var/Foo.java", 48), new TokenEntry("stuff", "/var/Foo.java", 73)); match.setLineCount(6); - match.setSourceCodeSlice("code fragment"); + match.setSourceCodeSlice("code\nfragment"); list.add(match); String report = renderer.render(list.iterator()); - System.out.println(report); try { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(report.getBytes(ENCODING))); NodeList dupes = doc.getElementsByTagName("duplication"); @@ -73,7 +72,7 @@ public class XMLRendererTest { } if (file != null) assertEquals("73", file.getAttributes().getNamedItem("line").getNodeValue()); assertEquals(1, doc.getElementsByTagName("codefragment").getLength()); - assertEquals("code fragment", doc.getElementsByTagName("codefragment").item(0).getTextContent()); + assertEquals("code\nfragment", doc.getElementsByTagName("codefragment").item(0).getTextContent()); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage());