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