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 *******************************************************************************/
12package org.jacoco.report.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.ReportOutputFolder;
20
21/**
22 * Static resource that are included with the coverage report and might be
23 * referenced from created HTML pages.
24 *
25 * @author Marc R. Hoffmann
26 * @version 0.4.1.20101007204400
27 */
28public class Resources {
29
30 /** The name of the style sheet */
31 public static final String STYLESHEET = "report.css";
32
33 /** The name of the prettify style sheet */
34 public static final String PRETTIFY_STYLESHEET = "prettify.css";
35
36 /** The name of the prettify script */
37 public static final String PRETTIFY_SCRIPT = "prettify.js";
38
39 /** The name of the sort script */
40 public static final String SORT_SCRIPT = "sort.js";
41
42 /** The name of the red part of the coverage bar */
43 public static final String REDBAR = "redbar.gif";
44
45 /** The name of the green part of the coverage bar */
46 public static final String GREENBAR = "greenbar.gif";
47
48 private final ReportOutputFolder folder;
49
50 /**
51 * Attaches resources to the report with the given root folder.
52 *
53 * @param root
54 * root folder of the report
55 */
56 public Resources(final ReportOutputFolder root) {
57 folder = root.subFolder(".resources");
58 }
59
60 /**
61 * Returns a relative link to a static resource.
62 *
63 * @param base
64 * base folder from where the link should be created
65 * @param name
66 * name of the static resource, see constants in this class
67 * @return relative link
68 */
69 public String getLink(final ReportOutputFolder base, final String name) {
70 return folder.getLink(base, name);
71 }
72
73 /**
74 * Determines the style sheet class for the given element type.
75 *
76 * @param type
77 * type of the element
78 * @return style class name
79 */
80 public static String getElementStyle(final ElementType type) {
81 switch (type) {
82 case GROUP:
83 return Styles.EL_GROUP;
84 case BUNDLE:
85 return Styles.EL_BUNDLE;
86 case PACKAGE:
87 return Styles.EL_PACKAGE;
88 case SOURCEFILE:
89 return Styles.EL_SOURCE;
90 case CLASS:
91 return Styles.EL_CLASS;
92 case METHOD:
93 return Styles.EL_METHOD;
94 }
95 throw new AssertionError("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("report.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("session.gif");
114 copyResource("sort.gif");
115 copyResource("up.gif");
116 copyResource("down.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}