1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.philemonworks.selfdiagnose.output;
18
19 import java.util.Date;
20 import java.util.Iterator;
21
22 import javax.servlet.http.HttpServletRequest;
23
24 import com.philemonworks.selfdiagnose.DiagnoseUtil;
25 import com.philemonworks.selfdiagnose.DiagnosticTaskResult;
26 import com.philemonworks.selfdiagnose.SelfDiagnose;
27 import com.philemonworks.selfdiagnose.SelfDiagnoseServlet;
28 import com.philemonworks.selfdiagnose.XMLUtils;
29
30
31
32
33
34
35
36 public class XMLReporter implements DiagnoseRunReporter {
37 private final StringBuffer xml = new StringBuffer();
38
39 public String getContent() {
40 return xml.toString();
41 }
42
43 public String getContentType() {
44 return "text/xml";
45 }
46
47 public void report(DiagnoseRun run) {
48 beginXML();
49 for (Iterator it = run.results.iterator(); it.hasNext();) {
50 this.report((DiagnosticTaskResult) it.next());
51 }
52 endXML();
53 }
54
55 public void report(DiagnosticTaskResult result) {
56 if (!result.wantsToBeReported()) return;
57 xml.append("\t\t<result\n\t\t\ttask=\"");
58 xml.append(result.getTask().getTaskName());
59 xml.append("\"\n\t\t\tstatus=\"");
60 xml.append(result.getStatus());
61 xml.append("\"\n\t\t\tmessage=\"");
62 xml.append(XMLUtils.encode(result.getMessage()));
63 if (result.getTask().hasComment()) {
64 xml.append("\"\n\t\t\tcomment=\"");
65 xml.append(XMLUtils.encode(result.getTask().getComment()));
66 }
67 xml.append("\"\n\t\t\trequestor=\"");
68 xml.append(result.getTask().getRequestor());
69 xml.append("\"\n\t\t\tduration=\"");
70 xml.append((String.valueOf(result.getExecutionTime())));
71 xml.append("\" />\n");
72 }
73
74 public void beginXML() {
75 xml.append("<?xml version=\"1.0\" ?>\n");
76 this.checkForStylesheet();
77 xml.append("<selfdiagnose ");
78 xml.append("run=\"");
79 xml.append(DiagnoseUtil.format(new Date()));
80 xml.append("\" ");
81 if (SelfDiagnoseServlet.getCurrentRequest() != null) {
82 xml.append("context=\"");
83 xml.append(SelfDiagnoseServlet.getCurrentRequest().getContextPath());
84 xml.append("\" ");
85 }
86 xml.append("version=\"");
87 xml.append(SelfDiagnose.VERSION);
88 xml.append("\" ");
89 xml.append(" >\n");
90 xml.append("\t<results>\n");
91 }
92
93 public void endXML() {
94 xml.append("\t</results>\n");
95 xml.append("</selfdiagnose>");
96 }
97
98
99
100
101
102 private void checkForStylesheet() {
103 HttpServletRequest request = SelfDiagnoseServlet.getCurrentRequest();
104 if (request != null && request.getParameter("xsl") != null) {
105 xml.append("<?xml-stylesheet type=\"text/xsl\" href=\"");
106 xml.append(request.getParameter("xsl"));
107 xml.append("\"?>\n");
108 }
109 }
110 }