01: package org.acm.seguin.pmd.renderers;
02:
03: import org.acm.seguin.pmd.PMD;
04: import org.acm.seguin.pmd.Report;
05: import org.acm.seguin.pmd.RuleViolation;
06: import org.acm.seguin.pmd.util.StringUtil;
07:
08: import java.util.Iterator;
09:
10: public class XMLRenderer implements Renderer {
11: public String render(Report report) {
12: StringBuffer buf = new StringBuffer(
13: "<?xml version=\"1.0\"?><pmd>" + PMD.EOL);
14: String filename = "*start*";
15:
16: // rule violations
17: for (Iterator i = report.iterator(); i.hasNext();) {
18: RuleViolation rv = (RuleViolation) i.next();
19: if (!rv.getFilename().equals(filename)) { // New File
20: if (!filename.equals("*start*")) {
21: buf.append("</file>");
22: }
23: filename = rv.getFilename();
24: buf.append("<file name=\"" + filename + "\">");
25: buf.append(PMD.EOL);
26: }
27:
28: buf.append("<violation ");
29: buf.append("line=\"" + Integer.toString(rv.getLine())
30: + "\" ");
31: buf.append("rule=\"" + rv.getRule().getName() + "\">");
32: buf.append(PMD.EOL);
33:
34: String d = rv.getDescription();
35: d = StringUtil.replaceString(d, '&', "&");
36: d = StringUtil.replaceString(d, '<', "<");
37: d = StringUtil.replaceString(d, '>', ">");
38: buf.append(d);
39:
40: buf.append(PMD.EOL);
41: buf.append("</violation>");
42: buf.append(PMD.EOL);
43: }
44: if (!filename.equals("*start*")) {
45: buf.append("</file>");
46: }
47:
48: // errors
49: for (Iterator i = report.errors(); i.hasNext();) {
50: Report.ProcessingError pe = (Report.ProcessingError) i
51: .next();
52: buf.append(PMD.EOL);
53: buf.append("<error ");
54: buf.append(PMD.EOL);
55: String attrs = "filename=\"" + pe.getFile() + "\" msg=\""
56: + pe.getMsg() + "\"";
57: attrs = StringUtil.replaceString(attrs, '&', "&");
58: attrs = StringUtil.replaceString(attrs, '<', "<");
59: attrs = StringUtil.replaceString(attrs, '>', ">");
60: buf.append(attrs);
61: buf.append(PMD.EOL);
62: buf.append("/>");
63: buf.append(PMD.EOL);
64: }
65:
66: buf.append("</pmd>");
67: return buf.toString();
68: }
69:
70: }
|