1/*******************************************************************************
2 * Copyright (c) 2009, 2015 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.ISourceNode;
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<ISourceNode> {
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	 *            coverage data for this source file
40	 * @param sourceReader
41	 *            reader for the source code
42	 * @param tabWidth
43	 *            number of character per tab
44	 * @param parent
45	 *            optional hierarchical parent
46	 * @param folder
47	 *            base folder for this page
48	 * @param context
49	 *            settings context
50	 */
51	public SourceFilePage(final ISourceNode sourceFileNode,
52			final Reader sourceReader, final int tabWidth,
53			final ReportPage parent, final ReportOutputFolder folder,
54			final IHTMLReportContext context) {
55		super(sourceFileNode, parent, folder, context);
56		this.sourceReader = sourceReader;
57		this.tabWidth = tabWidth;
58	}
59
60	@Override
61	protected void content(final HTMLElement body) throws IOException {
62		final SourceHighlighter hl = new SourceHighlighter(context.getLocale());
63		hl.render(body, getNode(), sourceReader);
64		sourceReader.close();
65	}
66
67	@Override
68	protected void head(final HTMLElement head) throws IOException {
69		super.head(head);
70		head.link(
71				"stylesheet",
72				context.getResources().getLink(folder,
73						Resources.PRETTIFY_STYLESHEET), "text/css");
74		head.script(
75				"text/javascript",
76				context.getResources().getLink(folder,
77						Resources.PRETTIFY_SCRIPT));
78	}
79
80	@Override
81	protected String getOnload() {
82		return format("window['PR_TAB_WIDTH']=%d;prettyPrint()",
83				Integer.valueOf(tabWidth));
84	}
85
86	@Override
87	protected String getFileName() {
88		return getNode().getName() + ".html";
89	}
90
91}
92