SourceFilePage.java
1/*******************************************************************************
2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG and others
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 * $Id: $
12 *******************************************************************************/
13package org.jacoco.report.html;
14
15import java.io.IOException;
16import java.io.Reader;
17
18import org.jacoco.core.analysis.ICoverageNode;
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 $Revision: $
31 */
32public class SourceFilePage extends ReportPage {
33
34 private Reader sourceReader;
35
36 /**
37 * Creates a new visitor in the given context.
38 *
39 * @param node
40 * @param parent
41 * @param outputFolder
42 * @param context
43 */
44 public SourceFilePage(final ICoverageNode node, final ReportPage parent,
45 final ReportOutputFolder outputFolder,
46 final IHTMLReportContext context) {
47 super(node, parent, outputFolder, context);
48 }
49
50 public IReportVisitor visitChild(final ICoverageNode node) {
51 throw new IllegalStateException("Source don't have child nodes.");
52 }
53
54 @Override
55 public void visitEnd(final ISourceFileLocator sourceFileLocator)
56 throws IOException {
57 final SourceFileCoverage s = (SourceFileCoverage) getNode();
58 sourceReader = sourceFileLocator.getSourceFile(s.getPackageName(), s
59 .getName());
60 if (sourceReader != null) {
61 super.visitEnd(sourceFileLocator);
62 }
63 }
64
65 @Override
66 protected void content(final HTMLElement body,
67 final ISourceFileLocator sourceFileLocator) throws IOException {
68 final SourceFileCoverage s = (SourceFileCoverage) getNode();
69 new SourceHighlighter().render(body, s.getLines(), sourceReader);
70 sourceReader.close();
71 }
72
73 @Override
74 protected void head(final HTMLElement head) throws IOException {
75 super.head(head);
76 head.link("stylesheet", context.getResources().getLink(outputFolder,
77 Resources.PRETTIFY_STYLESHEET), "text/css");
78 head.script("text/javascript", context.getResources().getLink(
79 outputFolder, Resources.PRETTIFY_SCRIPT));
80 }
81
82 @Override
83 protected void body(final HTMLElement body,
84 final ISourceFileLocator sourceFileLocator) throws IOException {
85 body.attr("onload", "prettyPrint()");
86 super.body(body, sourceFileLocator);
87 }
88
89 @Override
90 protected String getFileName() {
91 return getNode().getName() + ".html";
92 }
93
94 @Override
95 protected ReportOutputFolder getFolder(final ReportOutputFolder base) {
96 return base;
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}