1 /*
2  * Copyright (C) 2010 The Android Open Source Project
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.android.cts.apicoverage;
18 
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.PipedInputStream;
24 import java.io.PipedOutputStream;
25 import java.util.List;
26 
27 import javax.xml.transform.Transformer;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.stream.StreamResult;
31 import javax.xml.transform.stream.StreamSource;
32 
33 /**
34  * Class that outputs an HTML report of the {@link ApiCoverage} collected. It is the XML report
35  * transformed into HTML.
36  */
37 class HtmlReport {
38 
printHtmlReport(final List<File> testApks, final ApiCoverage apiCoverage, final CddCoverage cddCoverage, final PackageFilter packageFilter, final String reportTitle, final OutputStream out)39     public static void printHtmlReport(final List<File> testApks, final ApiCoverage apiCoverage,
40             final CddCoverage cddCoverage, final PackageFilter packageFilter,
41             final String reportTitle, final OutputStream out)
42                 throws IOException, TransformerException {
43         final PipedOutputStream xmlOut = new PipedOutputStream();
44         final PipedInputStream xmlIn = new PipedInputStream(xmlOut);
45 
46         Thread t = new Thread(new Runnable() {
47             @Override
48             public void run() {
49                 XmlReport.printXmlReport(
50                     testApks, apiCoverage, cddCoverage, packageFilter, reportTitle, xmlOut);
51 
52                 // Close the output stream to avoid "Write dead end" errors.
53                 try {
54                     xmlOut.close();
55                 } catch (IOException e) {
56                     e.printStackTrace();
57                 }
58             }
59         });
60         t.start();
61 
62         InputStream xsl = CtsApiCoverage.class.getResourceAsStream("/api-coverage.xsl");
63         StreamSource xslSource = new StreamSource(xsl);
64         TransformerFactory factory = TransformerFactory.newInstance();
65         Transformer transformer = factory.newTransformer(xslSource);
66 
67         StreamSource xmlSource = new StreamSource(xmlIn);
68         StreamResult result = new StreamResult(out);
69         transformer.transform(xmlSource, result);
70     }
71 }
72