All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scoverage.reporter.ScoverageXmlWriter.scala Maven / Gradle / Ivy

The newest version!
package scoverage.reporter

import java.io.File

import scala.xml.Node
import scala.xml.PrettyPrinter

import scoverage.domain.Coverage
import scoverage.domain.MeasuredClass
import scoverage.domain.MeasuredMethod
import scoverage.domain.MeasuredPackage
import scoverage.domain.Statement

/** @author Stephen Samuel */
class ScoverageXmlWriter(
    sourceDirectories: Seq[File],
    outputDir: File,
    debug: Boolean,
    sourceEncoding: Option[String]
) extends BaseReportWriter(sourceDirectories, outputDir, sourceEncoding) {

  def this(
      sourceDir: File,
      outputDir: File,
      debug: Boolean,
      sourceEncoding: Option[String]
  ) = {
    this(Seq(sourceDir), outputDir, debug, sourceEncoding)
  }

  def write(coverage: Coverage): Unit = {
    val file = IOUtils.reportFile(outputDir, debug)
    IOUtils.writeToFile(
      file,
      new PrettyPrinter(120, 4).format(xml(coverage)),
      sourceEncoding
    )
  }

  private def xml(coverage: Coverage): Node = {
    
      
        {coverage.packages.map(pack)}
      
    
  }

  private def statement(stmt: Statement): Node = {
    debug match {
      case true =>
        
          {escape(stmt.desc)}
        
      case false =>
        
    }
  }

  private def method(method: MeasuredMethod): Node = {
    
      
        {method.statements.map(statement)}
      
    
  }

  private def klass(klass: MeasuredClass): Node = {
    
      
        {klass.methods.map(method)}
      
    
  }

  private def pack(pack: MeasuredPackage): Node = {
    
      
        {pack.classes.map(klass)}
      
    
  }

  /** This method ensures that the output String has only
    * valid XML unicode characters as specified by the
    * XML 1.0 standard. For reference, please see
    * the
    * standard. This method will return an empty
    * String if the input is null or empty.
    *
    * @param in The String whose non-valid characters we want to remove.
    * @return The in String, stripped of non-valid characters.
    * @see http://blog.mark-mclaren.info/2007/02/invalid-xml-characters-when-valid-utf8_5873.html
    */
  def escape(in: String): String = {
    val out = new StringBuilder()
    for (current <- Option(in).getOrElse("").toCharArray) {
      if (
        (current == 0x9) || (current == 0xa) || (current == 0xd) ||
        ((current >= 0x20) && (current <= 0xd7ff)) ||
        ((current >= 0xe000) && (current <= 0xfffd)) ||
        ((current >= 0x10000) && (current <= 0x10ffff))
      )
        out.append(current)
    }
    out.mkString
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy