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
This commit is contained in:
Romain Pelisse 2009-08-16 13:18:39 +00:00
parent 64d3bc28cb
commit a73b7f1fb1
3 changed files with 45 additions and 1 deletions

View File

@ -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

View File

@ -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<Match> list = new ArrayList<Match>();
Match match1 = new Match(75, new TokenEntry("public", "/var/F" + XMLRenderer.BASIC_ESCAPE[2][0] + "oo.java", 48), new TokenEntry("void", "/var/F<oo.java", 73));
match1.setLineCount(6);
match1.setSourceCodeSlice("code fragment");
list.add(match1);
String report = renderer.render(list.iterator());
assertTrue(report.contains(XMLRenderer.BASIC_ESCAPE[2][1]));
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(XMLRendererTest.class);
}

View File

@ -44,7 +44,7 @@ public class XMLRenderer implements Renderer {
buffer.append("<file line=\"");
buffer.append(mark.getBeginLine());
buffer.append("\" path=\"");
buffer.append(mark.getTokenSrcID());
buffer.append(encode(mark.getTokenSrcID()));
buffer.append("\"/>").append(PMD.EOL);
}
String codeFragment = match.getSourceCodeSlice();
@ -58,4 +58,33 @@ public class XMLRenderer implements Renderer {
buffer.append("</pmd-cpd>");
return buffer.toString();
}
/*
* <p>Fixes bug : https://sourceforge.net/tracker/?func=detail&aid=2832322&group_id=56262&atid=479921</p>
*
* 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 = {
{"\"", "&quot;"}, // " - double-quote
{"&", "&amp;"}, // & - ampersand
{"<", "&lt;"}, // < - less-than
{">", "&gt;"}, // > - greater-than
};
}