Home | History | Annotate | Download | only in apicoverage
      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 
     39     public static void printHtmlReport(final List<File> testApks, final ApiCoverage apiCoverage,
     40             final String packageFilter, final String reportTitle, final OutputStream out)
     41                 throws IOException, TransformerException {
     42         final PipedOutputStream xmlOut = new PipedOutputStream();
     43         final PipedInputStream xmlIn = new PipedInputStream(xmlOut);
     44 
     45         Thread t = new Thread(new Runnable() {
     46             @Override
     47             public void run() {
     48                 XmlReport.printXmlReport(testApks, apiCoverage, packageFilter, reportTitle, xmlOut);
     49 
     50                 // Close the output stream to avoid "Write dead end" errors.
     51                 try {
     52                     xmlOut.close();
     53                 } catch (IOException e) {
     54                     e.printStackTrace();
     55                 }
     56             }
     57         });
     58         t.start();
     59 
     60         InputStream xsl = CtsApiCoverage.class.getResourceAsStream("/api-coverage.xsl");
     61         StreamSource xslSource = new StreamSource(xsl);
     62         TransformerFactory factory = TransformerFactory.newInstance();
     63         Transformer transformer = factory.newTransformer(xslSource);
     64 
     65         StreamSource xmlSource = new StreamSource(xmlIn);
     66         StreamResult result = new StreamResult(out);
     67         transformer.transform(xmlSource, result);
     68     }
     69 }
     70