View Javadoc

1   /*
2       Copyright 2008 Ernest Micklei @ PhilemonWorks.com
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
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   * XMLReporter creates an XML document (String) with all the results of running SelfDiagnose.
31   * The document conforms to the selfdiagnose.xsd.
32   * 
33   * @author ernestmicklei
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  	 * If the request has an paramter "xsl" then insert the stylesheet reference in the
100 	 * response.
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 }