Class CoverageHtmlResources

java.lang.Object
org.ek9lang.cli.CoverageHtmlResources

public final class CoverageHtmlResources extends Object
Manages HTML coverage report resources.

Extracts CSS, JavaScript, images, and HTML templates from the compiler JAR to the coverage output directory. Templates support placeholder substitution for dynamic content generation.

Resources are loaded from:

  • /site/ - Shared site resources (logo, favicon, base styles)
  • /coverage/ - Coverage-specific resources (coverage.css, coverage.js)
  • /coverage/templates/ - HTML templates with placeholders
  • Field Details

    • MAX_CYCLOMATIC_COMPLEXITY

      public static final int MAX_CYCLOMATIC_COMPLEXITY
      Maximum cyclomatic complexity before E11010 error.
      See Also:
    • MAX_COGNITIVE_COMPLEXITY

      public static final int MAX_COGNITIVE_COMPLEXITY
      Maximum cognitive complexity before E11021 error.
      See Also:
    • MAX_READABILITY

      public static final int MAX_READABILITY
      Maximum readability score (ARI capped at 20).
      See Also:
    • MAX_NESTING_DEPTH

      public static final int MAX_NESTING_DEPTH
      Maximum nesting depth before E11011 error.
      See Also:
  • Constructor Details

    • CoverageHtmlResources

      public CoverageHtmlResources()
  • Method Details

    • extractTo

      public void extractTo(Path outputDir) throws IOException
      Extract all static resources to the output directory.

      Copies site resources (logo, favicon) and coverage-specific resources (CSS, JS) to the coverage report directory.

      Parameters:
      outputDir - The directory to extract resources to
      Throws:
      IOException - if resource extraction fails
    • loadTemplate

      public String loadTemplate(String templateName, Map<String,String> substitutions) throws IOException
      Load an HTML template and substitute placeholders.

      Templates use {{PLACEHOLDER}} syntax for substitution. All placeholders in the substitutions map are replaced.

      Parameters:
      templateName - The template file name (e.g., "index.html.template")
      substitutions - Map of placeholder names to values
      Returns:
      The processed template content
      Throws:
      IOException - if template loading fails
    • loadResource

      public String loadResource(String resourcePath) throws IOException
      Load a text resource from the classpath.
      Parameters:
      resourcePath - The resource path (starting with /)
      Returns:
      The resource content as a string
      Throws:
      IOException - if the resource cannot be loaded
    • generateDonutChart

      public String generateDonutChart(double percentage, String color)
      Generate an SVG donut chart for coverage percentage.

      Creates a pure SVG chart with no external dependencies. The chart shows coverage as a colored arc with the percentage in the center.

      Parameters:
      percentage - The coverage percentage (0-100)
      color - The chart color (CSS color value)
      Returns:
      SVG markup as a string
    • generateMiniDonutChart

      public String generateMiniDonutChart(double percentage, String color)
      Generate a smaller SVG donut chart for file summary cards.

      Creates a compact version of the donut chart suitable for mini chart cards in file summary pages.

      Parameters:
      percentage - The coverage percentage (0-100)
      color - The chart color (CSS color value)
      Returns:
      SVG markup as a string
    • getChartColor

      public String getChartColor(double percentage)
      Get the chart color based on coverage percentage.

      Color thresholds:

      • 100%: Excellent (green)
      • 80-99%: Good (light green)
      • 50-79%: Warning (orange)
      • 0-49%: Error (red)
      Parameters:
      percentage - The coverage percentage
      Returns:
      CSS color value
    • getCoverageClass

      public String getCoverageClass(double percentage)
      Get the CSS class for coverage percentage styling.
      Parameters:
      percentage - The coverage percentage
      Returns:
      CSS class name (pct-excellent, pct-good, pct-warning, pct-error)
    • getBarClass

      public String getBarClass(double percentage)
      Get the CSS class for coverage bar fill.
      Parameters:
      percentage - The coverage percentage
      Returns:
      CSS class name (bar-excellent, bar-good, bar-warning, bar-error)
    • escapeHtml

      public String escapeHtml(String text)
      Escape HTML special characters in text.
      Parameters:
      text - The text to escape
      Returns:
      HTML-escaped text
    • toSafeFileName

      public String toSafeFileName(String filePath)
      Convert a file path to a safe HTML file name.

      Replaces path separators and dots with underscores to create a flat file structure in the coverage output directory.

      Parameters:
      filePath - The source file path (e.g., "src/main.ek9")
      Returns:
      Safe file name (e.g., "src_main_ek9.html")
    • extractPackageInfo

      public CoverageHtmlResources.PackageInfo extractPackageInfo(Path sourceFile)
      Extract package metadata from an EK9 source file.

      Parses the defines package block to extract description, version, license, and tags. Returns CoverageHtmlResources.PackageInfo.EMPTY if no package block is found or the file cannot be read.

      Parameters:
      sourceFile - Path to the EK9 source file
      Returns:
      PackageInfo with extracted metadata, or EMPTY if not found
    • extractPackageInfo

      public CoverageHtmlResources.PackageInfo extractPackageInfo(String content)
      Extract package metadata from EK9 source content.
      Parameters:
      content - The EK9 source content
      Returns:
      PackageInfo with extracted metadata
    • generatePackageInfoHtml

      public String generatePackageInfoHtml(CoverageHtmlResources.PackageInfo info)
      Generate HTML for package info display.

      Creates a formatted section showing package metadata when available. Returns an empty string if no metadata is present.

      Parameters:
      info - The package info to display
      Returns:
      HTML string for the package info section
    • generateMetricDonutChart

      public String generateMetricDonutChart(double value, double limit, String displayValue)
      Generate a donut chart for code quality metrics.

      Unlike coverage charts where higher is better, metric charts show "percentage of limit consumed" where LOWER is better. The chart fills up as you approach the compiler limit.

      Parameters:
      value - The actual metric value (e.g., complexity = 10)
      limit - The compiler limit (e.g., 45)
      displayValue - The value to show in center (formatted string)
      Returns:
      SVG markup as a string
    • getMetricChartColor

      public String getMetricChartColor(double percentOfLimit)
      Get the chart color for a code quality metric based on percentage of limit.

      INVERSE of coverage colors - lower percentage is better:

      • 0-49%: Green (safe headroom)
      • 50-79%: Orange (warning - approaching limit)
      • 80-100%+: Red (critical - near or at compiler error)
      Parameters:
      percentOfLimit - The percentage of the compiler limit consumed
      Returns:
      CSS color value
    • getMetricClass

      public String getMetricClass(double percentOfLimit)
      Get the CSS class for a metric value based on percentage of limit.

      INVERSE of coverage classes - lower percentage is better.

      Parameters:
      percentOfLimit - The percentage of the compiler limit consumed
      Returns:
      CSS class name (metric-safe, metric-warning, metric-critical)