From a73b7f1fb19744f026279ff67ac6314191234d43 Mon Sep 17 00:00:00 2001 From: Romain Pelisse Date: Sun, 16 Aug 2009 13:18:39 +0000 Subject: [PATCH] Fix bug 2832322 - cpd.xml file tag path attribute should be entity-encoded. git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/branches/pmd/4.2.x@6988 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 1 + .../sourceforge/pmd/cpd/XMLRendererTest.java | 14 +++++++++ .../net/sourceforge/pmd/cpd/XMLRenderer.java | 31 ++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 8265f8d053..62e3a1ed86 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,5 +1,6 @@ ???? - 4.2.6: +Fixed bug 2832322 - cpd.xml file tag path attribute should be entity-encoded Fixed bug 2590258 - NPE with nicerhtml output Fixed bug 2317099 - False + in SimplifyCondition Fixed bug 2606609 - False "UnusedImports" positive in package-info.java diff --git a/pmd/regress/test/net/sourceforge/pmd/cpd/XMLRendererTest.java b/pmd/regress/test/net/sourceforge/pmd/cpd/XMLRendererTest.java index 36a84cf45c..e2abee5a53 100644 --- a/pmd/regress/test/net/sourceforge/pmd/cpd/XMLRendererTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/cpd/XMLRendererTest.java @@ -4,6 +4,7 @@ package test.net.sourceforge.pmd.cpd; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import net.sourceforge.pmd.cpd.Match; import net.sourceforge.pmd.cpd.Renderer; @@ -101,6 +102,19 @@ public class XMLRendererTest { } } + @Test + public void testRendererEncodedPath() { + Renderer renderer = new XMLRenderer(); + List list = new ArrayList(); + Match match1 = new Match(75, new TokenEntry("public", "/var/F" + XMLRenderer.BASIC_ESCAPE[2][0] + "oo.java", 48), new TokenEntry("void", "/var/F").append(PMD.EOL); } String codeFragment = match.getSourceCodeSlice(); @@ -58,4 +58,33 @@ public class XMLRenderer implements Renderer { buffer.append(""); return buffer.toString(); } + + + /* + *

Fixes bug : https://sourceforge.net/tracker/?func=detail&aid=2832322&group_id=56262&atid=479921

+ * + * TODO: The following method - and its static arrays - should + * most likely be place somewhere else, like some kind of utility + * classes to solve issue on encoding. + */ + private static String encode(String path) { + for ( int i = 0; i < BASIC_ESCAPE.length; i++ ) { + if ( path.indexOf(BASIC_ESCAPE[i][0]) != -1 ) { + path = path.replaceAll(BASIC_ESCAPE[i][0],BASIC_ESCAPE[i][1]); + } + } + return path; + } + + /* + * Cut'n'paster from Apache Commons Lang + * + */ + public static final String[][] BASIC_ESCAPE = { + {"\"", """}, // " - double-quote + {"&", "&"}, // & - ampersand + {"<", "<"}, // < - less-than + {">", ">"}, // > - greater-than + }; + }