KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > cpd > XMLRenderer


1 /**
2  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3  */

4 package net.sourceforge.pmd.cpd;
5
6 import net.sourceforge.pmd.PMD;
7 import net.sourceforge.pmd.util.StringUtil;
8
9 import java.util.Iterator JavaDoc;
10
11 /**
12  * @author Philippe T'Seyen
13  */

14 public class XMLRenderer implements Renderer {
15     
16     public String JavaDoc render(Iterator JavaDoc matches) {
17         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(300);
18         buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
19         buffer.append("<pmd-cpd>").append(PMD.EOL);
20         Match match;
21         while (matches.hasNext()) {
22             match = (Match) matches.next();
23             buffer.append("<duplication lines=\"");
24             buffer.append(match.getLineCount());
25             buffer.append("\" tokens=\"");
26             buffer.append(match.getTokenCount());
27             buffer.append("\">").append(PMD.EOL);
28
29             TokenEntry mark;
30             for (Iterator JavaDoc iterator = match.iterator(); iterator.hasNext();) {
31                 mark = (TokenEntry) iterator.next();
32                 buffer.append("<file line=\"");
33                 buffer.append(mark.getBeginLine());
34                 buffer.append("\" path=\"");
35                 buffer.append(mark.getTokenSrcID());
36                 buffer.append("\"/>").append(PMD.EOL);
37             }
38             String JavaDoc codeFragment = match.getSourceCodeSlice();
39             if (codeFragment != null) {
40                 buffer.append("<codefragment>").append(PMD.EOL);
41                 buffer.append("<![CDATA[").append(PMD.EOL);
42                 buffer.append(StringUtil.replaceString(codeFragment, "]]>", "]]&gt;")).append(PMD.EOL + "]]>" + PMD.EOL + "</codefragment>" + PMD.EOL);
43             }
44             buffer.append("</duplication>").append(PMD.EOL);
45         }
46         buffer.append("</pmd-cpd>");
47         return buffer.toString();
48     }
49 }
50
Popular Tags