• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Marc R. Hoffmann - initial API and implementation
11  *
12  *******************************************************************************/
13 package org.jacoco.report.internal.html.resources;
14 
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 
19 import org.jacoco.core.analysis.ICoverageNode.ElementType;
20 import org.jacoco.report.internal.ReportOutputFolder;
21 
22 /**
23  * Static resource that are included with the coverage report and might be
24  * referenced from created HTML pages.
25  */
26 public class Resources {
27 
28 	/** The name of the style sheet */
29 	public static final String STYLESHEET = "report.css";
30 
31 	/** The name of the prettify style sheet */
32 	public static final String PRETTIFY_STYLESHEET = "prettify.css";
33 
34 	/** The name of the prettify script */
35 	public static final String PRETTIFY_SCRIPT = "prettify.js";
36 
37 	/** The name of the sort script */
38 	public static final String SORT_SCRIPT = "sort.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 	 */
Resources(final ReportOutputFolder root)54 	public Resources(final ReportOutputFolder root) {
55 		folder = root.subFolder("jacoco-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 	 */
getLink(final ReportOutputFolder base, final String name)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 	 */
getElementStyle(final ElementType type)78 	public static String getElementStyle(final ElementType type) {
79 		switch (type) {
80 		case GROUP:
81 			return Styles.EL_GROUP;
82 		case BUNDLE:
83 			return Styles.EL_BUNDLE;
84 		case PACKAGE:
85 			return Styles.EL_PACKAGE;
86 		case SOURCEFILE:
87 			return Styles.EL_SOURCE;
88 		case CLASS:
89 			return Styles.EL_CLASS;
90 		case METHOD:
91 			return Styles.EL_METHOD;
92 		}
93 		throw new AssertionError("Unknown element type: " + type);
94 	}
95 
96 	/**
97 	 * Copies all static resources into the report.
98 	 *
99 	 * @throws IOException
100 	 *             if the resources can't be written to the report
101 	 */
copyResources()102 	public void copyResources() throws IOException {
103 		copyResource(STYLESHEET);
104 		copyResource("report.gif");
105 		copyResource("group.gif");
106 		copyResource("bundle.gif");
107 		copyResource("package.gif");
108 		copyResource("source.gif");
109 		copyResource("class.gif");
110 		copyResource("method.gif");
111 		copyResource("session.gif");
112 		copyResource("sort.gif");
113 		copyResource("up.gif");
114 		copyResource("down.gif");
115 		copyResource("branchfc.gif");
116 		copyResource("branchnc.gif");
117 		copyResource("branchpc.gif");
118 		copyResource(REDBAR);
119 		copyResource(GREENBAR);
120 		copyResource(PRETTIFY_STYLESHEET);
121 		copyResource(PRETTIFY_SCRIPT);
122 		copyResource(SORT_SCRIPT);
123 	}
124 
copyResource(final String name)125 	private void copyResource(final String name) throws IOException {
126 		final InputStream in = Resources.class.getResourceAsStream(name);
127 		final OutputStream out = folder.createFile(name);
128 		final byte[] buffer = new byte[256];
129 		int len;
130 		while ((len = in.read(buffer)) != -1) {
131 			out.write(buffer, 0, len);
132 		}
133 		in.close();
134 		out.close();
135 	}
136 
137 }
138