SourceFilePage.java revision 61e905db9ae03f604fbc38890dce997c05559d0a
1/*******************************************************************************
2 * 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.page;
13
14import static java.lang.String.format;
15
16import java.io.IOException;
17import java.io.Reader;
18
19import org.jacoco.core.analysis.ISourceFileCoverage;
20import org.jacoco.report.internal.ReportOutputFolder;
21import org.jacoco.report.internal.html.HTMLElement;
22import org.jacoco.report.internal.html.IHTMLReportContext;
23import org.jacoco.report.internal.html.resources.Resources;
24
25/**
26 * Page showing the content of a source file with numbered and highlighted
27 * source lines.
28 */
29public class SourceFilePage extends NodePage<ISourceFileCoverage> {
30
31	private final Reader sourceReader;
32
33	private final int tabWidth;
34
35	/**
36	 * Creates a new page with given information.
37	 *
38	 * @param sourceFileNode
39	 * @param sourceReader
40	 * @param tabWidth
41	 * @param parent
42	 * @param folder
43	 * @param context
44	 */
45	public SourceFilePage(final ISourceFileCoverage sourceFileNode,
46			final Reader sourceReader, final int tabWidth,
47			final ReportPage parent, final ReportOutputFolder folder,
48			final IHTMLReportContext context) {
49		super(sourceFileNode, parent, folder, context);
50		this.sourceReader = sourceReader;
51		this.tabWidth = tabWidth;
52	}
53
54	@Override
55	protected void content(final HTMLElement body) throws IOException {
56		final SourceHighlighter hl = new SourceHighlighter(context.getLocale());
57		hl.render(body, getNode(), sourceReader);
58		sourceReader.close();
59	}
60
61	@Override
62	protected void headExtra(final HTMLElement head) throws IOException {
63		super.headExtra(head);
64		head.link(
65				"stylesheet",
66				context.getResources().getLink(folder,
67						Resources.PRETTIFY_STYLESHEET), "text/css");
68		head.script(
69				"text/javascript",
70				context.getResources().getLink(folder,
71						Resources.PRETTIFY_SCRIPT));
72	}
73
74	@Override
75	protected String getOnload() {
76		return format("window['PR_TAB_WIDTH']=%d;prettyPrint()",
77				Integer.valueOf(tabWidth));
78	}
79
80	@Override
81	protected String getFileName() {
82		return getNode().getName() + ".html";
83	}
84
85}
86