1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package gov.nasa.pds.tools.report;
16
17 import java.io.InputStream;
18 import java.io.OutputStream;
19
20 import javax.xml.transform.Result;
21 import javax.xml.transform.Source;
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerException;
24 import javax.xml.transform.TransformerFactory;
25 import javax.xml.transform.stream.StreamResult;
26 import javax.xml.transform.stream.StreamSource;
27
28 /***
29 * Class to generate a human-readable report from an XML log.
30 *
31 * @author mcayanan
32 *
33 */
34 public class Report {
35 private InputStream xml;
36 private String stylesheet;
37
38
39 /***
40 * Constructor
41 *
42 * @param xml A stream representation of the XML log to transform.
43 * @param stylesheet An xsl stylesheet file name.
44 */
45 public Report(InputStream xml, String stylesheet) {
46 this.xml = xml;
47 this.stylesheet = stylesheet;
48 }
49
50 /***
51 * Generates the human-readable report.
52 *
53 * @param output The stream where the human-readable report will be
54 * produced.
55 * @param level Specify the severity level and above to include in the
56 * report. Valid values are 'info', 'warning', and 'error'.
57 * @throws SeverityException For an invalid severity level.
58 * @throws TransformerException If there was an error producing the report.
59 */
60 public void generateReport(OutputStream output, String level)
61 throws TransformerException {
62 Source xmlSource = new StreamSource(this.xml);
63 Source xsltSource = new StreamSource(getClass().getResourceAsStream(this.stylesheet));
64 Result result = new StreamResult(output);
65 TransformerFactory factory = TransformerFactory.newInstance();
66 Transformer transformer = factory.newTransformer(xsltSource);
67 transformer.setParameter("level", level.toUpperCase());
68 transformer.transform(xmlSource, result);
69 }
70 }