View Javadoc

1   //Copyright 2007-2008, by the California Institute of Technology.
2   //ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   //Any commercial use must be negotiated with the Office of Technology Transfer
4   //at the California Institute of Technology.
5   //
6   //This software is subject to U. S. export control laws and regulations
7   //(22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   //is subject to U.S. export control laws and regulations, the recipient has
9   //the responsibility to obtain export licenses or other export authority as
10  //may be required before exporting such information to foreign countries or
11  //providing access to foreign nationals.
12  //
13  // $Id: Report.java 3461 2008-08-07 17:43:26Z pramirez $
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  }