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