Resources.java revision 10a3ed3d5af2cbfbec2b35405d8d9420a9bf8776
1/*******************************************************************************
2 * Copyright (c) Copyright (c) Copyright (c) 2009, 2012 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 *******************************************************************************/
12package org.jacoco.report.internal.html.resources;
13
14import java.io.IOException;
15import java.io.InputStream;
16import java.io.OutputStream;
17
18import org.jacoco.core.analysis.ICoverageNode.ElementType;
19import org.jacoco.report.internal.ReportOutputFolder;
20
21/**
22 * Static resource that are included with the coverage report and might be
23 * referenced from created HTML pages.
24 */
25public class Resources {
26
27	/** The name of the style sheet */
28	public static final String STYLESHEET = "report.css";
29
30	/** The name of the prettify style sheet */
31	public static final String PRETTIFY_STYLESHEET = "prettify.css";
32
33	/** The name of the prettify script */
34	public static final String PRETTIFY_SCRIPT = "prettify.js";
35
36	/** The name of the sort script */
37	public static final String SORT_SCRIPT = "sort.js";
38
39	/** The name of the red part of the coverage bar */
40	public static final String REDBAR = "redbar.gif";
41
42	/** The name of the green part of the coverage bar */
43	public static final String GREENBAR = "greenbar.gif";
44
45	private final ReportOutputFolder folder;
46
47	/**
48	 * Attaches resources to the report with the given root folder.
49	 *
50	 * @param root
51	 *            root folder of the report
52	 */
53	public Resources(final ReportOutputFolder root) {
54		folder = root.subFolder(".resources");
55	}
56
57	/**
58	 * Returns a relative link to a static resource.
59	 *
60	 * @param base
61	 *            base folder from where the link should be created
62	 * @param name
63	 *            name of the static resource, see constants in this class
64	 * @return relative link
65	 */
66	public String getLink(final ReportOutputFolder base, final String name) {
67		return folder.getLink(base, name);
68	}
69
70	/**
71	 * Determines the style sheet class for the given element type.
72	 *
73	 * @param type
74	 *            type of the element
75	 * @return style class name
76	 */
77	public static String getElementStyle(final ElementType type) {
78		switch (type) {
79		case GROUP:
80			return Styles.EL_GROUP;
81		case BUNDLE:
82			return Styles.EL_BUNDLE;
83		case PACKAGE:
84			return Styles.EL_PACKAGE;
85		case SOURCEFILE:
86			return Styles.EL_SOURCE;
87		case CLASS:
88			return Styles.EL_CLASS;
89		case METHOD:
90			return Styles.EL_METHOD;
91		}
92		throw new AssertionError("Unknown element type: " + type);
93	}
94
95	/**
96	 * Copies all static resources into the report.
97	 *
98	 * @throws IOException
99	 *             if the resources can't be written to the report
100	 */
101	public void copyResources() throws IOException {
102		copyResource(STYLESHEET);
103		copyResource("report.gif");
104		copyResource("group.gif");
105		copyResource("bundle.gif");
106		copyResource("package.gif");
107		copyResource("source.gif");
108		copyResource("class.gif");
109		copyResource("method.gif");
110		copyResource("session.gif");
111		copyResource("sort.gif");
112		copyResource("up.gif");
113		copyResource("down.gif");
114		copyResource("branchfc.gif");
115		copyResource("branchnc.gif");
116		copyResource("branchpc.gif");
117		copyResource(REDBAR);
118		copyResource(GREENBAR);
119		copyResource(PRETTIFY_STYLESHEET);
120		copyResource(PRETTIFY_SCRIPT);
121		copyResource(SORT_SCRIPT);
122	}
123
124	private void copyResource(final String name) throws IOException {
125		final InputStream in = Resources.class.getResourceAsStream(name);
126		final OutputStream out = folder.createFile(name);
127		final byte[] buffer = new byte[256];
128		int len;
129		while ((len = in.read(buffer)) != -1) {
130			out.write(buffer, 0, len);
131		}
132		in.close();
133		out.close();
134	}
135
136}
137