
scoverage.report.ScoverageXmlWriter.scala Maven / Gradle / Ivy
package scoverage.report
import java.io.File
import scala.xml.Node
import scala.xml.PrettyPrinter
import scoverage._
/** @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
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy