Resources.java

    1/*******************************************************************************
    2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
    3 * All rights reserved. This program and the accompanying materials
    4 * are made available under the terms of the Eclipse Public License v1.0
    5 * which accompanies this distribution, and is available at
    6 * http://www.eclipse.org/legal/epl-v10.html
    7 *
    8 * Contributors:
    9 *    Marc R. Hoffmann - initial API and implementation
   10 *    
   11 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.report.html.resources;
   14
   15import java.io.IOException;
   16import java.io.InputStream;
   17import java.io.OutputStream;
   18
   19import org.jacoco.core.analysis.ICoverageNode.ElementType;
   20import org.jacoco.report.ReportOutputFolder;
   21
   22/**
   23 * Static resource that are included with the coverage report and might be
   24 * referenced from created HTML pages.
   25 * 
   26 * @author Marc R. Hoffmann
   27 * @version $Revision: $
   28 */
   29public class Resources {
   30
   31    /** The name of the style sheet */
   32    public static final String STYLESHEET = "report.css";
   33
   34    /** The name of the prettify style sheet */
   35    public static final String PRETTIFY_STYLESHEET = "prettify.css";
   36
   37    /** The name of the prettify script */
   38    public static final String PRETTIFY_SCRIPT = "prettify.js";
   39
   40    /** The name of the red part of the coverage bar */
   41    public static final String REDBAR = "redbar.gif";
   42
   43    /** The name of the green part of the coverage bar */
   44    public static final String GREENBAR = "greenbar.gif";
   45
   46    private final ReportOutputFolder folder;
   47
   48    /**
   49     * Attaches resources to the report with the given root folder.
   50     * 
   51     * @param root
   52     *            root folder of the report
   53     */
   54    public Resources(final ReportOutputFolder root) {
   55        folder = root.subFolder(".resources");
   56    }
   57
   58    /**
   59     * Returns a relative link to a static resource.
   60     * 
   61     * @param base
   62     *            base folder from where the link should be created
   63     * @param name
   64     *            name of the static resource, see constants in this class
   65     * @return relative link
   66     */
   67    public String getLink(final ReportOutputFolder base, final String name) {
   68        return folder.getLink(base, name);
   69    }
   70
   71    /**
   72     * Determines the style sheet class for the given element type.
   73     * 
   74     * @param type
   75     *            type of the element
   76     * @return style class name
   77     */
   78    public static String getElementStyle(final ElementType type) {
   79        switch (type) {
   80        case SESSION:
   81            return "el_session";
   82        case GROUP:
   83            return "el_group";
   84        case BUNDLE:
   85            return "el_bundle";
   86        case PACKAGE:
   87            return "el_package";
   88        case SOURCEFILE:
   89            return "el_source";
   90        case CLASS:
   91            return "el_class";
   92        case METHOD:
   93            return "el_method";
   94        }
   95        throw new IllegalArgumentException("Unknown element type: " + type);
   96    }
   97
   98    /**
   99     * Copies all static resources into the report.
  100     * 
  101     * @throws IOException
  102     *             if the resources can't be written to the report
  103     */
  104    public void copyResources() throws IOException {
  105        copyResource(STYLESHEET);
  106        copyResource("session.gif");
  107        copyResource("group.gif");
  108        copyResource("bundle.gif");
  109        copyResource("package.gif");
  110        copyResource("source.gif");
  111        copyResource("class.gif");
  112        copyResource("method.gif");
  113        copyResource(REDBAR);
  114        copyResource(GREENBAR);
  115        copyResource(PRETTIFY_STYLESHEET);
  116        copyResource(PRETTIFY_SCRIPT);
  117    }
  118
  119    private void copyResource(final String name) throws IOException {
  120        final InputStream in = Resources.class.getResourceAsStream(name);
  121        final OutputStream out = folder.createFile(name);
  122        final byte[] buffer = new byte[256];
  123        int len;
  124        while ((len = in.read(buffer)) != -1) {
  125            out.write(buffer, 0, len);
  126        }
  127        in.close();
  128        out.close();
  129    }
  130
  131}