Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 118 |  | Methods: | 5
| NCLOC: | 96 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
XMLRenderer.java | 77.8% | 81.8% | 100% | 82% |

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