Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |

file stats: | LOC: | 109 |  | Methods: | 5
| NCLOC: | 87 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL
XMLRenderer.java | 72.2% | 81.4% | 100% | 80.5% |

1 |  | /**
2 |  | * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 |  | */
4 |  | package net.sourceforge.pmd.renderers;
5 |  |
6 |  | import net.sourceforge.pmd.PMD;
7 |  | import net.sourceforge.pmd.Report;
8 |  | import net.sourceforge.pmd.IRuleViolation;
9 |  | import net.sourceforge.pmd.util.StringUtil;
10 |  |
11 |  | import java.text.SimpleDateFormat;
12 |  | import java.util.Date;
13 |  | import java.util.Iterator;
14 |  |
15 |  | public class XMLRenderer extends AbstractRenderer implements Renderer {
16 |  |
17 | 5 | public String render(Report report) {
18 |  |
19 | 5 | StringBuffer buf = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + PMD.EOL + createVersionAttr() + createTimestampAttr() + createTimeElapsedAttr(report) + ">" + PMD.EOL);
20 | 5 | String filename = null;
21 |  |
22 |  | // rule violations
23 | 5 | for (Iterator i = report.iterator(); i.hasNext();) {
24 | 5 | IRuleViolation rv = (IRuleViolation) i.next();
25 | 5 | if (!rv.getFilename().equals(filename)) { // New File
26 | 4 | if (filename != null) {// Not first file ?
27 | 1 | buf.append("</file>" + PMD.EOL);
28 |  | }
29 | 4 | filename = rv.getFilename();
30 | 4 | buf.append("<file name=\"");
31 | 4 | StringUtil.appendXmlEscaped(buf, filename);
32 | 4 | buf.append("\">").append(PMD.EOL);
33 |  | }
34 |  |
35 | 5 | buf.append("<violation line=\"").append(rv.getBeginLine()).append("\"");
36 | 5 | buf.append(" rule=\"");
37 | 5 | StringUtil.appendXmlEscaped(buf, rv.getRule().getName());
38 | 5 | buf.append("\"");
39 | 5 | buf.append(" ruleset=\"");
40 | 5 | StringUtil.appendXmlEscaped(buf, rv.getRule().getRuleSetName());
41 | 5 | buf.append("\"");
42 | 5 | maybeAdd("package", rv.getPackageName(), buf);
43 | 5 | maybeAdd("class", rv.getClassName(), buf);
44 | 5 | maybeAdd("method", rv.getMethodName(), buf);
45 | 5 | maybeAdd("externalInfoUrl", rv.getRule().getExternalInfoUrl(), buf);
46 | 5 | buf.append(" priority=\"");
47 | 5 | buf.append(rv.getRule().getPriority());
48 | 5 | buf.append("\">");
49 | 5 | buf.append(PMD.EOL);
50 | 5 | StringUtil.appendXmlEscaped(buf, rv.getDescription());
51 |  |
52 | 5 | buf.append(PMD.EOL);
53 | 5 | buf.append("</violation>");
54 | 5 | buf.append(PMD.EOL);
55 |  | }
56 | 5 | if (filename != null) { // Not first file ?
57 | 3 | buf.append("</file>" + PMD.EOL);
58 |  | }
59 |  |
60 |  | // errors
61 | 5 | for (Iterator i = report.errors(); i.hasNext();) {
62 | 1 | Report.ProcessingError pe = (Report.ProcessingError) i.next();
63 | 1 | buf.append("<error ").append("filename=\"");
64 | 1 | StringUtil.appendXmlEscaped(buf, pe.getFile());
65 | 1 | buf.append("\" msg=\"");
66 | 1 | StringUtil.appendXmlEscaped(buf, pe.getMsg());
67 | 1 | buf.append("\"/>").append(PMD.EOL);
68 |  | }
69 |  |
70 |  | // suppressed violations
71 | 5 | if (showSuppressedViolations) {
72 | 5 | for (Iterator i = report.getSuppressedRuleViolations().iterator(); i.hasNext();) {
73 | 0 | Report.SuppressedViolation suppressed = (Report.SuppressedViolation) i.next();
74 | 0 | buf.append("<suppressedviolation ").append("filename=\"");
75 | 0 | StringUtil.appendXmlEscaped(buf, suppressed.getRuleViolation().getFilename());
76 | 0 | buf.append("\" suppressiontype=\"");
77 | 0 | StringUtil.appendXmlEscaped(buf, suppressed.suppressedByNOPMD() ? "nopmd" : "annotation");
78 | 0 | buf.append("\" msg=\"");
79 | 0 | StringUtil.appendXmlEscaped(buf, suppressed.getRuleViolation().getDescription());
80 | 0 | buf.append("\"/>").append(PMD.EOL);
81 |  | }
82 |  | }
83 |  |
84 | 5 | buf.append("</pmd>");
85 | 5 | return buf.toString();
86 |  | }
87 |  |
88 | 20 | private void maybeAdd(String attr, String value, StringBuffer buf) {
89 | 20 | if (value != null && value.length() > 0) {
90 | 0 | buf.append(" " + attr + "=\"");
91 | 0 | StringUtil.appendXmlEscaped(buf, value);
92 | 0 | buf.append("\"");
93 |  | }
94 |  | }
95 |  |
96 | 5 | private String createVersionAttr() {
97 | 5 | return "<pmd version=\"" + PMD.VERSION + "\"";
98 |  | }
99 |  |
100 | 5 | private String createTimestampAttr() {
101 | 5 | return " timestamp=\"" + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(new Date()) + "\"";
102 |  | }
103 |  |
104 | 5 | private String createTimeElapsedAttr(Report rpt) {
105 | 5 | Report.ReadableDuration d = new Report.ReadableDuration(rpt.getElapsedTimeInMillis());
106 | 5 | return " elapsedTime=\"" + d.getTime() + "\"";
107 |  | }
108 |  |
109 |  | }