View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit.coverage.reporting;
6   
7   import edu.umd.cs.findbugs.annotations.NonNull;
8   
9   import java.io.File;
10  import java.io.IOException;
11  import java.io.PrintWriter;
12  import java.nio.charset.StandardCharsets;
13  import java.nio.file.Path;
14  import java.util.regex.Pattern;
15  
16  public final class OutputFile extends PrintWriter {
17      private static final Pattern PATH_SEPARATOR = Pattern.compile("/");
18  
19      @NonNull
20      private final String relPathToOutDir;
21      private final boolean sourceFile;
22  
23      public OutputFile(@NonNull File file) throws IOException {
24          super(file, StandardCharsets.UTF_8);
25          relPathToOutDir = "";
26          sourceFile = false;
27      }
28  
29      public OutputFile(@NonNull String outputDir, @NonNull String sourceFilePath) throws IOException {
30          super(getOutputFileCreatingDirIfNeeded(outputDir, sourceFilePath));
31          relPathToOutDir = getRelativeSubPathToOutputDir(sourceFilePath);
32          sourceFile = true;
33      }
34  
35      @NonNull
36      private static File getOutputFileCreatingDirIfNeeded(@NonNull String outputDir, @NonNull String sourceFilePath) {
37          File outputFile = getOutputFile(outputDir, sourceFilePath);
38          File parentDir = outputFile.getParentFile();
39  
40          if (!parentDir.exists()) {
41              boolean outputDirCreated = parentDir.mkdirs();
42              assert outputDirCreated : "Failed to create output dir: " + outputDir;
43          }
44  
45          return outputFile;
46      }
47  
48      @NonNull
49      static File getOutputFile(@NonNull String outputDir, @NonNull String sourceFilePath) {
50          int p = sourceFilePath.lastIndexOf('.');
51          String outputFileName = sourceFilePath.substring(0, p) + ".html";
52          return Path.of(outputDir, outputFileName).toFile();
53      }
54  
55      @NonNull
56      private static String getRelativeSubPathToOutputDir(@NonNull String filePath) {
57          StringBuilder cssRelPath = new StringBuilder();
58          int n = PATH_SEPARATOR.split(filePath).length;
59  
60          for (int i = 1; i < n; i++) {
61              cssRelPath.append("../");
62          }
63  
64          return cssRelPath.toString();
65      }
66  
67      public void writeCommonHeader(@NonNull String pageTitle) {
68          println("<!DOCTYPE html>");
69          println("<html>");
70          println("<head>");
71          println("  <title>" + pageTitle + "</title>");
72          println("  <meta charset='UTF-8'>");
73          println("  <link rel='stylesheet' type='text/css' href='" + relPathToOutDir + (sourceFile ? "source" : "index")
74                  + ".css'>");
75          println("  <link rel='shortcut icon' type='image/png' href='" + relPathToOutDir + "logo.png'>");
76          println("  <script src='" + relPathToOutDir + "coverage.js'></script>");
77          println("  <base target='_blank'>");
78  
79          if (sourceFile) {
80              println("  <script src='" + relPathToOutDir + "prettify.js'></script>");
81          }
82  
83          println("</head>");
84          println(sourceFile ? "<body onload='prettyPrint()'>" : "<body>");
85      }
86  
87      public void writeCommonFooter() {
88          println("</body>");
89          println("</html>");
90      }
91  }