01: package org.acm.seguin.pmd.cpd;
02:
03: import org.acm.seguin.pmd.PMD;
04: import org.acm.seguin.pmd.util.StringUtil;
05:
06: import java.util.Iterator;
07:
08: /**
09: * @author Philippe T'Seyen
10: */
11: public class XMLRenderer implements Renderer {
12: public String render(Iterator matches) {
13: StringBuffer buffer = new StringBuffer();
14: buffer.append("<?xml version=\"1.0\"?>");
15: buffer.append("<pmd-cpd>");
16: for (; matches.hasNext();) {
17: Match match = (Match) matches.next();
18: buffer.append("<duplication");
19: buffer.append(" lines=\"");
20: buffer.append(match.getLineCount());
21: buffer.append("\"");
22: buffer.append(" tokens=\"");
23: buffer.append(match.getTokenCount());
24: buffer.append("\">");
25:
26: for (Iterator iterator = match.iterator(); iterator
27: .hasNext();) {
28: Mark mark = (Mark) iterator.next();
29: buffer.append("<file");
30: buffer.append(" line=\"");
31: buffer.append(mark.getBeginLine());
32: buffer.append("\"");
33: buffer.append(" path=\"");
34: buffer.append(mark.getTokenSrcID());
35: buffer.append("\"/>");
36: }
37: String codeFragment = match.getSourceCodeSlice();
38: if (codeFragment != null) {
39: buffer.append("<codefragment><![CDATA["
40: + PMD.EOL
41: + StringUtil.replaceString(codeFragment, "]]>",
42: "]]>") + PMD.EOL
43: + "]]></codefragment>");
44: }
45: buffer.append("</duplication>");
46: }
47: buffer.append("</pmd-cpd>");
48: return buffer.toString();
49: }
50: }
|