SourceFilePage.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;
13
14import java.io.IOException;
15import java.io.Reader;
16
17import org.jacoco.core.analysis.ICoverageNode;
18import org.jacoco.core.analysis.ILines;
19import org.jacoco.core.analysis.SourceFileCoverage;
20import org.jacoco.report.IReportVisitor;
21import org.jacoco.report.ISourceFileLocator;
22import org.jacoco.report.ReportOutputFolder;
23import org.jacoco.report.html.resources.Resources;
24
25/**
26 * Page showing the content of a source file with numbered and highlighted
27 * source lines.
28 *
29 * @author Marc R. Hoffmann
30 * @version 0.4.1.20101007204400
31 */
32public class SourceFilePage extends NodePage {
33
34 private Reader sourceReader;
35
36 private final String packageName;
37
38 private final ILines lines;
39
40 /**
41 * Creates a new page with given information.
42 *
43 * @param sourceFileNode
44 * @param parent
45 * @param folder
46 * @param context
47 */
48 public SourceFilePage(final SourceFileCoverage sourceFileNode,
49 final ReportPage parent, final ReportOutputFolder folder,
50 final IHTMLReportContext context) {
51 super(sourceFileNode, parent, folder, context);
52 packageName = sourceFileNode.getPackageName();
53 lines = sourceFileNode.getLines();
54 }
55
56 public IReportVisitor visitChild(final ICoverageNode node) {
57 throw new IllegalStateException("Source don't have child nodes.");
58 }
59
60 @Override
61 public void visitEnd(final ISourceFileLocator sourceFileLocator)
62 throws IOException {
63 sourceReader = sourceFileLocator.getSourceFile(packageName, getNode()
64 .getName());
65 if (sourceReader != null) {
66 super.visitEnd(sourceFileLocator);
67 }
68 }
69
70 @Override
71 protected void content(final HTMLElement body) throws IOException {
72 new SourceHighlighter().render(body, lines, sourceReader);
73 sourceReader.close();
74 }
75
76 @Override
77 protected void headExtra(final HTMLElement head) throws IOException {
78 super.headExtra(head);
79 head.link(
80 "stylesheet",
81 context.getResources().getLink(folder,
82 Resources.PRETTIFY_STYLESHEET), "text/css");
83 head.script(
84 "text/javascript",
85 context.getResources().getLink(folder,
86 Resources.PRETTIFY_SCRIPT));
87 }
88
89 @Override
90 protected String getOnload() {
91 return "prettyPrint()";
92 }
93
94 @Override
95 protected String getFileName() {
96 return getNode().getName() + ".html";
97 }
98
99 /**
100 * Checks whether this page has actually been rendered. This might not be
101 * the case if no source file has been found.
102 *
103 * @return whether the page has been created
104 */
105 public boolean exists() {
106 return sourceReader != null;
107 }
108
109}