scoverage.report.ScoverageXmlWriter.scala Maven / Gradle / Ivy
The newest version!
package scoverage.report
import java.io.File
import scoverage._
import scala.xml.{Node, PrettyPrinter}
/** @author Stephen Samuel */
class ScoverageXmlWriter(sourceDirectories: Seq[File], outputDir: File, debug: Boolean) extends BaseReportWriter(sourceDirectories, outputDir) {
def this (sourceDir: File, outputDir: File, debug: Boolean) {
this(Seq(sourceDir), outputDir, debug)
}
def write(coverage: Coverage): Unit = {
val file = IOUtils.reportFile(outputDir, debug)
IOUtils.writeToFile(file, new PrettyPrinter(120, 4).format(xml(coverage)))
}
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
}
}